has_two_factor_examples.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'User::HasTwoFactor' do
  3. subject(:user) { create(:user) }
  4. describe 'Instance methods:' do
  5. describe '#two_factor_configured?' do
  6. before do
  7. Setting.set('two_factor_authentication_method_authenticator_app', true)
  8. end
  9. context 'with no two factor configured' do
  10. it { is_expected.not_to be_two_factor_configured }
  11. end
  12. context 'with two factor configured' do
  13. before do
  14. Setting.set('two_factor_authentication_method_authenticator_app', true)
  15. create(:user_two_factor_preference, :authenticator_app, user: user)
  16. user.reload
  17. end
  18. it { is_expected.to be_two_factor_configured }
  19. end
  20. end
  21. describe '#two_factor_destroy_all_authentication_methods' do
  22. let(:authenticator_app_preference) { create(:user_two_factor_preference, :authenticator_app, user: user) }
  23. let(:security_keys_preference) { create(:user_two_factor_preference, :security_keys, user: user) }
  24. before do
  25. Setting.set('two_factor_authentication_method_authenticator_app', true)
  26. authenticator_app_preference
  27. end
  28. it 'destroys both enabled and disabled methods' do
  29. security_keys_preference
  30. expect { user.two_factor_destroy_all_authentication_methods }
  31. .to change { user.two_factor_preferences.exists? }
  32. .to false
  33. end
  34. it 'destroys created authentication method' do
  35. expect { user.two_factor_destroy_all_authentication_methods }
  36. .to change { User::TwoFactorPreference.exists? authenticator_app_preference.id }
  37. .to false
  38. end
  39. end
  40. end
  41. end