registrations_controller_spec.rb 5.7 KB
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

3 4 5
require 'spec_helper'

describe RegistrationsController do
6 7
  include TermsHelper

8
  describe '#create' do
9 10
    let(:base_user_params) { { name: 'new_user', username: 'new_username', email: 'new@user.com', password: 'Any_password' } }
    let(:user_params) { { user: base_user_params } }
11 12

    context 'email confirmation' do
13
      around do |example|
14 15 16
        perform_enqueued_jobs do
          example.run
        end
17 18
      end

19 20
      context 'when send_user_confirmation_email is false' do
        it 'signs the user in' do
21
          stub_application_setting(send_user_confirmation_email: false)
22

B
blackst0ne 已提交
23
          expect { post(:create, params: user_params) }.not_to change { ActionMailer::Base.deliveries.size }
24 25 26
          expect(subject.current_user).not_to be_nil
        end
      end
27

28 29
      context 'when send_user_confirmation_email is true' do
        it 'does not authenticate user and sends confirmation email' do
30
          stub_application_setting(send_user_confirmation_email: true)
31

B
blackst0ne 已提交
32
          post(:create, params: user_params)
33 34 35 36

          expect(ActionMailer::Base.deliveries.last.to.first).to eq(user_params[:user][:email])
          expect(subject.current_user).to be_nil
        end
37 38 39 40
      end

      context 'when signup_enabled? is false' do
        it 'redirects to sign_in' do
41
          stub_application_setting(signup_enabled: false)
42

B
blackst0ne 已提交
43
          expect { post(:create, params: user_params) }.not_to change(User, :count)
44 45
          expect(response).to redirect_to(new_user_session_path)
        end
46 47 48
      end
    end

49
    context 'when reCAPTCHA is enabled' do
50 51 52 53 54
      def fail_recaptcha
        # Without this, `verify_recaptcha` arbitrarily returns true in test env
        Recaptcha.configuration.skip_verify_env.delete('test')
      end

55 56 57 58 59
      before do
        stub_application_setting(recaptcha_enabled: true)
      end

      it 'displays an error when the reCAPTCHA is not solved' do
60
        fail_recaptcha
61

B
blackst0ne 已提交
62
        post(:create, params: user_params)
63 64

        expect(response).to render_template(:new)
65
        expect(flash[:alert]).to include 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'
66 67 68 69 70 71 72 73
      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

B
blackst0ne 已提交
74
        post(:create, params: user_params)
75 76

        expect(flash[:notice]).to include 'Welcome! You have signed up successfully.'
77
      end
78 79 80 81 82 83 84 85 86 87 88

      it 'does not require reCAPTCHA if disabled by feature flag' do
        stub_feature_flags(registrations_recaptcha: false)
        fail_recaptcha

        post(:create, params: user_params)

        expect(controller).not_to receive(:verify_recaptcha)
        expect(flash[:alert]).to be_nil
        expect(flash[:notice]).to include 'Welcome! You have signed up successfully.'
      end
89
    end
90 91 92 93 94 95 96

    context 'when terms are enforced' do
      before do
        enforce_terms
      end

      it 'redirects back with a notice when the checkbox was not checked' do
B
blackst0ne 已提交
97
        post :create, params: user_params
98 99 100 101 102

        expect(flash[:alert]).to match /you must accept our terms/i
      end

      it 'creates the user with agreement when terms are accepted' do
B
blackst0ne 已提交
103
        post :create, params: user_params.merge(terms_opt_in: '1')
104 105 106 107 108

        expect(subject.current_user).to be_present
        expect(subject.current_user.terms_accepted?).to be(true)
      end
    end
109 110 111 112 113 114 115 116 117 118 119 120 121 122

    it "logs a 'User Created' message" do
      stub_feature_flags(registrations_recaptcha: false)

      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)
    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
    end
123
  end
S
Stan Hu 已提交
124 125 126 127 128 129 130 131

  describe '#destroy' do
    let(:user) { create(:user) }

    before do
      sign_in(user)
    end

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    def expect_failure(message)
      expect(flash[:alert]).to eq(message)
      expect(response.status).to eq(303)
      expect(response).to redirect_to profile_account_path
    end

    def expect_password_failure
      expect_failure('Invalid password')
    end

    def expect_username_failure
      expect_failure('Invalid username')
    end

    def expect_success
      expect(flash[:notice]).to eq 'Account scheduled for removal.'
      expect(response.status).to eq(303)
      expect(response).to redirect_to new_user_session_path
    end
S
Stan Hu 已提交
151

152 153 154
    context 'user requires password confirmation' do
      it 'fails if password confirmation is not provided' do
        post :destroy
S
Stan Hu 已提交
155

156 157 158 159
        expect_password_failure
      end

      it 'fails if password confirmation is wrong' do
B
blackst0ne 已提交
160
        post :destroy, params: { password: 'wrong password' }
161 162 163 164 165

        expect_password_failure
      end

      it 'succeeds if password is confirmed' do
B
blackst0ne 已提交
166
        post :destroy, params: { password: '12345678' }
167 168 169 170 171 172 173

        expect_success
      end
    end

    context 'user does not require password confirmation' do
      before do
174 175
        stub_application_setting(password_authentication_enabled_for_web: false)
        stub_application_setting(password_authentication_enabled_for_git: false)
176 177 178 179 180 181 182 183 184
      end

      it 'fails if username confirmation is not provided' do
        post :destroy

        expect_username_failure
      end

      it 'fails if username confirmation is wrong' do
B
blackst0ne 已提交
185
        post :destroy, params: { username: 'wrong username' }
186 187 188 189 190

        expect_username_failure
      end

      it 'succeeds if username is confirmed' do
B
blackst0ne 已提交
191
        post :destroy, params: { username: user.username }
192 193 194

        expect_success
      end
S
Stan Hu 已提交
195 196
    end
  end
197
end