logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

confirmations_controller_spec.rb (3513B)


  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(:success)
  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
  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
  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. expect { post :create, params: { } }.to raise_error(ActionController::ParameterMissing)
  43. end
  44. end
  45. describe 'when creation succeeds' do
  46. it 'renders page with success' do
  47. otp_backup_codes = user.generate_otp_backup_codes!
  48. expect_any_instance_of(User).to receive(:generate_otp_backup_codes!) do |value|
  49. expect(value).to eq user
  50. otp_backup_codes
  51. end
  52. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, arg|
  53. expect(value).to eq user
  54. expect(arg).to eq '123456'
  55. true
  56. end
  57. post :create, params: { form_two_factor_confirmation: { code: '123456' } }
  58. expect(assigns(:recovery_codes)).to eq otp_backup_codes
  59. expect(flash[:notice]).to eq 'Two-factor authentication successfully enabled'
  60. expect(response).to have_http_status(:success)
  61. expect(response).to render_template('settings/two_factor_authentication/recovery_codes/index')
  62. end
  63. end
  64. describe 'when creation fails' do
  65. subject do
  66. expect_any_instance_of(User).to receive(:validate_and_consume_otp!) do |value, arg|
  67. expect(value).to eq user
  68. expect(arg).to eq '123456'
  69. false
  70. end
  71. post :create, params: { form_two_factor_confirmation: { code: '123456' } }
  72. end
  73. it 'renders the new view' do
  74. subject
  75. expect(response.body).to include 'The entered code was invalid! Are server time and device time correct?'
  76. end
  77. include_examples 'renders :new'
  78. end
  79. end
  80. context 'when not signed in' do
  81. it 'redirects if not signed in' do
  82. post :create, params: { form_two_factor_confirmation: { code: '123456' } }
  83. expect(response).to redirect_to('/auth/sign_in')
  84. end
  85. end
  86. end
  87. end