The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

275 lines
8.0 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Auth::SessionsController, type: :controller do
  4. render_views
  5. before do
  6. request.env['devise.mapping'] = Devise.mappings[:user]
  7. end
  8. describe 'GET #new' do
  9. it 'returns http success' do
  10. get :new
  11. expect(response).to have_http_status(200)
  12. end
  13. end
  14. describe 'DELETE #destroy' do
  15. let(:user) { Fabricate(:user) }
  16. context 'with a regular user' do
  17. it 'redirects to home after sign out' do
  18. sign_in(user, scope: :user)
  19. delete :destroy
  20. expect(response).to redirect_to(new_user_session_path)
  21. end
  22. it 'does not delete redirect location with continue=true' do
  23. sign_in(user, scope: :user)
  24. controller.store_location_for(:user, '/authorize')
  25. delete :destroy, params: { continue: 'true' }
  26. expect(controller.stored_location_for(:user)).to eq '/authorize'
  27. end
  28. end
  29. context 'with a suspended user' do
  30. it 'redirects to home after sign out' do
  31. Fabricate(:account, user: user, suspended: true)
  32. sign_in(user, scope: :user)
  33. delete :destroy
  34. expect(response).to redirect_to(new_user_session_path)
  35. end
  36. end
  37. end
  38. describe 'POST #create' do
  39. context 'using PAM authentication', if: ENV['PAM_ENABLED'] == 'true' do
  40. context 'using a valid password' do
  41. before do
  42. post :create, params: { user: { email: "pam_user1", password: '123456' } }
  43. end
  44. it 'redirects to home' do
  45. expect(response).to redirect_to(root_path)
  46. end
  47. it 'logs the user in' do
  48. expect(controller.current_user).to be_instance_of(User)
  49. end
  50. end
  51. context 'using an invalid password' do
  52. before do
  53. post :create, params: { user: { email: "pam_user1", password: 'WRONGPW' } }
  54. end
  55. it 'shows a login error' do
  56. expect(flash[:alert]).to match I18n.t('devise.failure.invalid', authentication_keys: 'Email')
  57. end
  58. it "doesn't log the user in" do
  59. expect(controller.current_user).to be_nil
  60. end
  61. end
  62. context 'using a valid email and existing user' do
  63. let(:user) do
  64. account = Fabricate.build(:account, username: 'pam_user1')
  65. account.save!(validate: false)
  66. user = Fabricate(:user, email: 'pam@example.com', password: nil, account: account, external: true)
  67. user
  68. end
  69. before do
  70. post :create, params: { user: { email: user.email, password: '123456' } }
  71. end
  72. it 'redirects to home' do
  73. expect(response).to redirect_to(root_path)
  74. end
  75. it 'logs the user in' do
  76. expect(controller.current_user).to eq user
  77. end
  78. end
  79. end
  80. context 'using password authentication' do
  81. let(:user) { Fabricate(:user, email: 'foo@bar.com', password: 'abcdefgh') }
  82. context 'using a valid password' do
  83. before do
  84. post :create, params: { user: { email: user.email, password: user.password } }
  85. end
  86. it 'redirects to home' do
  87. expect(response).to redirect_to(root_path)
  88. end
  89. it 'logs the user in' do
  90. expect(controller.current_user).to eq user
  91. end
  92. end
  93. context 'using email with uppercase letters' do
  94. before do
  95. post :create, params: { user: { email: user.email.upcase, password: user.password } }
  96. end
  97. it 'redirects to home' do
  98. expect(response).to redirect_to(root_path)
  99. end
  100. it 'logs the user in' do
  101. expect(controller.current_user).to eq user
  102. end
  103. end
  104. context 'using an invalid password' do
  105. before do
  106. post :create, params: { user: { email: user.email, password: 'wrongpw' } }
  107. end
  108. it 'shows a login error' do
  109. expect(flash[:alert]).to match I18n.t('devise.failure.invalid', authentication_keys: 'Email')
  110. end
  111. it "doesn't log the user in" do
  112. expect(controller.current_user).to be_nil
  113. end
  114. end
  115. context 'using an unconfirmed password' do
  116. before do
  117. request.headers['Accept-Language'] = accept_language
  118. post :create, params: { user: { email: unconfirmed_user.email, password: unconfirmed_user.password } }
  119. end
  120. let(:unconfirmed_user) { user.tap { |u| u.update!(confirmed_at: nil) } }
  121. let(:accept_language) { 'fr' }
  122. it 'redirects to home' do
  123. expect(response).to redirect_to(root_path)
  124. end
  125. end
  126. context "logging in from the user's page" do
  127. before do
  128. allow(controller).to receive(:single_user_mode?).and_return(single_user_mode)
  129. allow(controller).to receive(:stored_location_for).with(:user).and_return("/@#{user.account.username}")
  130. post :create, params: { user: { email: user.email, password: user.password } }
  131. end
  132. context "in single user mode" do
  133. let(:single_user_mode) { true }
  134. it 'redirects to home' do
  135. expect(response).to redirect_to(root_path)
  136. end
  137. end
  138. context "in non-single user mode" do
  139. let(:single_user_mode) { false }
  140. it "redirects back to the user's page" do
  141. expect(response).to redirect_to(short_account_path(username: user.account))
  142. end
  143. end
  144. end
  145. end
  146. context 'using two-factor authentication' do
  147. let!(:user) do
  148. Fabricate(:user, email: 'x@y.com', password: 'abcdefgh', otp_required_for_login: true, otp_secret: User.generate_otp_secret(32))
  149. end
  150. let!(:recovery_codes) do
  151. codes = user.generate_otp_backup_codes!
  152. user.save
  153. return codes
  154. end
  155. context 'using email and password' do
  156. before do
  157. post :create, params: { user: { email: user.email, password: user.password } }
  158. end
  159. it 'renders two factor authentication page' do
  160. expect(controller).to render_template("two_factor")
  161. end
  162. end
  163. context 'using upcase email and password' do
  164. before do
  165. post :create, params: { user: { email: user.email.upcase, password: user.password } }
  166. end
  167. it 'renders two factor authentication page' do
  168. expect(controller).to render_template("two_factor")
  169. end
  170. end
  171. context 'using a valid OTP' do
  172. before do
  173. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  174. end
  175. it 'redirects to home' do
  176. expect(response).to redirect_to(root_path)
  177. end
  178. it 'logs the user in' do
  179. expect(controller.current_user).to eq user
  180. end
  181. end
  182. context 'when the server has an decryption error' do
  183. before do
  184. allow_any_instance_of(User).to receive(:validate_and_consume_otp!).and_raise(OpenSSL::Cipher::CipherError)
  185. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  186. end
  187. it 'shows a login error' do
  188. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  189. end
  190. it "doesn't log the user in" do
  191. expect(controller.current_user).to be_nil
  192. end
  193. end
  194. context 'using a valid recovery code' do
  195. before do
  196. post :create, params: { user: { otp_attempt: recovery_codes.first } }, session: { otp_user_id: user.id }
  197. end
  198. it 'redirects to home' do
  199. expect(response).to redirect_to(root_path)
  200. end
  201. it 'logs the user in' do
  202. expect(controller.current_user).to eq user
  203. end
  204. end
  205. context 'using an invalid OTP' do
  206. before do
  207. post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { otp_user_id: user.id }
  208. end
  209. it 'shows a login error' do
  210. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  211. end
  212. it "doesn't log the user in" do
  213. expect(controller.current_user).to be_nil
  214. end
  215. end
  216. end
  217. end
  218. end