2019-03-30 03:15:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-20 14:47:09 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe EmailReceiverWorker, :mailer do
|
2015-08-20 15:34:45 -04:00
|
|
|
let(:raw_message) { fixture_file('emails/valid_reply.eml') }
|
2015-08-20 14:47:09 -04:00
|
|
|
|
|
|
|
context "when reply by email is enabled" do
|
|
|
|
before do
|
2015-09-21 03:46:47 -04:00
|
|
|
allow(Gitlab::IncomingEmail).to receive(:enabled?).and_return(true)
|
2015-08-20 14:47:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls the email receiver" do
|
|
|
|
expect(Gitlab::Email::Receiver).to receive(:new).with(raw_message).and_call_original
|
|
|
|
expect_any_instance_of(Gitlab::Email::Receiver).to receive(:execute)
|
2021-05-09 20:10:37 -04:00
|
|
|
expect(Sidekiq.logger).to receive(:info).with(hash_including(message: "Successfully processed message")).and_call_original
|
2015-08-20 14:47:09 -04:00
|
|
|
|
|
|
|
described_class.new.perform(raw_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when an error occurs" do
|
|
|
|
before do
|
2017-08-02 11:13:27 -04:00
|
|
|
allow_any_instance_of(Gitlab::Email::Receiver).to receive(:execute).and_raise(error)
|
2015-08-20 14:47:09 -04:00
|
|
|
end
|
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
context 'when error is a processing error' do
|
2021-05-09 20:10:37 -04:00
|
|
|
let(:error) { Gitlab::Email::EmptyEmailError.new }
|
2017-08-02 11:13:27 -04:00
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
it 'triggers email failure handler' do
|
|
|
|
expect(Gitlab::Email::FailureHandler).to receive(:handle) do |receiver, received_error|
|
|
|
|
expect(receiver).to be_a(Gitlab::Email::Receiver)
|
|
|
|
expect(receiver.mail.encoded).to eql(Mail::Message.new(raw_message).encoded)
|
|
|
|
expect(received_error).to be(error)
|
2017-08-02 11:13:27 -04:00
|
|
|
end
|
2015-11-30 09:12:31 -05:00
|
|
|
|
2021-10-29 08:14:45 -04:00
|
|
|
described_class.new.perform(raw_message)
|
|
|
|
end
|
2018-10-24 12:01:44 -04:00
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
it 'logs the error' do
|
|
|
|
expect(Sidekiq.logger).to receive(:error).with(hash_including('exception.class' => error.class.name)).and_call_original
|
2018-10-24 12:01:44 -04:00
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
described_class.new.perform(raw_message)
|
2018-10-24 12:01:44 -04:00
|
|
|
end
|
|
|
|
end
|
2021-05-09 20:10:37 -04:00
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
context 'when error is not a processing error' do
|
2021-05-09 20:10:37 -04:00
|
|
|
let(:error) { ActiveRecord::StatementTimeout.new("Statement timeout") }
|
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
it 'triggers email failure handler' do
|
|
|
|
expect(Gitlab::Email::FailureHandler).to receive(:handle) do |receiver, received_error|
|
|
|
|
expect(receiver).to be_a(Gitlab::Email::Receiver)
|
|
|
|
expect(receiver.mail.encoded).to eql(Mail::Message.new(raw_message).encoded)
|
|
|
|
expect(received_error).to be(error)
|
2021-05-09 20:10:37 -04:00
|
|
|
end
|
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
described_class.new.perform(raw_message)
|
2021-05-09 20:10:37 -04:00
|
|
|
end
|
2021-10-12 23:12:30 -04:00
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
it 'reports the error' do
|
2021-10-12 23:12:30 -04:00
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(error).and_call_original
|
|
|
|
|
2022-01-13 07:14:38 -05:00
|
|
|
described_class.new.perform(raw_message)
|
2021-10-12 23:12:30 -04:00
|
|
|
end
|
|
|
|
end
|
2015-08-20 14:47:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when reply by email is disabled" do
|
|
|
|
before do
|
2015-09-21 03:46:47 -04:00
|
|
|
allow(Gitlab::IncomingEmail).to receive(:enabled?).and_return(false)
|
2015-08-20 14:47:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't call the email receiver" do
|
|
|
|
expect(Gitlab::Email::Receiver).not_to receive(:new)
|
|
|
|
|
|
|
|
described_class.new.perform(raw_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|