scope_spec.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe TemplatePolicy::Scope do
  4. subject(:scope) { described_class.new(user, original_collection) }
  5. let(:original_collection) { Template }
  6. let(:active_template) { create(:template, :dummy_data, active: true) }
  7. let(:inactive_template) { create(:template, :dummy_data, active: false) }
  8. before do
  9. Template.destroy_all
  10. active_template && inactive_template
  11. end
  12. describe '#resolve' do
  13. context 'without user' do
  14. let(:user) { nil }
  15. it 'throws exception' do
  16. expect { scope.resolve }.to raise_error %r{Authentication required}
  17. end
  18. end
  19. context 'with customer' do
  20. let(:user) { create(:customer) }
  21. it 'returns empty' do
  22. expect(scope.resolve).to be_empty
  23. end
  24. end
  25. context 'with agent' do
  26. let(:user) { create(:agent) }
  27. it 'returns active template only' do
  28. expect(scope.resolve).to contain_exactly(active_template)
  29. end
  30. end
  31. context 'with admin' do
  32. let(:user) { create(:admin) }
  33. it 'returns all templates' do
  34. expect(scope.resolve).to contain_exactly(active_template, inactive_template)
  35. end
  36. end
  37. end
  38. end