mentions_controller_policy_spec.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. describe Controllers::MentionsControllerPolicy do
  4. subject(:policy) { described_class.new(user, record) }
  5. let(:user) { create(:agent_and_customer) }
  6. let(:ticket) { create(:ticket) }
  7. let(:record_class) { MentionsController }
  8. let(:record) do
  9. rec = record_class.new
  10. rec.params = params
  11. rec
  12. end
  13. context 'with ticket' do
  14. let(:params) do
  15. {
  16. mentionable_type: 'Ticket',
  17. mentionable_id: ticket.id
  18. }
  19. end
  20. context 'with agent access' do
  21. before { user.user_groups.create! group: ticket.group, access: 'full' }
  22. it { is_expected.to permit_actions %i[index create] }
  23. end
  24. context 'with agent read access' do
  25. before { user.user_groups.create! group: ticket.group, access: 'read' }
  26. it { is_expected.to permit_actions %i[index create] }
  27. end
  28. context 'with customer access' do
  29. before { ticket.update! customer: user }
  30. it { is_expected.to forbid_actions %i[index create] }
  31. end
  32. context 'with no access' do
  33. it { is_expected.to forbid_actions %i[index create] }
  34. end
  35. end
  36. context 'with non-ticket' do
  37. let(:params) do
  38. {
  39. mentionable_type: 'NonTicket',
  40. mentionable_id: 123
  41. }
  42. end
  43. it { is_expected.to forbid_actions(%i[index create]) }
  44. it { expect { policy.index? }.to change(policy, :custom_exception).to(Exceptions::UnprocessableEntity) }
  45. it { expect { policy.create? }.to change(policy, :custom_exception).to(Exceptions::UnprocessableEntity) }
  46. end
  47. context 'with mention' do
  48. let(:params) { { id: mention.id } }
  49. let(:mention) do
  50. mention = build(:mention, mentionable: ticket, user: mention_user)
  51. mention.save(validate: false)
  52. mention
  53. end
  54. context 'when self mention exists' do
  55. let(:mention_user) { user }
  56. context 'when user has agent access to object' do
  57. before { user.user_groups.create! group: ticket.group, access: 'full' }
  58. it { is_expected.to permit_action :destroy }
  59. end
  60. context 'when user has no agent access to object' do
  61. it { is_expected.to permit_action :destroy }
  62. end
  63. end
  64. context 'when another user\'s mention exists' do
  65. let(:mention_user) { create(:user) }
  66. it { is_expected.to forbid_action :destroy }
  67. end
  68. context 'when mention does not exist' do
  69. let(:params) { { id: 0 } }
  70. it { is_expected.to forbid_action :destroy }
  71. end
  72. end
  73. end