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.

190 lines
5.5 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. context "logging in from the user's page" do
  75. before do
  76. allow(controller).to receive(:single_user_mode?).and_return(single_user_mode)
  77. allow(controller).to receive(:stored_location_for).with(:user).and_return("/@#{user.account.username}")
  78. post :create, params: { user: { email: user.email, password: user.password } }
  79. end
  80. context "in single user mode" do
  81. let(:single_user_mode) { true }
  82. it 'redirects to home' do
  83. expect(response).to redirect_to(root_path)
  84. end
  85. end
  86. context "in non-single user mode" do
  87. let(:single_user_mode) { false }
  88. it "redirects back to the user's page" do
  89. expect(response).to redirect_to(short_account_path(username: user.account))
  90. end
  91. end
  92. end
  93. end
  94. context 'using two-factor authentication' do
  95. let(:user) do
  96. Fabricate(:user, email: 'x@y.com', password: 'abcdefgh',
  97. otp_required_for_login: true, otp_secret: User.generate_otp_secret(32))
  98. end
  99. let(:recovery_codes) do
  100. codes = user.generate_otp_backup_codes!
  101. user.save
  102. return codes
  103. end
  104. context 'using a valid OTP' do
  105. before do
  106. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  107. end
  108. it 'redirects to home' do
  109. expect(response).to redirect_to(root_path)
  110. end
  111. it 'logs the user in' do
  112. expect(controller.current_user).to eq user
  113. end
  114. end
  115. context 'when the server has an decryption error' do
  116. before do
  117. allow_any_instance_of(User).to receive(:validate_and_consume_otp!).and_raise(OpenSSL::Cipher::CipherError)
  118. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  119. end
  120. it 'shows a login error' do
  121. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  122. end
  123. it "doesn't log the user in" do
  124. expect(controller.current_user).to be_nil
  125. end
  126. end
  127. context 'using a valid recovery code' do
  128. before do
  129. post :create, params: { user: { otp_attempt: recovery_codes.first } }, session: { otp_user_id: user.id }
  130. end
  131. it 'redirects to home' do
  132. expect(response).to redirect_to(root_path)
  133. end
  134. it 'logs the user in' do
  135. expect(controller.current_user).to eq user
  136. end
  137. end
  138. context 'using an invalid OTP' do
  139. before do
  140. post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { otp_user_id: user.id }
  141. end
  142. it 'shows a login error' do
  143. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  144. end
  145. it "doesn't log the user in" do
  146. expect(controller.current_user).to be_nil
  147. end
  148. end
  149. end
  150. end
  151. end