logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

sessions_controller_spec.rb (8124B)


  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 PAM authentication' 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)
  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 'shows a translated login error' do
  123. expect(flash[:alert]).to eq(I18n.t('devise.failure.unconfirmed', locale: accept_language))
  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',
  149. otp_required_for_login: true, otp_secret: User.generate_otp_secret(32))
  150. end
  151. let(:recovery_codes) do
  152. codes = user.generate_otp_backup_codes!
  153. user.save
  154. return codes
  155. end
  156. context 'using email and password' do
  157. before do
  158. post :create, params: { user: { email: user.email, password: user.password } }
  159. end
  160. it 'renders two factor authentication page' do
  161. expect(controller).to render_template("two_factor")
  162. end
  163. end
  164. context 'using upcase email and password' do
  165. before do
  166. post :create, params: { user: { email: user.email.upcase, password: user.password } }
  167. end
  168. it 'renders two factor authentication page' do
  169. expect(controller).to render_template("two_factor")
  170. end
  171. end
  172. context 'using a valid OTP' do
  173. before do
  174. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  175. end
  176. it 'redirects to home' do
  177. expect(response).to redirect_to(root_path)
  178. end
  179. it 'logs the user in' do
  180. expect(controller.current_user).to eq user
  181. end
  182. end
  183. context 'when the server has an decryption error' do
  184. before do
  185. allow_any_instance_of(User).to receive(:validate_and_consume_otp!).and_raise(OpenSSL::Cipher::CipherError)
  186. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  187. end
  188. it 'shows a login error' do
  189. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  190. end
  191. it "doesn't log the user in" do
  192. expect(controller.current_user).to be_nil
  193. end
  194. end
  195. context 'using a valid recovery code' do
  196. before do
  197. post :create, params: { user: { otp_attempt: recovery_codes.first } }, session: { otp_user_id: user.id }
  198. end
  199. it 'redirects to home' do
  200. expect(response).to redirect_to(root_path)
  201. end
  202. it 'logs the user in' do
  203. expect(controller.current_user).to eq user
  204. end
  205. end
  206. context 'using an invalid OTP' do
  207. before do
  208. post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { otp_user_id: user.id }
  209. end
  210. it 'shows a login error' do
  211. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  212. end
  213. it "doesn't log the user in" do
  214. expect(controller.current_user).to be_nil
  215. end
  216. end
  217. end
  218. end
  219. end