email_parser_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 => 'd62c8530fc8fd35bcff0b599db95a170',
  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. :body_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. :body_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. :body => "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. :body_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. :body_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 => 'AW: OTRS / Anfrage OTRS Einführung/Präsentation [Ticket#11545]',
  170. :body => "Enjoy!\n\n-Martin\n\n--\nOld programmers never die. They just branch to a new address."
  171. },
  172. },
  173. {
  174. :data => IO.read('test/fixtures/mail10.box'),
  175. :body_md5 => '8d6ea42f7f36a790e67f50e3a5f27063',
  176. :attachments => [
  177. {
  178. :md5 => '08b0c83fd155db23f22bed845715225d',
  179. :filename => 'message.html',
  180. },
  181. {
  182. :md5 => 'a618d671348735744d4c9a4005b56799',
  183. :filename => 'image001.jpg',
  184. },
  185. ],
  186. :params => {
  187. :from => 'Smith Sepp <smith@example.com>',
  188. :from_email => 'smith@example.com',
  189. :from_display_name => 'Smith Sepp',
  190. :subject => 'Gruß aus Oberalteich',
  191. # :body => "Herzliche Grüße aus Oberalteich sendet Herrn Smith\n\n \n\nSepp Smith - Dipl.Ing. agr. (FH)\n\nGeschäftsführer der example Straubing-Bogen\n\nKlosterhof 1 | 94327 Bogen-Oberalteich\n\nTel: 09422-505601 | Fax: 09422-505620\n\nInternet: http://example-straubing-bogen.de <http://example-straubing-bogen.de/> \n\nFacebook: http://facebook.de/examplesrbog <http://facebook.de/examplesrbog> \n\n - European Foundation für Quality Management\n\n"
  192. },
  193. },
  194. {
  195. :data => IO.read('test/fixtures/mail11.box'),
  196. :body_md5 => 'cf8b26d9fc4ce9abb19a36ce3a130c79',
  197. :attachments => [
  198. {
  199. :md5 => '08660cd33ce8c64b95bcf0207ff6c4d6',
  200. :filename => 'message.html',
  201. },
  202. ],
  203. :params => {
  204. :from => 'CYLEX Newsletter <carina.merkant@cylex.de>',
  205. :from_email => 'carina.merkant@cylex.de',
  206. :from_display_name => 'CYLEX Newsletter',
  207. :subject => 'Eine schöne Adventszeit für ZNUNY GMBH - ENTERPRISE SERVICES FÜR OTRS',
  208. :to => 'enjoy_us@znuny.com',
  209. },
  210. },
  211. {
  212. :data => IO.read('test/fixtures/mail12.box'),
  213. :body_md5 => 'c89a6ba15143aa23c090bf5fe5cd39dd',
  214. :attachments => [
  215. {
  216. :md5 => 'a928e4665c3e59ea27d57d67ef15ecc5',
  217. :filename => 'message.html',
  218. },
  219. {
  220. :md5 => 'b6e70f587c4b1810facbb20bb5ec69ef',
  221. :filename => 'image002.png',
  222. },
  223. ],
  224. :params => {
  225. :from => 'Alex.Smith@example.com',
  226. :from_email => 'Alex.Smith@example.com',
  227. :from_display_name => nil,
  228. :subject => 'AW: Agenda [Ticket#11995]',
  229. :to => 'example@znuny.com',
  230. },
  231. },
  232. {
  233. :data => IO.read('test/fixtures/mail13.box'),
  234. :body_md5 => '58806e006b14b04a535784a5462d09b0',
  235. :attachments => [
  236. {
  237. :md5 => '29cc1679f8a44c72be6be7c1da4278ac',
  238. :filename => 'message.html',
  239. },
  240. ],
  241. :params => {
  242. :from => 'thomas.smith@example.com',
  243. :from_email => 'thomas.smith@example.com',
  244. :from_display_name => nil,
  245. :subject => 'Antwort: Probleme ADB / Anlegen von Tickets [Ticket#111079]',
  246. :to => 'q1@znuny.com',
  247. },
  248. },
  249. ]
  250. files.each { |file|
  251. parser = Channel::EmailParser.new
  252. data = parser.parse( file[:data] )
  253. # check body
  254. md5 = Digest::MD5.hexdigest( data[:body] )
  255. assert_equal( file[:body_md5], md5 )
  256. # check params
  257. file[:params].each { |key, value|
  258. if key.to_s == 'body_md5'
  259. # puts 'md5'
  260. # puts '++' + data[:body].to_s + '++'
  261. # puts '++' + file[:params][key.to_sym].to_s + '++'
  262. assert_equal( Digest::MD5.hexdigest( file[:params][key.to_sym].to_s ), Digest::MD5.hexdigest( data[:body].to_s ) )
  263. else
  264. assert_equal( file[:params][key.to_sym], data[key.to_sym] )
  265. end
  266. }
  267. # check attachments
  268. if file[:attachments]
  269. attachment_count_config = file[:attachments].length
  270. attachment_count_email = 0
  271. file[:attachments].each { |attachment|
  272. attachment_count_email += 1
  273. found = false
  274. data[:attachments].each { |attachment_parser|
  275. next if found
  276. file_md5 = Digest::MD5.hexdigest( attachment_parser[:data] )
  277. # puts 'Attachment:' + attachment_parser.inspect + '-' + file_md5
  278. if attachment[:md5] == file_md5
  279. found = true
  280. assert_equal( attachment[:filename], attachment_parser[:filename] )
  281. end
  282. }
  283. if !found
  284. assert( false, "Attachment not found! MD5: #{attachment[:md5]} - #{attachment[:filename].to_s}" )
  285. end
  286. }
  287. assert_equal( attachment_count_config, attachment_count_email )
  288. end
  289. }
  290. end
  291. end