two_factor_preference_spec.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe User::TwoFactorPreference, type: :model do
  4. describe 'hooks' do
  5. context 'when after_destroy/after_save is triggered' do
  6. let(:user) { create(:user) }
  7. let(:authenticator_app_preference) { create(:user_two_factor_preference, :authenticator_app, user: user) }
  8. let(:security_keys_preference) { create(:user_two_factor_preference, :security_keys, user: user) }
  9. before do
  10. Setting.set('two_factor_authentication_method_security_keys', true)
  11. Setting.set('two_factor_authentication_method_authenticator_app', true)
  12. end
  13. context 'when user has no two-factor preferences' do
  14. before do
  15. authenticator_app_preference
  16. end
  17. it 'removes the default method from user preferences' do
  18. expect { user.reload.two_factor_preferences.destroy_all }
  19. .to change { user.reload.two_factor_default }
  20. .from('authenticator_app')
  21. .to(nil)
  22. end
  23. end
  24. context 'when user has two-factor preferences' do
  25. before do
  26. security_keys_preference
  27. authenticator_app_preference
  28. end
  29. context 'when default method is removed' do
  30. it 'updates the default method in user preferences' do
  31. expect { security_keys_preference.destroy! }
  32. .to change { user.reload.two_factor_default }
  33. .from('security_keys')
  34. .to('authenticator_app')
  35. end
  36. end
  37. end
  38. end
  39. end
  40. end