debian-mirror-gitlab/spec/workers/email_receiver_worker_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.7 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-09-25 12:07:36 +05:30
require "spec_helper"
2023-05-27 22:25:52 +05:30
RSpec.describe EmailReceiverWorker, :mailer, feature_category: :team_planning do
2015-09-25 12:07:36 +05:30
let(:raw_message) { fixture_file('emails/valid_reply.eml') }
context "when reply by email is enabled" do
before do
2023-06-20 00:43:36 +05:30
allow(Gitlab::Email::IncomingEmail).to receive(:enabled?).and_return(true)
2015-09-25 12:07:36 +05:30
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-06-08 01:23:25 +05:30
expect(Sidekiq.logger).to receive(:info).with(hash_including(message: "Successfully processed message")).and_call_original
2015-09-25 12:07:36 +05:30
described_class.new.perform(raw_message)
end
context "when an error occurs" do
before do
2017-09-10 17:25:29 +05:30
allow_any_instance_of(Gitlab::Email::Receiver).to receive(:execute).and_raise(error)
2015-09-25 12:07:36 +05:30
end
2022-03-02 08:16:31 +05:30
context 'when error is a processing error' do
2021-06-08 01:23:25 +05:30
let(:error) { Gitlab::Email::EmptyEmailError.new }
2017-09-10 17:25:29 +05:30
2022-03-02 08:16:31 +05:30
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-09-10 17:25:29 +05:30
end
2015-12-23 02:04:40 +05:30
2021-12-11 22:18:48 +05:30
described_class.new.perform(raw_message)
end
2018-12-13 13:39:08 +05:30
2022-03-02 08:16:31 +05:30
it 'logs the error' do
expect(Sidekiq.logger).to receive(:error).with(hash_including('exception.class' => error.class.name)).and_call_original
2018-12-13 13:39:08 +05:30
2022-03-02 08:16:31 +05:30
described_class.new.perform(raw_message)
2018-12-13 13:39:08 +05:30
end
end
2021-06-08 01:23:25 +05:30
2022-03-02 08:16:31 +05:30
context 'when error is not a processing error' do
2021-06-08 01:23:25 +05:30
let(:error) { ActiveRecord::StatementTimeout.new("Statement timeout") }
2022-03-02 08:16:31 +05:30
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-06-08 01:23:25 +05:30
end
2022-03-02 08:16:31 +05:30
described_class.new.perform(raw_message)
2021-06-08 01:23:25 +05:30
end
2021-11-18 22:05:49 +05:30
2022-03-02 08:16:31 +05:30
it 'reports the error' do
2021-11-18 22:05:49 +05:30
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(error).and_call_original
2022-03-02 08:16:31 +05:30
described_class.new.perform(raw_message)
2021-11-18 22:05:49 +05:30
end
end
2015-09-25 12:07:36 +05:30
end
end
context "when reply by email is disabled" do
before do
2023-06-20 00:43:36 +05:30
allow(Gitlab::Email::IncomingEmail).to receive(:enabled?).and_return(false)
2015-09-25 12:07:36 +05:30
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