email_parser_test.rb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class EmailParserTest < ActiveSupport::TestCase
  4. test 'parse' do
  5. files = [
  6. {
  7. :data => IO.read('test/fixtures/mail1.box'),
  8. :body_md5 => 'b57d21dcac6b05e1aa67af51a9e4c1ec',
  9. :params => {
  10. :from => 'John.Smith@example.com',
  11. :from_email => 'John.Smith@example.com',
  12. :from_display_name => nil,
  13. :subject => 'CI Daten für PublicView ',
  14. },
  15. },
  16. {
  17. :data => IO.read('test/fixtures/mail2.box'),
  18. :body_md5 => '548917e0bff0806f9b27c09bbf23bb38',
  19. :params => {
  20. :from => 'Martin Edenhofer <martin@example.com>',
  21. :from_email => 'martin@example.com',
  22. :from_display_name => 'Martin Edenhofer',
  23. :subject => 'aaäöüßad asd',
  24. :plain_part_md5 => "äöüß ad asd\n\n-Martin\n\n--\nOld programmers never die. They just branch to a new address.",
  25. },
  26. },
  27. {
  28. :data => IO.read('test/fixtures/mail3.box'),
  29. :body_md5 => '218971f005cddf7a1b70a980507f589d',
  30. :params => {
  31. :from => '"Günther John | Example GmbH" <k.guenther@example.com>',
  32. :from_email => 'k.guenther@example.com',
  33. :from_display_name => 'Günther John | Example GmbH',
  34. :subject => 'Ticket Templates',
  35. },
  36. },
  37. {
  38. :data => IO.read('test/fixtures/mail4.box'),
  39. :body_md5 => '2f2c3a5c233dbd9658ab37d39469b7d0',
  40. :params => {
  41. :from => '"Günther Katja | Example GmbH" <k.guenther@example.com>',
  42. :from_email => 'k.guenther@example.com',
  43. :from_display_name => 'Günther Katja | Example GmbH',
  44. :subject => 'AW: Ticket Templates [Ticket#11168]',
  45. :plain_part_md5 => "Hallo Katja,
  46. super! Ich freu mich!
  47. Wir würden gerne die Präsentation/Einführung in die Ticket Templates per Screensharing oder zumindest per Telefon machen.
  48. Mögliche Termine:
  49. o Do, 10.05.2012 15:00-16:00
  50. o Fr, 11.05.2012 13:00-14:00
  51. o Di, 15.05.2012 17:00-18:00
  52. Über Feedback würde ich mich freuen!
  53. PS: Zur besseren Übersicht habe ich ein Ticket erstellt. :) Im Footer sind unsere geschäftlichen Kontaktdaten (falls diese irgendwann einmal benötigt werden sollten), mehr dazu in ein paar Tagen.
  54. Liebe Grüße!
  55. -Martin
  56. ",
  57. },
  58. },
  59. {
  60. :data => IO.read('test/fixtures/mail5.box'),
  61. :body_md5 => '51364a306362f513f53f2bbea7820f37',
  62. :params => {
  63. :from => 'marc.smith@example.com (Marc Smith)',
  64. :from_email => 'marc.smith@example.com',
  65. :from_display_name => 'Marc Smith',
  66. :subject => 'Re: XXXX Betatest Ticket Templates [Ticket#11162]',
  67. },
  68. },
  69. {
  70. :data => IO.read('test/fixtures/mail6.box'),
  71. :body_md5 => '1fc492b8d762d82f861dbb70b7cf7610',
  72. :params => {
  73. :from => '"Hans BÄKOSchönland" <me@bogen.net>',
  74. :from_email => 'me@bogen.net',
  75. :from_display_name => 'Hans BÄKOSchönland',
  76. :subject => 'utf8: 使って / ISO-8859-1: Priorität" / cp-1251: Сергей Углицких',
  77. :plain_part => "this is a test [1]Compare Cable, DSL or Satellite plans: As low as $2.95.
  78. Test1:8
  79. Test2:&amp;
  80. Test3:&ni;
  81. Test4:&amp;
  82. Test5:=
  83. [1] http://localhost/8HMZENUS/2737??PS=
  84. "
  85. },
  86. },
  87. {
  88. :data => IO.read('test/fixtures/mail7.box'),
  89. :body_md5 => '775a69acf8ba0495712a3953f2ecff6a',
  90. :params => {
  91. :from => 'Eike.Ehringer@example.com',
  92. :from_email => 'Eike.Ehringer@example.com',
  93. :from_display_name => nil,
  94. :subject => 'AW:Installation [Ticket#11392]',
  95. :plain_part_md5 => "Hallo.
  96. Jetzt muss ich dir noch kurzfristig absagen für morgen.
  97. Lass uns evtl morgen Tel.
  98. Mfg eike
  99. Martin Edenhofer via Znuny Team --- Installation [Ticket#11392] ---
  100. Von:&quot;Martin Edenhofer via Znuny Team&quot; &lt;support@example.com&gt;Aneike.xx@xx-corpxx.comDatum:Mi., 13.06.2012 14:30BetreffInstallation [Ticket#11392]
  101. Hi Eike,
  102. anbei wie gestern telefonisch besprochen Informationen zur Vorbereitung.
  103. a) Installation von http://ftp.gwdg.de/pub/misc/zammad/RPMS/fedora/4/zammad-3.0.13-01.noarch.rpm (dieses RPM ist RHEL kompatible) und dessen Abhängigkeiten.
  104. b) Installation von &quot;mysqld&quot; und &quot;perl-DBD-MySQL&quot;.
  105. Das wäre es zur Vorbereitung!
  106. Bei Fragen nur zu!
  107. -Martin
  108. --
  109. Martin Edenhofer
  110. Znuny GmbH // Marienstraße 11 // 10117 Berlin // Germany
  111. P: +49 (0) 30 60 98 54 18-0
  112. F: +49 (0) 30 60 98 54 18-8
  113. W: http://example.com
  114. Location: Berlin - HRB 139852 B Amtsgericht Berlin-Charlottenburg
  115. Managing Director: Martin Edenhofer
  116. ",
  117. },
  118. },
  119. {
  120. :data => IO.read('test/fixtures/mail8.box'),
  121. :body_md5 => 'b506a6aa5f76e608c982d15a449ee163',
  122. :attachments => [
  123. {
  124. :md5 => '635e03d2ddde520b925262c8ffd03234',
  125. :filename => 'message.html',
  126. },
  127. ],
  128. :params => {
  129. :from => 'Franz.Schaefer@example.com',
  130. :from_email => 'Franz.Schaefer@example.com',
  131. :from_display_name => nil,
  132. :subject => 'could not rename: ZZZAAuto',
  133. :plain_part_md5 => "Gravierend?
  134. Mit freundlichen Grüßen
  135. Franz Schäfer
  136. Manager Information Systems
  137. Telefon
  138. +49 000 000 8565
  139. franz.schaefer@example.com
  140. Example Stoff GmbH
  141. Fakultaet
  142. Düsseldorfer Landstraße 395
  143. D-00000 Hof
  144. www.example.com
  145. Geschäftsführung/Management Board: Jan Bauer (Vorsitzender/Chairman),
  146. Oliver Bauer, Heiko Bauer, Boudewijn Bauer
  147. Sitz der Gesellschaft / Registered Office: Hof
  148. Registergericht / Commercial Register of the Local Court: HRB 0000 AG
  149. Hof",
  150. },
  151. },
  152. {
  153. :data => IO.read('test/fixtures/mail9.box'),
  154. :body_md5 => 'd2f0a663dd0e371e9f0c3d5688441a6f',
  155. :attachments => [
  156. {
  157. :md5 => '9964263c167ab47f8ec59c48e57cb905',
  158. :filename => 'message.html',
  159. },
  160. {
  161. :md5 => 'ddbdf67aa2f5c60c294008a54d57082b',
  162. :filename => 'super-seven.jpg',
  163. },
  164. ],
  165. :params => {
  166. :from => 'Martin Edenhofer <martin@example.de>',
  167. :from_email => 'martin@example.de',
  168. :from_display_name => 'Martin Edenhofer',
  169. :subject => 'Attchment Test',
  170. :plain_part => "Enjoy!\n\n-Martin\n\n--\nOld programmers never die. They just branch to a new address."
  171. },
  172. },
  173. ]
  174. files.each { |file|
  175. parser = Channel::EmailParser.new
  176. data = parser.parse( file[:data] )
  177. # check body
  178. md5 = Digest::MD5.hexdigest( data[:plain_part] )
  179. assert_equal( file[:body_md5], md5 )
  180. # check params
  181. file[:params].each { |key, value|
  182. if key.to_s == 'plain_part_md5'
  183. # puts 'md5'
  184. # puts '++' + data[:plain_part].to_s + '++'
  185. # puts '++' + file[:params][key.to_sym].to_s + '++'
  186. assert_equal( Digest::MD5.hexdigest( file[:params][key.to_sym].to_s ), Digest::MD5.hexdigest( data[:plain_part].to_s ) )
  187. else
  188. assert_equal( file[:params][key.to_sym], data[key.to_sym] )
  189. end
  190. }
  191. # check attachments
  192. if file[:attachments]
  193. attachment_count_config = file[:attachments].length
  194. attachment_count_email = 0
  195. file[:attachments].each { |attachment|
  196. attachment_count_email += 1
  197. found = false
  198. data[:attachments].each { |attachment_parser|
  199. next if found
  200. file_md5 = Digest::MD5.hexdigest( attachment_parser[:data] )
  201. if attachment[:md5] == file_md5
  202. found = true
  203. assert_equal( attachment[:filename], attachment_parser[:filename] )
  204. end
  205. }
  206. if !found
  207. assert( false, "Attachment not found! MD5: #{attachment[:md5]} - #{attachment[:filename].to_s}" )
  208. end
  209. }
  210. assert_equal( attachment_count_config, attachment_count_email )
  211. end
  212. }
  213. end
  214. end