two_factor_preference_spec.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. context 'when user has no two-factor preferences' do
  8. before do
  9. create(:user_two_factor_preference, :authenticator_app, user: user)
  10. end
  11. it 'removes the default method from user preferences' do
  12. user.reload.two_factor_preferences.destroy_all
  13. expect(user.preferences).not_to include(two_factor_authentication: { default: 'authenticator_app' })
  14. end
  15. end
  16. context 'when user has two-factor preferences' do
  17. before do
  18. create(:user_two_factor_preference, :authenticator_app, user: user)
  19. create(:user_two_factor_preference, :security_keys, user: user)
  20. end
  21. context 'when default method is removed' do
  22. it 'updates the default method in user preferences' do
  23. user.reload.two_factor_preferences.last.destroy
  24. expect(user.preferences.dig(:two_factor_authentication, :default)).to eq('authenticator_app')
  25. end
  26. end
  27. end
  28. end
  29. end
  30. end