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
|
2016-09-13 17:45:13 +05:30
|
|
|
include_context :email_shared_context
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
shared_examples 'correctly finds the mail key and adds metric event' do
|
|
|
|
let(:metric_transaction) { double('Gitlab::Metrics::WebTransaction') }
|
|
|
|
|
|
|
|
specify :aggregate_failures 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-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
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the email contains a valid email address in a header' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:handler) { double(:handler) }
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:metadata) { receiver.mail_metadata }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow(handler).to receive(:execute)
|
|
|
|
allow(handler).to receive(:metrics_params)
|
2019-12-04 20:38:33 +05:30
|
|
|
allow(handler).to receive(:metrics_event)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.example.com")
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
expect(receiver.mail_metadata.keys).to match_array(%i(mail_uid from_address to_address mail_key references delivered_to envelope_to x_envelope_to))
|
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') }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it_behaves_like 'correctly finds the mail key and adds metric event'
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
it 'parses the metadata' do
|
|
|
|
expect(metadata[:delivered_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com", "support@example.com"])
|
|
|
|
end
|
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-04 01:27:46 +05:30
|
|
|
it_behaves_like 'correctly finds the mail key and adds metric event'
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
it 'parses the metadata' do
|
|
|
|
expect(metadata[:envelope_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"])
|
|
|
|
end
|
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-04 01:27:46 +05:30
|
|
|
it_behaves_like 'correctly finds the mail key and adds metric event'
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
it 'parses the metadata' do
|
|
|
|
expect(metadata[:x_envelope_to]). to eq(["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"])
|
|
|
|
end
|
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-04 01:27:46 +05:30
|
|
|
it_behaves_like 'correctly finds the mail key and adds metric event'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
context "when we cannot find a capable handler" do
|
|
|
|
let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "!!!") }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it "raises an UnknownIncomingEmail error" do
|
2016-09-13 17:45:13 +05:30
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::UnknownIncomingEmail)
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the email is blank" do
|
|
|
|
let(:email_raw) { "" }
|
|
|
|
|
|
|
|
it "raises an EmptyEmailError" do
|
2016-09-13 17:45:13 +05:30
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::EmptyEmailError)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context "when the email was auto generated with Auto-Submitted header" do
|
|
|
|
let(:email_raw) { fixture_file("emails/auto_submitted.eml") }
|
|
|
|
|
|
|
|
it "raises an AutoGeneratedEmailError" do
|
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::AutoGeneratedEmailError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the email was auto generated with X-Autoreply header" do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:email_raw) { fixture_file("emails/auto_reply.eml") }
|
|
|
|
|
|
|
|
it "raises an AutoGeneratedEmailError" do
|
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::AutoGeneratedEmailError)
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
it "requires all handlers to have a unique metric_event" do
|
|
|
|
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
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|