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