Commit e7d35627 authored by James Lopez's avatar James Lopez

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

[Backend] Step 3 API for trials

See merge request gitlab-org/gitlab-ee!16339
parents 94c2b462 3176a7ff
...@@ -133,7 +133,7 @@ class GroupsController < Groups::ApplicationController ...@@ -133,7 +133,7 @@ class GroupsController < Groups::ApplicationController
protected protected
def render_show_html def render_show_html
render 'groups/show' render 'groups/show', locals: { trial: params[:trial] }
end end
def render_details_html def render_details_html
......
...@@ -10,7 +10,7 @@ class TrialRegistrationsController < RegistrationsController ...@@ -10,7 +10,7 @@ class TrialRegistrationsController < RegistrationsController
def create def create
super do |new_user| super do |new_user|
new_user.system_hook_service.execute_hooks_for(new_user, :create) new_user.system_hook_service.execute_hooks_for(new_user, :create) if new_user.persisted?
end end
end end
......
...@@ -4,6 +4,7 @@ class TrialsController < ApplicationController ...@@ -4,6 +4,7 @@ class TrialsController < ApplicationController
before_action :check_if_gl_com before_action :check_if_gl_com
before_action :check_if_improved_trials_enabled before_action :check_if_improved_trials_enabled
before_action :authenticate_user! before_action :authenticate_user!
before_action :fetch_namespace, only: :apply
def new def new
end end
...@@ -15,12 +16,22 @@ class TrialsController < ApplicationController ...@@ -15,12 +16,22 @@ class TrialsController < ApplicationController
result = GitlabSubscriptions::CreateLeadService.new.execute({ trial_user: company_params }) result = GitlabSubscriptions::CreateLeadService.new.execute({ trial_user: company_params })
if result[:success] if result[:success]
redirect_to select_namespace_trials_url redirect_to select_trials_url
else else
render :new render :new
end end
end end
def apply
result = GitlabSubscriptions::ApplyTrialService.new.execute(apply_trial_params)
if result[:success]
redirect_to group_url(@namespace, { trial: true })
else
redirect_to select_trials_url
end
end
private private
def authenticate_user! def authenticate_user!
...@@ -48,4 +59,19 @@ class TrialsController < ApplicationController ...@@ -48,4 +59,19 @@ class TrialsController < ApplicationController
def check_if_improved_trials_enabled def check_if_improved_trials_enabled
render_404 unless Feature.enabled?(:improved_trial_signup) render_404 unless Feature.enabled?(:improved_trial_signup)
end end
def apply_trial_params
gl_com_params = { gitlab_com_trial: true, sync_to_gl: true }
{
trial_user: params.permit(:namespace_id).merge(gl_com_params),
uid: current_user.id
}
end
def fetch_namespace
@namespace = current_user.namespaces.find(params[:namespace_id])
render_404 unless @namespace
end
end end
# frozen_string_literal: true
module GitlabSubscriptions
class ApplyTrialService
def execute(apply_trial_params)
response = client.generate_trial(apply_trial_params)
if response[:success]
{ success: true }
else
{ success: false, errors: response.dig(:data, :errors) }
end
end
end
private
def client
Gitlab::SubscriptionPortal::Client.new
end
end
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
resources :trials, only: [:new] do resources :trials, only: [:new] do
collection do collection do
post :create_lead post :create_lead
get :select_namespace, action: :select get :select
post :apply
end end
end end
...@@ -69,6 +69,14 @@ describe TrialRegistrationsController do ...@@ -69,6 +69,14 @@ describe TrialRegistrationsController do
post :create, params: { user: user_params } post :create, params: { user: user_params }
end end
it 'does not trigger user_create event when data is invalid' do
user_params[:email] = ''
expect_any_instance_of(SystemHooksService).not_to receive(:execute_hooks_for).with(an_instance_of(User), :create)
post :create, params: { user: user_params }
end
end end
end end
end end
......
...@@ -61,7 +61,7 @@ describe TrialsController do ...@@ -61,7 +61,7 @@ describe TrialsController do
it 'redirects user to Step 3' do it 'redirects user to Step 3' do
post :create_lead post :create_lead
expect(response).to redirect_to(select_namespace_trials_url) expect(response).to redirect_to(select_trials_url)
end end
end end
...@@ -76,4 +76,42 @@ describe TrialsController do ...@@ -76,4 +76,42 @@ describe TrialsController do
end end
end end
end end
describe '#select' do
it_behaves_like 'an authenticated endpoint', :get, :select
end
describe '#apply' do
let(:user) { create(:user) }
let(:namespace) { create(:namespace, owner_id: user.id, path: 'namespace-test') }
let(:apply_trial_result) { nil }
before do
sign_in(user)
expect_any_instance_of(GitlabSubscriptions::ApplyTrialService).to receive(:execute) do
{ success: apply_trial_result }
end
end
context 'on success' do
let(:apply_trial_result) { true }
it "redirects to group's path with the parameter trial as true" do
post :apply, params: { namespace_id: namespace.id }
expect(response).to redirect_to("/#{namespace.path}?trial=true")
end
end
context 'on failure' do
let(:apply_trial_result) { false }
it 'redirects to new select namespaces for trials path' do
post :apply, params: { namespace_id: namespace.id }
expect(response).to redirect_to(select_trials_path)
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