email_build_test.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 =~ /<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 =~ /<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. assert_equal( should, data[:body] )
  53. # check count of attachments, only 2, because 3 part is text message and is already in body
  54. assert_equal( 2, data[:attachments].length )
  55. # check attachments
  56. if data[:attachments]
  57. data[:attachments].each { |attachment|
  58. if attachment[:filename] == 'message.html'
  59. assert_equal( nil, attachment[:preferences]['Content-ID'] )
  60. assert_equal( true, attachment[:preferences]['content-alternative'] )
  61. assert_equal( 'text/html', attachment[:preferences]['Mime-Type'] )
  62. assert_equal( 'UTF-8', attachment[:preferences]['Charset'] )
  63. elsif attachment[:filename] == 'somename.png'
  64. assert_equal( nil, attachment[:preferences]['Content-ID'] )
  65. assert_equal( nil, attachment[:preferences]['content-alternative'] )
  66. assert_equal( 'image/png', attachment[:preferences]['Mime-Type'] )
  67. assert_equal( 'UTF-8', attachment[:preferences]['Charset'] )
  68. else
  69. assert( false, "invalid attachment, should not be there, #{attachment.inspect}" )
  70. end
  71. }
  72. end
  73. end
  74. test 'plain email + attachment check' do
  75. text = '> Welcome!
  76. >
  77. > Thank you for installing Zammad. äöüß
  78. >'
  79. mail = Channel::EmailBuild.build(
  80. from: 'sender@example.com',
  81. to: 'recipient@example.com',
  82. body: text,
  83. attachments: [
  84. {
  85. 'Mime-Type' => 'image/png',
  86. :content => 'xxx',
  87. :filename => 'somename.png',
  88. },
  89. ],
  90. )
  91. should = '> Welcome!
  92. >
  93. > Thank you for installing Zammad. äöüß
  94. >'
  95. assert_equal( should, mail.text_part.body.to_s )
  96. assert_equal( nil, mail.html_part )
  97. parser = Channel::EmailParser.new
  98. data = parser.parse( mail.to_s )
  99. # check body
  100. assert_equal( should, data[:body] )
  101. # check count of attachments, 2
  102. assert_equal( 1, data[:attachments].length )
  103. # check attachments
  104. if data[:attachments]
  105. data[:attachments].each { |attachment|
  106. if attachment[:filename] == 'somename.png'
  107. assert_equal( nil, attachment[:preferences]['Content-ID'] )
  108. assert_equal( nil, attachment[:preferences]['content-alternative'] )
  109. assert_equal( 'image/png', attachment[:preferences]['Mime-Type'] )
  110. assert_equal( 'UTF-8', attachment[:preferences]['Charset'] )
  111. else
  112. assert( false, "invalid attachment, should not be there, #{attachment.inspect}" )
  113. end
  114. }
  115. end
  116. end
  117. test 'plain email + without attachment check' do
  118. text = '> Welcome!
  119. >
  120. > Thank you for installing Zammad. äöüß
  121. >'
  122. mail = Channel::EmailBuild.build(
  123. from: 'sender@example.com',
  124. to: 'recipient@example.com',
  125. body: text,
  126. )
  127. should = '> Welcome!
  128. >
  129. > Thank you for installing Zammad. äöüß
  130. >'
  131. assert_equal( should, mail.body.to_s )
  132. assert_equal( nil, mail.html_part )
  133. parser = Channel::EmailParser.new
  134. data = parser.parse( mail.to_s )
  135. # check body
  136. assert_equal( should, data[:body] )
  137. # check count of attachments, 0
  138. assert_equal( 0, data[:attachments].length )
  139. end
  140. end