otp_authentication_controller_spec.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Settings::TwoFactorAuthentication::OtpAuthenticationController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. describe 'GET #show' do
  7. context 'when signed in' do
  8. before do
  9. sign_in user, scope: :user
  10. end
  11. describe 'when user has OTP enabled' do
  12. before do
  13. user.update(otp_required_for_login: true)
  14. end
  15. it 'redirects to two factor authentication methods list page' do
  16. get :show
  17. expect(response).to redirect_to settings_two_factor_authentication_methods_path
  18. end
  19. end
  20. describe 'when user does not have OTP enabled' do
  21. before do
  22. user.update(otp_required_for_login: false)
  23. end
  24. it 'returns http success' do
  25. get :show
  26. expect(response).to have_http_status(200)
  27. end
  28. end
  29. end
  30. context 'when not signed in' do
  31. it 'redirects' do
  32. get :show
  33. expect(response).to redirect_to new_user_session_path
  34. end
  35. end
  36. end
  37. describe 'POST #create' do
  38. context 'when signed in' do
  39. before do
  40. sign_in user, scope: :user
  41. end
  42. describe 'when user has OTP enabled' do
  43. before do
  44. user.update(otp_required_for_login: true)
  45. end
  46. describe 'when creation succeeds' do
  47. it 'redirects to code confirmation page without updating user secret and setting otp secret in the session' do
  48. expect do
  49. post :create, session: { challenge_passed_at: Time.now.utc }
  50. end.to not_change { user.reload.otp_secret }
  51. .and(change { session[:new_otp_secret] })
  52. expect(response).to redirect_to(new_settings_two_factor_authentication_confirmation_path)
  53. end
  54. end
  55. end
  56. describe 'when user does not have OTP enabled' do
  57. before do
  58. user.update(otp_required_for_login: false)
  59. end
  60. describe 'when creation succeeds' do
  61. it 'redirects to code confirmation page without updating user secret and setting otp secret in the session' do
  62. expect do
  63. post :create, session: { challenge_passed_at: Time.now.utc }
  64. end.to not_change { user.reload.otp_secret }
  65. .and(change { session[:new_otp_secret] })
  66. expect(response).to redirect_to(new_settings_two_factor_authentication_confirmation_path)
  67. end
  68. end
  69. end
  70. end
  71. context 'when not signed in' do
  72. it 'redirects to login' do
  73. get :show
  74. expect(response).to redirect_to new_user_session_path
  75. end
  76. end
  77. end
  78. end