email_build_test.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class EmailBuildTest < ActiveSupport::TestCase
  4. test 'document complete check' do
  5. html = '<b>test</b>'
  6. result = Channel::EmailBuild.html_complete_check(html)
  7. assert(result =~ /^<\!DOCTYPE/, 'test 1')
  8. assert(result !~ /^.+?<\!DOCTYPE/, 'test 1')
  9. assert(result =~ /<html>/, 'test 1')
  10. assert(result =~ /font-family/, 'test 1')
  11. assert(result =~ %r{<b>test</b>}, 'test 1')
  12. html = 'invalid <!DOCTYPE html><html><b>test</b></html>'
  13. result = Channel::EmailBuild.html_complete_check(html)
  14. assert(result !~ /^<\!DOCTYPE/, 'test 2')
  15. assert(result =~ /^.+?<\!DOCTYPE/, 'test 2')
  16. assert(result =~ /<html>/, 'test 2')
  17. assert(result !~ /font-family/, 'test 2')
  18. assert(result =~ %r{<b>test</b>}, 'test 2')
  19. end
  20. test 'html email + attachment check' do
  21. html = '<!DOCTYPE html>
  22. <html>
  23. <head>
  24. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  25. <head>
  26. <body style="font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;">
  27. <div>&gt; Welcome!</div><div>&gt;</div><div>&gt; Thank you for installing Zammad. äöüß</div><div>&gt;</div>
  28. </body>
  29. </html>'
  30. mail = Channel::EmailBuild.build(
  31. from: 'sender@example.com',
  32. to: 'recipient@example.com',
  33. body: html,
  34. content_type: 'text/html',
  35. attachments: [
  36. {
  37. 'Mime-Type' => 'image/png',
  38. :content => 'xxx',
  39. :filename => 'somename.png',
  40. },
  41. ],
  42. )
  43. should = '> Welcome!
  44. >
  45. > Thank you for installing Zammad. äöüß
  46. >'
  47. assert_equal(should, mail.text_part.body.to_s)
  48. assert_equal(html, mail.html_part.body.to_s)
  49. parser = Channel::EmailParser.new
  50. data = parser.parse(mail.to_s)
  51. # check body
  52. should = '&gt; Welcome!<br>&gt;<br>&gt; Thank you for installing Zammad. äöüß<br>&gt;'
  53. assert_equal(should, data[:body])
  54. assert_equal('text/html', data[:content_type])
  55. # check count of attachments, only 2, because 3 part is text message and is already in body
  56. assert_equal(2, data[:attachments].length)
  57. # check attachments
  58. if data[:attachments]
  59. data[:attachments].each { |attachment|
  60. if attachment[:filename] == 'message.html'
  61. assert_equal(nil, attachment[:preferences]['Content-ID'])
  62. assert_equal(true, attachment[:preferences]['content-alternative'])
  63. assert_equal('text/html', attachment[:preferences]['Mime-Type'])
  64. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  65. elsif attachment[:filename] == 'somename.png'
  66. assert_equal(nil, attachment[:preferences]['Content-ID'])
  67. assert_equal(nil, attachment[:preferences]['content-alternative'])
  68. assert_equal('image/png', attachment[:preferences]['Mime-Type'])
  69. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  70. else
  71. assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
  72. end
  73. }
  74. end
  75. end
  76. test 'plain email + attachment check' do
  77. text = '> Welcome!
  78. >
  79. > Thank you for installing Zammad. äöüß
  80. >'
  81. mail = Channel::EmailBuild.build(
  82. from: 'sender@example.com',
  83. to: 'recipient@example.com',
  84. body: text,
  85. attachments: [
  86. {
  87. 'Mime-Type' => 'image/png',
  88. :content => 'xxx',
  89. :filename => 'somename.png',
  90. },
  91. ],
  92. )
  93. should = '> Welcome!
  94. >
  95. > Thank you for installing Zammad. äöüß
  96. >'
  97. assert_equal(should, mail.text_part.body.to_s)
  98. assert_equal(nil, mail.html_part)
  99. parser = Channel::EmailParser.new
  100. data = parser.parse(mail.to_s)
  101. # check body
  102. assert_equal(should, data[:body])
  103. # check count of attachments, 2
  104. assert_equal(1, data[:attachments].length)
  105. # check attachments
  106. if data[:attachments]
  107. data[:attachments].each { |attachment|
  108. if attachment[:filename] == 'somename.png'
  109. assert_equal(nil, attachment[:preferences]['Content-ID'])
  110. assert_equal(nil, attachment[:preferences]['content-alternative'])
  111. assert_equal('image/png', attachment[:preferences]['Mime-Type'])
  112. assert_equal('UTF-8', attachment[:preferences]['Charset'])
  113. else
  114. assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
  115. end
  116. }
  117. end
  118. end
  119. test 'plain email + without attachment check' do
  120. text = '> Welcome!
  121. >
  122. > Thank you for installing Zammad. äöüß
  123. >'
  124. mail = Channel::EmailBuild.build(
  125. from: 'sender@example.com',
  126. to: 'recipient@example.com',
  127. body: text,
  128. )
  129. should = '> Welcome!
  130. >
  131. > Thank you for installing Zammad. äöüß
  132. >'
  133. assert_equal(should, mail.body.to_s)
  134. assert_equal(nil, mail.html_part)
  135. parser = Channel::EmailParser.new
  136. data = parser.parse(mail.to_s)
  137. # check body
  138. assert_equal(should, data[:body])
  139. # check count of attachments, 0
  140. assert_equal(0, data[:attachments].length)
  141. end
  142. test 'email - html email client fixes' do
  143. # https://github.com/martini/zammad/issues/165
  144. html_raw = '<blockquote type="cite">some
  145. text
  146. </blockquote>
  147. 123
  148. <blockquote type="cite">some
  149. text
  150. </blockquote>'
  151. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  152. assert_not_equal(html_with_fixes, html_raw)
  153. html_should = '<blockquote type="cite" style="border-left: 2px solid blue; margin: 0 0 16px; padding: 8px 12px 8px 12px;">some
  154. text
  155. </blockquote>
  156. 123
  157. <blockquote type="cite" style="border-left: 2px solid blue; margin: 0 0 16px; padding: 8px 12px 8px 12px;">some
  158. text
  159. </blockquote>'
  160. assert_equal(html_should, html_with_fixes)
  161. html_raw = '<p>some
  162. text
  163. </p>
  164. <p>123</p>'
  165. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  166. assert_not_equal(html_with_fixes, html_raw)
  167. html_should = '<p style="margin: 0;">some
  168. text
  169. </p>
  170. <p style="margin: 0;">123</p>'
  171. assert_equal(html_should, html_with_fixes)
  172. html_raw = '<p>sometext</p><hr><p>123</p>'
  173. html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
  174. assert_not_equal(html_with_fixes, html_raw)
  175. 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>'
  176. assert_equal(html_should, html_with_fixes)
  177. end
  178. test 'from checks' do
  179. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody @ "Company"', 'some.body@example.com')
  180. assert_equal('"Somebody @ \"Company\"" <some.body@example.com>', quoted_in_one_line)
  181. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody', 'some.body@example.com')
  182. assert_equal('Somebody <some.body@example.com>', quoted_in_one_line)
  183. quoted_in_one_line = Channel::EmailBuild.recipient_line('Somebody | Some Org', 'some.body@example.com')
  184. assert_equal('"Somebody | Some Org" <some.body@example.com>', quoted_in_one_line)
  185. quoted_in_one_line = Channel::EmailBuild.recipient_line('Test Master Agent via Support', 'some.body@example.com')
  186. assert_equal('"Test Master Agent via Support" <some.body@example.com>', quoted_in_one_line)
  187. end
  188. end