group_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::History::Group, current_user_id: -> { user.id } do
  4. subject(:service) { described_class.new(current_user: user) }
  5. before do
  6. object
  7. end
  8. context 'when history object is a ticket' do
  9. let(:group) { create(:group) }
  10. let(:object) { create(:ticket, group: group) }
  11. context 'when user is not authorized to view the ticket' do
  12. let(:user) { create(:agent) }
  13. it 'raises an error' do
  14. expect { service.execute(object:) }.to raise_error(Pundit::NotAuthorizedError)
  15. end
  16. end
  17. context 'when user is authorized to view the ticket' do
  18. let(:user) { create(:agent, groups: [group]) }
  19. it 'returns a group of history records for the ticket', :aggregate_failures do
  20. expect { service.execute(object:) }.not_to raise_error
  21. expect(service.execute(object:)).to be_an_instance_of(Array)
  22. expect(service.execute(object:).first).to include(
  23. :created_at, :records
  24. )
  25. expect(service.execute(object:).first[:records].first).to include(
  26. :issuer, :events
  27. )
  28. expect(service.execute(object:).first[:records].first[:events].first).not_to include(:issuer)
  29. end
  30. end
  31. end
  32. context 'when history object is a user' do
  33. let(:object) { create(:user) }
  34. context 'when user is not authorized to view the user' do
  35. let(:user) { create(:customer) }
  36. it 'raises an error' do
  37. expect { service.execute(object:) }.to raise_error(Pundit::NotAuthorizedError)
  38. end
  39. end
  40. context 'when user is authorized to view the user' do
  41. let(:user) { create(:admin) }
  42. it 'returns a group of history records for the user', :aggregate_failures do
  43. expect { service.execute(object:) }.not_to raise_error
  44. expect(service.execute(object:)).to be_an_instance_of(Array)
  45. expect(service.execute(object:).first).to include(
  46. :created_at, :records
  47. )
  48. expect(service.execute(object:).first[:records].first).to include(
  49. :issuer, :events
  50. )
  51. expect(service.execute(object:).first[:records].first[:events].first).not_to include(:issuer)
  52. end
  53. end
  54. end
  55. context 'when history object is a organization' do
  56. let(:object) { create(:organization) }
  57. context 'when user is not authorized to view the organization' do
  58. let(:user) { create(:customer) }
  59. it 'raises an error' do
  60. expect { service.execute(object:) }.to raise_error(Pundit::NotAuthorizedError)
  61. end
  62. end
  63. context 'when user is authorized to view the organization' do
  64. let(:user) { create(:admin) }
  65. it 'returns a group of history records for the organization', :aggregate_failures do
  66. expect { service.execute(object:) }.not_to raise_error
  67. expect(service.execute(object:)).to be_an_instance_of(Array)
  68. expect(service.execute(object:).first).to include(
  69. :created_at, :records
  70. )
  71. expect(service.execute(object:).first[:records].first).to include(
  72. :issuer, :events
  73. )
  74. expect(service.execute(object:).first[:records].first[:events].first).not_to include(:issuer)
  75. end
  76. end
  77. end
  78. end