Welcome to mirror list, hosted at ThFree Co, Russian Federation.

connection_spec.rb « smtp_pool « mail « lib « spec « mail-smtp_pool « gems « vendor - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 78426296406368f8c990c2b4310153f0859b2057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

require 'spec_helper'

describe Mail::SMTPPool::Connection do
  let(:connection) { described_class.new({}) }
  let(:mail) do
    Mail.new do
      from    'mikel@test.lindsaar.net'
      to      'you@test.lindsaar.net'
      subject 'This is a test email'
      body    'Test body'
    end
  end

  after do
    MockSMTP.clear_deliveries
  end

  describe '#deliver!' do
    it 'delivers mail using the same SMTP connection' do
      mock_smtp = MockSMTP.new

      expect(Net::SMTP).to receive(:new).once.and_return(mock_smtp)
      expect(mock_smtp).to receive(:sendmail).twice.and_call_original
      expect(mock_smtp).to receive(:rset).once.and_call_original

      connection.deliver!(mail)
      connection.deliver!(mail)

      expect(MockSMTP.deliveries.size).to eq(2)
    end

    context 'when RSET fails' do
      let(:mock_smtp) { MockSMTP.new }
      let(:mock_smtp_2) { MockSMTP.new }

      before do
        expect(Net::SMTP).to receive(:new).twice.and_return(mock_smtp, mock_smtp_2)
      end

      context 'with an IOError' do
        before do
          expect(mock_smtp).to receive(:rset).once.and_raise(IOError)
        end

        it 'creates a new SMTP connection' do
          expect(mock_smtp).to receive(:sendmail).once.and_call_original
          expect(mock_smtp).to receive(:finish).once.and_call_original
          expect(mock_smtp_2).to receive(:sendmail).once.and_call_original

          connection.deliver!(mail)
          connection.deliver!(mail)

          expect(MockSMTP.deliveries.size).to eq(2)
        end
      end

      context 'with an SMTP error' do
        before do
          expect(mock_smtp).to receive(:rset).once.and_raise(Net::SMTPServerBusy)
        end

        it 'creates a new SMTP connection' do
          expect(mock_smtp).to receive(:sendmail).once.and_call_original
          expect(mock_smtp).to receive(:finish).once.and_call_original
          expect(mock_smtp_2).to receive(:sendmail).once.and_call_original

          connection.deliver!(mail)
          connection.deliver!(mail)

          expect(MockSMTP.deliveries.size).to eq(2)
        end

        context 'and closing the old connection fails' do
          before do
            expect(mock_smtp).to receive(:finish).once.and_raise(IOError)
          end

          it 'creates a new SMTP connection' do
            expect(mock_smtp).to receive(:sendmail).once.and_call_original
            expect(mock_smtp_2).to receive(:sendmail).once.and_call_original

            connection.deliver!(mail)
            connection.deliver!(mail)

            expect(MockSMTP.deliveries.size).to eq(2)
          end
        end
      end
    end
  end
end