ticket_policy_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. describe TicketPolicy do
  4. subject(:policy) { described_class.new(user, record) }
  5. let(:record) { create(:ticket) }
  6. context "when given ticket's owner" do
  7. let(:user) { record.owner }
  8. it { is_expected.to forbid_actions(%i[show full]) }
  9. context 'when owner has ticket.agent permission' do
  10. let(:user) do
  11. create(:agent, groups: [record.group]).tap do |user|
  12. record.update!(owner: user)
  13. end
  14. end
  15. it { is_expected.to permit_actions(%i[show full]) }
  16. end
  17. end
  18. context 'when given user that is agent and customer' do
  19. let(:user) { create(:agent_and_customer, groups: [record.group]) }
  20. it { is_expected.to permit_actions(%i[show full]) }
  21. end
  22. context 'when given a user that is neither owner nor customer' do
  23. let(:user) { create(:agent) }
  24. it { is_expected.to forbid_actions(%i[show full]) }
  25. context "but the user is an agent with full access to ticket's group" do
  26. before { user.group_names_access_map = { record.group.name => 'full' } }
  27. it { is_expected.to permit_actions(%i[show full]) }
  28. end
  29. context "but the user is a customer from the same organization as ticket's customer" do
  30. let(:record) { create(:ticket, customer: customer) }
  31. let(:customer) { create(:customer, organization: create(:organization)) }
  32. let(:user) { create(:customer, organization: customer.organization) }
  33. context 'and organization.shared is true (default)' do
  34. it { is_expected.to permit_actions(%i[show full]) }
  35. end
  36. context 'but organization.shared is false' do
  37. before { customer.organization.update(shared: false) }
  38. it { is_expected.to forbid_actions(%i[show full]) }
  39. end
  40. end
  41. context 'when user is admin with group access' do
  42. let(:user) { create(:user, roles: Role.where(name: %w[Admin])) }
  43. it { is_expected.to forbid_actions(%i[show full]) }
  44. end
  45. end
  46. context 'when user is agent' do
  47. context 'when user is owner but has no access to the group' do
  48. let(:user) do
  49. create(:agent, groups: []).tap do |user|
  50. record.update!(owner: user)
  51. end
  52. end
  53. it { is_expected.to forbid_actions(%i[show full]) }
  54. end
  55. context 'when owner has ticket.agent permission' do
  56. let(:user) do
  57. create(:agent, groups: [record.group]).tap do |user|
  58. record.update!(owner: user)
  59. end
  60. end
  61. it { is_expected.to permit_actions(%i[show full]) }
  62. end
  63. context 'when groups.follow_up_possible is set' do
  64. let(:record) { create(:ticket, customer: customer, group: group, state: Ticket::State.find_by(name: 'closed')) }
  65. let(:customer) { create(:customer, organization: create(:organization)) }
  66. let(:user) { create(:agent) }
  67. context 'to yes' do
  68. let(:group) { create(:group, follow_up_possible: 'yes') }
  69. context 'when user is customer' do
  70. let(:user) { record.customer }
  71. it { is_expected.to permit_actions(%i[follow_up]) }
  72. end
  73. context 'when user has no access' do
  74. it { is_expected.to forbid_actions(%i[follow_up]) }
  75. end
  76. context 'when user has change access' do
  77. before do
  78. user.user_groups.create! group: group, access: 'change'
  79. end
  80. it { is_expected.to permit_actions(%i[follow_up]) }
  81. end
  82. context 'when user has read access' do
  83. before do
  84. user.user_groups.create! group: group, access: 'read'
  85. end
  86. it { is_expected.to forbid_actions(%i[follow_up]) }
  87. end
  88. end
  89. context 'to new_ticket' do
  90. let(:group) { create(:group, follow_up_possible: 'new_ticket') }
  91. context 'when user is customer' do
  92. let(:user) { record.customer }
  93. it { is_expected.to forbid_actions(%i[follow_up]) }
  94. end
  95. context 'when user has no access' do
  96. it { is_expected.to forbid_actions(%i[follow_up]) }
  97. end
  98. context 'when user has change access' do
  99. before do
  100. user.user_groups.create! group: group, access: 'change'
  101. end
  102. it { is_expected.to permit_actions(%i[follow_up]) }
  103. end
  104. context 'when user has read access' do
  105. before do
  106. user.user_groups.create! group: group, access: 'read'
  107. end
  108. it { is_expected.to forbid_actions(%i[follow_up]) }
  109. end
  110. end
  111. context 'to new_ticket_after_certain_time' do
  112. let(:group) { create(:group, follow_up_possible: 'new_ticket_after_certain_time', reopen_time_in_days: 2) }
  113. context 'when reopen_time_in_days is within configured time frame' do
  114. context 'when user is customer' do
  115. let(:user) { record.customer }
  116. it { is_expected.to permit_actions(%i[follow_up]) }
  117. end
  118. context 'when user has no access' do
  119. it { is_expected.to forbid_actions(%i[follow_up]) }
  120. end
  121. context 'when user has change access' do
  122. before do
  123. user.user_groups.create! group: group, access: 'change'
  124. end
  125. it { is_expected.to permit_actions(%i[follow_up]) }
  126. end
  127. context 'when user has read access' do
  128. before do
  129. user.user_groups.create! group: group, access: 'read'
  130. end
  131. it { is_expected.to forbid_actions(%i[follow_up]) }
  132. end
  133. end
  134. context 'when reopen_time_in_days is outside configured time frame' do
  135. before do
  136. policy
  137. travel 3.days
  138. end
  139. context 'when user is customer' do
  140. let(:user) { record.customer }
  141. it { is_expected.to forbid_actions(%i[follow_up]) }
  142. end
  143. context 'when user has no access' do
  144. it { is_expected.to forbid_actions(%i[follow_up]) }
  145. end
  146. context 'when user has change access' do
  147. before do
  148. user.user_groups.create! group: group, access: 'change'
  149. end
  150. it { is_expected.to permit_actions(%i[follow_up]) }
  151. end
  152. context 'when user has read access' do
  153. before do
  154. user.user_groups.create! group: group, access: 'read'
  155. end
  156. it { is_expected.to forbid_actions(%i[follow_up]) }
  157. end
  158. end
  159. end
  160. end
  161. end
  162. context 'when user is customer' do
  163. context 'when groups.follow_up_possible is yes' do
  164. let(:record) { create(:ticket, customer: user, group: group, state: Ticket::State.find_by(name: 'closed')) }
  165. let(:group) { create(:group, follow_up_possible: 'yes') }
  166. let(:user) { create(:customer, organization: create(:organization)) }
  167. it { is_expected.to permit_actions(%i[follow_up]) }
  168. end
  169. context 'when groups.follow_up_possible is new_ticket' do
  170. let(:record) { create(:ticket, customer: user, group: group, state: Ticket::State.find_by(name: 'closed')) }
  171. let(:group) { create(:group, follow_up_possible: 'new_ticket') }
  172. let(:user) { create(:customer, organization: create(:organization)) }
  173. it { is_expected.to forbid_action(:follow_up) }
  174. it { expect { policy.follow_up? }.to change(policy, :custom_exception).to(Exceptions::UnprocessableEntity) }
  175. end
  176. context 'when groups.follow_up_possible is new_ticket_after_certain_time' do
  177. let(:record) { create(:ticket, customer: user, group: group, state: Ticket::State.find_by(name: 'closed')) }
  178. let(:group) { create(:group, follow_up_possible: 'new_ticket_after_certain_time', reopen_time_in_days: 2) }
  179. let(:user) { create(:customer, organization: create(:organization)) }
  180. context 'when reopen_time_in_days is within reopen time frame' do
  181. it { is_expected.to permit_actions(%i[follow_up]) }
  182. end
  183. context 'when reopen_time_in_days is without reopen time frame' do
  184. before do
  185. policy
  186. travel 3.days
  187. end
  188. it { is_expected.to forbid_action(:follow_up) }
  189. it { expect { policy.follow_up? }.to change(policy, :custom_exception).to(Exceptions::UnprocessableEntity) }
  190. end
  191. end
  192. end
  193. describe 'agent access' do
  194. context 'when user is customer' do
  195. let(:user) { create(:customer) }
  196. let(:record) { create(:ticket, customer: user) }
  197. it { is_expected.to forbid_actions(%i[agent_read_access agent_update_access agent_create_access]) }
  198. end
  199. context 'when user is agent with read access' do
  200. let(:user) { create(:agent) }
  201. before do
  202. user.user_groups.create! group: record.group, access: 'read'
  203. end
  204. it { is_expected.to permit_actions(%i[agent_read_access]) }
  205. it { is_expected.to forbid_actions(%i[agent_update_access agent_create_access]) }
  206. end
  207. context 'when user is agent with update access' do
  208. let(:user) { create(:agent) }
  209. before do
  210. user.user_groups.create! group: record.group, access: 'change'
  211. end
  212. it { is_expected.to permit_actions(%i[agent_update_access]) }
  213. it { is_expected.to forbid_actions(%i[agent_read_access agent_create_access]) }
  214. end
  215. context 'when user is agent with full access' do
  216. let(:user) { create(:agent, groups: [record.group]) }
  217. it { is_expected.to permit_actions(%i[agent_read_access agent_update_access agent_update_access]) }
  218. end
  219. context 'when user is agent-customer with customer access to ticket' do
  220. let(:user) { create(:agent_and_customer) }
  221. let(:record) { create(:ticket, customer: user) }
  222. it { is_expected.to forbid_actions(%i[agent_read_access agent_update_access agent_create_access]) }
  223. end
  224. context 'when user is agent-customer with agent read access to ticket' do
  225. let(:user) { create(:agent_and_customer) }
  226. before do
  227. user.user_groups.create! group: record.group, access: 'read'
  228. end
  229. it { is_expected.to permit_actions(%i[agent_read_access]) }
  230. it { is_expected.to forbid_actions(%i[agent_update_access agent_create_access]) }
  231. end
  232. context 'when user is agent-customer with agent change access to ticket' do
  233. let(:user) { create(:agent_and_customer) }
  234. before do
  235. user.user_groups.create! group: record.group, access: 'change'
  236. end
  237. it { is_expected.to forbid_actions(%i[agent_read_access agent_create_access]) }
  238. it { is_expected.to permit_actions(%i[agent_update_access]) }
  239. end
  240. context 'when user is agent-customer with full agent access to ticket' do
  241. let(:user) { create(:agent_and_customer, groups: [record.group]) }
  242. it { is_expected.to permit_actions(%i[agent_read_access agent_update_access agent_create_access]) }
  243. end
  244. end
  245. describe '#create_mentions?' do
  246. let(:user) { create(:agent) }
  247. it 'delegates to #agent_read_access?' do
  248. allow(policy).to receive(:agent_read_access?)
  249. policy.create_mentions?
  250. expect(policy).to have_received(:agent_read_access?)
  251. end
  252. end
  253. describe 'fields restriction' do
  254. context 'when user is agent' do
  255. let(:user) { create(:agent, groups: [record.group]) }
  256. it 'does not forbid time unit and checklist fields' do
  257. expect(policy.show?).to permit_fields(%i[time_unit time_units_per_type checklist referencing_checklist_tickets])
  258. end
  259. end
  260. context 'when user is customer' do
  261. let(:user) { create(:customer) }
  262. before { record.update!(customer: user) }
  263. it 'forbids time unit and checklist fields' do
  264. expect(policy.show?)
  265. .to be_truthy
  266. .and(forbid_fields(%i[time_unit time_units_per_type checklist referencing_checklist_tickets]))
  267. end
  268. it 'permits other fields' do
  269. expect(policy.show?).to permit_fields(%i[id subject])
  270. end
  271. end
  272. context 'when user is customer via shared organization' do
  273. let(:organization) { create(:organization) }
  274. let(:user) { create(:customer, organization:) }
  275. let(:customer) { create(:customer, organization:) }
  276. let(:record) { create(:ticket, customer:) }
  277. it 'forbids time unit and checklist fields' do
  278. expect(policy.show?)
  279. .to be_truthy
  280. .and(forbid_fields(%i[time_unit time_units_per_type checklist referencing_checklist_tickets]))
  281. end
  282. it 'permits other fields' do
  283. expect(policy.show?).to permit_fields(%i[id subject])
  284. end
  285. end
  286. end
  287. end