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

122 lines
4.1 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"
2020-07-28 23:09:34 +05:30
RSpec.describe EmailReceiverWorker, :mailer 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
allow(Gitlab::IncomingEmail).to receive(:enabled?).and_return(true)
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)
2021-06-08 01:23:25 +05:30
expect(Sidekiq.logger).to receive(:error).with(hash_including('exception.class' => error.class.name)).and_call_original
2015-09-25 12:07:36 +05:30
end
2017-09-10 17:25:29 +05:30
context 'when the error is Gitlab::Email::EmptyEmailError' do
2021-06-08 01:23:25 +05:30
let(:error) { Gitlab::Email::EmptyEmailError.new }
2017-09-10 17:25:29 +05:30
it 'sends out a rejection email' do
perform_enqueued_jobs do
described_class.new.perform(raw_message)
end
2015-12-23 02:04:40 +05:30
email = ActionMailer::Base.deliveries.last
expect(email).not_to be_nil
expect(email.to).to eq(["jake@adventuretime.ooo"])
expect(email.subject).to include("Rejected")
end
2021-12-11 22:18:48 +05:30
it 'strips out the body before passing to EmailRejectionMailer' do
mail = Mail.new(raw_message)
mail.body = nil
expect(EmailRejectionMailer).to receive(:rejection).with(anything, mail.encoded, anything).and_call_original
described_class.new.perform(raw_message)
end
2015-09-25 12:07:36 +05:30
end
2017-09-10 17:25:29 +05:30
context 'when the error is Gitlab::Email::AutoGeneratedEmailError' do
2021-06-08 01:23:25 +05:30
let(:error) { Gitlab::Email::AutoGeneratedEmailError.new }
2017-09-10 17:25:29 +05:30
it 'does not send out any rejection email' do
perform_enqueued_jobs do
described_class.new.perform(raw_message)
end
should_not_email_anyone
end
end
2018-12-13 13:39:08 +05:30
context 'when the error is Gitlab::Email::InvalidAttachment' do
let(:error) { Gitlab::Email::InvalidAttachment.new("Could not deal with that") }
it 'reports the error to the sender' do
perform_enqueued_jobs do
described_class.new.perform(raw_message)
end
email = ActionMailer::Base.deliveries.last
expect(email).not_to be_nil
expect(email.to).to eq(["jake@adventuretime.ooo"])
expect(email.body.parts.last.to_s).to include("Could not deal with that")
end
end
2021-06-08 01:23:25 +05:30
context 'when the error is ActiveRecord::StatementTimeout' do
let(:error) { ActiveRecord::StatementTimeout.new("Statement timeout") }
it 'does not report the error to the sender' do
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(error).and_call_original
perform_enqueued_jobs do
described_class.new.perform(raw_message)
end
email = ActionMailer::Base.deliveries.last
expect(email).to be_nil
end
end
2021-11-18 22:05:49 +05:30
context 'when the error is RateLimitedService::RateLimitedError' do
let(:error) { RateLimitedService::RateLimitedError.new(key: :issues_create, rate_limiter: Gitlab::ApplicationRateLimiter) }
it 'does not report the error to the sender' do
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(error).and_call_original
perform_enqueued_jobs do
described_class.new.perform(raw_message)
end
email = ActionMailer::Base.deliveries.last
expect(email).to be_nil
end
end
2015-09-25 12:07:36 +05:30
end
end
context "when reply by email is disabled" do
before do
allow(Gitlab::IncomingEmail).to receive(:enabled?).and_return(false)
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