two_factors_controller_policy_spec.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. describe Controllers::User::TwoFactorsControllerPolicy do
  4. subject { described_class.new(user, record) }
  5. let(:record_class) { User::TwoFactorsController }
  6. let(:record) do
  7. rec = record_class.new
  8. rec.params = params
  9. rec
  10. end
  11. let(:twofactoree) { create(:agent) }
  12. describe 'endpoints for current user' do
  13. let(:user) { twofactoree }
  14. let(:params) { {} }
  15. let(:permitted_actions) do
  16. %i[two_factor_verify_configuration two_factor_authentication_method_initiate_configuration two_factor_default_authentication_method two_factor_authentication_method_configuration two_factor_authentication_method_configuration_save]
  17. end
  18. it { is_expected.to permit_actions(permitted_actions) }
  19. end
  20. describe 'endpoints allowing to manage other users' do
  21. let(:params) { { id: twofactoree.id } }
  22. context 'with an admin' do
  23. let(:user) { create(:admin) }
  24. let(:permitted_actions) do
  25. %i[two_factor_enabled_authentication_methods two_factor_remove_authentication_method two_factor_remove_all_authentication_methods]
  26. end
  27. it { is_expected.to permit_actions(permitted_actions) }
  28. end
  29. context 'with a different user' do
  30. let(:user) { create(:agent) }
  31. let(:forbidden_actions) do
  32. %i[two_factor_enabled_authentication_methods two_factor_remove_authentication_method two_factor_remove_all_authentication_methods]
  33. end
  34. it { is_expected.to forbid_actions(forbidden_actions) }
  35. end
  36. context 'with the user' do
  37. let(:user) { twofactoree }
  38. let(:permitted_actions) do
  39. %i[two_factor_enabled_authentication_methods two_factor_remove_authentication_method two_factor_remove_all_authentication_methods]
  40. end
  41. it { is_expected.to permit_actions(permitted_actions) }
  42. end
  43. end
  44. end