email_signatur_detection_test.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class EmailSignaturDetectionTest < ActiveSupport::TestCase
  4. test 'test case 1 - sender a' do
  5. # fixtures of sender a
  6. fixture_files = {
  7. 'email_signature_detection/client_a_1.txt' => { line: 10, content_type: 'text/plain' },
  8. 'email_signature_detection/client_a_2.txt' => { line: 20, content_type: 'text/plain' },
  9. 'email_signature_detection/client_a_3.txt' => { line: 6, content_type: 'text/plain' },
  10. }
  11. fixture_messages = []
  12. fixture_files.each do |filepath, value|
  13. value[:content] = File.new("#{Rails.root}/test/fixtures/#{filepath}", 'r').read
  14. fixture_messages.push value
  15. end
  16. signature = SignatureDetection.find_signature(fixture_messages)
  17. expected_signature = "\nMit freundlichen Grüßen\n\nBob Smith\nBerechtigungen und dez. Department\n________________________________\n\nMusik AG\nBerechtigungen und dez. Department (ITPBM)\nKastanien 2"
  18. assert_equal(expected_signature, signature)
  19. fixture_files.each do |_filepath, value|
  20. assert_equal(value[:line], SignatureDetection.find_signature_line(signature, value[:content], value[:content_type]))
  21. end
  22. end
  23. test 'test case 2 - sender b' do
  24. fixture_files = {
  25. 'email_signature_detection/client_b_1.txt' => { line: 26, content_type: 'text/plain' },
  26. 'email_signature_detection/client_b_2.txt' => { line: 4, content_type: 'text/plain' },
  27. 'email_signature_detection/client_b_3.txt' => { line: 6, content_type: 'text/plain' },
  28. }
  29. fixture_messages = []
  30. fixture_files.each do |filepath, value|
  31. value[:content] = File.new("#{Rails.root}/test/fixtures/#{filepath}", 'r').read
  32. fixture_messages.push value
  33. end
  34. signature = SignatureDetection.find_signature(fixture_messages)
  35. expected_signature = "\nFreundliche Grüße\n\nGünter Lässig\nLokale Daten\n\nMusic GmbH\nBaustraße 123, 12345 Max City\nTelefon 0123 5432114\nTelefax 0123 5432139"
  36. assert_equal(expected_signature, signature)
  37. fixture_files.each do |_filepath, value|
  38. assert_equal(value[:line], SignatureDetection.find_signature_line(signature, value[:content], value[:content_type]))
  39. end
  40. end
  41. test 'test case 3 - just tests' do
  42. signature = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nBob Smith\nABC Organisation\n\nEXAMPLE IT-Service GmbH\nDorten 5 F&E\n12345 Da / Germany\nPhone: +49 (0) 1234 567 890 / +49 (0) 1234 567 891\nFax:     +49 (0) 1234 567 892"
  43. message = File.new("#{Rails.root}/test/fixtures/email_signature_detection/example1.html", 'r').read
  44. signature_line = SignatureDetection.find_signature_line(signature, message, 'text/html')
  45. assert_equal(11, signature_line)
  46. end
  47. test 'test case 4 - sender c' do
  48. fixture_files = {
  49. 'email_signature_detection/client_c_1.html' => { line: 8, content_type: 'text/html' },
  50. 'email_signature_detection/client_c_2.html' => { line: 29, content_type: 'text/html' },
  51. 'email_signature_detection/client_c_3.html' => { line: 9, content_type: 'text/html' },
  52. }
  53. fixture_messages = []
  54. fixture_files.each do |filepath, value|
  55. value[:content] = File.new("#{Rails.root}/test/fixtures/#{filepath}", 'r').read
  56. fixture_messages.push value
  57. end
  58. signature = SignatureDetection.find_signature(fixture_messages)
  59. expected_signature = "\nChristianSmith\nTechnik\n\nTel: +49 12 34 56 78 441\nFax: +49 12 34 56 78 499\nEmail: Christian.Smith@example.com\nWeb: www.example.com\nABC KFZ- und Flugzeug B.V. & Co. KG\nHauptverwaltung"
  60. assert_equal(expected_signature, signature)
  61. fixture_files.each do |filepath, value|
  62. assert_equal(value[:line], SignatureDetection.find_signature_line(signature, value[:content], value[:content_type]), filepath)
  63. end
  64. end
  65. test 'test case III - sender a - full cycle' do
  66. raw_email_header = "From: Bob.Smith@music.com\nTo: test@zammad.org\nSubject: test\n\n"
  67. # process email I
  68. file = File.open("#{Rails.root}/test/fixtures/email_signature_detection/client_a_1.txt", 'rb')
  69. raw_email = raw_email_header + file.read
  70. ticket1, article1, user1, mail = Channel::EmailParser.new.process({}, raw_email)
  71. assert(ticket1)
  72. assert(article1)
  73. Scheduler.worker(true)
  74. # process email II
  75. file = File.open("#{Rails.root}/test/fixtures/email_signature_detection/client_a_2.txt", 'rb')
  76. raw_email = raw_email_header + file.read
  77. ticket2, article2, user2, mail = Channel::EmailParser.new.process({}, raw_email)
  78. assert(ticket2)
  79. assert(article2)
  80. Scheduler.worker(true)
  81. # check if user2 has a signature_detection value
  82. user2 = User.find(user2.id)
  83. assert(user2.preferences[:signature_detection])
  84. # process email III
  85. file = File.open("#{Rails.root}/test/fixtures/email_signature_detection/client_a_3.txt", 'rb')
  86. raw_email = raw_email_header + file.read
  87. ticket3, article3, user3, mail = Channel::EmailParser.new.process({}, raw_email)
  88. assert(ticket3)
  89. assert(article3)
  90. Scheduler.worker(true)
  91. # check if article3 has a signature_detection value
  92. article3 = Ticket::Article.find(article3.id)
  93. assert_equal(article3.preferences[:signature_detection], 6)
  94. # relbuild all
  95. SignatureDetection.rebuild_all_articles
  96. article1 = Ticket::Article.find(article1.id)
  97. assert_equal(article1.preferences[:signature_detection], 10)
  98. article2 = Ticket::Article.find(article2.id)
  99. assert_equal(article2.preferences[:signature_detection], 20)
  100. article3 = Ticket::Article.find(article3.id)
  101. assert_equal(article3.preferences[:signature_detection], 6)
  102. end
  103. end