PageRenderTime 152ms CodeModel.GetById 81ms app.highlight 66ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/features/users/signup_spec.rb

https://gitlab.com/tnir/gitlab-ce
Ruby | 505 lines | 418 code | 86 blank | 1 comment | 11 complexity | b136664992c4f1b268029ba2f92cdc0c MD5 | raw file
  1# frozen_string_literal: true
  2
  3require 'spec_helper'
  4
  5shared_examples 'Signup' do
  6  include TermsHelper
  7
  8  let(:new_user) { build_stubbed(:user) }
  9
 10  describe 'username validation', :js do
 11    before do
 12      visit new_user_registration_path
 13    end
 14
 15    it 'does not show an error border if the username is available' do
 16      fill_in 'new_user_username', with: 'new-user'
 17      wait_for_requests
 18
 19      expect(find('.username')).not_to have_css '.gl-field-error-outline'
 20    end
 21
 22    it 'does not show an error border if the username contains dots (.)' do
 23      simulate_input('#new_user_username', 'new.user.username')
 24      wait_for_requests
 25
 26      expect(find('.username')).not_to have_css '.gl-field-error-outline'
 27    end
 28
 29    it 'does not show an error border if the username length is not longer than 255 characters' do
 30      fill_in 'new_user_username', with: 'u' * 255
 31      wait_for_requests
 32
 33      expect(find('.username')).not_to have_css '.gl-field-error-outline'
 34    end
 35
 36    it 'shows an error border if the username already exists' do
 37      existing_user = create(:user)
 38
 39      fill_in 'new_user_username', with: existing_user.username
 40      wait_for_requests
 41
 42      expect(find('.username')).to have_css '.gl-field-error-outline'
 43    end
 44
 45    it 'shows a success border if the username is available' do
 46      fill_in 'new_user_username', with: 'new-user'
 47      wait_for_requests
 48
 49      expect(find('.username')).to have_css '.gl-field-success-outline'
 50    end
 51
 52    it 'shows an error border if the username contains special characters' do
 53      fill_in 'new_user_username', with: 'new$user!username'
 54      wait_for_requests
 55
 56      expect(find('.username')).to have_css '.gl-field-error-outline'
 57    end
 58
 59    it 'shows an error border if the username is longer than 255 characters' do
 60      fill_in 'new_user_username', with: 'u' * 256
 61      wait_for_requests
 62
 63      expect(find('.username')).to have_css '.gl-field-error-outline'
 64    end
 65
 66    it 'shows an error message if the username is longer than 255 characters' do
 67      fill_in 'new_user_username', with: 'u' * 256
 68      wait_for_requests
 69
 70      expect(page).to have_content("Username is too long (maximum is 255 characters).")
 71    end
 72
 73    it 'shows an error message on submit if the username contains special characters' do
 74      fill_in 'new_user_username', with: 'new$user!username'
 75      wait_for_requests
 76
 77      click_button "Register"
 78
 79      expect(page).to have_content("Please create a username with only alphanumeric characters.")
 80    end
 81
 82    it 'shows an error border if the username contains emojis' do
 83      simulate_input('#new_user_username', 'ehsan😀')
 84
 85      expect(find('.username')).to have_css '.gl-field-error-outline'
 86    end
 87
 88    it 'shows an error message if the username contains emojis' do
 89      simulate_input('#new_user_username', 'ehsan😀')
 90
 91      expect(page).to have_content("Invalid input, please avoid emojis")
 92    end
 93
 94    it 'shows a pending message if the username availability is being fetched', :quarantine do
 95      fill_in 'new_user_username', with: 'new-user'
 96
 97      expect(find('.username > .validation-pending')).not_to have_css '.hide'
 98    end
 99
100    it 'shows a success message if the username is available' do
101      fill_in 'new_user_username', with: 'new-user'
102      wait_for_requests
103
104      expect(find('.username > .validation-success')).not_to have_css '.hide'
105    end
106
107    it 'shows an error message if the username is unavailable' do
108      existing_user = create(:user)
109
110      fill_in 'new_user_username', with: existing_user.username
111      wait_for_requests
112
113      expect(find('.username > .validation-error')).not_to have_css '.hide'
114    end
115
116    it 'shows a success message if the username is corrected and then available' do
117      fill_in 'new_user_username', with: 'new-user$'
118      wait_for_requests
119      fill_in 'new_user_username', with: 'new-user'
120      wait_for_requests
121
122      expect(page).to have_content("Username is available.")
123    end
124  end
125
126  context 'with no errors' do
127    context 'when sending confirmation email' do
128      before do
129        stub_application_setting(send_user_confirmation_email: true)
130      end
131
132      context 'when soft email confirmation is not enabled' do
133        before do
134          stub_feature_flags(soft_email_confirmation: false)
135        end
136
137        it 'creates the user account and sends a confirmation email' do
138          visit new_user_registration_path
139
140          fill_in 'new_user_username', with: new_user.username
141          fill_in 'new_user_email', with: new_user.email
142
143          if Gitlab::Experimentation.enabled?(:signup_flow)
144            fill_in 'new_user_first_name', with: new_user.first_name
145            fill_in 'new_user_last_name', with: new_user.last_name
146          else
147            fill_in 'new_user_name', with: new_user.name
148            fill_in 'new_user_email_confirmation', with: new_user.email
149          end
150
151          fill_in 'new_user_password', with: new_user.password
152
153          expect { click_button 'Register' }.to change { User.count }.by(1)
154
155          expect(current_path).to eq users_almost_there_path
156          expect(page).to have_content('Please check your email to confirm your account')
157        end
158      end
159
160      context 'when soft email confirmation is enabled' do
161        before do
162          stub_feature_flags(soft_email_confirmation: true)
163        end
164
165        it 'creates the user account and sends a confirmation email' do
166          visit new_user_registration_path
167
168          fill_in 'new_user_username', with: new_user.username
169          fill_in 'new_user_email', with: new_user.email
170
171          if Gitlab::Experimentation.enabled?(:signup_flow)
172            fill_in 'new_user_first_name', with: new_user.first_name
173            fill_in 'new_user_last_name', with: new_user.last_name
174          else
175            fill_in 'new_user_name', with: new_user.name
176            fill_in 'new_user_email_confirmation', with: new_user.email
177          end
178
179          fill_in 'new_user_password', with: new_user.password
180
181          expect { click_button 'Register' }.to change { User.count }.by(1)
182
183          if Gitlab::Experimentation.enabled?(:signup_flow)
184            expect(current_path).to eq users_sign_up_welcome_path
185          else
186            expect(current_path).to eq dashboard_projects_path
187            expect(page).to have_content("Please check your email (#{new_user.email}) to verify that you own this address and unlock the power of CI/CD.")
188          end
189        end
190      end
191    end
192
193    context "when sigining up with different cased emails" do
194      it "creates the user successfully" do
195        visit new_user_registration_path
196
197        fill_in 'new_user_username', with: new_user.username
198        fill_in 'new_user_email', with: new_user.email
199
200        if Gitlab::Experimentation.enabled?(:signup_flow)
201          fill_in 'new_user_first_name', with: new_user.first_name
202          fill_in 'new_user_last_name', with: new_user.last_name
203        else
204          fill_in 'new_user_name', with: new_user.name
205          fill_in 'new_user_email_confirmation', with: new_user.email.capitalize
206        end
207
208        fill_in 'new_user_password', with: new_user.password
209        click_button "Register"
210
211        if Gitlab::Experimentation.enabled?(:signup_flow)
212          expect(current_path).to eq users_sign_up_welcome_path
213        else
214          expect(current_path).to eq dashboard_projects_path
215          expect(page).to have_content("Welcome! You have signed up successfully.")
216        end
217      end
218    end
219
220    context "when not sending confirmation email" do
221      before do
222        stub_application_setting(send_user_confirmation_email: false)
223      end
224
225      it 'creates the user account and goes to dashboard' do
226        visit new_user_registration_path
227
228        fill_in 'new_user_username', with: new_user.username
229        fill_in 'new_user_email', with: new_user.email
230
231        if Gitlab::Experimentation.enabled?(:signup_flow)
232          fill_in 'new_user_first_name', with: new_user.first_name
233          fill_in 'new_user_last_name', with: new_user.last_name
234        else
235          fill_in 'new_user_name', with: new_user.name
236          fill_in 'new_user_email_confirmation', with: new_user.email
237        end
238
239        fill_in 'new_user_password', with: new_user.password
240        click_button "Register"
241
242        if Gitlab::Experimentation.enabled?(:signup_flow)
243          expect(current_path).to eq users_sign_up_welcome_path
244        else
245          expect(current_path).to eq dashboard_projects_path
246          expect(page).to have_content("Welcome! You have signed up successfully.")
247        end
248      end
249    end
250  end
251
252  context 'with errors' do
253    it "displays the errors" do
254      existing_user = create(:user)
255
256      visit new_user_registration_path
257
258      if Gitlab::Experimentation.enabled?(:signup_flow)
259        fill_in 'new_user_first_name', with: new_user.first_name
260        fill_in 'new_user_last_name', with: new_user.last_name
261      else
262        fill_in 'new_user_name', with: new_user.name
263      end
264
265      fill_in 'new_user_username', with: new_user.username
266      fill_in 'new_user_email', with: existing_user.email
267      fill_in 'new_user_password', with: new_user.password
268      click_button "Register"
269
270      expect(current_path).to eq user_registration_path
271
272      if Gitlab::Experimentation.enabled?(:signup_flow)
273        expect(page).to have_content("error prohibited this user from being saved")
274      else
275        expect(page).to have_content("errors prohibited this user from being saved")
276        expect(page).to have_content("Email confirmation doesn't match")
277      end
278
279      expect(page).to have_content("Email has already been taken")
280    end
281
282    it 'does not redisplay the password' do
283      existing_user = create(:user)
284
285      visit new_user_registration_path
286
287      if Gitlab::Experimentation.enabled?(:signup_flow)
288        fill_in 'new_user_first_name', with: new_user.first_name
289        fill_in 'new_user_last_name', with: new_user.last_name
290      else
291        fill_in 'new_user_name', with: new_user.name
292      end
293
294      fill_in 'new_user_username', with: new_user.username
295      fill_in 'new_user_email', with: existing_user.email
296      fill_in 'new_user_password', with: new_user.password
297      click_button "Register"
298
299      expect(current_path).to eq user_registration_path
300      expect(page.body).not_to match(/#{new_user.password}/)
301    end
302  end
303
304  context 'when terms are enforced' do
305    before do
306      enforce_terms
307    end
308
309    it 'requires the user to check the checkbox' do
310      visit new_user_registration_path
311
312      fill_in 'new_user_username', with: new_user.username
313      fill_in 'new_user_email', with: new_user.email
314
315      if Gitlab::Experimentation.enabled?(:signup_flow)
316        fill_in 'new_user_first_name', with: new_user.first_name
317        fill_in 'new_user_last_name', with: new_user.last_name
318      else
319        fill_in 'new_user_name', with: new_user.name
320        fill_in 'new_user_email_confirmation', with: new_user.email
321      end
322
323      fill_in 'new_user_password', with: new_user.password
324
325      click_button 'Register'
326
327      expect(current_path).to eq new_user_session_path
328      expect(page).to have_content(/you must accept our terms of service/i)
329    end
330
331    it 'asks the user to accept terms before going to the dashboard' do
332      visit new_user_registration_path
333
334      fill_in 'new_user_username', with: new_user.username
335      fill_in 'new_user_email', with: new_user.email
336
337      if Gitlab::Experimentation.enabled?(:signup_flow)
338        fill_in 'new_user_first_name', with: new_user.first_name
339        fill_in 'new_user_last_name', with: new_user.last_name
340      else
341        fill_in 'new_user_name', with: new_user.name
342        fill_in 'new_user_email_confirmation', with: new_user.email
343      end
344
345      fill_in 'new_user_password', with: new_user.password
346      check :terms_opt_in
347
348      click_button "Register"
349
350      if Gitlab::Experimentation.enabled?(:signup_flow)
351        expect(current_path).to eq users_sign_up_welcome_path
352      else
353        expect(current_path).to eq dashboard_projects_path
354      end
355    end
356  end
357
358  context 'when reCAPTCHA and invisible captcha are enabled' do
359    before do
360      InvisibleCaptcha.timestamp_enabled = true
361      stub_application_setting(recaptcha_enabled: true)
362      allow_next_instance_of(RegistrationsController) do |instance|
363        allow(instance).to receive(:verify_recaptcha).and_return(true)
364      end
365    end
366
367    after do
368      InvisibleCaptcha.timestamp_enabled = false
369    end
370
371    context 'when reCAPTCHA detects malicious behaviour' do
372      before do
373        allow_next_instance_of(RegistrationsController) do |instance|
374          allow(instance).to receive(:verify_recaptcha).and_return(false)
375        end
376      end
377
378      it 'prevents from signing up' do
379        visit new_user_registration_path
380
381        fill_in 'new_user_username', with: new_user.username
382        fill_in 'new_user_email', with: new_user.email
383
384        if Gitlab::Experimentation.enabled?(:signup_flow)
385          fill_in 'new_user_first_name', with: new_user.first_name
386          fill_in 'new_user_last_name', with: new_user.last_name
387        else
388          fill_in 'new_user_name', with: new_user.name
389          fill_in 'new_user_email_confirmation', with: new_user.email
390        end
391
392        fill_in 'new_user_password', with: new_user.password
393
394        expect { click_button 'Register' }.not_to change { User.count }
395        expect(page).to have_content('There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.')
396      end
397    end
398
399    context 'when invisible captcha detects malicious behaviour' do
400      it 'prevents from signing up' do
401        visit new_user_registration_path
402
403        fill_in 'new_user_username', with: new_user.username
404        fill_in 'new_user_email', with: new_user.email
405
406        if Gitlab::Experimentation.enabled?(:signup_flow)
407          fill_in 'new_user_first_name', with: new_user.first_name
408          fill_in 'new_user_last_name', with: new_user.last_name
409        else
410          fill_in 'new_user_name', with: new_user.name
411          fill_in 'new_user_email_confirmation', with: new_user.email
412        end
413
414        fill_in 'new_user_password', with: new_user.password
415
416        expect { click_button 'Register' }.not_to change { User.count }
417        expect(page).to have_content('That was a bit too quick! Please resubmit.')
418      end
419    end
420  end
421end
422
423shared_examples 'Signup name validation' do |field, max_length|
424  before do
425    visit new_user_registration_path
426  end
427
428  describe "#{field} validation", :js do
429    it "does not show an error border if the user's fullname length is not longer than #{max_length} characters" do
430      fill_in field, with: 'u' * max_length
431
432      expect(find('.name')).not_to have_css '.gl-field-error-outline'
433    end
434
435    it 'shows an error border if the user\'s fullname contains an emoji' do
436      simulate_input("##{field}", 'Ehsan 🦋')
437
438      expect(find('.name')).to have_css '.gl-field-error-outline'
439    end
440
441    it "shows an error border if the user\'s fullname is longer than #{max_length} characters" do
442      fill_in field, with: 'n' * (max_length + 1)
443
444      expect(find('.name')).to have_css '.gl-field-error-outline'
445    end
446
447    it "shows an error message if the user\'s fullname is longer than #{max_length} characters" do
448      fill_in field, with: 'n' * (max_length + 1)
449
450      expect(page).to have_content("Name is too long (maximum is #{max_length} characters).")
451    end
452
453    it 'shows an error message if the username contains emojis' do
454      simulate_input("##{field}", 'Ehsan 🦋')
455
456      expect(page).to have_content("Invalid input, please avoid emojis")
457    end
458  end
459end
460
461describe 'With original flow' do
462  before do
463    stub_experiment(signup_flow: false)
464    stub_experiment_for_user(signup_flow: false)
465  end
466
467  it_behaves_like 'Signup'
468  it_behaves_like 'Signup name validation', 'new_user_name', 255
469end
470
471describe 'With experimental flow' do
472  before do
473    stub_experiment(signup_flow: true)
474    stub_experiment_for_user(signup_flow: true)
475  end
476
477  it_behaves_like 'Signup'
478  it_behaves_like 'Signup name validation', 'new_user_first_name', 127
479  it_behaves_like 'Signup name validation', 'new_user_last_name', 127
480
481  describe 'when role is required' do
482    it 'after registering, it redirects to step 2 of the signup process, sets the name and role and then redirects to the original requested url' do
483      new_user = build_stubbed(:user)
484      visit new_user_registration_path
485      fill_in 'new_user_first_name', with: new_user.first_name
486      fill_in 'new_user_last_name', with: new_user.last_name
487      fill_in 'new_user_username', with: new_user.username
488      fill_in 'new_user_email', with: new_user.email
489      fill_in 'new_user_password', with: new_user.password
490      click_button 'Register'
491      visit new_project_path
492
493      expect(page).to have_current_path(users_sign_up_welcome_path)
494
495      select 'Software Developer', from: 'user_role'
496      choose 'user_setup_for_company_true'
497      click_button 'Get started!'
498      new_user = User.find_by_username(new_user.username)
499
500      expect(new_user.software_developer_role?).to be_truthy
501      expect(new_user.setup_for_company).to be_truthy
502      expect(page).to have_current_path(new_project_path)
503    end
504  end
505end