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.
 
 
 
 

109 lines
2.9 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. end
  42. context 'using two-factor authentication' do
  43. let(:user) do
  44. Fabricate(:user, email: 'x@y.com', password: 'abcdefgh',
  45. otp_required_for_login: true, otp_secret: User.generate_otp_secret(32))
  46. end
  47. let(:recovery_codes) do
  48. codes = user.generate_otp_backup_codes!
  49. user.save
  50. return codes
  51. end
  52. context 'using a valid OTP' do
  53. before do
  54. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { otp_user_id: user.id }
  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 a valid recovery code' do
  64. before do
  65. post :create, params: { user: { otp_attempt: recovery_codes.first } }, 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 an invalid OTP' do
  75. before do
  76. post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { otp_user_id: user.id }
  77. end
  78. it 'shows a login error' do
  79. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  80. end
  81. it "doesn't log the user in" do
  82. expect(controller.current_user).to be_nil
  83. end
  84. end
  85. end
  86. end
  87. end