diff options
author | Drew Blessing <drew@gitlab.com> | 2017-01-03 10:08:21 +0300 |
---|---|---|
committer | Drew Blessing <drew@gitlab.com> | 2017-01-03 10:35:42 +0300 |
commit | ca1c492bafa024ad4c185e930af57e1f8090ca77 (patch) | |
tree | e193e6b02c2abef0d9c830e2230f047f6fadf2d2 /spec/controllers/registrations_controller_spec.rb | |
parent | de25604fbca2f7005754d821d571bbcb1cc510ac (diff) |
Properly handle failed reCAPTCHA on user registration
If a user presses the 'Register' button too quickly after attempting
to solve the reCAPTCHA, or the reCAPTCHA is not solved at all, the
user would experience a 500 error. Now, the case is properly
handled and the user will be sent back to the registration page
with a clear error message and can try again.
Diffstat (limited to 'spec/controllers/registrations_controller_spec.rb')
-rw-r--r-- | spec/controllers/registrations_controller_spec.rb | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb index 026f41c926b..42fbfe89368 100644 --- a/spec/controllers/registrations_controller_spec.rb +++ b/spec/controllers/registrations_controller_spec.rb @@ -2,30 +2,60 @@ require 'spec_helper' describe RegistrationsController do describe '#create' do - around(:each) do |example| - perform_enqueued_jobs do - example.run + let(:user_params) { { user: { name: 'new_user', username: 'new_username', email: 'new@user.com', password: 'Any_password' } } } + + context 'email confirmation' do + around(:each) do |example| + perform_enqueued_jobs do + example.run + end end - end - let(:user_params) { { user: { name: "new_user", username: "new_username", email: "new@user.com", password: "Any_password" } } } + context 'when send_user_confirmation_email is false' do + it 'signs the user in' do + allow_any_instance_of(ApplicationSetting).to receive(:send_user_confirmation_email).and_return(false) + + expect { post(:create, user_params) }.not_to change{ ActionMailer::Base.deliveries.size } + expect(subject.current_user).not_to be_nil + end + end - context 'when sending email confirmation' do - before { allow_any_instance_of(ApplicationSetting).to receive(:send_user_confirmation_email).and_return(false) } + context 'when send_user_confirmation_email is true' do + it 'does not authenticate user and sends confirmation email' do + allow_any_instance_of(ApplicationSetting).to receive(:send_user_confirmation_email).and_return(true) - it 'logs user in directly' do - expect { post(:create, user_params) }.not_to change{ ActionMailer::Base.deliveries.size } - expect(subject.current_user).not_to be_nil + post(:create, user_params) + + expect(ActionMailer::Base.deliveries.last.to.first).to eq(user_params[:user][:email]) + expect(subject.current_user).to be_nil + end end end - context 'when not sending email confirmation' do - before { allow_any_instance_of(ApplicationSetting).to receive(:send_user_confirmation_email).and_return(true) } + context 'when reCAPTCHA is enabled' do + before do + stub_application_setting(recaptcha_enabled: true) + end + + it 'displays an error when the reCAPTCHA is not solved' do + # Without this, `verify_recaptcha` arbitraily returns true in test env + Recaptcha.configuration.skip_verify_env.delete('test') - it 'does not authenticate user and sends confirmation email' do post(:create, user_params) - expect(ActionMailer::Base.deliveries.last.to.first).to eq(user_params[:user][:email]) - expect(subject.current_user).to be_nil + + expect(response).to render_template(:new) + expect(flash[:alert]).to include 'There was an error with the reCAPTCHA. Please re-solve the reCAPTCHA.' + end + + it 'redirects to the dashboard when the recaptcha is solved' do + # Avoid test ordering issue and ensure `verify_recaptcha` returns true + unless Recaptcha.configuration.skip_verify_env.include?('test') + Recaptcha.configuration.skip_verify_env << 'test' + end + + post(:create, user_params) + + expect(flash[:notice]).to include 'Welcome! You have signed up successfully.' end end end |