The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

108 lines
3.7 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. let(:user_without_otp_secret) { Fabricate(:user, email: 'local-part@domain') }
  7. shared_examples 'renders :new' do
  8. it 'renders the new view' do
  9. subject
  10. expect(assigns(:confirmation)).to be_instance_of Form::TwoFactorConfirmation
  11. expect(assigns(:provision_url)).to eq 'otpauth://totp/local-part@domain?secret=thisisasecretforthespecofnewview&issuer=cb6e6126.ngrok.io'
  12. expect(assigns(:qrcode)).to be_instance_of RQRCode::QRCode
  13. expect(response).to have_http_status(200)
  14. expect(response).to render_template(:new)
  15. end
  16. end
  17. describe 'GET #new' do
  18. context 'when signed in' do
  19. subject do
  20. sign_in user, scope: :user
  21. get :new, session: { challenge_passed_at: Time.now.utc }
  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 user do not have otp_secret' do
  30. sign_in user_without_otp_secret, scope: :user
  31. get :new, session: { challenge_passed_at: Time.now.utc }
  32. expect(response).to redirect_to('/settings/two_factor_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 }
  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, arg|
  54. expect(value).to eq user
  55. expect(arg).to eq '123456'
  56. true
  57. end
  58. post :create, params: { form_two_factor_confirmation: { otp_attempt: '123456' } }, session: { challenge_passed_at: Time.now.utc }
  59. expect(assigns(:recovery_codes)).to eq otp_backup_codes
  60. expect(flash[:notice]).to eq 'Two-factor authentication successfully enabled'
  61. expect(response).to have_http_status(200)
  62. expect(response).to render_template('settings/two_factor_authentication/recovery_codes/index')
  63. end
  64. end
  65. describe 'when creation fails' do
  66. subject do
  67. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, arg|
  68. expect(value).to eq user
  69. expect(arg).to eq '123456'
  70. false
  71. end
  72. post :create, params: { form_two_factor_confirmation: { otp_attempt: '123456' } }, session: { challenge_passed_at: Time.now.utc }
  73. end
  74. it 'renders the new view' do
  75. subject
  76. expect(response.body).to include 'The entered code was invalid! Are server time and device time correct?'
  77. end
  78. include_examples 'renders :new'
  79. end
  80. end
  81. context 'when not signed in' do
  82. it 'redirects if not signed in' do
  83. post :create, params: { form_two_factor_confirmation: { otp_attempt: '123456' } }
  84. expect(response).to redirect_to('/auth/sign_in')
  85. end
  86. end
  87. end
  88. end