You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.6 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Auth::SessionsController, type: :controller do
  4. render_views
  5. describe 'GET #new' do
  6. before do
  7. request.env['devise.mapping'] = Devise.mappings[:user]
  8. end
  9. it 'returns http success' do
  10. get :new
  11. expect(response).to have_http_status(:success)
  12. end
  13. end
  14. describe 'DELETE #destroy' do
  15. let(:user) { Fabricate(:user) }
  16. before do
  17. request.env['devise.mapping'] = Devise.mappings[:user]
  18. end
  19. context 'with a regular user' do
  20. it 'redirects to home after sign out' do
  21. sign_in(user, scope: :user)
  22. delete :destroy
  23. expect(response).to redirect_to(root_path)
  24. end
  25. end
  26. context 'with a suspended user' do
  27. it 'redirects to home after sign out' do
  28. Fabricate(:account, user: user, suspended: true)
  29. sign_in(user, scope: :user)
  30. delete :destroy
  31. expect(response).to redirect_to(root_path)
  32. end
  33. end
  34. end
  35. describe 'POST #create' do
  36. before do
  37. request.env['devise.mapping'] = Devise.mappings[:user]
  38. end
  39. context 'using password authentication' do
  40. let(:user) { Fabricate(:user, email: 'foo@bar.com', password: 'abcdefgh') }
  41. context 'using a valid password' do
  42. before do
  43. post :create, params: { user: { email: user.email, password: user.password } }
  44. end
  45. it 'redirects to home' do
  46. expect(response).to redirect_to(root_path)
  47. end
  48. it 'logs the user in' do
  49. expect(controller.current_user).to eq user
  50. end
  51. end
  52. context 'using an invalid password' do
  53. before do
  54. post :create, params: { user: { email: user.email, password: 'wrongpw' } }
  55. end
  56. it 'shows a login error' do
  57. expect(flash[:alert]).to match I18n.t('devise.failure.invalid', authentication_keys: 'Email')
  58. end
  59. it "doesn't log the user in" do
  60. expect(controller.current_user).to be_nil
  61. end
  62. end
  63. context 'using an unconfirmed password' do
  64. before do
  65. request.headers['Accept-Language'] = accept_language
  66. post :create, params: { user: { email: unconfirmed_user.email, password: unconfirmed_user.password } }
  67. end
  68. let(:unconfirmed_user) { user.tap { |u| u.update!(confirmed_at: nil) } }
  69. let(:accept_language) { 'fr' }
  70. it 'shows a translated login error' do
  71. expect(flash[:alert]).to eq(I18n.t('devise.failure.unconfirmed', locale: accept_language))
  72. end
  73. end
  74. end
  75. context 'using two-factor authentication' do
  76. let(:user) do
  77. Fabricate(:user, email: 'x@y.com', password: 'abcdefgh',
  78. otp_required_for_login: true, otp_secret: User.generate_otp_secret(32))
  79. end
  80. let(:recovery_codes) do
  81. codes = user.generate_otp_backup_codes!
  82. user.save
  83. return codes
  84. end
  85. context 'using a valid OTP' do
  86. before do
  87. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  88. end
  89. it 'redirects to home' do
  90. expect(response).to redirect_to(root_path)
  91. end
  92. it 'logs the user in' do
  93. expect(controller.current_user).to eq user
  94. end
  95. end
  96. context 'when the server has an decryption error' do
  97. before do
  98. allow_any_instance_of(User).to receive(:validate_and_consume_otp!).and_raise(OpenSSL::Cipher::CipherError)
  99. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  100. end
  101. it 'shows a login error' do
  102. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  103. end
  104. it "doesn't log the user in" do
  105. expect(controller.current_user).to be_nil
  106. end
  107. end
  108. context 'using a valid recovery code' do
  109. before do
  110. post :create, params: { user: { otp_attempt: recovery_codes.first } }, session: { otp_user_id: user.id }
  111. end
  112. it 'redirects to home' do
  113. expect(response).to redirect_to(root_path)
  114. end
  115. it 'logs the user in' do
  116. expect(controller.current_user).to eq user
  117. end
  118. end
  119. context 'using an invalid OTP' do
  120. before do
  121. post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { otp_user_id: user.id }
  122. end
  123. it 'shows a login error' do
  124. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  125. end
  126. it "doesn't log the user in" do
  127. expect(controller.current_user).to be_nil
  128. end
  129. end
  130. end
  131. end
  132. end