email_signature_detection_test.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. require 'test_helper'
  2. class EmailSignatureDetectionTest < ActiveSupport::TestCase
  3. test 'test case 1 - sender a' do
  4. message_files = [Rails.root.join('test', 'data', 'email_signature_detection', 'client_a_1.txt'),
  5. Rails.root.join('test', 'data', 'email_signature_detection', 'client_a_2.txt'),
  6. Rails.root.join('test', 'data', 'email_signature_detection', 'client_a_3.txt')]
  7. signature_lines = [10, 20, 6]
  8. messages = message_files.zip(signature_lines).map do |f, l|
  9. { content: File.read(Rails.root.join('test', 'data', f)),
  10. content_type: 'text/plain',
  11. line: l }
  12. end
  13. signature = SignatureDetection.find_signature(messages)
  14. expected_signature = "\nMit freundlichen Grüßen\n\nBob Smith\nBerechtigungen und dez. Department\n________________________________\n\nMusik AG\nBerechtigungen und dez. Department (ITPBM)\nKastanien 2"
  15. assert_equal(expected_signature, signature)
  16. messages.each do |m|
  17. assert_equal(m[:line], SignatureDetection.find_signature_line(signature, m[:content], m[:content_type]))
  18. end
  19. end
  20. test 'test case 2 - sender b' do
  21. message_files = [Rails.root.join('test', 'data', 'email_signature_detection', 'client_b_1.txt'),
  22. Rails.root.join('test', 'data', 'email_signature_detection', 'client_b_2.txt'),
  23. Rails.root.join('test', 'data', 'email_signature_detection', 'client_b_3.txt')]
  24. signature_lines = [26, 4, 6]
  25. messages = message_files.zip(signature_lines).map do |f, l|
  26. { content: File.read(Rails.root.join('test', 'data', f)),
  27. content_type: 'text/plain',
  28. line: l }
  29. end
  30. signature = SignatureDetection.find_signature(messages)
  31. 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"
  32. assert_equal(expected_signature, signature)
  33. messages.each do |m|
  34. assert_equal(m[:line], SignatureDetection.find_signature_line(signature, m[:content], m[:content_type]))
  35. end
  36. end
  37. test 'test case 3 - just tests' do
  38. 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"
  39. message = File.read(Rails.root.join('test', 'data', 'email_signature_detection', 'example1.html'))
  40. signature_line = SignatureDetection.find_signature_line(signature, message, 'text/html')
  41. assert_equal(11, signature_line)
  42. end
  43. test 'test case 4 - sender c' do
  44. message_files = [Rails.root.join('test', 'data', 'email_signature_detection', 'client_c_1.html'),
  45. Rails.root.join('test', 'data', 'email_signature_detection', 'client_c_2.html'),
  46. Rails.root.join('test', 'data', 'email_signature_detection', 'client_c_3.html')]
  47. signature_lines = [8, 29, 6]
  48. messages = message_files.zip(signature_lines).map do |f, l|
  49. { content: File.read(Rails.root.join('test', 'data', f)),
  50. content_type: 'text/html',
  51. line: l }
  52. end
  53. signature = SignatureDetection.find_signature(messages)
  54. 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"
  55. assert_equal(expected_signature, signature)
  56. messages.each do |m|
  57. assert_equal(m[:line], SignatureDetection.find_signature_line(signature, m[:content], m[:content_type]))
  58. end
  59. end
  60. test 'test case III - sender a - full cycle' do
  61. header = "From: Bob.Smith@music.com\nTo: test@zammad.org\nSubject: test\n\n"
  62. # process email I
  63. body = File.binread(Rails.root.join('test', 'data', 'email_signature_detection', 'client_a_1.txt'))
  64. raw_email = header + body
  65. ticket1, article1, user1, mail = Channel::EmailParser.new.process({}, raw_email)
  66. assert(ticket1)
  67. assert(article1)
  68. Scheduler.worker(true)
  69. # process email II
  70. body = File.binread(Rails.root.join('test', 'data', 'email_signature_detection', 'client_a_2.txt'))
  71. raw_email = header + body
  72. ticket2, article2, user2, mail = Channel::EmailParser.new.process({}, raw_email)
  73. assert(ticket2)
  74. assert(article2)
  75. Scheduler.worker(true)
  76. # check if user2 has a signature_detection value
  77. user2 = User.find(user2.id)
  78. assert(user2.preferences[:signature_detection])
  79. # process email III
  80. body = File.binread(Rails.root.join('test', 'data', 'email_signature_detection', 'client_a_3.txt'))
  81. raw_email = header + body
  82. ticket3, article3, user3, mail = Channel::EmailParser.new.process({}, raw_email)
  83. assert(ticket3)
  84. assert(article3)
  85. Scheduler.worker(true)
  86. # check if article3 has a signature_detection value
  87. article3 = Ticket::Article.find(article3.id)
  88. assert_equal(article3.preferences[:signature_detection], 6)
  89. # relbuild all
  90. SignatureDetection.rebuild_all_articles
  91. article1 = Ticket::Article.find(article1.id)
  92. assert_equal(article1.preferences[:signature_detection], 10)
  93. article2 = Ticket::Article.find(article2.id)
  94. assert_equal(article2.preferences[:signature_detection], 20)
  95. article3 = Ticket::Article.find(article3.id)
  96. assert_equal(article3.preferences[:signature_detection], 6)
  97. end
  98. end