2018-06-19 03:53:00 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Channel::EmailParser, type: :model do
|
2018-12-05 05:31:13 +00:00
|
|
|
let(:subject) { described_class.new }
|
2018-06-19 03:53:00 +00:00
|
|
|
let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail001.box') }
|
2018-12-05 05:31:13 +00:00
|
|
|
let(:raw_mail) { File.read(mail_file) }
|
2018-06-19 03:53:00 +00:00
|
|
|
|
|
|
|
describe '#process' do
|
2018-12-05 05:31:13 +00:00
|
|
|
let(:raw_mail) { File.read(mail_file).sub(/(?<=^Subject: ).*$/, test_string) }
|
|
|
|
let(:test_string) { Setting.get('ticket_hook') + Setting.get('ticket_hook_divider') + ticket.number }
|
|
|
|
let(:ticket) { create(:ticket) }
|
|
|
|
|
|
|
|
context 'for creating new users' do
|
|
|
|
context 'with one unrecognized email address' do
|
|
|
|
let(:raw_mail) { <<~RAW }
|
|
|
|
From: #{Faker::Internet.unique.email}
|
|
|
|
To: #{User.pluck(:email).reject(&:blank?).sample}
|
|
|
|
Subject: Foo bar
|
|
|
|
|
|
|
|
Lorem ipsum dolor
|
|
|
|
RAW
|
|
|
|
|
|
|
|
it 'creates one new user' do
|
|
|
|
expect { Channel::EmailParser.new.process({}, raw_mail) }
|
|
|
|
.to change { User.count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a large number (42+) of unrecognized email addresses' do
|
|
|
|
let(:raw_mail) { <<~RAW }
|
|
|
|
From: #{Faker::Internet.unique.email}
|
|
|
|
To: #{Array.new(22) { Faker::Internet.unique.email }.join(', ')}
|
|
|
|
Cc: #{Array.new(22) { Faker::Internet.unique.email }.join(', ')}
|
|
|
|
Subject: test max sender identify
|
|
|
|
|
|
|
|
Some Text
|
|
|
|
RAW
|
2018-08-10 07:19:37 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
it 'never creates more than 41 users per email' do
|
|
|
|
expect { Channel::EmailParser.new.process({}, raw_mail) }
|
|
|
|
.to change { User.count }.by(41)
|
|
|
|
end
|
2018-06-19 03:53:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
context 'for associating emails to tickets' do
|
|
|
|
context 'when email subject contains ticket reference' do
|
|
|
|
it 'adds message to ticket' do
|
|
|
|
expect { described_class.new.process({}, raw_mail) }
|
|
|
|
.to change { ticket.articles.length }
|
|
|
|
end
|
|
|
|
end
|
2018-06-19 03:53:00 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
context 'when configured to search body' do
|
|
|
|
before { Setting.set('postmaster_follow_up_search_in', 'body') }
|
2018-06-19 03:53:00 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
context 'when body contains ticket reference' do
|
|
|
|
context 'in visible text' do
|
|
|
|
let(:raw_mail) { File.read(mail_file).sub(/Hallo =\nMartin,(?=<o:p>)/, test_string) }
|
|
|
|
|
|
|
|
it 'adds message to ticket' do
|
|
|
|
expect { described_class.new.process({}, raw_mail) }
|
|
|
|
.to change { ticket.articles.length }
|
|
|
|
end
|
2018-06-19 03:53:00 +00:00
|
|
|
end
|
2018-06-19 04:41:18 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
context 'as part of a larger word' do
|
|
|
|
let(:raw_mail) { File.read(mail_file).sub(/(?<=Hallo) =\n(?=Martin,<o:p>)/, test_string) }
|
2018-06-19 04:41:18 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
it 'creates a separate ticket' do
|
|
|
|
expect { described_class.new.process({}, raw_mail) }
|
|
|
|
.not_to change { ticket.articles.length }
|
|
|
|
end
|
2018-06-19 04:41:18 +00:00
|
|
|
end
|
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
context 'in html attributes' do
|
|
|
|
let(:raw_mail) { File.read(mail_file).sub(%r{<a href.*?/a>}m, %(<table bgcolor="#{test_string}"> </table>)) }
|
2018-06-19 04:41:18 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
it 'creates a separate ticket' do
|
|
|
|
expect { described_class.new.process({}, raw_mail) }
|
|
|
|
.not_to change { ticket.articles.length }
|
|
|
|
end
|
2018-06-19 04:41:18 +00:00
|
|
|
end
|
|
|
|
end
|
2018-06-19 03:53:00 +00:00
|
|
|
end
|
|
|
|
end
|
2018-08-30 08:39:50 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
context 'for sender/recipient address formatting' do
|
|
|
|
# see https://github.com/zammad/zammad/issues/2198
|
|
|
|
context 'when sender address contains spaces (#2198)' do
|
|
|
|
let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail071.box') }
|
|
|
|
let(:sender_email) { 'powerquadrantsystem@example.com' }
|
2018-08-30 08:39:50 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
it 'removes them before creating a new user' do
|
|
|
|
expect { described_class.new.process({}, raw_mail) }
|
|
|
|
.to change { User.where(email: sender_email).count }.to(1)
|
|
|
|
end
|
2018-08-30 08:39:50 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
it 'marks new user email as invalid' do
|
|
|
|
described_class.new.process({}, raw_mail)
|
2018-08-30 08:39:50 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
expect(User.find_by(email: sender_email).preferences)
|
|
|
|
.to include('mail_delivery_failed' => true)
|
|
|
|
.and include('mail_delivery_failed_reason' => 'invalid email')
|
|
|
|
.and include('mail_delivery_failed_data' => a_kind_of(ActiveSupport::TimeWithZone))
|
|
|
|
end
|
2018-08-30 08:39:50 +00:00
|
|
|
end
|
2018-09-05 07:36:44 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
# see https://github.com/zammad/zammad/issues/2254
|
|
|
|
context 'when sender address contains > (#2254)' do
|
|
|
|
let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail076.box') }
|
|
|
|
let(:sender_email) { 'millionslotteryspaintransfer@example.com' }
|
2018-09-24 18:16:28 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
it 'removes them before creating a new user' do
|
|
|
|
expect { described_class.new.process({}, raw_mail) }
|
|
|
|
.to change { User.where(email: sender_email).count }.to(1)
|
|
|
|
end
|
2018-09-24 18:16:28 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
it 'marks new user email as invalid' do
|
|
|
|
described_class.new.process({}, raw_mail)
|
2018-09-24 18:16:28 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
expect(User.find_by(email: sender_email).preferences)
|
|
|
|
.to include('mail_delivery_failed' => true)
|
|
|
|
.and include('mail_delivery_failed_reason' => 'invalid email')
|
|
|
|
.and include('mail_delivery_failed_data' => a_kind_of(ActiveSupport::TimeWithZone))
|
|
|
|
end
|
2018-09-24 18:16:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
context 'for charset handling' do
|
|
|
|
# see https://github.com/zammad/zammad/issues/2224
|
|
|
|
context 'when header specifies Windows-1258 charset (#2224)' do
|
|
|
|
let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail072.box') }
|
2018-09-05 07:36:44 +00:00
|
|
|
|
2018-12-05 05:31:13 +00:00
|
|
|
it 'does not raise Encoding::ConverterNotFoundError' do
|
|
|
|
expect { described_class.new.process({}, raw_mail) }
|
|
|
|
.not_to raise_error
|
|
|
|
end
|
2018-09-05 07:36:44 +00:00
|
|
|
end
|
|
|
|
end
|
2018-12-19 02:54:03 +00:00
|
|
|
|
|
|
|
context 'mail with links' do
|
|
|
|
|
|
|
|
def mock_mail(number_of_links)
|
|
|
|
link = '<a href="https://zammad.com/">Dummy Link</a> '
|
|
|
|
|
|
|
|
mail = Mail.new
|
|
|
|
mail.html_part = "<html><body>#{link * number_of_links}</body></html>"
|
|
|
|
mail
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:mail_10) { mock_mail(10).to_s }
|
|
|
|
let(:mail_5k) { mock_mail(5001).to_s }
|
|
|
|
|
|
|
|
# regression test for issue 2390 - Add a postmaster filter to not show emails with potential issue
|
|
|
|
it '(>5k links) are replaced by a warning message' do
|
|
|
|
expect( described_class.new.parse(mail_5k)[:body] )
|
|
|
|
.to eql( Channel::EmailParser::EXCESSIVE_LINKS_MSG )
|
|
|
|
end
|
|
|
|
|
|
|
|
it '(10 links) are not touched' do
|
|
|
|
expect( described_class.new.parse(mail_10)[:body] )
|
|
|
|
.to start_with( '<a href="https://zammad.com/"' )
|
|
|
|
end
|
|
|
|
end
|
2019-01-30 06:12:12 +00:00
|
|
|
|
|
|
|
context 'Mail::Encodings.value_decode' do
|
|
|
|
it 'decode us-ascii encoded strings' do
|
|
|
|
expect( Mail::Encodings.value_decode('=?us-ascii?Q?Test?=') ).to eql( 'Test' )
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'decode utf-8 encoded strings' do
|
2019-01-30 15:58:48 +00:00
|
|
|
expect( Mail::Encodings.value_decode('=?UTF-8?Q? Personal=C3=A4nderung?=') ).to eql( ' Personaländerung' )
|
2019-01-30 06:12:12 +00:00
|
|
|
end
|
|
|
|
end
|
2019-02-20 11:07:09 +00:00
|
|
|
|
|
|
|
context 'when handling Content-Transfer-Encoding of attachments' do
|
|
|
|
|
|
|
|
context 'with x-uuencode' do
|
|
|
|
|
|
|
|
let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail078-content_transfer_encoding_x_uuencode.box') }
|
|
|
|
|
|
|
|
it 'does not raise RuntimeError' do
|
|
|
|
expect { described_class.new.process({}, raw_mail) }
|
|
|
|
.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses the content correctly' do
|
|
|
|
_ticket, article, _user, _mail = described_class.new.process({}, raw_mail)
|
|
|
|
expect(article.attachments.first.filename).to eq('PGP_Cmts_on_12-14-01_Pkg.txt')
|
|
|
|
expect(article.attachments.first.content).to eq('Hello Zammad')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-19 03:53:00 +00:00
|
|
|
end
|
|
|
|
end
|