confirmations_controller_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 setted 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 setted 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. it 'renders page with success' do
  48. otp_backup_codes = user.generate_otp_backup_codes!
  49. expect_any_instance_of(User).to receive(:generate_otp_backup_codes!) do |value|
  50. expect(value).to eq user
  51. otp_backup_codes
  52. end
  53. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, code, options|
  54. expect(value).to eq user
  55. expect(code).to eq '123456'
  56. expect(options).to eq({ otp_secret: 'thisisasecretforthespecofnewview' })
  57. true
  58. end
  59. expect do
  60. post :create,
  61. params: { form_two_factor_confirmation: { otp_attempt: '123456' } },
  62. session: { challenge_passed_at: Time.now.utc, new_otp_secret: 'thisisasecretforthespecofnewview' }
  63. end.to change { user.reload.otp_secret }.to 'thisisasecretforthespecofnewview'
  64. expect(assigns(:recovery_codes)).to eq otp_backup_codes
  65. expect(flash[:notice]).to eq 'Two-factor authentication successfully enabled'
  66. expect(response).to have_http_status(200)
  67. expect(response).to render_template('settings/two_factor_authentication/recovery_codes/index')
  68. end
  69. end
  70. describe 'when creation fails' do
  71. subject do
  72. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, code, options|
  73. expect(value).to eq user
  74. expect(code).to eq '123456'
  75. expect(options).to eq({ otp_secret: 'thisisasecretforthespecofnewview' })
  76. false
  77. end
  78. expect do
  79. post :create,
  80. params: { form_two_factor_confirmation: { otp_attempt: '123456' } },
  81. session: { challenge_passed_at: Time.now.utc, new_otp_secret: 'thisisasecretforthespecofnewview' }
  82. end.to not_change { user.reload.otp_secret }
  83. end
  84. it 'renders the new view' do
  85. subject
  86. expect(response.body).to include 'The entered code was invalid! Are server time and device time correct?'
  87. end
  88. include_examples 'renders :new'
  89. end
  90. end
  91. context 'when not signed in' do
  92. it 'redirects if not signed in' do
  93. post :create, params: { form_two_factor_confirmation: { otp_attempt: '123456' } }
  94. expect(response).to redirect_to('/auth/sign_in')
  95. end
  96. end
  97. end
  98. end
  99. end