email_build_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. require 'test_helper'
  2. class EmailBuildTest < ActiveSupport::TestCase
  3. test 'document complete check' do
  4. html = '<b>test</b>'
  5. result = Channel::EmailBuild.html_complete_check(html)
  6. assert(result =~ /^<\!DOCTYPE/, 'test 1')
  7. assert(result !~ /^.+?<\!DOCTYPE/, 'test 1')
  8. assert(result =~ /<html>/, 'test 1')
  9. assert(result =~ /font-family/, 'test 1')
  10. assert(result =~ %r{<b>test</b>}, 'test 1')
  11. html = 'invalid <!DOCTYPE html><html><b>test</b></html>'
  12. result = Channel::EmailBuild.html_complete_check(html)
  13. assert(result !~ /^<\!DOCTYPE/, 'test 2')
  14. assert(result =~ /^.+?<\!DOCTYPE/, 'test 2')
  15. assert(result =~ /<html>/, 'test 2')
  16. assert(result !~ /font-family/, 'test 2')
  17. assert(result =~ %r{<b>test</b>}, 'test 2')
  18. # Issue #1230, missing backslashes
  19. # 'Test URL: \\storage\project\100242-Inc'
  20. html = '<b>Test URL</b>: \\\\storage\\project\\100242-Inc'
  21. result = Channel::EmailBuild.html_complete_check(html)
  22. assert(result.include?(html), 'backslashes must be kept')
  23. end
  24. test 'html email + attachment check' do
  25. html = <<~MSG_HTML.chomp
  26. <!DOCTYPE html>
  27. <html>
  28. <head>
  29. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  30. </head>
  31. <body style="font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;">
  32. <div>&gt; Welcome!</div><div>&gt;</div><div>&gt; Thank you for installing Zammad. äöüß</div><div>&gt;</div>
  33. </body>
  34. </html>
  35. MSG_HTML
  36. mail = Channel::EmailBuild.build(
  37. from: 'sender@example.com',
  38. to: 'recipient@example.com',
  39. body: html,
  40. content_type: 'text/html',
  41. attachments: [
  42. {
  43. 'Mime-Type' => 'image/png',
  44. :content => 'xxx',
  45. :filename => 'somename.png'
  46. }
  47. ],
  48. )
  49. text_should = <<~MSG_TEXT.chomp
  50. > Welcome!\r
  51. >\r
  52. > Thank you for installing Zammad. äöüß\r
  53. >\r
  54. MSG_TEXT
  55. assert_equal(text_should, mail.text_part.body.to_s)
  56. html_should = <<~MSG_HTML.chomp
  57. <!DOCTYPE html>\r
  58. <html>\r
  59. <head>\r
  60. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\r
  61. </head>\r
  62. <body style="font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;">\r
  63. <div>&gt; Welcome!</div><div>&gt;</div><div>&gt; Thank you for installing Zammad. äöüß</div><div>&gt;</div>\r
  64. </body>\r
  65. </html>
  66. MSG_HTML
  67. assert_equal(html_should, mail.html_part.body.to_s)
  68. parser = Channel::EmailParser.new
  69. data = parser.parse(mail.to_s)
  70. # check body
  71. should = '<div>&gt; Welcome!</div><div>&gt;</div><div>&gt; Thank you for installing Zammad. äöüß</div><div>&gt;</div>'
  72. assert_equal(should, data[:body])
  73. assert_equal('text/html', data[:content_type])
  74. # check count of attachments, only 2, because 3 part is text message and is already in body
  75. assert_equal(2, data[:attachments].length)
  76. # check attachments
  77. data[:attachments]&.each do |attachment|
  78. if attachment[:filename] == 'message.html'
  79. assert_nil(attachment[:preferences]['Content-ID'])
  80. assert_equal(true, attachment[:preferences]['content-alternative'])
  81. assert_equal('text/html', attachment[:preferences]['Mime-Type'])
  82. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  83. elsif attachment[:filename] == 'somename.png'
  84. assert_nil(attachment[:preferences]['Content-ID'])
  85. assert_nil(attachment[:preferences]['content-alternative'])
  86. assert_equal('image/png', attachment[:preferences]['Mime-Type'])
  87. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  88. else
  89. assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
  90. end
  91. end
  92. end
  93. test 'plain email + attachment check' do
  94. text = <<~MSG_TEXT.chomp
  95. > Welcome!
  96. >
  97. > Thank you for installing Zammad. äöüß
  98. >
  99. MSG_TEXT
  100. mail = Channel::EmailBuild.build(
  101. from: 'sender@example.com',
  102. to: 'recipient@example.com',
  103. body: text,
  104. attachments: [
  105. {
  106. 'Mime-Type' => 'image/png',
  107. :content => 'xxx',
  108. :filename => 'somename.png'
  109. }
  110. ],
  111. )
  112. text_should = <<~MSG_TEXT.chomp
  113. > Welcome!\r
  114. >\r
  115. > Thank you for installing Zammad. äöüß\r
  116. >\r
  117. MSG_TEXT
  118. assert_equal(text_should, mail.text_part.body.to_s)
  119. assert_nil(mail.html_part)
  120. assert_equal('image/png; filename=somename.png', mail.attachments[0].content_type)
  121. parser = Channel::EmailParser.new
  122. data = parser.parse(mail.to_s)
  123. # check body
  124. assert_equal(text, data[:body])
  125. # check count of attachments, 2
  126. assert_equal(1, data[:attachments].length)
  127. # check attachments
  128. data[:attachments]&.each do |attachment|
  129. if attachment[:filename] == 'somename.png'
  130. assert_nil(attachment[:preferences]['Content-ID'])
  131. assert_nil(attachment[:preferences]['content-alternative'])
  132. assert_equal('image/png', attachment[:preferences]['Mime-Type'])
  133. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  134. else
  135. assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
  136. end
  137. end
  138. end
  139. test 'plain email + attachment check 2' do
  140. ticket1 = Ticket.create!(
  141. title: 'some article helper test1',
  142. group: Group.lookup(name: 'Users'),
  143. customer_id: 2,
  144. state: Ticket::State.lookup(name: 'new'),
  145. priority: Ticket::Priority.lookup(name: '2 normal'),
  146. updated_by_id: 1,
  147. created_by_id: 1,
  148. )
  149. assert(ticket1, 'ticket created')
  150. # create inbound article #1
  151. article1 = Ticket::Article.create!(
  152. ticket_id: ticket1.id,
  153. from: 'some_sender@example.com',
  154. to: 'some_recipient@example.com',
  155. subject: 'some subject',
  156. message_id: 'some@id',
  157. content_type: 'text/html',
  158. 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>',
  159. internal: false,
  160. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  161. type: Ticket::Article::Type.find_by(name: 'email'),
  162. updated_by_id: 1,
  163. created_by_id: 1,
  164. )
  165. store1 = Store.add(
  166. object: 'Ticket::Article',
  167. o_id: article1.id,
  168. data: 'content_file1_normally_should_be_an_ics_calendar_file',
  169. filename: 'schedule.ics',
  170. preferences: {
  171. 'Mime-Type' => 'text/calendar'
  172. },
  173. created_by_id: 1,
  174. )
  175. text = <<~MSG_TEXT.chomp
  176. > Welcome!
  177. >
  178. > Thank you for installing Zammad. äöüß
  179. >
  180. MSG_TEXT
  181. mail = Channel::EmailBuild.build(
  182. from: 'sender@example.com',
  183. to: 'recipient@example.com',
  184. body: text,
  185. attachments: [
  186. store1
  187. ],
  188. )
  189. text_should = <<~MSG_TEXT.chomp
  190. > Welcome!\r
  191. >\r
  192. > Thank you for installing Zammad. äöüß\r
  193. >\r
  194. MSG_TEXT
  195. assert_equal(text_should, mail.text_part.body.to_s)
  196. assert_nil(mail.html_part)
  197. assert_equal('text/calendar; filename=schedule.ics', mail.attachments[0].content_type)
  198. parser = Channel::EmailParser.new
  199. data = parser.parse(mail.to_s)
  200. # check body
  201. assert_equal(text, data[:body])
  202. # check count of attachments, 2
  203. assert_equal(1, data[:attachments].length)
  204. # check attachments
  205. data[:attachments]&.each do |attachment|
  206. if attachment[:filename] == 'schedule.ics'
  207. assert(attachment[:preferences]['Content-ID'])
  208. assert_nil(attachment[:preferences]['content-alternative'])
  209. assert_equal('text/calendar', attachment[:preferences]['Mime-Type'])
  210. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  211. else
  212. assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
  213. end
  214. end
  215. end
  216. test 'plain email + without attachment check' do
  217. text = <<~MSG_TEXT.chomp
  218. > Welcome!
  219. >
  220. > Thank you for installing Zammad. äöüß
  221. >
  222. MSG_TEXT
  223. mail = Channel::EmailBuild.build(
  224. from: 'sender@example.com',
  225. to: 'recipient@example.com',
  226. body: text,
  227. )
  228. text_should = <<~MSG_TEXT.chomp
  229. > Welcome!\r
  230. >\r
  231. > Thank you for installing Zammad. äöüß\r
  232. >\r
  233. MSG_TEXT
  234. assert_equal(text_should, mail.body.to_s)
  235. assert_nil(mail.html_part)
  236. parser = Channel::EmailParser.new
  237. data = parser.parse(mail.to_s)
  238. # check body
  239. assert_equal(text, data[:body])
  240. # check count of attachments, 0
  241. assert_equal(0, data[:attachments].length)
  242. end
  243. test 'email - html email client fixes' do
  244. # https://github.com/martini/zammad/issues/165
  245. html_raw = '<blockquote type="cite">some
  246. text
  247. </blockquote>
  248. 123
  249. <blockquote type="cite">some
  250. text
  251. </blockquote>'
  252. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  253. assert_not_equal(html_with_fixes, html_raw)
  254. html_should = '<blockquote type="cite" style="border-left: 2px solid blue; margin: 0 0 16px; padding: 8px 12px 8px 12px;">some
  255. text
  256. </blockquote>
  257. 123
  258. <blockquote type="cite" style="border-left: 2px solid blue; margin: 0 0 16px; padding: 8px 12px 8px 12px;">some
  259. text
  260. </blockquote>'
  261. assert_equal(html_should, html_with_fixes)
  262. html_raw = '<p>some
  263. text
  264. </p>
  265. <p>123</p>'
  266. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  267. assert_not_equal(html_with_fixes, html_raw)
  268. html_should = '<p style="margin: 0;">some
  269. text
  270. </p>
  271. <p style="margin: 0;">123</p>'
  272. assert_equal(html_should, html_with_fixes)
  273. html_raw = '<p>sometext</p><hr><p>123</p>'
  274. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  275. assert_not_equal(html_with_fixes, html_raw)
  276. html_should = '<p style="margin: 0;">sometext</p><hr style="margin-top: 6px; margin-bottom: 6px; border: 0; border-top: 1px solid #dfdfdf;"><p style="margin: 0;">123</p>'
  277. assert_equal(html_should, html_with_fixes)
  278. end
  279. test 'from checks' do
  280. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody @ "Company"', 'some.body@example.com')
  281. assert_equal('"Somebody @ \"Company\"" <some.body@example.com>', quoted_in_one_line)
  282. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody', 'some.body@example.com')
  283. assert_equal('Somebody <some.body@example.com>', quoted_in_one_line)
  284. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody | Some Org', 'some.body@example.com')
  285. assert_equal('"Somebody | Some Org" <some.body@example.com>', quoted_in_one_line)
  286. quoted_in_one_line = Channel::EmailBuild.recipient_line('Test Master Agent via Support', 'some.body@example.com')
  287. assert_equal('"Test Master Agent via Support" <some.body@example.com>', quoted_in_one_line)
  288. end
  289. # #2362 - Attached text files get prepended on e-mail reply instead of appended
  290. test 'plain email + text attachment' do
  291. ticket1 = Ticket.create!(
  292. title: 'some article text attachment test',
  293. group: Group.lookup(name: 'Users'),
  294. customer_id: 2,
  295. state: Ticket::State.lookup(name: 'new'),
  296. priority: Ticket::Priority.lookup(name: '2 normal'),
  297. updated_by_id: 1,
  298. created_by_id: 1,
  299. )
  300. assert(ticket1, 'ticket created')
  301. article1 = Ticket::Article.create!(
  302. ticket_id: ticket1.id,
  303. from: 'some_sender@example.com',
  304. to: 'some_recipient@example.com',
  305. subject: 'some subject',
  306. message_id: 'some@id',
  307. content_type: 'text/html',
  308. 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>',
  309. internal: false,
  310. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  311. type: Ticket::Article::Type.find_by(name: 'email'),
  312. updated_by_id: 1,
  313. created_by_id: 1,
  314. )
  315. text = <<~MSG_TEXT.chomp
  316. > Welcome!
  317. >
  318. > Email Content
  319. MSG_TEXT
  320. store1 = Store.add(
  321. object: 'Ticket::Article',
  322. o_id: article1.id,
  323. data: 'Text Content',
  324. filename: 'text_file.txt',
  325. preferences: {
  326. 'Mime-Type' => 'text/plain'
  327. },
  328. created_by_id: 1,
  329. )
  330. mail = Channel::EmailBuild.build(
  331. from: 'sender@example.com',
  332. to: 'recipient@example.com',
  333. body: text,
  334. attachments: [
  335. store1
  336. ],
  337. )
  338. File.write('append_test.eml', mail.to_s)
  339. # Email Content should appear before the Text Content within the raw email
  340. assert_match(/Email Content[\s\S]*Text Content/, mail.to_s)
  341. end
  342. end