闭社主体 forked from https://github.com/tootsuite/mastodon
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.

122 lines
3.4 KiB

  1. require 'rails_helper'
  2. RSpec.describe Auth::SessionsController, type: :controller do
  3. render_views
  4. describe 'GET #new' do
  5. before do
  6. request.env['devise.mapping'] = Devise.mappings[:user]
  7. end
  8. it 'returns http success' do
  9. get :new
  10. expect(response).to have_http_status(:success)
  11. end
  12. end
  13. describe 'POST #create' do
  14. before do
  15. request.env['devise.mapping'] = Devise.mappings[:user]
  16. end
  17. context 'using password authentication' do
  18. let(:user) { Fabricate(:user, email: 'foo@bar.com', password: 'abcdefgh') }
  19. context 'using a valid password' do
  20. before do
  21. post :create, params: { user: { email: user.email, password: user.password } }
  22. end
  23. it 'redirects to home' do
  24. expect(response).to redirect_to(root_path)
  25. end
  26. it 'logs the user in' do
  27. expect(controller.current_user).to eq user
  28. end
  29. end
  30. context 'using an invalid password' do
  31. before do
  32. post :create, params: { user: { email: user.email, password: 'wrongpw' } }
  33. end
  34. it 'shows a login error' do
  35. expect(flash[:alert]).to match I18n.t('devise.failure.invalid', authentication_keys: 'Email')
  36. end
  37. it "doesn't log the user in" do
  38. expect(controller.current_user).to be_nil
  39. end
  40. end
  41. context 'using an unconfirmed password' do
  42. before do
  43. request.headers['Accept-Language'] = accept_language
  44. post :create, params: { user: { email: unconfirmed_user.email, password: unconfirmed_user.password } }
  45. end
  46. let(:unconfirmed_user) { user.tap { |u| u.update!(confirmed_at: nil) } }
  47. let(:accept_language) { 'fr' }
  48. it 'shows a translated login error' do
  49. expect(flash[:alert]).to eq(I18n.t('devise.failure.unconfirmed', locale: accept_language))
  50. end
  51. end
  52. end
  53. context 'using two-factor authentication' do
  54. let(:user) do
  55. Fabricate(:user, email: 'x@y.com', password: 'abcdefgh',
  56. otp_required_for_login: true, otp_secret: User.generate_otp_secret(32))
  57. end
  58. let(:recovery_codes) do
  59. codes = user.generate_otp_backup_codes!
  60. user.save
  61. return codes
  62. end
  63. context 'using a valid OTP' do
  64. before do
  65. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  66. end
  67. it 'redirects to home' do
  68. expect(response).to redirect_to(root_path)
  69. end
  70. it 'logs the user in' do
  71. expect(controller.current_user).to eq user
  72. end
  73. end
  74. context 'using a valid recovery code' do
  75. before do
  76. post :create, params: { user: { otp_attempt: recovery_codes.first } }, session: { otp_user_id: user.id }
  77. end
  78. it 'redirects to home' do
  79. expect(response).to redirect_to(root_path)
  80. end
  81. it 'logs the user in' do
  82. expect(controller.current_user).to eq user
  83. end
  84. end
  85. context 'using an invalid OTP' do
  86. before do
  87. post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { otp_user_id: user.id }
  88. end
  89. it 'shows a login error' do
  90. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  91. end
  92. it "doesn't log the user in" do
  93. expect(controller.current_user).to be_nil
  94. end
  95. end
  96. end
  97. end
  98. end