ticket_policy_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. # Copyright (C) 2012-2024 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 owner has ticket.agent permission' do
  48. let(:user) do
  49. create(:agent, groups: [record.group]).tap do |user|
  50. record.update!(owner: user)
  51. end
  52. end
  53. it { is_expected.to permit_actions(%i[show full]) }
  54. end
  55. context 'when groups.follow_up_possible is set' do
  56. let(:record) { create(:ticket, customer: customer, group: group, state: Ticket::State.find_by(name: 'closed')) }
  57. let(:customer) { create(:customer, organization: create(:organization)) }
  58. let(:user) { create(:agent) }
  59. context 'to yes' do
  60. let(:group) { create(:group, follow_up_possible: 'yes') }
  61. context 'when user is customer' do
  62. let(:user) { record.customer }
  63. it { is_expected.to permit_actions(%i[follow_up]) }
  64. end
  65. context 'when user has no access' do
  66. it { is_expected.to forbid_actions(%i[follow_up]) }
  67. end
  68. context 'when user has change access' do
  69. before do
  70. user.user_groups.create! group: group, access: 'change'
  71. end
  72. it { is_expected.to permit_actions(%i[follow_up]) }
  73. end
  74. context 'when user has read access' do
  75. before do
  76. user.user_groups.create! group: group, access: 'read'
  77. end
  78. it { is_expected.to forbid_actions(%i[follow_up]) }
  79. end
  80. end
  81. context 'to new_ticket' do
  82. let(:group) { create(:group, follow_up_possible: 'new_ticket') }
  83. context 'when user is customer' do
  84. let(:user) { record.customer }
  85. it { is_expected.to forbid_actions(%i[follow_up]) }
  86. end
  87. context 'when user has no access' do
  88. it { is_expected.to forbid_actions(%i[follow_up]) }
  89. end
  90. context 'when user has change access' do
  91. before do
  92. user.user_groups.create! group: group, access: 'change'
  93. end
  94. it { is_expected.to permit_actions(%i[follow_up]) }
  95. end
  96. context 'when user has read access' do
  97. before do
  98. user.user_groups.create! group: group, access: 'read'
  99. end
  100. it { is_expected.to forbid_actions(%i[follow_up]) }
  101. end
  102. end
  103. context 'to new_ticket_after_certain_time' do
  104. let(:group) { create(:group, follow_up_possible: 'new_ticket_after_certain_time', reopen_time_in_days: 2) }
  105. context 'when reopen_time_in_days is within configured time frame' do
  106. context 'when user is customer' do
  107. let(:user) { record.customer }
  108. it { is_expected.to permit_actions(%i[follow_up]) }
  109. end
  110. context 'when user has no access' do
  111. it { is_expected.to forbid_actions(%i[follow_up]) }
  112. end
  113. context 'when user has change access' do
  114. before do
  115. user.user_groups.create! group: group, access: 'change'
  116. end
  117. it { is_expected.to permit_actions(%i[follow_up]) }
  118. end
  119. context 'when user has read access' do
  120. before do
  121. user.user_groups.create! group: group, access: 'read'
  122. end
  123. it { is_expected.to forbid_actions(%i[follow_up]) }
  124. end
  125. end
  126. context 'when reopen_time_in_days is outside configured time frame' do
  127. before do
  128. policy
  129. travel 3.days
  130. end
  131. context 'when user is customer' do
  132. let(:user) { record.customer }
  133. it { is_expected.to forbid_actions(%i[follow_up]) }
  134. end
  135. context 'when user has no access' do
  136. it { is_expected.to forbid_actions(%i[follow_up]) }
  137. end
  138. context 'when user has change access' do
  139. before do
  140. user.user_groups.create! group: group, access: 'change'
  141. end
  142. it { is_expected.to permit_actions(%i[follow_up]) }
  143. end
  144. context 'when user has read access' do
  145. before do
  146. user.user_groups.create! group: group, access: 'read'
  147. end
  148. it { is_expected.to forbid_actions(%i[follow_up]) }
  149. end
  150. end
  151. end
  152. end
  153. end
  154. context 'when user is customer' do
  155. context 'when groups.follow_up_possible is yes' do
  156. let(:record) { create(:ticket, customer: user, group: group, state: Ticket::State.find_by(name: 'closed')) }
  157. let(:group) { create(:group, follow_up_possible: 'yes') }
  158. let(:user) { create(:customer, organization: create(:organization)) }
  159. it { is_expected.to permit_actions(%i[follow_up]) }
  160. end
  161. context 'when groups.follow_up_possible is new_ticket' do
  162. let(:record) { create(:ticket, customer: user, group: group, state: Ticket::State.find_by(name: 'closed')) }
  163. let(:group) { create(:group, follow_up_possible: 'new_ticket') }
  164. let(:user) { create(:customer, organization: create(:organization)) }
  165. it { is_expected.to forbid_action(:follow_up) }
  166. it { expect { policy.follow_up? }.to change(policy, :custom_exception).to(Exceptions::UnprocessableEntity) }
  167. end
  168. context 'when groups.follow_up_possible is new_ticket_after_certain_time' do
  169. let(:record) { create(:ticket, customer: user, group: group, state: Ticket::State.find_by(name: 'closed')) }
  170. let(:group) { create(:group, follow_up_possible: 'new_ticket_after_certain_time', reopen_time_in_days: 2) }
  171. let(:user) { create(:customer, organization: create(:organization)) }
  172. context 'when reopen_time_in_days is within reopen time frame' do
  173. it { is_expected.to permit_actions(%i[follow_up]) }
  174. end
  175. context 'when reopen_time_in_days is without reopen time frame' do
  176. before do
  177. policy
  178. travel 3.days
  179. end
  180. it { is_expected.to forbid_action(:follow_up) }
  181. it { expect { policy.follow_up? }.to change(policy, :custom_exception).to(Exceptions::UnprocessableEntity) }
  182. end
  183. end
  184. end
  185. describe 'agent access' do
  186. context 'when user is customer' do
  187. let(:user) { create(:customer) }
  188. let(:record) { create(:ticket, customer: user) }
  189. it { is_expected.to forbid_actions(%i[agent_read_access agent_update_access agent_create_access]) }
  190. end
  191. context 'when user is agent with read access' do
  192. let(:user) { create(:agent) }
  193. before do
  194. user.user_groups.create! group: record.group, access: 'read'
  195. end
  196. it { is_expected.to permit_actions(%i[agent_read_access]) }
  197. it { is_expected.to forbid_actions(%i[agent_update_access agent_create_access]) }
  198. end
  199. context 'when user is agent with update access' do
  200. let(:user) { create(:agent) }
  201. before do
  202. user.user_groups.create! group: record.group, access: 'change'
  203. end
  204. it { is_expected.to permit_actions(%i[agent_update_access]) }
  205. it { is_expected.to forbid_actions(%i[agent_read_access agent_create_access]) }
  206. end
  207. context 'when user is agent with full access' do
  208. let(:user) { create(:agent, groups: [record.group]) }
  209. it { is_expected.to permit_actions(%i[agent_read_access agent_update_access agent_update_access]) }
  210. end
  211. context 'when user is agent-customer with customer access to ticket' do
  212. let(:user) { create(:agent_and_customer) }
  213. let(:record) { create(:ticket, customer: user) }
  214. it { is_expected.to forbid_actions(%i[agent_read_access agent_update_access agent_create_access]) }
  215. end
  216. context 'when user is agent-customer with agent read access to ticket' do
  217. let(:user) { create(:agent_and_customer) }
  218. before do
  219. user.user_groups.create! group: record.group, access: 'read'
  220. end
  221. it { is_expected.to permit_actions(%i[agent_read_access]) }
  222. it { is_expected.to forbid_actions(%i[agent_update_access agent_create_access]) }
  223. end
  224. context 'when user is agent-customer with agent change access to ticket' do
  225. let(:user) { create(:agent_and_customer) }
  226. before do
  227. user.user_groups.create! group: record.group, access: 'change'
  228. end
  229. it { is_expected.to forbid_actions(%i[agent_read_access agent_create_access]) }
  230. it { is_expected.to permit_actions(%i[agent_update_access]) }
  231. end
  232. context 'when user is agent-customer with full agent access to ticket' do
  233. let(:user) { create(:agent_and_customer, groups: [record.group]) }
  234. it { is_expected.to permit_actions(%i[agent_read_access agent_update_access agent_create_access]) }
  235. end
  236. end
  237. describe '#create_mentions?' do
  238. let(:user) { create(:agent) }
  239. it 'delegates to #agent_read_access?' do
  240. allow(policy).to receive(:agent_read_access?)
  241. policy.create_mentions?
  242. expect(policy).to have_received(:agent_read_access?)
  243. end
  244. end
  245. describe 'fields restriction' do
  246. context 'when user is agent' do
  247. let(:user) { create(:agent, groups: [record.group]) }
  248. it 'does not forbid time unit fields' do
  249. expect(policy.show?).to permit_fields(%i[time_unit time_units_per_type])
  250. end
  251. end
  252. context 'when user is customer' do
  253. let(:user) { create(:customer) }
  254. before { record.update!(customer: user) }
  255. it 'forbids time unit fields' do
  256. expect(policy.show?)
  257. .to be_truthy
  258. .and(forbid_fields(%i[time_unit time_units_per_type]))
  259. end
  260. it 'permits other fields' do
  261. expect(policy.show?).to permit_fields(%i[id subject])
  262. end
  263. end
  264. context 'when user is customer via shared organization' do
  265. let(:organization) { create(:organization) }
  266. let(:user) { create(:customer, organization:) }
  267. let(:customer) { create(:customer, organization:) }
  268. let(:record) { create(:ticket, customer:) }
  269. it 'forbids time unit fields' do
  270. expect(policy.show?)
  271. .to be_truthy
  272. .and(forbid_fields(%i[time_unit time_units_per_type]))
  273. end
  274. it 'permits other fields' do
  275. expect(policy.show?).to permit_fields(%i[id subject])
  276. end
  277. end
  278. end
  279. end