list_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::History::List, 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 list of history events 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, :issuer, :action, :object, :attribute, :changes
  24. )
  25. end
  26. end
  27. end
  28. context 'when history object is a user' do
  29. let(:object) { create(:user) }
  30. context 'when user is not authorized to view the user' do
  31. let(:user) { create(:customer) }
  32. it 'raises an error' do
  33. expect { service.execute(object:) }.to raise_error(Pundit::NotAuthorizedError)
  34. end
  35. end
  36. context 'when user is authorized to view the user' do
  37. let(:user) { create(:admin) }
  38. it 'returns a list of history events for the user', :aggregate_failures do
  39. expect { service.execute(object:) }.not_to raise_error
  40. expect(service.execute(object:)).to be_an_instance_of(Array)
  41. expect(service.execute(object:).first).to include(
  42. :created_at, :issuer, :action, :object, :attribute, :changes
  43. )
  44. end
  45. end
  46. end
  47. context 'when history object is a organization' do
  48. let(:object) { create(:organization) }
  49. context 'when user is not authorized to view the organization' do
  50. let(:user) { create(:customer) }
  51. it 'raises an error' do
  52. expect { service.execute(object:) }.to raise_error(Pundit::NotAuthorizedError)
  53. end
  54. end
  55. context 'when user is authorized to view the organization' do
  56. let(:user) { create(:admin) }
  57. it 'returns a list of history events for the organization', :aggregate_failures do
  58. expect { service.execute(object:) }.not_to raise_error
  59. expect(service.execute(object:)).to be_an_instance_of(Array)
  60. expect(service.execute(object:).first).to include(
  61. :created_at, :issuer, :action, :object, :attribute, :changes
  62. )
  63. end
  64. end
  65. end
  66. end