Commit 917df161 authored by James Lopez's avatar James Lopez

Merge branch 'step-2-trial' into 'master'

[Backend]  API for Step 2 of Trial

See merge request gitlab-org/gitlab-ee!15490
parents 08621d00 6f628e26
......@@ -111,6 +111,7 @@ Rails.application.routes.draw do
draw :smartcard
draw :jira_connect
draw :username
draw :trial
draw :trial_registration
end
......
......@@ -9,6 +9,10 @@ module EE
around_action :set_current_ip_address
end
def check_if_gl_com
render_404 unless ::Gitlab.com?
end
def verify_namespace_plan_check_enabled
render_404 unless ::Gitlab::CurrentSettings.should_check_namespace_plan?
end
......
......@@ -20,6 +20,17 @@ module EE
end
end
protected
override :auth_options
def auth_options
if params[:trial]
{ scope: resource_name, recall: "trial_registrations#new" }
else
super
end
end
private
def gitlab_geo_logout
......
# frozen_string_literal: true
class TrialRegistrationsController < RegistrationsController
extend ::Gitlab::Utils::Override
before_action :check_if_gl_com
before_action :check_if_improved_trials_enabled
before_action :set_redirect_url, only: [:new]
before_action :skip_confirmation, only: [:create]
def create
super do |new_user|
......@@ -10,20 +14,32 @@ class TrialRegistrationsController < RegistrationsController
end
end
def new
end
private
def set_redirect_url
store_location_for(:user, new_trial_url)
end
def skip_confirmation
params[:user][:skip_confirmation] = true
end
override :sign_up_params
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :username, :email, :password, :skip_confirmation, :email_opted_in)
if params[:user]
params.require(:user).permit(:first_name, :last_name, :username, :email, :password, :skip_confirmation, :email_opted_in)
else
{}
end
end
def resource
@resource ||= Users::BuildService.new(current_user, sign_up_params).execute(skip_authorization: true)
end
def check_if_gl_com
render_404 unless Gitlab.com?
end
def check_if_improved_trials_enabled
render_404 unless Feature.enabled?(:improved_trial_signup)
end
......
# frozen_string_literal: true
class TrialsController < ApplicationController
before_action :check_if_gl_com
before_action :check_if_improved_trials_enabled
before_action :authenticate_user!
def new
end
def select
end
def create_lead
result = GitlabSubscriptions::CreateLeadService.new.execute({ trial_user: company_params })
if result[:success]
redirect_to select_namespace_trials_url
else
render :new
end
end
private
def authenticate_user!
return if current_user
redirect_to new_trial_registration_path, alert: I18n.t('devise.failure.unauthenticated')
end
def company_params
params.permit(:company_name, :company_size, :phone_number, :number_of_users, :country)
.merge(extra_params)
end
def extra_params
attrs = current_user.slice(:first_name, :last_name)
attrs[:work_email] = current_user.email
attrs[:uid] = current_user.id
attrs[:skip_email_confirmation] = true
attrs[:gitlab_com_trial] = true
attrs[:provider] = 'gitlab'
attrs
end
def check_if_improved_trials_enabled
render_404 unless Feature.enabled?(:improved_trial_signup)
end
end
# frozen_string_literal: true
module GitlabSubscriptions
class CreateLeadService
def execute(company_params)
{ success: true }
end
end
end
# frozen_string_literal: true
resources :trials, only: [:new] do
collection do
post :create_lead
get :select_namespace, action: :select
end
end
......@@ -6,11 +6,11 @@ describe SessionsController, :geo do
include DeviseHelpers
include EE::GeoHelpers
describe '#new' do
before do
set_devise_mapping(context: @request)
end
before do
set_devise_mapping(context: @request)
end
describe '#new' do
context 'on a Geo secondary node' do
set(:primary_node) { create(:geo_node, :primary) }
set(:secondary_node) { create(:geo_node) }
......@@ -53,4 +53,28 @@ describe SessionsController, :geo do
end
end
end
describe '#create' do
before do
allow(::Gitlab).to receive(:com?).and_return(true)
end
context 'with wrong credentials' do
context 'when is a trial form' do
it 'redirects to new trial sign in page' do
post :create, params: { trial: true, user: { login: 'foo@bar.com', password: '11111' } }
expect(response).to render_template("trial_registrations/new")
end
end
context 'when is a regular form' do
it 'redirects to the regular sign in page' do
post :create, params: { user: { login: 'foo@bar.com', password: '11111' } }
expect(response).to render_template("devise/sessions/new")
end
end
end
end
end
......@@ -49,20 +49,10 @@ describe TrialRegistrationsController do
allow(Gitlab).to receive(:com?).and_return(true)
end
context 'with skip_confirmation' do
it 'creates the account as confirmed' do
post :create, params: { user: user_params.merge(skip_confirmation: true) }
expect(User.last).to be_confirmed
end
end
context 'without skip_confirmation' do
it 'creates the account with pending confirmation' do
post :create, params: { user: user_params }
it 'marks the account as confirmed' do
post :create, params: { user: user_params }
expect(User.last).not_to be_confirmed
end
expect(User.last).to be_confirmed
end
context 'derivation of name' do
......
# frozen_string_literal: true
require 'spec_helper'
describe TrialsController do
shared_examples 'an authenticated endpoint' do |verb, action|
it 'redirects to login page' do
send(verb, action)
expect(response).to redirect_to(new_trial_registration_url)
end
end
before do
allow(::Gitlab).to receive(:com?).and_return(true)
stub_feature_flags(improved_trial_signup: true)
end
describe '#new' do
it_behaves_like 'an authenticated endpoint', :get, :new
context 'when invalid - instance is not GL.com' do
it 'returns 404 not found' do
allow(::Gitlab).to receive(:com?).and_return(false)
get :new
expect(response.status).to eq(404)
end
end
context 'when feature is turned off' do
it 'returns 404 not found' do
stub_feature_flags(improved_trial_signup: false)
get :new
expect(response.status).to eq(404)
end
end
end
describe '#create_lead' do
it_behaves_like 'an authenticated endpoint', :post, :create_lead
describe 'authenticated' do
let(:user) { create(:user) }
let(:create_lead_result) { nil }
before do
sign_in(user)
expect_any_instance_of(GitlabSubscriptions::CreateLeadService).to receive(:execute) do
{ success: create_lead_result }
end
end
context 'on success' do
let(:create_lead_result) { true }
it 'redirects user to Step 3' do
post :create_lead
expect(response).to redirect_to(select_namespace_trials_url)
end
end
context 'on failure' do
let(:create_lead_result) { false }
it 'renders the :new template' do
post :create_lead
expect(response).to render_template(:new)
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment