ticket_policy_spec.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. require 'rails_helper'
  2. describe TicketPolicy do
  3. subject { described_class.new(user, record) }
  4. let(:record) { create(:ticket) }
  5. context 'when given ticket’s owner' do
  6. let(:user) { record.owner }
  7. it { is_expected.not_to permit_actions(%i[show full]) }
  8. context 'when owner has ticket.agent permission' do
  9. let(:user) do
  10. create(:agent, groups: [record.group]).tap do |user|
  11. record.update!(owner: user)
  12. end
  13. end
  14. it { is_expected.to permit_actions(%i[show full]) }
  15. end
  16. end
  17. context 'when given user that is agent and customer' do
  18. let(:user) { create(:agent_and_customer, groups: [record.group]) }
  19. it { is_expected.to permit_actions(%i[show full]) }
  20. end
  21. context 'when given a user that is neither owner nor customer' do
  22. let(:user) { create(:agent) }
  23. it { is_expected.not_to permit_actions(%i[show full]) }
  24. context 'but the user is an agent with full access to ticket’s group' do
  25. before { user.group_names_access_map = { record.group.name => 'full' } }
  26. it { is_expected.to permit_actions(%i[show full]) }
  27. end
  28. context 'but the user is a customer from the same organization as ticket’s customer' do
  29. let(:record) { create(:ticket, customer: customer) }
  30. let(:customer) { create(:customer, organization: create(:organization)) }
  31. let(:user) { create(:customer, organization: customer.organization) }
  32. context 'and organization.shared is true (default)' do
  33. it { is_expected.to permit_actions(%i[show full]) }
  34. end
  35. context 'but organization.shared is false' do
  36. before { customer.organization.update(shared: false) }
  37. it { is_expected.not_to permit_actions(%i[show full]) }
  38. end
  39. end
  40. context 'when user is admin with group access' do
  41. let(:user) { create(:user, roles: Role.where(name: %w[Admin])) }
  42. it { is_expected.not_to permit_actions(%i[show full]) }
  43. end
  44. end
  45. context 'when user is agent' do
  46. context 'when owner has ticket.agent permission' do
  47. let(:user) do
  48. create(:agent, groups: [record.group]).tap do |user|
  49. record.update!(owner: user)
  50. end
  51. end
  52. it { is_expected.to permit_actions(%i[show full]) }
  53. end
  54. end
  55. end