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.

99 lines
3.2 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Settings::TwoFactorAuthentication::ConfirmationsController do
  4. render_views
  5. let(:user) { Fabricate(:user, email: 'local-part@domain', otp_secret: 'thisisasecretforthespecofnewview') }
  6. shared_examples 'renders :new' do
  7. it 'renders the new view' do
  8. subject
  9. expect(assigns(:confirmation)).to be_instance_of Form::TwoFactorConfirmation
  10. expect(assigns(:provision_url)).to eq 'otpauth://totp/local-part@domain?secret=thisisasecretforthespecofnewview&issuer=cb6e6126.ngrok.io'
  11. expect(assigns(:qrcode)).to be_instance_of RQRCode::QRCode
  12. expect(response).to have_http_status(:success)
  13. expect(response).to render_template(:new)
  14. end
  15. end
  16. describe 'GET #new' do
  17. context 'when signed in' do
  18. subject do
  19. sign_in user, scope: :user
  20. get :new
  21. end
  22. include_examples 'renders :new'
  23. end
  24. it 'redirects if not signed in' do
  25. get :new
  26. expect(response).to redirect_to('/auth/sign_in')
  27. end
  28. end
  29. describe 'POST #create' do
  30. context 'when signed in' do
  31. before do
  32. sign_in user, scope: :user
  33. end
  34. describe 'when form_two_factor_confirmation parameter is not provided' do
  35. it 'raises ActionController::ParameterMissing' do
  36. expect { post :create, params: { } }.to raise_error(ActionController::ParameterMissing)
  37. end
  38. end
  39. describe 'when creation succeeds' do
  40. it 'renders page with success' do
  41. otp_backup_codes = user.generate_otp_backup_codes!
  42. expect_any_instance_of(User).to receive(:generate_otp_backup_codes!) do |value|
  43. expect(value).to eq user
  44. otp_backup_codes
  45. end
  46. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, arg|
  47. expect(value).to eq user
  48. expect(arg).to eq '123456'
  49. true
  50. end
  51. post :create, params: { form_two_factor_confirmation: { code: '123456' } }
  52. expect(assigns(:recovery_codes)).to eq otp_backup_codes
  53. expect(flash[:notice]).to eq 'Two-factor authentication successfully enabled'
  54. expect(response).to have_http_status(:success)
  55. expect(response).to render_template('settings/two_factor_authentication/recovery_codes/index')
  56. end
  57. end
  58. describe 'when creation fails' do
  59. subject do
  60. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, arg|
  61. expect(value).to eq user
  62. expect(arg).to eq '123456'
  63. false
  64. end
  65. post :create, params: { form_two_factor_confirmation: { code: '123456' } }
  66. end
  67. it 'renders the new view' do
  68. subject
  69. expect(response.body).to include 'The entered code was invalid! Are server time and device time correct?'
  70. end
  71. include_examples 'renders :new'
  72. end
  73. end
  74. context 'when not signed in' do
  75. it 'redirects if not signed in' do
  76. post :create, params: { form_two_factor_confirmation: { code: '123456' } }
  77. expect(response).to redirect_to('/auth/sign_in')
  78. end
  79. end
  80. end
  81. end