two_factor_authentication_methods_controller_spec.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Settings::TwoFactorAuthenticationMethodsController do
  4. render_views
  5. context 'when not signed in' do
  6. describe 'GET to #index' do
  7. it 'redirects' do
  8. get :index
  9. expect(response).to redirect_to '/auth/sign_in'
  10. end
  11. end
  12. end
  13. context 'when signed in' do
  14. let(:user) { Fabricate(:user) }
  15. before do
  16. sign_in user, scope: :user
  17. end
  18. describe 'GET #index' do
  19. describe 'when user has enabled otp' do
  20. before do
  21. user.update(otp_required_for_login: true)
  22. get :index
  23. end
  24. it 'returns http success' do
  25. expect(response).to have_http_status(200)
  26. end
  27. it 'returns private cache control headers' do
  28. expect(response.headers['Cache-Control']).to include('private, no-store')
  29. end
  30. end
  31. describe 'when user has not enabled otp' do
  32. before do
  33. user.update(otp_required_for_login: false)
  34. get :index
  35. end
  36. it 'redirects to enable otp' do
  37. expect(response).to redirect_to(settings_otp_authentication_path)
  38. end
  39. end
  40. end
  41. describe 'POST to #disable' do
  42. before do
  43. user.update(otp_required_for_login: true)
  44. end
  45. context 'when user has not passed challenge' do
  46. it 'renders challenge page' do
  47. post :disable
  48. expect(response).to have_http_status(200)
  49. expect(response).to render_template('auth/challenges/new')
  50. end
  51. end
  52. context 'when user has passed challenge' do
  53. before do
  54. mailer = instance_double(ApplicationMailer::MessageDelivery, deliver_later!: true)
  55. allow(UserMailer).to receive(:two_factor_disabled).with(user).and_return(mailer)
  56. end
  57. it 'redirects to settings page' do
  58. post :disable, session: { challenge_passed_at: 10.minutes.ago }
  59. expect(UserMailer).to have_received(:two_factor_disabled).with(user)
  60. expect(response).to redirect_to(settings_otp_authentication_path)
  61. end
  62. end
  63. end
  64. end
  65. end