email_parser_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. require 'rails_helper'
  2. RSpec.describe Channel::EmailParser, type: :model do
  3. let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail001.box') }
  4. let(:raw_mail) { File.read(mail_file) }
  5. describe '#process' do
  6. let(:raw_mail) { File.read(mail_file).sub(/(?<=^Subject: ).*$/, test_string) }
  7. let(:test_string) do
  8. Setting.get('ticket_hook') + Setting.get('ticket_hook_divider') + ticket.number
  9. end
  10. let(:ticket) { create(:ticket) }
  11. context 'when email subject contains ticket reference' do
  12. it 'adds message to ticket' do
  13. expect { described_class.new.process({}, raw_mail) }
  14. .to change { ticket.articles.length }
  15. end
  16. end
  17. context 'when configured to search body' do
  18. before { Setting.set('postmaster_follow_up_search_in', 'body') }
  19. context 'when body contains ticket reference' do
  20. context 'in visible text' do
  21. let(:raw_mail) { File.read(mail_file).sub(/Hallo =\nMartin,(?=<o:p>)/, test_string) }
  22. it 'adds message to ticket' do
  23. expect { described_class.new.process({}, raw_mail) }
  24. .to change { ticket.articles.length }
  25. end
  26. end
  27. context 'as part of a larger word' do
  28. let(:raw_mail) { File.read(mail_file).sub(/(?<=Hallo) =\n(?=Martin,<o:p>)/, test_string) }
  29. it 'creates a separate ticket' do
  30. expect { described_class.new.process({}, raw_mail) }
  31. .not_to change { ticket.articles.length }
  32. end
  33. end
  34. context 'in html attributes' do
  35. let(:raw_mail) { File.read(mail_file).sub(%r{<a href.*?/a>}m, %(<table bgcolor="#{test_string}"> </table>)) }
  36. it 'creates a separate ticket' do
  37. expect { described_class.new.process({}, raw_mail) }
  38. .not_to change { ticket.articles.length }
  39. end
  40. end
  41. end
  42. end
  43. # see https://github.com/zammad/zammad/issues/2198
  44. context 'when sender address contains spaces (#2198)' do
  45. let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail071.box') }
  46. let(:sender_email) { 'powerquadrantsystem@example.com' }
  47. it 'removes them before creating a new user' do
  48. expect { described_class.new.process({}, raw_mail) }
  49. .to change { User.where(email: sender_email).count }.to(1)
  50. end
  51. it 'marks new user email as invalid' do
  52. described_class.new.process({}, raw_mail)
  53. expect(User.find_by(email: sender_email).preferences)
  54. .to include('mail_delivery_failed' => true)
  55. .and include('mail_delivery_failed_reason' => 'invalid email')
  56. .and include('mail_delivery_failed_data' => a_kind_of(ActiveSupport::TimeWithZone))
  57. end
  58. end
  59. # see https://github.com/zammad/zammad/issues/2254
  60. context 'when sender address contains > (#2254)' do
  61. let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail076.box') }
  62. let(:sender_email) { 'millionslotteryspaintransfer@example.com' }
  63. it 'removes them before creating a new user' do
  64. expect { described_class.new.process({}, raw_mail) }
  65. .to change { User.where(email: sender_email).count }.to(1)
  66. end
  67. it 'marks new user email as invalid' do
  68. described_class.new.process({}, raw_mail)
  69. expect(User.find_by(email: sender_email).preferences)
  70. .to include('mail_delivery_failed' => true)
  71. .and include('mail_delivery_failed_reason' => 'invalid email')
  72. .and include('mail_delivery_failed_data' => a_kind_of(ActiveSupport::TimeWithZone))
  73. end
  74. end
  75. # see https://github.com/zammad/zammad/issues/2224
  76. context 'when header specifies Windows-1258 charset (#2224)' do
  77. let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail072.box') }
  78. it 'does not raise Encoding::ConverterNotFoundError' do
  79. expect { described_class.new.process({}, raw_mail) }
  80. .not_to raise_error
  81. end
  82. end
  83. end
  84. end