confirmations_controller_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Settings::TwoFactorAuthentication::ConfirmationsController do
  4. render_views
  5. shared_examples 'renders :new' do
  6. it 'renders the new view' do
  7. subject
  8. expect(assigns(:confirmation)).to be_instance_of Form::TwoFactorConfirmation
  9. expect(assigns(:provision_url)).to eq 'otpauth://totp/cb6e6126.ngrok.io:local-part%40domain?secret=thisisasecretforthespecofnewview&issuer=cb6e6126.ngrok.io'
  10. expect(assigns(:qrcode)).to be_instance_of RQRCode::QRCode
  11. expect(response).to have_http_status(200)
  12. expect(response).to render_template(:new)
  13. end
  14. end
  15. [true, false].each do |with_otp_secret|
  16. let(:user) { Fabricate(:user, email: 'local-part@domain', otp_secret: with_otp_secret ? 'oldotpsecret' : nil) }
  17. describe 'GET #new' do
  18. context 'when signed in and a new otp secret has been set in the session' do
  19. subject do
  20. sign_in user, scope: :user
  21. get :new, session: { challenge_passed_at: Time.now.utc, new_otp_secret: 'thisisasecretforthespecofnewview' }
  22. end
  23. include_examples 'renders :new'
  24. end
  25. it 'redirects if not signed in' do
  26. get :new
  27. expect(response).to redirect_to('/auth/sign_in')
  28. end
  29. it 'redirects if a new otp_secret has not been set in the session' do
  30. sign_in user, scope: :user
  31. get :new, session: { challenge_passed_at: Time.now.utc }
  32. expect(response).to redirect_to('/settings/otp_authentication')
  33. end
  34. end
  35. describe 'POST #create' do
  36. context 'when signed in' do
  37. before do
  38. sign_in user, scope: :user
  39. end
  40. describe 'when form_two_factor_confirmation parameter is not provided' do
  41. it 'raises ActionController::ParameterMissing' do
  42. post :create, params: {}, session: { challenge_passed_at: Time.now.utc, new_otp_secret: 'thisisasecretforthespecofnewview' }
  43. expect(response).to have_http_status(400)
  44. end
  45. end
  46. describe 'when creation succeeds' do
  47. let!(:otp_backup_codes) { user.generate_otp_backup_codes! }
  48. it 'renders page with success' do
  49. prepare_user_otp_generation
  50. prepare_user_otp_consumption
  51. expect do
  52. post :create,
  53. params: { form_two_factor_confirmation: { otp_attempt: '123456' } },
  54. session: { challenge_passed_at: Time.now.utc, new_otp_secret: 'thisisasecretforthespecofnewview' }
  55. end.to change { user.reload.otp_secret }.to 'thisisasecretforthespecofnewview'
  56. expect(assigns(:recovery_codes)).to eq otp_backup_codes
  57. expect(flash[:notice]).to eq 'Two-factor authentication successfully enabled'
  58. expect(response).to have_http_status(200)
  59. expect(response).to render_template('settings/two_factor_authentication/recovery_codes/index')
  60. end
  61. def prepare_user_otp_generation
  62. expect_any_instance_of(User).to receive(:generate_otp_backup_codes!) do |value|
  63. expect(value).to eq user
  64. otp_backup_codes
  65. end
  66. end
  67. def prepare_user_otp_consumption
  68. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, code, options|
  69. expect(value).to eq user
  70. expect(code).to eq '123456'
  71. expect(options).to eq({ otp_secret: 'thisisasecretforthespecofnewview' })
  72. true
  73. end
  74. end
  75. end
  76. describe 'when creation fails' do
  77. subject do
  78. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, code, options|
  79. expect(value).to eq user
  80. expect(code).to eq '123456'
  81. expect(options).to eq({ otp_secret: 'thisisasecretforthespecofnewview' })
  82. false
  83. end
  84. expect do
  85. post :create,
  86. params: { form_two_factor_confirmation: { otp_attempt: '123456' } },
  87. session: { challenge_passed_at: Time.now.utc, new_otp_secret: 'thisisasecretforthespecofnewview' }
  88. end.to(not_change { user.reload.otp_secret })
  89. end
  90. it 'renders the new view' do
  91. subject
  92. expect(response.body).to include 'The entered code was invalid! Are server time and device time correct?'
  93. end
  94. include_examples 'renders :new'
  95. end
  96. end
  97. context 'when not signed in' do
  98. it 'redirects if not signed in' do
  99. post :create, params: { form_two_factor_confirmation: { otp_attempt: '123456' } }
  100. expect(response).to redirect_to('/auth/sign_in')
  101. end
  102. end
  103. end
  104. end
  105. end