Commit fb4286fe authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'company-form-submit' into 'master'

Add hidden fields to new about your company page and direct to correct service call

See merge request gitlab-org/gitlab!83124
parents eff8d2b4 f6b210ac
......@@ -66,7 +66,7 @@ Rails.application.routes.draw do
end
Gitlab.ee do
resources :company, only: [:new]
resource :company, only: [:new, :create], controller: 'company'
resources :groups, only: [:new, :create]
resources :projects, only: [:new, :create]
resources :groups_projects, only: [:new, :create] do
......
......@@ -5,12 +5,14 @@ import RegistrationForm from 'ee/registrations/components/company_form.vue';
export default () => {
const el = document.querySelector('#js-company-registration-form');
const { trial, createLeadPath } = el.dataset;
const { submitPath, trial } = el.dataset;
return new Vue({
el,
apolloProvider,
provide: { createLeadPath },
provide: {
submitPath,
},
render(createElement) {
return createElement(RegistrationForm, {
props: { trial },
......
......@@ -28,7 +28,7 @@ export default {
CountryOrRegionSelector,
RegistrationTrialToggle,
},
inject: ['createLeadPath'],
inject: ['submitPath'],
props: {
trial: {
type: Boolean,
......@@ -84,7 +84,7 @@ export default {
</script>
<template>
<gl-form :action="createLeadPath" method="post">
<gl-form :action="submitPath" method="post">
<input :value="$options.csrf.token" type="hidden" name="authenticity_token" />
<gl-form-text class="gl-font-base gl-text-gray-400 gl-pb-3">{{ descriptionText }}</gl-form-text>
<div class="gl-display-flex gl-flex-direction-column gl-sm-flex-direction-row gl-mt-5">
......
......@@ -5,9 +5,47 @@ module Registrations
layout 'minimal'
before_action :check_if_gl_com_or_dev
before_action :authenticate_user!
feature_category :onboarding
def new
end
def create
result = GitlabSubscriptions::CreateTrialOrLeadService.new(user: current_user, params: permitted_params).execute
if result.success?
redirect_to new_users_sign_up_groups_project_path(redirect_param)
else
flash.now[:alert] = result[:message]
render :new, status: result.http_status
end
end
private
def permitted_params
params.permit(
:company_name,
:company_size,
:phone_number,
:country,
:state,
:website_url,
# previous step(s) data
:role,
:jtbd,
:comment,
:trial
)
end
def redirect_param
if params[:trial] == 'true'
{ trial_onboarding_flow: true }
else
{ skip_trial: true }
end
end
end
end
......@@ -25,6 +25,13 @@ module EE
}.merge(params.slice(:first_name, :last_name, :company_name, :company_size, :phone_number, :country, :state).to_unsafe_h.symbolize_keys)
end
def create_company_form_data
submit_params = glm_params.merge(params.slice(:trial, :role, :jtbd, :comment).to_unsafe_h.symbolize_keys)
{
submit_path: users_sign_up_company_path(submit_params)
}
end
def should_ask_company_question?
TRIAL_ONBOARDING_SOURCE_URLS.exclude?(glm_params[:glm_source])
end
......
# frozen_string_literal: true
module GitlabSubscriptions
class CreateTrialOrLeadService
def initialize(user:, params:)
@params = params.merge(hardcoded_values).merge(user_values(user))
end
def execute
generate_response
result
end
private
attr_reader :response, :params
def hardcoded_values
{
provider: 'gitlab',
skip_email_confirmation: true,
gitlab_com_trial: true
}
end
def user_values(user)
{
uid: user.id,
first_name: user.first_name,
last_name: user.last_name,
work_email: user.email,
setup_for_company: user.setup_for_company,
newsletter_segment: user.email_opted_in
}
end
def result
if response[:success]
ServiceResponse.success
else
ServiceResponse.error(message: response.dig(:data, :errors), http_status: :unprocessable_entity)
end
end
def generate_response
@response = if trial?
client.generate_trial(trial_user: params)
else
client.generate_hand_raise_lead(params)
end
end
def trial?
Gitlab::Utils.to_boolean(params[:trial])
end
def client
Gitlab::SubscriptionPortal::Client
end
end
end
......@@ -5,7 +5,7 @@
%h2.gl-pb-5.gl-my-0
= _("About your company")
#js-company-registration-form{ data: { trial: params[:trial] == 'true', create_lead_path: create_lead_trials_path } }
#js-company-registration-form{ data: create_company_form_data }
.col-md-4.gl-display-inline-flex.gl-vertical-align-middle
= image_tag 'illustrations/saas-trial-illustration.svg', alt: '', class: 'gl-display-none d-md-inline gl-w-full'
......@@ -5,32 +5,104 @@ require 'spec_helper'
RSpec.describe Registrations::CompanyController do
let_it_be(:user) { create(:user) }
describe 'GET #new' do
subject { get :new }
let(:logged_in) { true }
before do
sign_in(user) if logged_in
allow(::Gitlab).to receive(:com?).and_return(true)
end
shared_examples 'user authentication' do
context 'when not authenticated' do
let(:logged_in) { false }
context 'with an unauthenticated user' do
it { is_expected.to have_gitlab_http_status(:redirect) }
it { is_expected.to redirect_to(new_user_session_path) }
end
context 'with an authenticated user' do
let(:com) { true }
context 'when authenticated' do
it { is_expected.to have_gitlab_http_status(:ok) }
end
end
shared_examples 'a dot-com only feature' do
context 'when not on gitlab.com' do
before do
sign_in(user)
allow(::Gitlab).to receive(:com?).and_return(com)
allow(::Gitlab).to receive(:com?).and_return(false)
end
it { is_expected.to have_gitlab_http_status(:not_found) }
end
context 'when on .com' do
context 'when on gitlab.com' do
it { is_expected.to have_gitlab_http_status(:ok) }
end
end
describe '#new' do
subject { get :new }
it_behaves_like 'user authentication'
it_behaves_like 'a dot-com only feature'
context 'on render' do
it { is_expected.to render_template 'layouts/minimal' }
it { is_expected.to render_template(:new) }
end
end
context 'when not on .com' do
let(:com) { false }
describe '#create' do
using RSpec::Parameterized::TableSyntax
it { is_expected.to have_gitlab_http_status(:not_found) }
let(:params) do
{
company_name: 'GitLab',
company_size: '1-99',
phone_number: '+1 23 456-78-90',
country: 'US',
state: 'CA',
website_url: 'gitlab.com'
}
end
context 'on success' do
where(:trial, :redirect_query) do
'true' | { trial_onboarding_flow: true }
'false' | { skip_trial: true }
end
with_them do
it 'creates trial or lead and redirects to the correct path' do
expect_next_instance_of(
GitlabSubscriptions::CreateTrialOrLeadService,
user: user,
params: ActionController::Parameters.new(params.merge({ trial: trial })).permit!
) do |service|
expect(service).to receive(:execute).and_return(ServiceResponse.success)
end
post :create, params: params.merge({ trial: trial })
expect(response).to have_gitlab_http_status(:redirect)
expect(response).to redirect_to(new_users_sign_up_groups_project_path(redirect_query))
end
end
end
context 'on failure' do
where(trial: %w[true false])
with_them do
it 'renders company page :new' do
expect_next_instance_of(GitlabSubscriptions::CreateTrialOrLeadService) do |service|
expect(service).to receive(:execute).and_return(ServiceResponse.error(message: 'failed'))
end
post :create, params: params.merge({ trial: trial })
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:new)
expect(flash[:alert]).to eq('failed')
end
end
end
end
......
......@@ -18,27 +18,27 @@ RSpec.describe 'Company Information', :js do
end
context 'send company information to create lead' do
it 'with all required fields' do
trial_user_params = {
"company_name" => 'GitLab',
"company_size" => '1 - 99'.delete(' '),
"phone_number" => '+1 23 456-78-90',
"country" => 'US',
"state" => 'CA',
"work_email" => user.email,
"uid" => user.id,
"setup_for_company" => user.setup_for_company,
"skip_email_confirmation" => true,
"gitlab_com_trial" => true,
"provider" => "gitlab",
"newsletter_segment" => user.email_opted_in,
"website_url" => 'gitlab.com'
}
using RSpec::Parameterized::TableSyntax
lead_params = {
trial_user: ActionController::Parameters.new(trial_user_params).permit!
let(:params) do
{
company_name: 'GitLab',
company_size: '1-99',
phone_number: '+1 23 456-78-90',
country: 'US',
state: 'CA',
website_url: 'gitlab.com',
trial: 'false'
}
end
where(:service_response, :current_path, :page_content) do
ServiceResponse.success | new_users_sign_up_groups_project_path | 'Create or import your first project'
ServiceResponse.error(message: 'failed') | users_sign_up_company_path | 'failed'
end
with_them do
it 'redirects to correct path' do
fill_in 'company_name', with: 'GitLab'
select '1 - 99', from: 'company_size'
select 'United States of America', from: 'country'
......@@ -46,11 +46,19 @@ RSpec.describe 'Company Information', :js do
fill_in 'website_url', with: 'gitlab.com'
fill_in 'phone_number', with: '+1 23 456-78-90'
expect_next_instance_of(GitlabSubscriptions::CreateLeadService) do |service|
expect(service).to receive(:execute).with(lead_params).and_return({ success: true })
expect_next_instance_of(
GitlabSubscriptions::CreateTrialOrLeadService,
user: user,
params: ActionController::Parameters.new(params).permit!
) do |service|
expect(service).to receive(:execute).and_return(service_response)
end
click_button 'Continue'
expect(page).to have_current_path(current_path, ignore_query: true)
expect(page).to have_content(page_content)
end
end
end
end
......@@ -17,7 +17,7 @@ describe('RegistrationForm', () => {
return mountFunction(RegistrationForm, {
localVue,
provide: {
createLeadPath: SUBMIT_PATH,
submitPath: SUBMIT_PATH,
},
propsData: { trial: true },
});
......
......@@ -56,6 +56,38 @@ RSpec.describe EE::TrialHelper do
end
end
describe '#create_company_form_data' do
let(:extra_params) do
{
role: '_params_role_',
jtbd: '_params_jtbd_',
comment: '_params_comment_'
}
end
let(:params) do
ActionController::Parameters.new(extra_params)
end
before do
allow(helper).to receive(:params).and_return(params)
end
it 'provides expected form data' do
keys = [:submit_path]
expect(helper.create_company_form_data.keys.map(&:to_sym)).to match_array(keys)
end
it 'allows overriding data with params' do
submit_path = {
submit_path: '/users/sign_up/company?comment=_params_comment_&jtbd=_params_jtbd_&role=_params_role_'
}
expect(helper.create_company_form_data).to match(submit_path)
end
end
describe '#should_ask_company_question?' do
before do
allow(helper).to receive(:glm_params).and_return(glm_source ? { glm_source: glm_source } : {})
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GitlabSubscriptions::CreateTrialOrLeadService do
let(:user) { build(:user) }
describe '#execute' do
using RSpec::Parameterized::TableSyntax
where(:trial, :service) do
'true' | :generate_trial
'false' | :generate_hand_raise_lead
end
with_them do
it 'successfully creates a trial or lead' do
allow(Gitlab::SubscriptionPortal::Client).to receive(service).and_return({ success: true })
result = described_class.new(**{ user: user, params: { trial: trial } }).execute
expect(result.is_a?(ServiceResponse)).to be true
expect(result.success?).to be true
end
it 'error while creating trial or lead' do
allow(Gitlab::SubscriptionPortal::Client).to receive(service).and_return({ success: false })
result = described_class.new(**{ user: user, params: { trial: trial } }).execute
expect(result.is_a?(ServiceResponse)).to be true
expect(result.success?).to be false
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