scope_spec.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe MacroPolicy::Scope do
  4. subject(:scope) { described_class.new(user, original_collection) }
  5. let(:original_collection) { Macro }
  6. let(:group_a) { create(:group) }
  7. let(:macro_a) { create(:macro, groups: [group_a]) }
  8. let(:group_b) { create(:group) }
  9. let(:macro_b) { create(:macro, groups: [group_b]) }
  10. let(:macro_c) { create(:macro, groups: []) }
  11. before do
  12. Macro.destroy_all
  13. macro_a && macro_b && macro_c
  14. end
  15. describe '#resolve' do
  16. context 'without user' do
  17. let(:user) { nil }
  18. it 'throws exception' do
  19. expect { scope.resolve }.to raise_error %r{Authentication required}
  20. end
  21. end
  22. context 'with customer' do
  23. let(:user) { create(:customer) }
  24. it 'returns empty' do
  25. expect(scope.resolve).to be_empty
  26. end
  27. end
  28. context 'with agent' do
  29. let(:user) { create(:agent) }
  30. before { user.groups << group_a }
  31. it 'returns global and group macro' do
  32. expect(scope.resolve).to contain_exactly(macro_a, macro_c)
  33. end
  34. end
  35. context 'with admin' do
  36. let(:user) { create(:admin) }
  37. before { user.groups << group_b }
  38. it 'returns all macros' do
  39. expect(scope.resolve).to contain_exactly(macro_a, macro_b, macro_c)
  40. end
  41. end
  42. end
  43. end