ticket_test.rb 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class TicketTest < ActiveSupport::TestCase
  4. setup do
  5. Ticket.destroy_all
  6. end
  7. test 'ticket create' do
  8. ticket = Ticket.create!(
  9. title: "some title\n äöüß",
  10. group: Group.lookup(name: 'Users'),
  11. customer_id: 2,
  12. state: Ticket::State.lookup(name: 'new'),
  13. priority: Ticket::Priority.lookup(name: '2 normal'),
  14. updated_by_id: 1,
  15. created_by_id: 1,
  16. )
  17. assert(ticket, 'ticket created')
  18. assert_equal(ticket.title, 'some title äöüß', 'ticket.title verify')
  19. assert_equal(ticket.group.name, 'Users', 'ticket.group verify')
  20. assert_equal(ticket.state.name, 'new', 'ticket.state verify')
  21. # create inbound article #1
  22. article_inbound1 = Ticket::Article.create!(
  23. ticket_id: ticket.id,
  24. from: 'some_sender@example.com',
  25. to: 'some_recipient@example.com',
  26. subject: 'some subject',
  27. message_id: 'some@id',
  28. body: 'some message article_inbound1 😍😍😍',
  29. internal: false,
  30. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  31. type: Ticket::Article::Type.find_by(name: 'email'),
  32. updated_by_id: 1,
  33. created_by_id: 1,
  34. )
  35. assert_equal(article_inbound1.body, 'some message article_inbound1 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  36. ticket = Ticket.find(ticket.id)
  37. assert_equal(ticket.article_count, 1, 'ticket.article_count verify - inbound')
  38. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - inbound')
  39. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  40. assert_nil(ticket.last_contact_agent_at, 'ticket.last_contact_agent_at verify - inbound')
  41. assert_nil(ticket.first_response_at, 'ticket.first_response_at verify - inbound')
  42. assert_nil(ticket.close_at, 'ticket.close_at verify - inbound')
  43. # create inbound article #2
  44. travel 2.seconds
  45. article_inbound2 = Ticket::Article.create!(
  46. ticket_id: ticket.id,
  47. from: 'some_sender@example.com',
  48. to: 'some_recipient@example.com',
  49. subject: 'some subject',
  50. message_id: 'some@id',
  51. body: 'some message article_inbound2 😍😍😍',
  52. internal: false,
  53. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  54. type: Ticket::Article::Type.find_by(name: 'email'),
  55. updated_by_id: 1,
  56. created_by_id: 1,
  57. )
  58. assert_equal(article_inbound2.body, 'some message article_inbound2 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  59. ticket = Ticket.find(ticket.id)
  60. assert_equal(ticket.article_count, 2, 'ticket.article_count verify - inbound')
  61. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - inbound')
  62. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  63. assert_nil(ticket.last_contact_agent_at, 'ticket.last_contact_agent_at verify - inbound')
  64. assert_nil(ticket.first_response_at, 'ticket.first_response_at verify - inbound')
  65. assert_nil(ticket.close_at, 'ticket.close_at verify - inbound')
  66. # create note article
  67. article_note = Ticket::Article.create!(
  68. ticket_id: ticket.id,
  69. from: 'some person',
  70. subject: "some\nnote",
  71. body: "some\n message",
  72. internal: true,
  73. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  74. type: Ticket::Article::Type.find_by(name: 'note'),
  75. updated_by_id: 1,
  76. created_by_id: 1,
  77. )
  78. assert_equal(article_note.subject, 'some note', 'article_note.subject verify - inbound')
  79. assert_equal(article_note.body, "some\n message", 'article_note.body verify - inbound')
  80. ticket = Ticket.find(ticket.id)
  81. assert_equal(ticket.article_count, 3, 'ticket.article_count verify - note')
  82. assert_equal(ticket.last_contact_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact verify - note')
  83. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - note')
  84. assert_nil(ticket.last_contact_agent_at, 'ticket.last_contact_agent_at verify - note')
  85. assert_nil(ticket.first_response_at, 'ticket.first_response_at verify - note')
  86. assert_nil(ticket.close_at, 'ticket.close_at verify - note')
  87. # create outbound article
  88. travel 2.seconds
  89. article_outbound = Ticket::Article.create!(
  90. ticket_id: ticket.id,
  91. from: 'some_recipient@example.com',
  92. to: 'some_sender@example.com',
  93. subject: 'some subject',
  94. message_id: 'some@id2',
  95. body: 'some message 2',
  96. internal: false,
  97. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  98. type: Ticket::Article::Type.find_by(name: 'email'),
  99. updated_by_id: 1,
  100. created_by_id: 1,
  101. )
  102. ticket = Ticket.find(ticket.id)
  103. assert_equal(ticket.article_count, 4, 'ticket.article_count verify - outbound')
  104. assert_equal(ticket.last_contact_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact verify - outbound')
  105. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound1.created_at.to_s, 'ticket.last_contact_customer_at verify - outbound')
  106. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  107. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  108. assert_nil(ticket.close_at, 'ticket.close_at verify - outbound')
  109. # create inbound article #3
  110. article_inbound3 = Ticket::Article.create!(
  111. ticket_id: ticket.id,
  112. from: 'some_sender@example.com',
  113. to: 'some_recipient@example.com',
  114. subject: 'some subject',
  115. message_id: 'some@id',
  116. body: 'some message article_inbound3 😍😍😍',
  117. internal: false,
  118. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  119. type: Ticket::Article::Type.find_by(name: 'email'),
  120. updated_by_id: 1,
  121. created_by_id: 1,
  122. )
  123. assert_equal(article_inbound3.body, 'some message article_inbound3 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  124. ticket = Ticket.find(ticket.id)
  125. assert_equal(ticket.article_count, 5, 'ticket.article_count verify - inbound')
  126. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - inbound')
  127. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  128. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  129. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  130. assert_nil(ticket.close_at, 'ticket.close_at verify - outbound')
  131. # create inbound article #4
  132. travel 2.seconds
  133. article_inbound4 = Ticket::Article.create!(
  134. ticket_id: ticket.id,
  135. from: 'some_sender@example.com',
  136. to: 'some_recipient@example.com',
  137. subject: 'some subject',
  138. message_id: 'some@id',
  139. body: 'some message article_inbound4 😍😍😍',
  140. internal: false,
  141. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  142. type: Ticket::Article::Type.find_by(name: 'email'),
  143. updated_by_id: 1,
  144. created_by_id: 1,
  145. )
  146. assert_equal(article_inbound4.body, 'some message article_inbound4 😍😍😍'.utf8_to_3bytesutf8, 'article_inbound.body verify - inbound')
  147. ticket = Ticket.find(ticket.id)
  148. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - inbound')
  149. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - inbound')
  150. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - inbound')
  151. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - outbound')
  152. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - outbound')
  153. assert_nil(ticket.close_at, 'ticket.close_at verify - outbound')
  154. ticket.state_id = Ticket::State.where(name: 'closed').first.id
  155. ticket.save
  156. ticket = Ticket.find(ticket.id)
  157. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - state update')
  158. assert_equal(ticket.last_contact_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact verify - state update')
  159. assert_equal(ticket.last_contact_customer_at.to_s, article_inbound3.created_at.to_s, 'ticket.last_contact_customer_at verify - state update')
  160. assert_equal(ticket.last_contact_agent_at.to_s, article_outbound.created_at.to_s, 'ticket.last_contact_agent_at verify - state update')
  161. assert_equal(ticket.first_response_at.to_s, article_outbound.created_at.to_s, 'ticket.first_response_at verify - state update')
  162. assert(ticket.close_at, 'ticket.close_at verify - state update')
  163. # set pending time
  164. ticket.state_id = Ticket::State.find_by(name: 'pending reminder').id
  165. ticket.pending_time = Time.zone.parse('1977-10-27 22:00:00 +0000')
  166. ticket.save
  167. ticket = Ticket.find(ticket.id)
  168. assert_equal(ticket.state.name, 'pending reminder', 'state verify')
  169. assert_equal(ticket.pending_time, Time.zone.parse('1977-10-27 22:00:00 +0000'), 'pending_time verify')
  170. # reset pending state, should also reset pending time
  171. ticket.state_id = Ticket::State.find_by(name: 'closed').id
  172. ticket.save
  173. ticket = Ticket.find(ticket.id)
  174. assert_equal(ticket.state.name, 'closed', 'state verify')
  175. assert_nil(ticket.pending_time)
  176. # delete article
  177. article_note = Ticket::Article.create!(
  178. ticket_id: ticket.id,
  179. from: 'some person',
  180. subject: 'some note',
  181. body: 'some message',
  182. internal: true,
  183. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  184. type: Ticket::Article::Type.find_by(name: 'note'),
  185. updated_by_id: 1,
  186. created_by_id: 1,
  187. )
  188. ticket = Ticket.find(ticket.id)
  189. assert_equal(ticket.article_count, 7, 'ticket.article_count verify - note')
  190. article_note.destroy
  191. ticket = Ticket.find(ticket.id)
  192. assert_equal(ticket.article_count, 6, 'ticket.article_count verify - note')
  193. delete = ticket.destroy
  194. assert(delete, 'ticket destroy')
  195. travel_back
  196. end
  197. test 'ticket process_pending' do
  198. # close all other pending close tickets first
  199. Ticket.where.not(pending_time: nil).each do |ticket|
  200. ticket.state = Ticket::State.lookup(name: 'closed')
  201. ticket.save!
  202. end
  203. ticket = Ticket.create!(
  204. title: 'pending close test',
  205. group: Group.lookup(name: 'Users'),
  206. customer_id: 2,
  207. state: Ticket::State.lookup(name: 'pending close'),
  208. pending_time: Time.zone.now - 60,
  209. priority: Ticket::Priority.lookup(name: '2 normal'),
  210. updated_by_id: 1,
  211. created_by_id: 1,
  212. )
  213. lookup_ticket = Ticket.find_by('pending_time <= ?', Time.zone.now)
  214. assert_equal(lookup_ticket.id, ticket.id, 'ticket.pending_time verify')
  215. Ticket.process_pending
  216. lookup_ticket = Ticket.find_by('pending_time <= ?', Time.zone.now)
  217. assert_nil(lookup_ticket, 'ticket.pending_time processed verify')
  218. end
  219. test 'ticket subject' do
  220. ticket = Ticket.create!(
  221. title: 'subject test 1',
  222. group: Group.lookup(name: 'Users'),
  223. customer_id: 2,
  224. state: Ticket::State.lookup(name: 'new'),
  225. priority: Ticket::Priority.lookup(name: '2 normal'),
  226. updated_by_id: 1,
  227. created_by_id: 1,
  228. )
  229. assert_equal('subject test 1', ticket.title)
  230. assert_equal("ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build('ABC subject test 1'))
  231. assert_equal("RE: ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build('ABC subject test 1', 'reply'))
  232. assert_equal("RE: ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build(' ABC subject test 1', 'reply'))
  233. assert_equal("RE: ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build('ABC subject test 1 ', 'reply'))
  234. assert_equal("FWD: ABC subject test 1 [Ticket##{ticket.number}]", ticket.subject_build('ABC subject test 1 ', 'forward'))
  235. ticket.destroy
  236. Setting.set('ticket_hook_position', 'left')
  237. ticket = Ticket.create!(
  238. title: 'subject test 1',
  239. group: Group.lookup(name: 'Users'),
  240. customer_id: 2,
  241. state: Ticket::State.lookup(name: 'new'),
  242. priority: Ticket::Priority.lookup(name: '2 normal'),
  243. updated_by_id: 1,
  244. created_by_id: 1,
  245. )
  246. assert_equal('subject test 1', ticket.title)
  247. assert_equal("[Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build('ABC subject test 1'))
  248. assert_equal("RE: [Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build('ABC subject test 1', 'reply'))
  249. assert_equal("RE: [Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build(' ABC subject test 1', 'reply'))
  250. assert_equal("RE: [Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build('ABC subject test 1 ', 'reply'))
  251. assert_equal("FWD: [Ticket##{ticket.number}] ABC subject test 1", ticket.subject_build('ABC subject test 1 ', 'forward'))
  252. ticket.destroy
  253. Setting.set('ticket_hook_position', 'none')
  254. ticket = Ticket.create!(
  255. title: 'subject test 1',
  256. group: Group.lookup(name: 'Users'),
  257. customer_id: 2,
  258. state: Ticket::State.lookup(name: 'new'),
  259. priority: Ticket::Priority.lookup(name: '2 normal'),
  260. updated_by_id: 1,
  261. created_by_id: 1,
  262. )
  263. assert_equal('subject test 1', ticket.title)
  264. assert_equal('ABC subject test 1', ticket.subject_build('ABC subject test 1'))
  265. assert_equal('RE: ABC subject test 1', ticket.subject_build('ABC subject test 1', 'reply'))
  266. assert_equal('RE: ABC subject test 1', ticket.subject_build(' ABC subject test 1', 'reply'))
  267. assert_equal('RE: ABC subject test 1', ticket.subject_build('ABC subject test 1 ', 'reply'))
  268. assert_equal('FWD: ABC subject test 1', ticket.subject_build('ABC subject test 1 ', 'forward'))
  269. ticket.destroy
  270. end
  271. test 'ticket followup number check' do
  272. origin_backend = Setting.get('ticket_number')
  273. Setting.set('ticket_number', 'Ticket::Number::Increment')
  274. ticket1 = Ticket.create!(
  275. title: 'subject test 1234-1',
  276. group: Group.lookup(name: 'Users'),
  277. customer_id: 2,
  278. state: Ticket::State.lookup(name: 'new'),
  279. priority: Ticket::Priority.lookup(name: '2 normal'),
  280. updated_by_id: 1,
  281. created_by_id: 1,
  282. )
  283. assert_equal('subject test 1234-1', ticket1.title)
  284. assert_equal("ABC subject test 1 [Ticket##{ticket1.number}]", ticket1.subject_build('ABC subject test 1'))
  285. assert_equal(ticket1.id, Ticket::Number.check("Re: Help [Ticket##{ticket1.number}]").id)
  286. Setting.set('ticket_number', 'Ticket::Number::Date')
  287. ticket1 = Ticket.create!(
  288. title: 'subject test 1234-2',
  289. group: Group.lookup(name: 'Users'),
  290. customer_id: 2,
  291. state: Ticket::State.lookup(name: 'new'),
  292. priority: Ticket::Priority.lookup(name: '2 normal'),
  293. updated_by_id: 1,
  294. created_by_id: 1,
  295. )
  296. assert_equal('subject test 1234-2', ticket1.title)
  297. assert_equal("ABC subject test 1 [Ticket##{ticket1.number}]", ticket1.subject_build('ABC subject test 1'))
  298. assert_equal(ticket1.id, Ticket::Number.check("Re: Help [Ticket##{ticket1.number}]").id)
  299. Setting.set('ticket_number', origin_backend)
  300. end
  301. test 'article attachment helper 1' do
  302. ticket1 = Ticket.create!(
  303. title: 'some article helper test1',
  304. group: Group.lookup(name: 'Users'),
  305. customer_id: 2,
  306. state: Ticket::State.lookup(name: 'new'),
  307. priority: Ticket::Priority.lookup(name: '2 normal'),
  308. updated_by_id: 1,
  309. created_by_id: 1,
  310. )
  311. assert(ticket1, 'ticket created')
  312. # create inbound article #1
  313. article1 = Ticket::Article.create!(
  314. ticket_id: ticket1.id,
  315. from: 'some_sender@example.com',
  316. to: 'some_recipient@example.com',
  317. subject: 'some subject',
  318. message_id: 'some@id',
  319. content_type: 'text/html',
  320. 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>',
  321. internal: false,
  322. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  323. type: Ticket::Article::Type.find_by(name: 'email'),
  324. updated_by_id: 1,
  325. created_by_id: 1,
  326. )
  327. store1 = Store.create!(
  328. object: 'Ticket::Article',
  329. o_id: article1.id,
  330. data: 'content_file1_normally_should_be_an_image',
  331. filename: 'some_file1.jpg',
  332. preferences: {
  333. 'Content-Type' => 'image/jpeg',
  334. 'Mime-Type' => 'image/jpeg',
  335. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  336. 'Content-Disposition' => 'inline'
  337. },
  338. created_by_id: 1,
  339. )
  340. store2 = Store.create!(
  341. object: 'Ticket::Article',
  342. o_id: article1.id,
  343. data: 'content_file2_normally_should_be_an_image',
  344. filename: 'some_file2.jpg',
  345. preferences: {
  346. 'Content-Type' => 'image/jpeg',
  347. 'Mime-Type' => 'image/jpeg',
  348. 'Content-ID' => '15.274327094.140939@zammad.example.com',
  349. 'Content-Disposition' => 'inline'
  350. },
  351. created_by_id: 1,
  352. )
  353. store3 = Store.create!(
  354. object: 'Ticket::Article',
  355. o_id: article1.id,
  356. data: 'content_file3',
  357. filename: 'some_file3.txt',
  358. preferences: {
  359. 'Content-Type' => 'text/stream',
  360. 'Mime-Type' => 'text/stream',
  361. 'Content-ID' => '15.274327094.99999@zammad.example.com',
  362. 'Content-Disposition' => 'inline'
  363. },
  364. created_by_id: 1,
  365. )
  366. article_attributes = Ticket::Article.insert_urls(article1.attributes_with_association_ids)
  367. assert_no_match('15.274327094.140938@zammad.example.com', article_attributes['body'])
  368. assert_no_match('15.274327094.140939@zammad.example.com', article_attributes['body'])
  369. assert_no_match('15.274327094.99999@zammad.example.com', article_attributes['body'])
  370. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store1.id}", article_attributes['body'])
  371. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store2.id}", article_attributes['body'])
  372. assert_no_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store3.id}", article_attributes['body'])
  373. article1 = Ticket::Article.find(article1.id)
  374. attachments = article1.attachments_inline
  375. assert_equal(2, attachments.length)
  376. assert_equal(store1.id, attachments.first.id)
  377. ticket1.destroy
  378. end
  379. test 'article attachment helper 2' do
  380. ticket1 = Ticket.create!(
  381. title: 'some article helper test2',
  382. group: Group.lookup(name: 'Users'),
  383. customer_id: 2,
  384. state: Ticket::State.lookup(name: 'new'),
  385. priority: Ticket::Priority.lookup(name: '2 normal'),
  386. updated_by_id: 1,
  387. created_by_id: 1,
  388. )
  389. assert(ticket1, 'ticket created')
  390. # create inbound article #1
  391. article1 = Ticket::Article.create!(
  392. ticket_id: ticket1.id,
  393. from: 'some_sender@example.com',
  394. to: 'some_recipient@example.com',
  395. subject: 'some subject',
  396. message_id: 'some@id',
  397. content_type: 'text/html',
  398. body: 'some message article helper test2 <div><img src="cid:15.274327094.140938@zammad.example.com">asdasd<img border="0" width="60" height="19" src="cid:15.274327094.140939@zammad.example.com" alt="Beschreibung: Beschreibung: efqmLogo"><br>',
  399. internal: false,
  400. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  401. type: Ticket::Article::Type.find_by(name: 'email'),
  402. updated_by_id: 1,
  403. created_by_id: 1,
  404. )
  405. store1 = Store.create!(
  406. object: 'Ticket::Article',
  407. o_id: article1.id,
  408. data: 'content_file1_normally_should_be_an_image',
  409. filename: 'some_file1.jpg',
  410. preferences: {
  411. 'Content-Type' => 'image/jpeg',
  412. 'Mime-Type' => 'image/jpeg',
  413. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  414. 'Content-Disposition' => 'inline'
  415. },
  416. created_by_id: 1,
  417. )
  418. store2 = Store.create!(
  419. object: 'Ticket::Article',
  420. o_id: article1.id,
  421. data: 'content_file2_normally_should_be_an_image',
  422. filename: 'some_file2.jpg',
  423. preferences: {
  424. 'Content-Type' => 'image/jpeg',
  425. 'Mime-Type' => 'image/jpeg',
  426. 'Content-ID' => '15.274327094.140939@zammad.example.com',
  427. 'Content-Disposition' => 'inline'
  428. },
  429. created_by_id: 1,
  430. )
  431. store3 = Store.create!(
  432. object: 'Ticket::Article',
  433. o_id: article1.id,
  434. data: 'content_file3',
  435. filename: 'some_file3.txt',
  436. preferences: {
  437. 'Content-Type' => 'text/stream',
  438. 'Mime-Type' => 'text/stream',
  439. 'Content-ID' => '15.274327094.99999@zammad.example.com',
  440. 'Content-Disposition' => 'inline'
  441. },
  442. created_by_id: 1,
  443. )
  444. article_attributes = Ticket::Article.insert_urls(article1.attributes_with_association_ids)
  445. assert_no_match('15.274327094.140938@zammad.example.com', article_attributes['body'])
  446. assert_no_match('15.274327094.140939@zammad.example.com', article_attributes['body'])
  447. assert_no_match('15.274327094.99999@zammad.example.com', article_attributes['body'])
  448. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store1.id}", article_attributes['body'])
  449. assert_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store2.id}", article_attributes['body'])
  450. assert_no_match("api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store3.id}", article_attributes['body'])
  451. article1 = Ticket::Article.find(article1.id)
  452. attachments = article1.attachments_inline
  453. assert_equal(2, attachments.length)
  454. assert_equal(store1.id, attachments.first.id)
  455. ticket1.destroy
  456. end
  457. end