scope_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::OverviewsPolicy::Scope do
  4. subject(:scope) { described_class.new(user, original_collection) }
  5. let(:original_collection) { Overview }
  6. let(:overview_a) { create(:overview) }
  7. let(:overview_b) { create(:overview, organization_shared: true) }
  8. let(:overview_c) { create(:overview, out_of_office: true) }
  9. before do
  10. Overview.destroy_all
  11. overview_a && overview_b && overview_c
  12. end
  13. describe '#resolve' do
  14. context 'without user' do
  15. let(:user) { nil }
  16. it 'throws exception' do
  17. expect { scope.resolve }.to raise_error %r{Authentication required}
  18. end
  19. end
  20. context 'with customer' do
  21. let(:user) { create(:customer, organization: create(:organization, shared: false)) }
  22. it 'returns base' do
  23. expect(scope.resolve).to contain_exactly(overview_a)
  24. end
  25. context 'with shared organization' do
  26. before do
  27. user.organization.update! shared: true
  28. end
  29. it 'returns base and shared' do
  30. expect(scope.resolve).to contain_exactly(overview_a, overview_b)
  31. end
  32. end
  33. end
  34. context 'with agent' do
  35. let(:user) { create(:agent) }
  36. it 'returns base' do
  37. expect(scope.resolve).to contain_exactly(overview_a)
  38. end
  39. context 'when out of office replacement' do
  40. before do
  41. create(:agent).update!(
  42. out_of_office: true,
  43. out_of_office_start_at: 1.day.ago,
  44. out_of_office_end_at: 1.day.from_now,
  45. out_of_office_replacement_id: user.id,
  46. )
  47. end
  48. it 'returns base and out of office' do
  49. expect(scope.resolve).to contain_exactly(overview_a, overview_c)
  50. end
  51. end
  52. end
  53. context 'with agent-customer' do
  54. let(:user) { create(:agent_and_customer, organization: create(:organization, shared: false)) }
  55. it 'returns base' do
  56. expect(scope.resolve).to contain_exactly(overview_a)
  57. end
  58. context 'with shared organization' do
  59. before do
  60. user.organization.update! shared: true
  61. end
  62. it 'returns base and shared' do
  63. expect(scope.resolve).to contain_exactly(overview_a, overview_b)
  64. end
  65. context 'when out of office replacement' do
  66. before do
  67. create(:agent).update!(
  68. out_of_office: true,
  69. out_of_office_start_at: 1.day.ago,
  70. out_of_office_end_at: 1.day.from_now,
  71. out_of_office_replacement_id: user.id,
  72. )
  73. end
  74. it 'returns all' do
  75. expect(scope.resolve).to contain_exactly(overview_a, overview_b, overview_c)
  76. end
  77. end
  78. end
  79. context 'when out of office replacement' do
  80. before do
  81. create(:agent).update!(
  82. out_of_office: true,
  83. out_of_office_start_at: 1.day.ago,
  84. out_of_office_end_at: 1.day.from_now,
  85. out_of_office_replacement_id: user.id,
  86. )
  87. end
  88. it 'returns base and out of office' do
  89. expect(scope.resolve).to contain_exactly(overview_a, overview_c)
  90. end
  91. end
  92. end
  93. context 'without ticket permission' do
  94. let(:user) { create(:admin_only) }
  95. it 'returns nothing' do
  96. expect(scope.resolve).to be_empty
  97. end
  98. end
  99. end
  100. end