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.

224 lines
6.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(new_user_session_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(new_user_session_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 email with uppercase letters' do
  53. before do
  54. post :create, params: { user: { email: user.email.upcase, password: user.password } }
  55. end
  56. it 'redirects to home' do
  57. expect(response).to redirect_to(root_path)
  58. end
  59. it 'logs the user in' do
  60. expect(controller.current_user).to eq user
  61. end
  62. end
  63. context 'using an invalid password' do
  64. before do
  65. post :create, params: { user: { email: user.email, password: 'wrongpw' } }
  66. end
  67. it 'shows a login error' do
  68. expect(flash[:alert]).to match I18n.t('devise.failure.invalid', authentication_keys: 'Email')
  69. end
  70. it "doesn't log the user in" do
  71. expect(controller.current_user).to be_nil
  72. end
  73. end
  74. context 'using an unconfirmed password' do
  75. before do
  76. request.headers['Accept-Language'] = accept_language
  77. post :create, params: { user: { email: unconfirmed_user.email, password: unconfirmed_user.password } }
  78. end
  79. let(:unconfirmed_user) { user.tap { |u| u.update!(confirmed_at: nil) } }
  80. let(:accept_language) { 'fr' }
  81. it 'shows a translated login error' do
  82. expect(flash[:alert]).to eq(I18n.t('devise.failure.unconfirmed', locale: accept_language))
  83. end
  84. end
  85. context "logging in from the user's page" do
  86. before do
  87. allow(controller).to receive(:single_user_mode?).and_return(single_user_mode)
  88. allow(controller).to receive(:stored_location_for).with(:user).and_return("/@#{user.account.username}")
  89. post :create, params: { user: { email: user.email, password: user.password } }
  90. end
  91. context "in single user mode" do
  92. let(:single_user_mode) { true }
  93. it 'redirects to home' do
  94. expect(response).to redirect_to(root_path)
  95. end
  96. end
  97. context "in non-single user mode" do
  98. let(:single_user_mode) { false }
  99. it "redirects back to the user's page" do
  100. expect(response).to redirect_to(short_account_path(username: user.account))
  101. end
  102. end
  103. end
  104. end
  105. context 'using two-factor authentication' do
  106. let(:user) do
  107. Fabricate(:user, email: 'x@y.com', password: 'abcdefgh',
  108. otp_required_for_login: true, otp_secret: User.generate_otp_secret(32))
  109. end
  110. let(:recovery_codes) do
  111. codes = user.generate_otp_backup_codes!
  112. user.save
  113. return codes
  114. end
  115. context 'using email and password' do
  116. before do
  117. post :create, params: { user: { email: user.email, password: user.password } }
  118. end
  119. it 'renders two factor authentication page' do
  120. expect(controller).to render_template("two_factor")
  121. end
  122. end
  123. context 'using upcase email and password' do
  124. before do
  125. post :create, params: { user: { email: user.email.upcase, password: user.password } }
  126. end
  127. it 'renders two factor authentication page' do
  128. expect(controller).to render_template("two_factor")
  129. end
  130. end
  131. context 'using a valid OTP' do
  132. before do
  133. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  134. end
  135. it 'redirects to home' do
  136. expect(response).to redirect_to(root_path)
  137. end
  138. it 'logs the user in' do
  139. expect(controller.current_user).to eq user
  140. end
  141. end
  142. context 'when the server has an decryption error' do
  143. before do
  144. allow_any_instance_of(User).to receive(:validate_and_consume_otp!).and_raise(OpenSSL::Cipher::CipherError)
  145. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  146. end
  147. it 'shows a login error' do
  148. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  149. end
  150. it "doesn't log the user in" do
  151. expect(controller.current_user).to be_nil
  152. end
  153. end
  154. context 'using a valid recovery code' do
  155. before do
  156. post :create, params: { user: { otp_attempt: recovery_codes.first } }, session: { otp_user_id: user.id }
  157. end
  158. it 'redirects to home' do
  159. expect(response).to redirect_to(root_path)
  160. end
  161. it 'logs the user in' do
  162. expect(controller.current_user).to eq user
  163. end
  164. end
  165. context 'using an invalid OTP' do
  166. before do
  167. post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { otp_user_id: user.id }
  168. end
  169. it 'shows a login error' do
  170. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  171. end
  172. it "doesn't log the user in" do
  173. expect(controller.current_user).to be_nil
  174. end
  175. end
  176. end
  177. end
  178. end