summaryrefslogtreecommitdiff
path: root/spec/controllers/registrations_controller_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/controllers/registrations_controller_spec.rb')
-rw-r--r--spec/controllers/registrations_controller_spec.rb282
1 files changed, 129 insertions, 153 deletions
diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb
index 60957dc72e6..501d8d4a78d 100644
--- a/spec/controllers/registrations_controller_spec.rb
+++ b/spec/controllers/registrations_controller_spec.rb
@@ -37,65 +37,123 @@ RSpec.describe RegistrationsController do
expect(response).to redirect_to(new_user_session_path(anchor: 'register-pane'))
end
end
+ end
- context 'with sign up flow and terms_opt_in experiment being enabled' do
- before do
- stub_experiment(signup_flow: true, terms_opt_in: true)
- end
+ describe '#create' do
+ let(:base_user_params) { { first_name: 'first', last_name: 'last', username: 'new_username', email: 'new@user.com', password: 'Any_password' } }
+ let(:user_params) { { user: base_user_params } }
+
+ subject { post(:create, params: user_params) }
- context 'when user is not part of the experiment' do
+ context '`blocked_pending_approval` state' do
+ context 'when the feature is enabled' do
before do
- stub_experiment_for_user(signup_flow: true, terms_opt_in: false)
+ stub_feature_flags(admin_approval_for_new_user_signups: true)
end
- it 'tracks event with right parameters' do
- expect(Gitlab::Tracking).to receive(:event).with(
- 'Growth::Acquisition::Experiment::TermsOptIn',
- 'start',
- label: anything,
- property: 'control_group'
- )
+ context 'when the `require_admin_approval_after_user_signup` setting is turned on' do
+ before do
+ stub_application_setting(require_admin_approval_after_user_signup: true)
+ end
- subject
+ it 'signs up the user in `blocked_pending_approval` state' do
+ subject
+ created_user = User.find_by(email: 'new@user.com')
+
+ expect(created_user).to be_present
+ expect(created_user.blocked_pending_approval?).to eq(true)
+ end
+
+ it 'does not log in the user after sign up' do
+ subject
+
+ expect(controller.current_user).to be_nil
+ end
+
+ it 'shows flash message after signing up' do
+ subject
+
+ expect(response).to redirect_to(new_user_session_path(anchor: 'login-pane'))
+ expect(flash[:notice])
+ .to eq('You have signed up successfully. However, we could not sign you in because your account is awaiting approval from your GitLab administrator.')
+ end
+
+ context 'email confirmation' do
+ context 'when `send_user_confirmation_email` is true' do
+ before do
+ stub_application_setting(send_user_confirmation_email: true)
+ end
+
+ it 'does not send a confirmation email' do
+ expect { subject }
+ .not_to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
+ end
+ end
+ end
+ end
+
+ context 'when the `require_admin_approval_after_user_signup` setting is turned off' do
+ before do
+ stub_application_setting(require_admin_approval_after_user_signup: false)
+ end
+
+ it 'signs up the user in `active` state' do
+ subject
+ created_user = User.find_by(email: 'new@user.com')
+
+ expect(created_user).to be_present
+ expect(created_user.active?).to eq(true)
+ end
+
+ it 'does not show any flash message after signing up' do
+ subject
+
+ expect(flash[:notice]).to be_nil
+ end
+
+ context 'email confirmation' do
+ context 'when `send_user_confirmation_email` is true' do
+ before do
+ stub_application_setting(send_user_confirmation_email: true)
+ end
+
+ it 'sends a confirmation email' do
+ expect { subject }
+ .to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
+ end
+ end
+ end
end
end
- context 'when user is part of the experiment' do
+ context 'when the feature is disabled' do
before do
- stub_experiment_for_user(signup_flow: true, terms_opt_in: true)
+ stub_feature_flags(admin_approval_for_new_user_signups: false)
end
- it 'tracks event with right parameters' do
- expect(Gitlab::Tracking).to receive(:event).with(
- 'Growth::Acquisition::Experiment::TermsOptIn',
- 'start',
- label: anything,
- property: 'experimental_group'
- )
+ context 'when the `require_admin_approval_after_user_signup` setting is turned on' do
+ before do
+ stub_application_setting(require_admin_approval_after_user_signup: true)
+ end
- subject
+ it 'signs up the user in `active` state' do
+ subject
+
+ created_user = User.find_by(email: 'new@user.com')
+ expect(created_user).to be_present
+ expect(created_user.active?).to eq(true)
+ end
end
end
end
- end
-
- describe '#create' do
- let(:base_user_params) { { name: 'new_user', username: 'new_username', email: 'new@user.com', password: 'Any_password' } }
- let(:user_params) { { user: base_user_params } }
context 'email confirmation' do
- around do |example|
- perform_enqueued_jobs do
- example.run
- end
- end
-
context 'when send_user_confirmation_email is false' do
it 'signs the user in' do
stub_application_setting(send_user_confirmation_email: false)
- expect { post(:create, params: user_params) }.not_to change { ActionMailer::Base.deliveries.size }
- expect(subject.current_user).not_to be_nil
+ expect { subject }.not_to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
+ expect(controller.current_user).not_to be_nil
end
end
@@ -111,10 +169,8 @@ RSpec.describe RegistrationsController do
end
it 'does not authenticate the user and sends a confirmation email' do
- post(:create, params: user_params)
-
- expect(ActionMailer::Base.deliveries.last.to.first).to eq(user_params[:user][:email])
- expect(subject.current_user).to be_nil
+ expect { subject }.to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
+ expect(controller.current_user).to be_nil
end
end
@@ -125,9 +181,8 @@ RSpec.describe RegistrationsController do
end
it 'authenticates the user and sends a confirmation email' do
- post(:create, params: user_params)
-
- expect(ActionMailer::Base.deliveries.last.to.first).to eq(user_params[:user][:email])
+ expect { subject }.to have_enqueued_mail(DeviseMailer, :confirmation_instructions)
+ expect(controller.current_user).to be_present
expect(response).to redirect_to(users_sign_up_welcome_path)
end
end
@@ -137,7 +192,7 @@ RSpec.describe RegistrationsController do
it 'redirects to sign_in' do
stub_application_setting(signup_enabled: false)
- expect { post(:create, params: user_params) }.not_to change(User, :count)
+ expect { subject }.not_to change(User, :count)
expect(response).to redirect_to(new_user_session_path)
end
end
@@ -158,14 +213,14 @@ RSpec.describe RegistrationsController do
it 'displays an error when the reCAPTCHA is not solved' do
allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false)
- post(:create, params: user_params)
+ subject
expect(response).to render_template(:new)
expect(flash[:alert]).to eq(_('There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'))
end
it 'redirects to the welcome page when the reCAPTCHA is solved' do
- post(:create, params: user_params)
+ subject
expect(response).to redirect_to(users_sign_up_welcome_path)
end
@@ -258,102 +313,26 @@ RSpec.describe RegistrationsController do
end
end
- context 'when terms are enforced' do
- before do
- enforce_terms
- end
-
- it 'redirects back with a notice when the checkbox was not checked' do
- post :create, params: user_params
-
- expect(flash[:alert]).to eq(_('You must accept our Terms of Service and privacy policy in order to register an account'))
- end
-
- it 'creates the user with agreement when terms are accepted' do
- post :create, params: user_params.merge(terms_opt_in: '1')
-
- expect(subject.current_user).to be_present
- expect(subject.current_user.terms_accepted?).to be(true)
- end
-
- context 'when experiment terms_opt_in is enabled' do
+ context 'terms of service' do
+ context 'when terms are enforced' do
before do
- stub_experiment(terms_opt_in: true)
- end
-
- context 'when user is part of the experiment' do
- before do
- stub_experiment_for_user(terms_opt_in: true)
- end
-
- it 'creates the user with accepted terms' do
- post :create, params: user_params
-
- expect(subject.current_user).to be_present
- expect(subject.current_user.terms_accepted?).to be(true)
- end
+ enforce_terms
end
- context 'when user is not part of the experiment' do
- before do
- stub_experiment_for_user(terms_opt_in: false)
- end
-
- it 'creates the user without accepted terms' do
- post :create, params: user_params
+ it 'creates the user with accepted terms' do
+ subject
- expect(flash[:alert]).to eq(_('You must accept our Terms of Service and privacy policy in order to register an account'))
- end
+ expect(controller.current_user).to be_present
+ expect(controller.current_user.terms_accepted?).to be(true)
end
end
- end
-
- describe 'tracking data' do
- context 'with sign up flow and terms_opt_in experiment being enabled' do
- subject { post :create, params: user_params }
-
- before do
- stub_experiment(signup_flow: true, terms_opt_in: true)
- end
-
- it 'records user for the terms_opt_in experiment' do
- expect(controller).to receive(:record_experiment_user).with(:terms_opt_in)
+ context 'when terms are not enforced' do
+ it 'creates the user without accepted terms' do
subject
- end
- context 'when user is not part of the experiment' do
- before do
- stub_experiment_for_user(signup_flow: true, terms_opt_in: false)
- end
-
- it 'tracks event with right parameters' do
- expect(Gitlab::Tracking).to receive(:event).with(
- 'Growth::Acquisition::Experiment::TermsOptIn',
- 'end',
- label: anything,
- property: 'control_group'
- )
-
- subject
- end
- end
-
- context 'when user is part of the experiment' do
- before do
- stub_experiment_for_user(signup_flow: true, terms_opt_in: true)
- end
-
- it 'tracks event with right parameters' do
- expect(Gitlab::Tracking).to receive(:event).with(
- 'Growth::Acquisition::Experiment::TermsOptIn',
- 'end',
- label: anything,
- property: 'experimental_group'
- )
-
- subject
- end
+ expect(controller.current_user).to be_present
+ expect(controller.current_user.terms_accepted?).to be(false)
end
end
end
@@ -361,30 +340,21 @@ RSpec.describe RegistrationsController do
it "logs a 'User Created' message" do
expect(Gitlab::AppLogger).to receive(:info).with(/\AUser Created: username=new_username email=new@user.com.+\z/).and_call_original
- post(:create, params: user_params)
+ subject
end
it 'handles when params are new_user' do
post(:create, params: { new_user: base_user_params })
- expect(subject.current_user).not_to be_nil
+ expect(controller.current_user).not_to be_nil
end
- context 'with the experimental signup flow enabled and the user is part of the experimental group' do
- before do
- stub_experiment(signup_flow: true)
- stub_experiment_for_user(signup_flow: true)
- end
-
- let(:base_user_params) { { first_name: 'First', last_name: 'Last', username: 'new_username', email: 'new@user.com', password: 'Any_password' } }
-
- it 'sets name from first and last name' do
- post :create, params: { new_user: base_user_params }
+ it 'sets name from first and last name' do
+ post :create, params: { new_user: base_user_params }
- expect(User.last.first_name).to eq(base_user_params[:first_name])
- expect(User.last.last_name).to eq(base_user_params[:last_name])
- expect(User.last.name).to eq("#{base_user_params[:first_name]} #{base_user_params[:last_name]}")
- end
+ expect(User.last.first_name).to eq(base_user_params[:first_name])
+ expect(User.last.last_name).to eq(base_user_params[:last_name])
+ expect(User.last.name).to eq("#{base_user_params[:first_name]} #{base_user_params[:last_name]}")
end
end
@@ -507,10 +477,16 @@ RSpec.describe RegistrationsController do
patch :update_registration, params: { user: { role: 'software_developer', setup_for_company: 'false' } }
end
- before do
- sign_in(create(:user))
+ context 'without a signed in user' do
+ it { is_expected.to redirect_to new_user_registration_path }
end
- it { is_expected.to redirect_to(dashboard_projects_path)}
+ context 'with a signed in user' do
+ before do
+ sign_in(create(:user))
+ end
+
+ it { is_expected.to redirect_to(dashboard_projects_path)}
+ end
end
end