email_receiver_worker_spec.rb 2.42 KB
Newer Older
1 2
# frozen_string_literal: true

Douwe Maan's avatar
Douwe Maan committed
3 4
require "spec_helper"

5
describe EmailReceiverWorker, :mailer do
Douwe Maan's avatar
Douwe Maan committed
6
  let(:raw_message) { fixture_file('emails/valid_reply.eml') }
Douwe Maan's avatar
Douwe Maan committed
7 8 9

  context "when reply by email is enabled" do
    before do
10
      allow(Gitlab::IncomingEmail).to receive(:enabled?).and_return(true)
Douwe Maan's avatar
Douwe Maan committed
11 12 13 14 15 16 17 18 19 20 21
    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)

      described_class.new.perform(raw_message)
    end

    context "when an error occurs" do
      before do
22
        allow_any_instance_of(Gitlab::Email::Receiver).to receive(:execute).and_raise(error)
Douwe Maan's avatar
Douwe Maan committed
23 24
      end

25 26 27 28 29 30 31
      context 'when the error is Gitlab::Email::EmptyEmailError' do
        let(:error) { Gitlab::Email::EmptyEmailError }

        it 'sends out a rejection email' do
          perform_enqueued_jobs do
            described_class.new.perform(raw_message)
          end
Valery Sizov's avatar
Valery Sizov committed
32 33 34 35 36 37

          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
Douwe Maan's avatar
Douwe Maan committed
38
      end
39 40 41 42 43 44 45 46 47 48 49 50

      context 'when the error is Gitlab::Email::AutoGeneratedEmailError' do
        let(:error) { Gitlab::Email::AutoGeneratedEmailError }

        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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

      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
Douwe Maan's avatar
Douwe Maan committed
66 67 68 69 70
    end
  end

  context "when reply by email is disabled" do
    before do
71
      allow(Gitlab::IncomingEmail).to receive(:enabled?).and_return(false)
Douwe Maan's avatar
Douwe Maan committed
72 73 74 75 76 77 78 79 80
    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