registrations_controller_spec.rb 2.49 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe RegistrationsController do
  describe '#create' do
5 6 7 8 9 10 11
    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
12 13
      end

14 15 16 17 18 19 20 21
      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
22

23 24 25
      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)
26

27 28 29 30 31
          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
32 33 34 35 36 37 38 39 40
      end

      context 'when signup_enabled? is false' do
        it 'redirects to sign_in' do
          allow_any_instance_of(ApplicationSetting).to receive(:signup_enabled?).and_return(false)

          expect { post(:create, user_params) }.not_to change(User, :count)
          expect(response).to redirect_to(new_user_session_path)
        end
41 42 43
      end
    end

44 45 46 47 48 49 50 51
    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')
52 53

        post(:create, user_params)
54 55

        expect(response).to render_template(:new)
56
        expect(flash[:alert]).to include 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'
57 58 59 60 61 62 63 64 65 66 67
      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.'
68 69 70 71
      end
    end
  end
end