ticket_last_owner_update_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class TicketLastOwnerUpdateTest < ActiveSupport::TestCase
  4. setup do
  5. Group.create_or_update(
  6. name: 'LastOwnerUpdate',
  7. email_address: EmailAddress.first,
  8. assignment_timeout: 60,
  9. updated_by_id: 1,
  10. created_by_id: 1,
  11. )
  12. roles = Role.where(name: 'Agent')
  13. @agent1 = User.create_or_update(
  14. login: 'ticket-assignment_timeout-agent1@example.com',
  15. firstname: 'Overview',
  16. lastname: 'Agent1',
  17. email: 'ticket-assignment_timeout-agent1@example.com',
  18. password: 'agentpw',
  19. active: true,
  20. roles: roles,
  21. groups: Group.all,
  22. updated_by_id: 1,
  23. created_by_id: 1,
  24. )
  25. end
  26. test 'last_owner_update_at check by state' do
  27. ticket = Ticket.create!(
  28. title: 'assignment_timeout test by state 1',
  29. group: Group.lookup(name: 'LastOwnerUpdate'),
  30. owner: @agent1,
  31. customer_id: 2,
  32. state: Ticket::State.lookup(name: 'new'),
  33. updated_by_id: 1,
  34. created_by_id: 1,
  35. )
  36. assert_in_delta(ticket.last_owner_update_at.to_i, ticket.updated_at.to_i, 1)
  37. ticket.state = Ticket::State.lookup(name: 'closed')
  38. ticket.save!
  39. assert_nil(ticket.last_owner_update_at)
  40. ticket = Ticket.create!(
  41. title: 'assignment_timeout test by state 1',
  42. group: Group.lookup(name: 'LastOwnerUpdate'),
  43. owner: @agent1,
  44. customer_id: 2,
  45. state: Ticket::State.lookup(name: 'pending reminder'),
  46. updated_by_id: 1,
  47. created_by_id: 1,
  48. )
  49. assert_nil(ticket.last_owner_update_at)
  50. ticket.state = Ticket::State.lookup(name: 'open')
  51. ticket.save!
  52. assert_in_delta(ticket.updated_at.to_i, ticket.last_owner_update_at.to_i, 1)
  53. end
  54. test 'last_owner_update_at check with agent reply' do
  55. ticket = Ticket.create!(
  56. title: 'assignment_timeout test by state 1',
  57. group: Group.lookup(name: 'LastOwnerUpdate'),
  58. owner: @agent1,
  59. customer_id: 2,
  60. state: Ticket::State.lookup(name: 'open'),
  61. updated_by_id: 1,
  62. created_by_id: 1,
  63. )
  64. assert_in_delta(ticket.updated_at.to_i, ticket.last_owner_update_at.to_i, 1)
  65. travel 1.hour
  66. Ticket::Article.create(
  67. ticket_id: ticket.id,
  68. from: 'some_sender@example.com',
  69. to: 'some_recipient@example.com',
  70. subject: 'some subject',
  71. message_id: 'some@id',
  72. body: 'some message reply by customer email',
  73. internal: false,
  74. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  75. type: Ticket::Article::Type.find_by(name: 'email'),
  76. updated_by_id: 2,
  77. created_by_id: 2,
  78. )
  79. ticket_last_owner_update_at = ticket.last_owner_update_at
  80. ticket.reload
  81. assert_in_delta(ticket.last_owner_update_at.to_i, ticket_last_owner_update_at.to_i, 1)
  82. travel 1.hour
  83. Ticket::Article.create(
  84. ticket_id: ticket.id,
  85. from: 'some_sender@example.com',
  86. to: 'some_recipient@example.com',
  87. subject: 'some subject',
  88. message_id: 'some@id',
  89. body: 'some message reply by agent email',
  90. internal: false,
  91. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  92. type: Ticket::Article::Type.find_by(name: 'email'),
  93. updated_by_id: @agent1.id,
  94. created_by_id: @agent1.id,
  95. )
  96. ticket_last_owner_update_at = Time.zone.now
  97. ticket.reload
  98. assert_in_delta(ticket.last_owner_update_at.to_i, ticket_last_owner_update_at.to_i, 1)
  99. end
  100. test 'last_owner_update_at check' do
  101. ticket = Ticket.create!(
  102. title: 'assignment_timeout test 1',
  103. group: Group.lookup(name: 'LastOwnerUpdate'),
  104. customer_id: 2,
  105. state: Ticket::State.lookup(name: 'new'),
  106. updated_by_id: 1,
  107. created_by_id: 1,
  108. )
  109. assert_nil(ticket.last_owner_update_at)
  110. travel 1.hour
  111. ticket.owner = @agent1
  112. ticket.save!
  113. assert_in_delta(ticket.last_owner_update_at.to_i, ticket.updated_at.to_i, 1)
  114. ticket = Ticket.create!(
  115. title: 'assignment_timeout test 1',
  116. group: Group.lookup(name: 'LastOwnerUpdate'),
  117. customer_id: 2,
  118. state: Ticket::State.lookup(name: 'closed'),
  119. updated_by_id: 1,
  120. created_by_id: 1,
  121. )
  122. assert_nil(ticket.last_owner_update_at)
  123. travel 1.hour
  124. ticket.owner = @agent1
  125. ticket.save!
  126. assert_in_delta(ticket.last_owner_update_at.to_i, ticket.updated_at.to_i, 1)
  127. ticket = Ticket.create!(
  128. title: 'assignment_timeout test 1',
  129. group: Group.lookup(name: 'LastOwnerUpdate'),
  130. owner: @agent1,
  131. customer_id: 2,
  132. state: Ticket::State.lookup(name: 'new'),
  133. updated_by_id: 1,
  134. created_by_id: 1,
  135. )
  136. assert_in_delta(ticket.last_owner_update_at.to_i, ticket.updated_at.to_i, 1)
  137. ticket.owner_id = 1
  138. ticket.save!
  139. assert_nil(ticket.last_owner_update_at)
  140. ticket = Ticket.create!(
  141. title: 'assignment_timeout test 1',
  142. group: Group.lookup(name: 'LastOwnerUpdate'),
  143. owner: @agent1,
  144. customer_id: 2,
  145. state: Ticket::State.lookup(name: 'open'),
  146. updated_by_id: 1,
  147. created_by_id: 1,
  148. )
  149. assert_in_delta(ticket.last_owner_update_at.to_i, ticket.updated_at.to_i, 1)
  150. ticket.owner_id = 1
  151. ticket.save!
  152. assert_nil(ticket.last_owner_update_at)
  153. ticket = Ticket.create!(
  154. title: 'assignment_timeout test 2',
  155. group: Group.lookup(name: 'Users'),
  156. customer_id: 2,
  157. state: Ticket::State.lookup(name: 'new'),
  158. updated_by_id: 1,
  159. created_by_id: 1,
  160. )
  161. assert_nil(ticket.last_owner_update_at)
  162. travel 1.hour
  163. ticket.owner = @agent1
  164. ticket.save!
  165. assert_in_delta(ticket.last_owner_update_at.to_i, ticket.updated_at.to_i, 1)
  166. ticket = Ticket.create!(
  167. title: 'assignment_timeout test 2',
  168. group: Group.lookup(name: 'Users'),
  169. customer_id: 2,
  170. state: Ticket::State.lookup(name: 'closed'),
  171. updated_by_id: 1,
  172. created_by_id: 1,
  173. )
  174. assert_nil(ticket.last_owner_update_at)
  175. travel 1.hour
  176. ticket.owner = @agent1
  177. ticket.save!
  178. assert_in_delta(ticket.last_owner_update_at.to_i, ticket.updated_at.to_i, 1)
  179. ticket = Ticket.create!(
  180. title: 'assignment_timeout test 2',
  181. group: Group.lookup(name: 'LastOwnerUpdate'),
  182. owner: @agent1,
  183. customer_id: 2,
  184. state: Ticket::State.lookup(name: 'new'),
  185. updated_by_id: 1,
  186. created_by_id: 1,
  187. )
  188. assert_in_delta(ticket.last_owner_update_at.to_i, ticket.updated_at.to_i, 1)
  189. ticket.owner_id = 1
  190. ticket.save!
  191. assert_nil(ticket.last_owner_update_at)
  192. ticket = Ticket.create!(
  193. title: 'assignment_timeout test 2',
  194. group: Group.lookup(name: 'Users'),
  195. owner: @agent1,
  196. customer_id: 2,
  197. state: Ticket::State.lookup(name: 'closed'),
  198. updated_by_id: 1,
  199. created_by_id: 1,
  200. )
  201. assert_nil(ticket.last_owner_update_at)
  202. ticket.owner_id = 1
  203. ticket.save!
  204. assert_nil(ticket.last_owner_update_at)
  205. end
  206. test 'last_owner_update_at assignment_timeout check' do
  207. ticket1 = Ticket.create!(
  208. title: 'assignment_timeout test 1',
  209. group: Group.lookup(name: 'LastOwnerUpdate'),
  210. customer_id: 2,
  211. state: Ticket::State.lookup(name: 'new'),
  212. updated_by_id: 1,
  213. created_by_id: 1,
  214. )
  215. assert_nil(ticket1.last_owner_update_at)
  216. ticket2 = Ticket.create!(
  217. title: 'assignment_timeout test 2',
  218. group: Group.lookup(name: 'LastOwnerUpdate'),
  219. owner: @agent1,
  220. customer_id: 2,
  221. state: Ticket::State.lookup(name: 'new'),
  222. updated_by_id: 1,
  223. created_by_id: 1,
  224. )
  225. assert_in_delta(ticket2.last_owner_update_at.to_i, ticket2.updated_at.to_i, 1)
  226. ticket3 = Ticket.create!(
  227. title: 'assignment_timeout test 3',
  228. group: Group.lookup(name: 'LastOwnerUpdate'),
  229. owner: @agent1,
  230. customer_id: 2,
  231. state: Ticket::State.lookup(name: 'open'),
  232. updated_by_id: 1,
  233. created_by_id: 1,
  234. )
  235. assert_in_delta(ticket3.last_owner_update_at.to_i, ticket3.updated_at.to_i, 1)
  236. ticket4 = Ticket.create!(
  237. title: 'assignment_timeout test 4',
  238. group: Group.lookup(name: 'Users'),
  239. customer_id: 2,
  240. state: Ticket::State.lookup(name: 'new'),
  241. updated_by_id: 1,
  242. created_by_id: 1,
  243. )
  244. assert_nil(ticket4.last_owner_update_at)
  245. ticket5 = Ticket.create!(
  246. title: 'assignment_timeout test 5',
  247. group: Group.lookup(name: 'LastOwnerUpdate'),
  248. owner: @agent1,
  249. customer_id: 2,
  250. state: Ticket::State.lookup(name: 'new'),
  251. updated_by_id: 1,
  252. created_by_id: 1,
  253. )
  254. assert_in_delta(ticket5.last_owner_update_at.to_i, ticket5.updated_at.to_i, 1)
  255. travel 55.minutes
  256. Ticket.process_auto_unassign
  257. ticket1after = Ticket.find(ticket1.id)
  258. assert_nil(ticket1.last_owner_update_at)
  259. assert_equal(ticket1.updated_at.to_s, ticket1after.updated_at.to_s)
  260. ticket2after = Ticket.find(ticket2.id)
  261. assert_in_delta(ticket2.last_owner_update_at.to_i, ticket2after.last_owner_update_at.to_i, 1)
  262. assert_equal(ticket2.updated_at.to_s, ticket2after.updated_at.to_s)
  263. ticket3after = Ticket.find(ticket3.id)
  264. assert_in_delta(ticket3.last_owner_update_at.to_i, ticket3after.last_owner_update_at.to_i, 1)
  265. assert_equal(ticket3.updated_at.to_s, ticket3after.updated_at.to_s)
  266. ticket4after = Ticket.find(ticket4.id)
  267. assert_nil(ticket4.last_owner_update_at)
  268. assert_equal(ticket4.updated_at.to_s, ticket4after.updated_at.to_s)
  269. ticket5after = Ticket.find(ticket5.id)
  270. assert_equal(ticket5after.owner_id, @agent1.id)
  271. assert_equal(ticket5.updated_at.to_s, ticket5after.updated_at.to_s)
  272. travel 15.minutes
  273. Ticket.process_auto_unassign
  274. ticket_updated_at = Time.current
  275. ticket1after = Ticket.find(ticket1.id)
  276. assert_nil(ticket1.last_owner_update_at)
  277. assert_equal(ticket1.updated_at.to_s, ticket1after.updated_at.to_s)
  278. ticket2after = Ticket.find(ticket2.id)
  279. assert_nil(ticket2after.last_owner_update_at)
  280. assert_equal(ticket2after.owner_id, 1)
  281. assert_equal(ticket_updated_at.to_s, ticket2after.updated_at.to_s)
  282. ticket3after = Ticket.find(ticket3.id)
  283. assert_nil(ticket3after.last_owner_update_at)
  284. assert_equal(ticket3after.owner_id, 1)
  285. assert_equal(ticket_updated_at.to_s, ticket3after.updated_at.to_s)
  286. ticket4after = Ticket.find(ticket4.id)
  287. assert_nil(ticket4.last_owner_update_at)
  288. assert_equal(ticket4.updated_at.to_s, ticket4after.updated_at.to_s)
  289. ticket5after = Ticket.find(ticket5.id)
  290. assert_equal(ticket5after.owner_id, 1)
  291. assert_equal(ticket_updated_at.to_s, ticket5after.updated_at.to_s)
  292. end
  293. end