PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/controllers/registrations_controller_spec.rb

https://gitlab.com/tbeadle/gitlab-ce
Ruby | 165 lines | 122 code | 41 blank | 2 comment | 0 complexity | d4ca2b0ec0dcaa58fb167e76179a11d0 MD5 | raw file
  1. require 'spec_helper'
  2. describe RegistrationsController do
  3. include TermsHelper
  4. describe '#create' do
  5. let(:user_params) { { user: { name: 'new_user', username: 'new_username', email: 'new@user.com', password: 'Any_password' } } }
  6. context 'email confirmation' do
  7. around do |example|
  8. perform_enqueued_jobs do
  9. example.run
  10. end
  11. end
  12. context 'when send_user_confirmation_email is false' do
  13. it 'signs the user in' do
  14. allow_any_instance_of(ApplicationSetting).to receive(:send_user_confirmation_email).and_return(false)
  15. expect { post(:create, user_params) }.not_to change { ActionMailer::Base.deliveries.size }
  16. expect(subject.current_user).not_to be_nil
  17. end
  18. end
  19. context 'when send_user_confirmation_email is true' do
  20. it 'does not authenticate user and sends confirmation email' do
  21. allow_any_instance_of(ApplicationSetting).to receive(:send_user_confirmation_email).and_return(true)
  22. post(:create, user_params)
  23. expect(ActionMailer::Base.deliveries.last.to.first).to eq(user_params[:user][:email])
  24. expect(subject.current_user).to be_nil
  25. end
  26. end
  27. context 'when signup_enabled? is false' do
  28. it 'redirects to sign_in' do
  29. allow_any_instance_of(ApplicationSetting).to receive(:signup_enabled?).and_return(false)
  30. expect { post(:create, user_params) }.not_to change(User, :count)
  31. expect(response).to redirect_to(new_user_session_path)
  32. end
  33. end
  34. end
  35. context 'when reCAPTCHA is enabled' do
  36. before do
  37. stub_application_setting(recaptcha_enabled: true)
  38. end
  39. it 'displays an error when the reCAPTCHA is not solved' do
  40. # Without this, `verify_recaptcha` arbitraily returns true in test env
  41. Recaptcha.configuration.skip_verify_env.delete('test')
  42. post(:create, user_params)
  43. expect(response).to render_template(:new)
  44. expect(flash[:alert]).to include 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'
  45. end
  46. it 'redirects to the dashboard when the recaptcha is solved' do
  47. # Avoid test ordering issue and ensure `verify_recaptcha` returns true
  48. unless Recaptcha.configuration.skip_verify_env.include?('test')
  49. Recaptcha.configuration.skip_verify_env << 'test'
  50. end
  51. post(:create, user_params)
  52. expect(flash[:notice]).to include 'Welcome! You have signed up successfully.'
  53. end
  54. end
  55. context 'when terms are enforced' do
  56. before do
  57. enforce_terms
  58. end
  59. it 'redirects back with a notice when the checkbox was not checked' do
  60. post :create, user_params
  61. expect(flash[:alert]).to match /you must accept our terms/i
  62. end
  63. it 'creates the user with agreement when terms are accepted' do
  64. post :create, user_params.merge(terms_opt_in: '1')
  65. expect(subject.current_user).to be_present
  66. expect(subject.current_user.terms_accepted?).to be(true)
  67. end
  68. end
  69. end
  70. describe '#destroy' do
  71. let(:user) { create(:user) }
  72. before do
  73. sign_in(user)
  74. end
  75. def expect_failure(message)
  76. expect(flash[:alert]).to eq(message)
  77. expect(response.status).to eq(303)
  78. expect(response).to redirect_to profile_account_path
  79. end
  80. def expect_password_failure
  81. expect_failure('Invalid password')
  82. end
  83. def expect_username_failure
  84. expect_failure('Invalid username')
  85. end
  86. def expect_success
  87. expect(flash[:notice]).to eq 'Account scheduled for removal.'
  88. expect(response.status).to eq(303)
  89. expect(response).to redirect_to new_user_session_path
  90. end
  91. context 'user requires password confirmation' do
  92. it 'fails if password confirmation is not provided' do
  93. post :destroy
  94. expect_password_failure
  95. end
  96. it 'fails if password confirmation is wrong' do
  97. post :destroy, password: 'wrong password'
  98. expect_password_failure
  99. end
  100. it 'succeeds if password is confirmed' do
  101. post :destroy, password: '12345678'
  102. expect_success
  103. end
  104. end
  105. context 'user does not require password confirmation' do
  106. before do
  107. stub_application_setting(password_authentication_enabled_for_web: false)
  108. stub_application_setting(password_authentication_enabled_for_git: false)
  109. end
  110. it 'fails if username confirmation is not provided' do
  111. post :destroy
  112. expect_username_failure
  113. end
  114. it 'fails if username confirmation is wrong' do
  115. post :destroy, username: 'wrong username'
  116. expect_username_failure
  117. end
  118. it 'succeeds if username is confirmed' do
  119. post :destroy, username: user.username
  120. expect_success
  121. end
  122. end
  123. end
  124. end