email_build_test.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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!
  51. >
  52. > Thank you for installing Zammad. äöüß
  53. >
  54. MSG_TEXT
  55. assert_equal(text_should, mail.text_part.body.to_s)
  56. assert_equal(html, mail.html_part.body.to_s)
  57. parser = Channel::EmailParser.new
  58. data = parser.parse(mail.to_s)
  59. # check body
  60. should = '<div>&gt; Welcome!</div><div>&gt;</div><div>&gt; Thank you for installing Zammad. äöüß</div><div>&gt;</div>'
  61. assert_equal(should, data[:body])
  62. assert_equal('text/html', data[:content_type])
  63. # check count of attachments, only 2, because 3 part is text message and is already in body
  64. assert_equal(2, data[:attachments].length)
  65. # check attachments
  66. data[:attachments]&.each do |attachment|
  67. if attachment[:filename] == 'message.html'
  68. assert_nil(attachment[:preferences]['Content-ID'])
  69. assert_equal(true, attachment[:preferences]['content-alternative'])
  70. assert_equal('text/html', attachment[:preferences]['Mime-Type'])
  71. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  72. elsif attachment[:filename] == 'somename.png'
  73. assert_nil(attachment[:preferences]['Content-ID'])
  74. assert_nil(attachment[:preferences]['content-alternative'])
  75. assert_equal('image/png', attachment[:preferences]['Mime-Type'])
  76. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  77. else
  78. assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
  79. end
  80. end
  81. end
  82. test 'plain email + attachment check' do
  83. text = <<~MSG_TEXT.chomp
  84. > Welcome!
  85. >
  86. > Thank you for installing Zammad. äöüß
  87. >
  88. MSG_TEXT
  89. mail = Channel::EmailBuild.build(
  90. from: 'sender@example.com',
  91. to: 'recipient@example.com',
  92. body: text,
  93. attachments: [
  94. {
  95. 'Mime-Type' => 'image/png',
  96. :content => 'xxx',
  97. :filename => 'somename.png'
  98. }
  99. ],
  100. )
  101. assert_equal(text, mail.text_part.body.to_s)
  102. assert_nil(mail.html_part)
  103. assert_equal('image/png; filename=somename.png', mail.attachments[0].content_type)
  104. parser = Channel::EmailParser.new
  105. data = parser.parse(mail.to_s)
  106. # check body
  107. assert_equal(text, data[:body])
  108. # check count of attachments, 2
  109. assert_equal(1, data[:attachments].length)
  110. # check attachments
  111. data[:attachments]&.each do |attachment|
  112. if attachment[:filename] == 'somename.png'
  113. assert_nil(attachment[:preferences]['Content-ID'])
  114. assert_nil(attachment[:preferences]['content-alternative'])
  115. assert_equal('image/png', attachment[:preferences]['Mime-Type'])
  116. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  117. else
  118. assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
  119. end
  120. end
  121. end
  122. test 'plain email + attachment check 2' do
  123. ticket1 = Ticket.create!(
  124. title: 'some article helper test1',
  125. group: Group.lookup(name: 'Users'),
  126. customer_id: 2,
  127. state: Ticket::State.lookup(name: 'new'),
  128. priority: Ticket::Priority.lookup(name: '2 normal'),
  129. updated_by_id: 1,
  130. created_by_id: 1,
  131. )
  132. assert(ticket1, 'ticket created')
  133. # create inbound article #1
  134. article1 = Ticket::Article.create!(
  135. ticket_id: ticket1.id,
  136. from: 'some_sender@example.com',
  137. to: 'some_recipient@example.com',
  138. subject: 'some subject',
  139. message_id: 'some@id',
  140. content_type: 'text/html',
  141. 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>',
  142. internal: false,
  143. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  144. type: Ticket::Article::Type.find_by(name: 'email'),
  145. updated_by_id: 1,
  146. created_by_id: 1,
  147. )
  148. store1 = Store.add(
  149. object: 'Ticket::Article',
  150. o_id: article1.id,
  151. data: 'content_file1_normally_should_be_an_ics_calendar_file',
  152. filename: 'schedule.ics',
  153. preferences: {
  154. 'Mime-Type' => 'text/calendar'
  155. },
  156. created_by_id: 1,
  157. )
  158. text = <<~MSG_TEXT.chomp
  159. > Welcome!
  160. >
  161. > Thank you for installing Zammad. äöüß
  162. >
  163. MSG_TEXT
  164. mail = Channel::EmailBuild.build(
  165. from: 'sender@example.com',
  166. to: 'recipient@example.com',
  167. body: text,
  168. attachments: [
  169. store1
  170. ],
  171. )
  172. assert_equal(text, mail.text_part.body.to_s)
  173. assert_nil(mail.html_part)
  174. assert_equal('text/calendar; filename=schedule.ics', mail.attachments[0].content_type)
  175. parser = Channel::EmailParser.new
  176. data = parser.parse(mail.to_s)
  177. # check body
  178. assert_equal(text, data[:body])
  179. # check count of attachments, 2
  180. assert_equal(1, data[:attachments].length)
  181. # check attachments
  182. data[:attachments]&.each do |attachment|
  183. if attachment[:filename] == 'schedule.ics'
  184. assert(attachment[:preferences]['Content-ID'])
  185. assert_nil(attachment[:preferences]['content-alternative'])
  186. assert_equal('text/calendar', attachment[:preferences]['Mime-Type'])
  187. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  188. else
  189. assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
  190. end
  191. end
  192. end
  193. test 'plain email + without attachment check' do
  194. text = <<~MSG_TEXT.chomp
  195. > Welcome!
  196. >
  197. > Thank you for installing Zammad. äöüß
  198. >
  199. MSG_TEXT
  200. mail = Channel::EmailBuild.build(
  201. from: 'sender@example.com',
  202. to: 'recipient@example.com',
  203. body: text,
  204. )
  205. assert_equal(text, mail.body.to_s)
  206. assert_nil(mail.html_part)
  207. parser = Channel::EmailParser.new
  208. data = parser.parse(mail.to_s)
  209. # check body
  210. assert_equal(text, data[:body])
  211. # check count of attachments, 0
  212. assert_equal(0, data[:attachments].length)
  213. end
  214. test 'email - html email client fixes' do
  215. # https://github.com/martini/zammad/issues/165
  216. html_raw = '<blockquote type="cite">some
  217. text
  218. </blockquote>
  219. 123
  220. <blockquote type="cite">some
  221. text
  222. </blockquote>'
  223. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  224. assert_not_equal(html_with_fixes, html_raw)
  225. html_should = '<blockquote type="cite" style="border-left: 2px solid blue; margin: 0 0 16px; padding: 8px 12px 8px 12px;">some
  226. text
  227. </blockquote>
  228. 123
  229. <blockquote type="cite" style="border-left: 2px solid blue; margin: 0 0 16px; padding: 8px 12px 8px 12px;">some
  230. text
  231. </blockquote>'
  232. assert_equal(html_should, html_with_fixes)
  233. html_raw = '<p>some
  234. text
  235. </p>
  236. <p>123</p>'
  237. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  238. assert_not_equal(html_with_fixes, html_raw)
  239. html_should = '<p style="margin: 0;">some
  240. text
  241. </p>
  242. <p style="margin: 0;">123</p>'
  243. assert_equal(html_should, html_with_fixes)
  244. html_raw = '<p>sometext</p><hr><p>123</p>'
  245. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  246. assert_not_equal(html_with_fixes, html_raw)
  247. 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>'
  248. assert_equal(html_should, html_with_fixes)
  249. end
  250. test 'from checks' do
  251. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody @ "Company"', 'some.body@example.com')
  252. assert_equal('"Somebody @ \"Company\"" <some.body@example.com>', quoted_in_one_line)
  253. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody', 'some.body@example.com')
  254. assert_equal('Somebody <some.body@example.com>', quoted_in_one_line)
  255. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody | Some Org', 'some.body@example.com')
  256. assert_equal('"Somebody | Some Org" <some.body@example.com>', quoted_in_one_line)
  257. quoted_in_one_line = Channel::EmailBuild.recipient_line('Test Master Agent via Support', 'some.body@example.com')
  258. assert_equal('"Test Master Agent via Support" <some.body@example.com>', quoted_in_one_line)
  259. end
  260. end