2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'spec_helper'
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Email::Receiver do
|
2023-03-04 22:38:38 +05:30
|
|
|
include_context 'email shared context'
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:metric_transaction) { instance_double(Gitlab::Metrics::WebTransaction) }
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
shared_examples 'successful receive' do
|
|
|
|
let(:handler) { double(:handler, project: project, execute: true, metrics_event: nil, metrics_params: nil) }
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:client_id) { 'email/jake@example.com' }
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
it 'correctly finds the mail key' do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(Gitlab::Email::Handler).to receive(:for).with(an_instance_of(Mail::Message), 'gitlabhq/gitlabhq+auth_token').and_return(handler)
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
receiver.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds metric event' do
|
|
|
|
allow(receiver).to receive(:handler).and_return(handler)
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(::Gitlab::Metrics::BackgroundTransaction).to receive(:current).and_return(metric_transaction)
|
|
|
|
expect(metric_transaction).to receive(:add_event).with(handler.metrics_event, handler.metrics_params)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
receiver.execute
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
it 'returns valid metadata' do
|
|
|
|
allow(receiver).to receive(:handler).and_return(handler)
|
|
|
|
|
|
|
|
metadata = receiver.mail_metadata
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
expect(metadata.keys).to match_array(%i(mail_uid from_address to_address mail_key references delivered_to envelope_to x_envelope_to meta received_recipients))
|
2022-07-23 23:45:48 +05:30
|
|
|
expect(metadata[:meta]).to include(client_id: client_id, project: project.full_path)
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(metadata[meta_key]).to eq(meta_value)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
shared_examples 'failed receive with event' do
|
2022-05-07 20:08:51 +05:30
|
|
|
it 'adds metric event' do
|
|
|
|
expect(::Gitlab::Metrics::BackgroundTransaction).to receive(:current).and_return(metric_transaction)
|
|
|
|
expect(metric_transaction).to receive(:add_event).with('email_receiver_error', { error: expected_error.name })
|
|
|
|
|
|
|
|
expect { receiver.execute }.to raise_error(expected_error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
shared_examples 'failed receive without event' do
|
|
|
|
it 'adds metric event' do
|
|
|
|
expect(::Gitlab::Metrics::BackgroundTransaction).not_to receive(:current)
|
|
|
|
|
|
|
|
expect { receiver.execute }.to raise_error(expected_error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'when the email contains a valid email address in a header' do
|
2017-08-17 22:00:37 +05:30
|
|
|
before do
|
2020-03-13 15:44:24 +05:30
|
|
|
stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.example.com")
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'when in a Delivered-To header' do
|
|
|
|
let(:email_raw) { fixture_file('emails/forwarded_new_issue.eml') }
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:meta_key) { :delivered_to }
|
|
|
|
let(:meta_value) { ["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com", "support@example.com"] }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
it_behaves_like 'successful receive'
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in an Envelope-To header' do
|
|
|
|
let(:email_raw) { fixture_file('emails/envelope_to_header.eml') }
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:meta_key) { :envelope_to }
|
|
|
|
let(:meta_value) { ["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"] }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
it_behaves_like 'successful receive'
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'when in an X-Envelope-To header' do
|
|
|
|
let(:email_raw) { fixture_file('emails/x_envelope_to_header.eml') }
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:meta_key) { :x_envelope_to }
|
|
|
|
let(:meta_value) { ["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"] }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
it_behaves_like 'successful receive'
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'when enclosed with angle brackets in an Envelope-To header' do
|
|
|
|
let(:email_raw) { fixture_file('emails/envelope_to_header_with_angle_brackets.eml') }
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:meta_key) { :envelope_to }
|
|
|
|
let(:meta_value) { ["<incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com>"] }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
it_behaves_like 'successful receive'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
context 'when all other headers are missing' do
|
|
|
|
let(:email_raw) { fixture_file('emails/missing_delivered_to_header.eml') }
|
|
|
|
let(:meta_key) { :received_recipients }
|
|
|
|
let(:meta_value) { ['incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com', 'incoming+gitlabhq/gitlabhq@example.com'] }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
describe 'it uses receive headers to find the key' do
|
2022-05-07 20:08:51 +05:30
|
|
|
it_behaves_like 'successful receive'
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'when we cannot find a capable handler' do
|
|
|
|
let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, '!!!') }
|
|
|
|
let(:expected_error) { Gitlab::Email::UnknownIncomingEmail }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it_behaves_like 'failed receive with event'
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'when the email is blank' do
|
|
|
|
let(:email_raw) { '' }
|
|
|
|
let(:expected_error) { Gitlab::Email::EmptyEmailError }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it_behaves_like 'failed receive without event'
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'when the email was auto generated with Auto-Submitted header' do
|
|
|
|
let(:email_raw) { fixture_file('emails/auto_submitted.eml') }
|
|
|
|
let(:expected_error) { Gitlab::Email::AutoGeneratedEmailError }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it_behaves_like 'failed receive without event'
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
context "when the email's To field is blank" do
|
|
|
|
before do
|
|
|
|
stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.example.com")
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:email_raw) do
|
|
|
|
<<~EMAIL
|
|
|
|
Delivered-To: incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com
|
|
|
|
From: "jake@example.com" <jake@example.com>
|
|
|
|
Bcc: "support@example.com" <support@example.com>
|
|
|
|
|
|
|
|
Email content
|
|
|
|
EMAIL
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:meta_key) { :delivered_to }
|
|
|
|
let(:meta_value) { ["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"] }
|
|
|
|
|
|
|
|
it_behaves_like 'successful receive'
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the email's From field is blank" do
|
|
|
|
before do
|
|
|
|
stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.example.com")
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:email_raw) do
|
|
|
|
<<~EMAIL
|
|
|
|
Delivered-To: incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com
|
|
|
|
To: "support@example.com" <support@example.com>
|
|
|
|
|
|
|
|
Email content
|
|
|
|
EMAIL
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:meta_key) { :delivered_to }
|
|
|
|
let(:meta_value) { ["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"] }
|
|
|
|
|
|
|
|
it_behaves_like 'successful receive' do
|
|
|
|
let(:client_id) { 'email/' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'when the email was auto generated with X-Autoreply header' do
|
|
|
|
let(:email_raw) { fixture_file('emails/auto_reply.eml') }
|
|
|
|
let(:expected_error) { Gitlab::Email::AutoGeneratedEmailError }
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it_behaves_like 'failed receive without event'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'event raising via errors' do
|
|
|
|
let(:handler) { double(:handler, project: project, execute: true, metrics_event: nil, metrics_params: nil) }
|
|
|
|
let(:email_raw) { "arbitrary text. could be anything really. we're going to raise an error anyway." }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(receiver).to receive(:handler).and_return(handler)
|
|
|
|
allow(handler).to receive(:execute).and_raise(expected_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'handling errors which do not raise events' do
|
|
|
|
where(:expected_error) do
|
|
|
|
[
|
|
|
|
Gitlab::Email::AutoGeneratedEmailError,
|
|
|
|
Gitlab::Email::ProjectNotFound,
|
|
|
|
Gitlab::Email::EmptyEmailError,
|
|
|
|
Gitlab::Email::UserNotFoundError,
|
|
|
|
Gitlab::Email::UserBlockedError,
|
|
|
|
Gitlab::Email::UserNotAuthorizedError,
|
|
|
|
Gitlab::Email::NoteableNotFoundError,
|
|
|
|
Gitlab::Email::InvalidAttachment,
|
|
|
|
Gitlab::Email::InvalidRecordError,
|
|
|
|
Gitlab::Email::EmailTooLarge
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it_behaves_like 'failed receive without event'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'handling errors which do raise events' do
|
|
|
|
where(:expected_error) do
|
|
|
|
[Gitlab::Email::EmailUnparsableError, Gitlab::Email::UnknownIncomingEmail, ArgumentError, StandardError]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it_behaves_like 'failed receive with event'
|
|
|
|
end
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'requires all handlers to have a unique metric_event' do
|
2019-12-04 20:38:33 +05:30
|
|
|
events = Gitlab::Email::Handler.handlers.map do |handler|
|
|
|
|
handler.new(Mail::Message.new, 'gitlabhq/gitlabhq+auth_token').metrics_event
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(events.uniq.count).to eq events.count
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
it 'requires all handlers to respond to #project' do
|
|
|
|
Gitlab::Email::Handler.load_handlers.each do |handler|
|
|
|
|
expect { handler.new(nil, nil).project }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|