two_factor_authentications_controller_spec.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. require 'rails_helper'
  2. require 'webauthn/fake_client'
  3. describe Admin::TwoFactorAuthenticationsController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. before do
  7. sign_in Fabricate(:user, admin: true), scope: :user
  8. end
  9. describe 'DELETE #destroy' do
  10. context 'when user has OTP enabled' do
  11. before do
  12. user.update(otp_required_for_login: true)
  13. end
  14. it 'redirects to admin accounts page' do
  15. delete :destroy, params: { user_id: user.id }
  16. user.reload
  17. expect(user.otp_enabled?).to eq false
  18. expect(response).to redirect_to(admin_accounts_path)
  19. end
  20. end
  21. context 'when user has OTP and WebAuthn enabled' do
  22. let(:fake_client) { WebAuthn::FakeClient.new('http://test.host') }
  23. before do
  24. user.update(otp_required_for_login: true, webauthn_id: WebAuthn.generate_user_id)
  25. public_key_credential = WebAuthn::Credential.from_create(fake_client.create)
  26. Fabricate(:webauthn_credential,
  27. user_id: user.id,
  28. external_id: public_key_credential.id,
  29. public_key: public_key_credential.public_key,
  30. nickname: 'Security Key')
  31. end
  32. it 'redirects to admin accounts page' do
  33. delete :destroy, params: { user_id: user.id }
  34. user.reload
  35. expect(user.otp_enabled?).to eq false
  36. expect(user.webauthn_enabled?).to eq false
  37. expect(response).to redirect_to(admin_accounts_path)
  38. end
  39. end
  40. end
  41. end