ticket_test.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class TicketTest < ActiveSupport::TestCase
  4. test 'ticket create' do
  5. ticket = Ticket.create(
  6. title: "some title\n äöüß",
  7. group: Group.lookup(name: 'Users'),
  8. customer_id: 2,
  9. state: Ticket::State.lookup(name: 'new'),
  10. priority: Ticket::Priority.lookup(name: '2 normal'),
  11. updated_by_id: 1,
  12. created_by_id: 1,
  13. )
  14. assert(ticket, 'ticket created')
  15. assert_equal(ticket.title, 'some title äöüß', 'ticket.title verify')
  16. assert_equal(ticket.group.name, 'Users', 'ticket.group verify')
  17. assert_equal(ticket.state.name, 'new', 'ticket.state verify')
  18. # create inbound article #1
  19. article_inbound1 = Ticket::Article.create(
  20. ticket_id: ticket.id,
  21. from: 'some_sender@example.com',
  22. to: 'some_recipient@example.com',
  23. subject: 'some subject',
  24. message_id: 'some@id',
  25. body: 'some message article_inbound1 😍😍😍',
  26. internal: false,
  27. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  28. type: Ticket::Article::Type.find_by(name: 'email'),
  29. updated_by_id: 1,
  30. created_by_id: 1,
  31. )
  32. assert_equal(article_inbound1.body, 'some message article_inbound1 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  33. ticket = Ticket.find(ticket.id)
  34. assert_equal(ticket.article_count, 1, 'ticket.article_count verify - inbound')
  35. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - inbound')
  36. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  37. assert_equal(ticket.last_contact_agent_at, nil, 'ticket.last_contact_agent_at verify - inbound')
  38. assert_equal(ticket.first_response_at, nil, 'ticket.first_response_at verify - inbound')
  39. assert_equal(ticket.close_at, nil, 'ticket.close_at verify - inbound')
  40. # create inbound article #2
  41. travel 2.seconds
  42. article_inbound2 = Ticket::Article.create(
  43. ticket_id: ticket.id,
  44. from: 'some_sender@example.com',
  45. to: 'some_recipient@example.com',
  46. subject: 'some subject',
  47. message_id: 'some@id',
  48. body: 'some message article_inbound2 😍😍😍',
  49. internal: false,
  50. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  51. type: Ticket::Article::Type.find_by(name: 'email'),
  52. updated_by_id: 1,
  53. created_by_id: 1,
  54. )
  55. assert_equal(article_inbound2.body, 'some message article_inbound2 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  56. ticket = Ticket.find(ticket.id)
  57. assert_equal(ticket.article_count, 2, 'ticket.article_count verify - inbound')
  58. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - inbound')
  59. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  60. assert_equal(ticket.last_contact_agent_at, nil, 'ticket.last_contact_agent_at verify - inbound')
  61. assert_equal(ticket.first_response_at, nil, 'ticket.first_response_at verify - inbound')
  62. assert_equal(ticket.close_at, nil, 'ticket.close_at verify - inbound')
  63. # create note article
  64. article_note = Ticket::Article.create(
  65. ticket_id: ticket.id,
  66. from: 'some person',
  67. subject: "some\nnote",
  68. body: "some\n message",
  69. internal: true,
  70. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  71. type: Ticket::Article::Type.find_by(name: 'note'),
  72. updated_by_id: 1,
  73. created_by_id: 1,
  74. )
  75. assert_equal(article_note.subject, 'some note', 'article_note.subject verify - inbound')
  76. assert_equal(article_note.body, "some\n message", 'article_note.body verify - inbound')
  77. ticket = Ticket.find(ticket.id)
  78. assert_equal(ticket.article_count, 3, 'ticket.article_count verify - note')
  79. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - note')
  80. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - note')
  81. assert_equal(ticket.last_contact_agent_at, nil, 'ticket.last_contact_agent_at verify - note')
  82. assert_equal(ticket.first_response_at, nil, 'ticket.first_response_at verify - note')
  83. assert_equal(ticket.close_at, nil, 'ticket.close_at verify - note')
  84. # create outbound article
  85. travel 2.seconds
  86. article_outbound = Ticket::Article.create(
  87. ticket_id: ticket.id,
  88. from: 'some_recipient@example.com',
  89. to: 'some_sender@example.com',
  90. subject: 'some subject',
  91. message_id: 'some@id2',
  92. body: 'some message 2',
  93. internal: false,
  94. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  95. type: Ticket::Article::Type.find_by(name: 'email'),
  96. updated_by_id: 1,
  97. created_by_id: 1,
  98. )
  99. ticket = Ticket.find(ticket.id)
  100. assert_equal(ticket.article_count, 4, 'ticket.article_count verify - outbound')
  101. assert_equal(ticket.last_contact_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact verify - outbound')
  102. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - outbound')
  103. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  104. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  105. assert_equal(ticket.close_at, nil, 'ticket.close_at verify - outbound')
  106. # create inbound article #3
  107. article_inbound3 = Ticket::Article.create(
  108. ticket_id: ticket.id,
  109. from: 'some_sender@example.com',
  110. to: 'some_recipient@example.com',
  111. subject: 'some subject',
  112. message_id: 'some@id',
  113. body: 'some message article_inbound3 😍😍😍',
  114. internal: false,
  115. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  116. type: Ticket::Article::Type.find_by(name: 'email'),
  117. updated_by_id: 1,
  118. created_by_id: 1,
  119. )
  120. assert_equal(article_inbound3.body, 'some message article_inbound3 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  121. ticket = Ticket.find(ticket.id)
  122. assert_equal(ticket.article_count, 5, 'ticket.article_count verify - inbound')
  123. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - inbound')
  124. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  125. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  126. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  127. assert_equal(ticket.close_at, nil, 'ticket.close_at verify - outbound')
  128. # create inbound article #4
  129. travel 2.seconds
  130. article_inbound4 = Ticket::Article.create(
  131. ticket_id: ticket.id,
  132. from: 'some_sender@example.com',
  133. to: 'some_recipient@example.com',
  134. subject: 'some subject',
  135. message_id: 'some@id',
  136. body: 'some message article_inbound4 😍😍😍',
  137. internal: false,
  138. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  139. type: Ticket::Article::Type.find_by(name: 'email'),
  140. updated_by_id: 1,
  141. created_by_id: 1,
  142. )
  143. assert_equal(article_inbound4.body, 'some message article_inbound4 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  144. ticket = Ticket.find(ticket.id)
  145. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - inbound')
  146. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - inbound')
  147. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  148. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  149. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  150. assert_equal(ticket.close_at, nil, 'ticket.close_at verify - outbound')
  151. ticket.state_id = Ticket::State.where(name: 'closed').first.id
  152. ticket.save
  153. ticket = Ticket.find(ticket.id)
  154. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - state update')
  155. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - state update')
  156. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - state update')
  157. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - state update')
  158. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - state update')
  159. assert(ticket.close_at, 'ticket.close_at verify - state update')
  160. # set pending time
  161. ticket.state_id = Ticket::State.find_by(name: 'pending reminder').id
  162. ticket.pending_time = Time.zone.parse('1977-10-27 22:00:00 +0000')
  163. ticket.save
  164. ticket = Ticket.find(ticket.id)
  165. assert_equal(ticket.state.name, 'pending reminder', 'state verify')
  166. assert_equal(ticket.pending_time, Time.zone.parse('1977-10-27 22:00:00 +0000'), 'pending_time verify')
  167. # reset pending state, should also reset pending time
  168. ticket.state_id = Ticket::State.find_by(name: 'closed').id
  169. ticket.save
  170. ticket = Ticket.find(ticket.id)
  171. assert_equal(ticket.state.name, 'closed', 'state verify')
  172. assert_equal(ticket.pending_time, nil)
  173. # delete article
  174. article_note = Ticket::Article.create(
  175. ticket_id: ticket.id,
  176. from: 'some person',
  177. subject: 'some note',
  178. body: 'some message',
  179. internal: true,
  180. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  181. type: Ticket::Article::Type.find_by(name: 'note'),
  182. updated_by_id: 1,
  183. created_by_id: 1,
  184. )
  185. ticket = Ticket.find(ticket.id)
  186. assert_equal(ticket.article_count, 7, 'ticket.article_count verify - note')
  187. article_note.destroy
  188. ticket = Ticket.find(ticket.id)
  189. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - note')
  190. delete = ticket.destroy
  191. assert(delete, 'ticket destroy')
  192. travel_back
  193. end
  194. test 'ticket latest change' do
  195. ticket1 = Ticket.create(
  196. title: 'latest change 1',
  197. group: Group.lookup(name: 'Users'),
  198. customer_id: 2,
  199. state: Ticket::State.lookup(name: 'new'),
  200. priority: Ticket::Priority.lookup(name: '2 normal'),
  201. updated_by_id: 1,
  202. created_by_id: 1,
  203. )
  204. assert_equal(Ticket.latest_change.to_s, ticket1.updated_at.to_s)
  205. travel 1.minute
  206. ticket2 = Ticket.create(
  207. title: 'latest change 2',
  208. group: Group.lookup(name: 'Users'),
  209. customer_id: 2,
  210. state: Ticket::State.lookup(name: 'new'),
  211. priority: Ticket::Priority.lookup(name: '2 normal'),
  212. updated_by_id: 1,
  213. created_by_id: 1,
  214. )
  215. assert_equal(Ticket.latest_change.to_s, ticket2.updated_at.to_s)
  216. travel 1.minute
  217. ticket1.title = 'latest change 1 - 1'
  218. ticket1.save
  219. assert_equal(Ticket.latest_change.to_s, ticket1.updated_at.to_s)
  220. travel 1.minute
  221. ticket1.touch
  222. assert_equal(Ticket.latest_change.to_s, ticket1.updated_at.to_s)
  223. ticket1.destroy
  224. assert_equal(Ticket.latest_change.to_s, ticket2.updated_at.to_s)
  225. ticket2.destroy
  226. travel_back
  227. end
  228. test 'ticket process_pending' do
  229. # close all other pending close tickets first
  230. Ticket.where('pending_time IS NOT NULL').each { |ticket|
  231. ticket.state = Ticket::State.lookup(name: 'closed')
  232. ticket.save!
  233. }
  234. ticket = Ticket.create(
  235. title: 'pending close test',
  236. group: Group.lookup(name: 'Users'),
  237. customer_id: 2,
  238. state: Ticket::State.lookup(name: 'pending close'),
  239. pending_time: Time.zone.now - 60,
  240. priority: Ticket::Priority.lookup(name: '2 normal'),
  241. updated_by_id: 1,
  242. created_by_id: 1,
  243. )
  244. lookup_ticket = Ticket.find_by('pending_time <= ?', Time.zone.now)
  245. assert_equal(lookup_ticket.id, ticket.id, 'ticket.pending_time verify')
  246. Ticket.process_pending
  247. lookup_ticket = Ticket.find_by('pending_time <= ?', Time.zone.now)
  248. assert_nil(lookup_ticket, 'ticket.pending_time processed verify')
  249. end
  250. test 'ticket subject' do
  251. ticket1 = Ticket.create(
  252. title: 'subject test 1',
  253. group: Group.lookup(name: 'Users'),
  254. customer_id: 2,
  255. state: Ticket::State.lookup(name: 'new'),
  256. priority: Ticket::Priority.lookup(name: '2 normal'),
  257. updated_by_id: 1,
  258. created_by_id: 1,
  259. )
  260. assert_equal('subject test 1', ticket1.title)
  261. assert_equal("ABC subject test 1 [Ticket##{ticket1.number}]", ticket1.subject_build('ABC subject test 1'))
  262. assert_equal("RE: ABC subject test 1 [Ticket##{ticket1.number}]", ticket1.subject_build('ABC subject test 1', true))
  263. assert_equal("RE: ABC subject test 1 [Ticket##{ticket1.number}]", ticket1.subject_build(' ABC subject test 1', true))
  264. assert_equal("RE: ABC subject test 1 [Ticket##{ticket1.number}]", ticket1.subject_build('ABC subject test 1 ', true))
  265. ticket1.destroy
  266. end
  267. test 'article attachment helper' do
  268. ticket1 = Ticket.create(
  269. title: 'some article helper test1',
  270. group: Group.lookup(name: 'Users'),
  271. customer_id: 2,
  272. state: Ticket::State.lookup(name: 'new'),
  273. priority: Ticket::Priority.lookup(name: '2 normal'),
  274. updated_by_id: 1,
  275. created_by_id: 1,
  276. )
  277. assert(ticket1, 'ticket created')
  278. # create inbound article #1
  279. article1 = Ticket::Article.create(
  280. ticket_id: ticket1.id,
  281. from: 'some_sender@example.com',
  282. to: 'some_recipient@example.com',
  283. subject: 'some subject',
  284. message_id: 'some@id',
  285. content_type: 'text/html',
  286. body: 'some message article helper test1 <div><img style="width: 85.5px; height: 49.5px" src="cid:15.274327094.140938@zammad.example.com">asdasd<img src="cid:15.274327094.140939@zammad.example.com"><br>',
  287. internal: false,
  288. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  289. type: Ticket::Article::Type.find_by(name: 'email'),
  290. updated_by_id: 1,
  291. created_by_id: 1,
  292. )
  293. store1 = Store.add(
  294. object: 'Ticket::Article',
  295. o_id: article1.id,
  296. data: 'content_file1_normally_should_be_an_image',
  297. filename: 'some_file1.jpg',
  298. preferences: {
  299. 'Content-Type' => 'image/jpeg',
  300. 'Mime-Type' => 'image/jpeg',
  301. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  302. 'Content-Disposition' => 'inline'
  303. },
  304. created_by_id: 1,
  305. )
  306. store2 = Store.add(
  307. object: 'Ticket::Article',
  308. o_id: article1.id,
  309. data: 'content_file2_normally_should_be_an_image',
  310. filename: 'some_file2.jpg',
  311. preferences: {
  312. 'Content-Type' => 'image/jpeg',
  313. 'Mime-Type' => 'image/jpeg',
  314. 'Content-ID' => '15.274327094.140939@zammad.example.com',
  315. 'Content-Disposition' => 'inline'
  316. },
  317. created_by_id: 1,
  318. )
  319. store3 = Store.add(
  320. object: 'Ticket::Article',
  321. o_id: article1.id,
  322. data: 'content_file3',
  323. filename: 'some_file3.txt',
  324. preferences: {
  325. 'Content-Type' => 'text/stream',
  326. 'Mime-Type' => 'text/stream',
  327. 'Content-ID' => '15.274327094.99999@zammad.example.com',
  328. 'Content-Disposition' => 'inline'
  329. },
  330. created_by_id: 1,
  331. )
  332. article_attributes = Ticket::Article.insert_urls(
  333. article1.attributes,
  334. article1.attachments,
  335. )
  336. assert_no_match('15.274327094.140938@zammad.example.com', article_attributes['body'])
  337. assert_no_match('15.274327094.140939@zammad.example.com', article_attributes['body'])
  338. assert_no_match('15.274327094.99999@zammad.example.com', article_attributes['body'])
  339. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store1.id}", article_attributes['body'])
  340. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store2.id}", article_attributes['body'])
  341. assert_no_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store3.id}", article_attributes['body'])
  342. article1 = Ticket::Article.find(article1.id)
  343. attachments = article1.attachments_inline
  344. assert_equal(2, attachments.length)
  345. assert_equal(store1.id, attachments.first.id)
  346. ticket1.destroy
  347. end
  348. end