two_factor_default_authentication_method_spec.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe TwoFactorDefaultAuthenticationMethod, db_strategy: :reset, type: :db_migration do
  4. let(:user_without_two_factor) { create(:user) }
  5. let(:user_with_two_factor) do
  6. user = create(:user)
  7. create(:user_two_factor_preference, :authenticator_app, user: user)
  8. user_preferences = user.reload.preferences
  9. user_preferences[:two_factor_authentication] = user.preferences[:two_factor_authentication].except(:default)
  10. user.update!(preferences: user_preferences)
  11. user
  12. end
  13. let(:user_with_two_factor_and_default_method) do
  14. user = create(:user)
  15. create(:user_two_factor_preference, :authenticator_app, user: user)
  16. security_key_pref = create(:user_two_factor_preference, :security_keys, user: user)
  17. user.reload.two_factor_update_default_method(security_key_pref.method)
  18. user
  19. end
  20. context 'when there are no users with two-factor authentication' do
  21. it 'does not change anything' do
  22. user_without_two_factor
  23. expect { migrate }.not_to change { user_without_two_factor.reload.preferences.dig(:two_factor_authentication, :default) }
  24. end
  25. end
  26. context 'when there are users with two-factor authentication' do
  27. it 'sets the default authentication method to the first one' do
  28. user_with_two_factor
  29. expect { migrate }.to change { user_with_two_factor.reload.preferences.dig(:two_factor_authentication, :default) }.from(nil).to('authenticator_app')
  30. end
  31. end
  32. context 'when there are users with two-factor authentication and a default method' do
  33. it 'does not change the already stored default two-factor authentication method' do
  34. user_with_two_factor_and_default_method
  35. expect { migrate }.not_to change { user_with_two_factor_and_default_method.reload.preferences.dig(:two_factor_authentication, :default) }
  36. end
  37. end
  38. end