ticket_last_owner_update_test.rb 10 KB

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