Welcome to mirror list, hosted at ThFree Co, Russian Federation.

signup_spec.rb « users « features « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 968308938d1f300fef0077abe1a0d6b31e77a78f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# frozen_string_literal: true

require 'spec_helper'

RSpec.shared_examples 'Signup name validation' do |field, max_length, label|
  shared_examples 'signup validation' do
    before do
      visit new_user_registration_path
    end

    describe "#{field} validation" do
      it "does not show an error border if the user's fullname length is not longer than #{max_length} characters" do
        fill_in field, with: 'u' * max_length

        expect(find('.name')).not_to have_css '.gl-field-error-outline'
      end

      it 'shows an error border if the user\'s fullname contains an emoji' do
        simulate_input("##{field}", 'Ehsan 🦋')

        expect(find('.name')).to have_css '.gl-field-error-outline'
      end

      it "shows an error border if the user\'s fullname is longer than #{max_length} characters" do
        fill_in field, with: 'n' * (max_length + 1)

        expect(find('.name')).to have_css '.gl-field-error-outline'
      end

      it "shows an error message if the user\'s #{label} is longer than #{max_length} characters" do
        fill_in field, with: 'n' * (max_length + 1)

        expect(page).to have_content("#{label} is too long (maximum is #{max_length} characters).")
      end

      it 'shows an error message if the username contains emojis' do
        simulate_input("##{field}", 'Ehsan 🦋')

        expect(page).to have_content("Invalid input, please avoid emoji")
      end
    end
  end

  include_examples 'signup validation'

  # Inline `shared_example 'signup validation'` again after feature flag
  # `restyle_login_page` was removed.
  context 'with feature flag restyle_login_page disabled' do
    before do
      stub_feature_flags(restyle_login_page: false)
    end

    include_examples 'signup validation'
  end
end

RSpec.describe 'Signup', :js, feature_category: :user_profile do
  include TermsHelper

  let(:new_user) { build_stubbed(:user) }

  let(:terms_text) do
    <<~TEXT.squish
      By clicking Register or registering through a third party you accept the
      Terms of Use and acknowledge the Privacy Policy and Cookie Policy
    TEXT
  end

  shared_examples 'signup process' do
    def confirm_email
      new_user_token = User.find_by_email(new_user.email).confirmation_token

      visit user_confirmation_path(confirmation_token: new_user_token)
    end

    before do
      stub_feature_flags(arkose_labs_signup_challenge: false)
      stub_application_setting(require_admin_approval_after_user_signup: false)
    end

    describe 'username validation' do
      before do
        visit new_user_registration_path
      end

      it 'does not show an error border if the username is available' do
        fill_in 'new_user_username', with: 'new-user'
        wait_for_requests

        expect(find('.username')).not_to have_css '.gl-field-error-outline'
      end

      it 'does not show an error border if the username contains dots (.)' do
        simulate_input('#new_user_username', 'new.user.username')
        wait_for_requests

        expect(find('.username')).not_to have_css '.gl-field-error-outline'
      end

      it 'does not show an error border if the username length is not longer than 255 characters' do
        fill_in 'new_user_username', with: 'u' * 255
        wait_for_requests

        expect(find('.username')).not_to have_css '.gl-field-error-outline'
      end

      it 'shows an error border if the username already exists' do
        existing_user = create(:user)

        fill_in 'new_user_username', with: existing_user.username
        wait_for_requests

        expect(find('.username')).to have_css '.gl-field-error-outline'
      end

      it 'shows a success border if the username is available' do
        fill_in 'new_user_username', with: 'new-user'
        wait_for_requests

        expect(find('.username')).to have_css '.gl-field-success-outline'
      end

      it 'shows an error border if the username contains special characters' do
        fill_in 'new_user_username', with: 'new$user!username'
        wait_for_requests

        expect(find('.username')).to have_css '.gl-field-error-outline'
      end

      it 'shows an error border if the username is longer than 255 characters' do
        fill_in 'new_user_username', with: 'u' * 256
        wait_for_requests

        expect(find('.username')).to have_css '.gl-field-error-outline'
      end

      it 'shows an error message if the username is longer than 255 characters' do
        fill_in 'new_user_username', with: 'u' * 256
        wait_for_requests

        expect(page).to have_content("Username is too long (maximum is 255 characters).")
      end

      it 'shows an error message if the username is less than 2 characters' do
        fill_in 'new_user_username', with: 'u'
        wait_for_requests

        expect(page).to have_content("Username is too short (minimum is 2 characters).")
      end

      it 'shows an error message on submit if the username contains special characters' do
        fill_in 'new_user_username', with: 'new$user!username'
        wait_for_requests

        click_button "Register"

        expect(page).to have_content("Please create a username with only alphanumeric characters.")
      end

      it 'shows an error border if the username contains emojis' do
        simulate_input('#new_user_username', 'ehsan😀')

        expect(find('.username')).to have_css '.gl-field-error-outline'
      end

      it 'shows an error message if the username contains emojis' do
        simulate_input('#new_user_username', 'ehsan😀')

        expect(page).to have_content("Invalid input, please avoid emoji")
      end

      it 'shows a pending message if the username availability is being fetched',
        quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/31484' do
        fill_in 'new_user_username', with: 'new-user'

        expect(find('.username > .validation-pending')).not_to have_css '.hide'
      end

      it 'shows a success message if the username is available' do
        fill_in 'new_user_username', with: 'new-user'
        wait_for_requests

        expect(find('.username > .validation-success')).not_to have_css '.hide'
      end

      it 'shows an error message if the username is unavailable' do
        existing_user = create(:user)

        fill_in 'new_user_username', with: existing_user.username
        wait_for_requests

        expect(find('.username > .validation-error')).not_to have_css '.hide'
      end

      it 'shows a success message if the username is corrected and then available' do
        fill_in 'new_user_username', with: 'new-user$'
        wait_for_requests
        fill_in 'new_user_username', with: 'new-user'
        wait_for_requests

        expect(page).to have_content("Username is available.")
      end
    end

    context 'with no errors' do
      context 'when sending confirmation email' do
        before do
          stub_application_setting_enum('email_confirmation_setting', 'hard')
        end

        context 'when email confirmation setting is not `soft`' do
          before do
            stub_feature_flags(identity_verification: false)
          end

          it 'creates the user account and sends a confirmation email, and pre-fills email address after confirming' do
            visit new_user_registration_path

            expect { fill_in_sign_up_form(new_user) }.to change { User.count }.by(1)
            expect(page).to have_current_path users_almost_there_path, ignore_query: true
            expect(page).to have_content("Please check your email (#{new_user.email}) to confirm your account")

            confirm_email

            expect(find_field('Username or primary email').value).to eq(new_user.email)
          end
        end

        context 'when email confirmation setting is `soft`' do
          before do
            stub_application_setting_enum('email_confirmation_setting', 'soft')
          end

          it 'creates the user account and sends a confirmation email' do
            visit new_user_registration_path

            expect { fill_in_sign_up_form(new_user) }.to change { User.count }.by(1)
            expect(page).to have_current_path dashboard_projects_path
          end
        end
      end

      context "when not sending confirmation email" do
        before do
          stub_application_setting_enum('email_confirmation_setting', 'off')
        end

        it 'creates the user account and goes to dashboard' do
          visit new_user_registration_path

          fill_in_sign_up_form(new_user)

          expect(page).to have_current_path dashboard_projects_path
        end
      end

      context 'with required admin approval enabled' do
        before do
          stub_application_setting(require_admin_approval_after_user_signup: true)
        end

        it 'creates the user but does not sign them in' do
          visit new_user_registration_path

          expect { fill_in_sign_up_form(new_user) }.to change { User.count }.by(1)
          expect(page).to have_current_path new_user_session_path, ignore_query: true
          expect(page).to have_content(<<~TEXT.squish)
            You have signed up successfully. However, we could not sign you in
            because your account is awaiting approval from your GitLab administrator
          TEXT
        end
      end
    end

    context 'with errors' do
      it "displays the errors" do
        create(:user, email: new_user.email)
        visit new_user_registration_path

        fill_in_sign_up_form(new_user)

        expect(page).to have_current_path user_registration_path, ignore_query: true
        expect(page).to have_content("error prohibited this user from being saved")
        expect(page).to have_content("Email has already been taken")
      end

      it 'redisplays all fields except password' do
        create(:user, email: new_user.email)
        visit new_user_registration_path

        fill_in_sign_up_form(new_user)

        expect(page).to have_current_path user_registration_path, ignore_query: true
        expect(page.body).not_to match(/#{new_user.password}/)

        expect(find_field('First name').value).to eq(new_user.first_name)
        expect(find_field('Last name').value).to eq(new_user.last_name)
        expect(find_field('Username').value).to eq(new_user.username)
        expect(find_field('Email').value).to eq(new_user.email)
      end
    end

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

      it 'renders text that the user confirms terms by signing in' do
        visit new_user_registration_path
        expect(page).to have_content(terms_text)

        fill_in_sign_up_form(new_user)

        expect(page).to have_current_path(dashboard_projects_path)
      end

      it_behaves_like 'Signup name validation', 'new_user_first_name', 127, 'First name'
      it_behaves_like 'Signup name validation', 'new_user_last_name', 127, 'Last name'
    end

    context 'when reCAPTCHA and invisible captcha are enabled' do
      before do
        stub_application_setting(invisible_captcha_enabled: true)
        stub_application_setting(recaptcha_enabled: true)
        allow_next_instance_of(RegistrationsController) do |instance|
          allow(instance).to receive(:verify_recaptcha).and_return(true)
        end
      end

      context 'when reCAPTCHA detects malicious behaviour' do
        before do
          allow_next_instance_of(RegistrationsController) do |instance|
            allow(instance).to receive(:verify_recaptcha).and_return(false)
          end
        end

        it 'prevents from signing up' do
          visit new_user_registration_path

          expect { fill_in_sign_up_form(new_user) }.not_to change { User.count }
          expect(page).to have_content(_('There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'))
          expect(page).to have_content(
            "Minimum length is #{Gitlab::CurrentSettings.minimum_password_length} characters")
        end
      end

      context 'when invisible captcha detects malicious behaviour' do
        it 'prevents from signing up' do
          visit new_user_registration_path

          expect { fill_in_sign_up_form(new_user) }.not_to change { User.count }
          expect(page).to have_content('That was a bit too quick! Please resubmit.')
        end
      end
    end

    it 'allows visiting of a page after initial registration' do
      visit new_user_registration_path

      fill_in_sign_up_form(new_user)

      visit new_project_path

      expect(page).to have_current_path(new_project_path)
    end

    it 'does not redisplay the password' do
      create(:user, email: new_user.email)
      visit new_user_registration_path

      fill_in_sign_up_form(new_user)

      expect(page).to have_current_path user_registration_path, ignore_query: true
      expect(page.body).not_to match(/#{new_user.password}/)
    end

    context 'with invalid email' do
      it_behaves_like 'user email validation' do
        let(:path) { new_user_registration_path }
      end
    end
  end

  include_examples 'signup process'

  # Inline `shared_example 'signup process'` again after feature flag
  # `restyle_login_page` was removed.
  context 'with feature flag restyle_login_page disabled' do
    let(:terms_text) do
      <<~TEXT.squish
        By clicking Register, I agree that I have read and accepted the Terms of
        Use and Privacy Policy
      TEXT
    end

    before do
      stub_feature_flags(restyle_login_page: false)
    end

    include_examples 'signup process'
  end
end