File: exim_spec.rb

package info (click to toggle)
ruby-mail 2.6.4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,256 kB
  • ctags: 1,327
  • sloc: ruby: 44,678; makefile: 3
file content (205 lines) | stat: -rw-r--r-- 6,896 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# encoding: utf-8
# frozen_string_literal: true
require 'spec_helper'

describe "exim delivery agent" do
  
  before(:each) do
    # Reset all defaults back to original state
    Mail.defaults do
      delivery_method :smtp, { :address              => "localhost",
                               :port                 => 25,
                               :domain               => 'localhost.localdomain',
                               :user_name            => nil,
                               :password             => nil,
                               :authentication       => nil,
                               :enable_starttls_auto => true  }
    end
  end

  it "should send an email using exim" do
    Mail.defaults do
      delivery_method :exim
    end
    
    mail = Mail.new do
      from    'roger@test.lindsaar.net'
      to      'marcel@test.lindsaar.net, bob@test.lindsaar.net'
      subject 'invalid RFC2822'
    end
    
    expect(Mail::Exim).to receive(:call).with('/usr/sbin/exim', 
                                          '-i -t -f "roger@test.lindsaar.net" --', 
                                          '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"', 
                                          mail.encoded)
    mail.deliver!
  end

  describe "return path" do

    it "should send an email with a return-path using exim" do
      Mail.defaults do
        delivery_method :exim
      end

      mail = Mail.new do
        to "to@test.lindsaar.net"
        from "from@test.lindsaar.net"
        sender "sender@test.lindsaar.net"
        subject "Can't set the return-path"
        return_path "return@test.lindsaar.net"
        message_id "<1234@test.lindsaar.net>"
        body "body"
      end
      
      expect(Mail::Exim).to receive(:call).with('/usr/sbin/exim',
                                                '-i -t -f "return@test.lindsaar.net" --', 
                                                '"to@test.lindsaar.net"', 
                                                mail.encoded)
                                                
      mail.deliver

    end

    it "should use the sender address is no return path is specified" do
      Mail.defaults do
        delivery_method :exim
      end

      mail = Mail.new do
        to "to@test.lindsaar.net"
        from "from@test.lindsaar.net"
        sender "sender@test.lindsaar.net"
        subject "Can't set the return-path"
        message_id "<1234@test.lindsaar.net>"
        body "body"
      end

      expect(Mail::Exim).to receive(:call).with('/usr/sbin/exim',
                                                '-i -t -f "sender@test.lindsaar.net" --', 
                                                '"to@test.lindsaar.net"', 
                                                mail.encoded)

      mail.deliver
    end
    
    it "should use the from address is no return path or sender are specified" do
      Mail.defaults do
        delivery_method :exim
      end

      mail = Mail.new do
        to "to@test.lindsaar.net"
        from "from@test.lindsaar.net"
        subject "Can't set the return-path"
        message_id "<1234@test.lindsaar.net>"
        body "body"
      end

      expect(Mail::Exim).to receive(:call).with('/usr/sbin/exim',
                                                '-i -t -f "from@test.lindsaar.net" --', 
                                                '"to@test.lindsaar.net"', 
                                                mail.encoded)
      mail.deliver
    end

    it "should escape the return path address" do
      Mail.defaults do
        delivery_method :exim
      end

      mail = Mail.new do
        to 'to@test.lindsaar.net'
        from '"from+suffix test"@test.lindsaar.net'
        subject 'Can\'t set the return-path'
        message_id '<1234@test.lindsaar.net>'
        body 'body'
      end

      expect(Mail::Exim).to receive(:call).with('/usr/sbin/exim',
                                                '-i -t -f "\"from+suffix test\"@test.lindsaar.net" --',
                                                '"to@test.lindsaar.net"',
                                                mail.encoded)
      mail.deliver
    end

    it "should quote the destinations to ensure leading -hyphen doesn't confuse exim" do
      Mail.defaults do
        delivery_method :exim
      end

      mail = Mail.new do
        to '-hyphen@test.lindsaar.net'
        from 'from@test.lindsaar.net'
      end

      expect(Mail::Exim).to receive(:call).with('/usr/sbin/exim',
                                                '-i -t -f "from@test.lindsaar.net" --',
                                                '"-hyphen@test.lindsaar.net"',
                                                mail.encoded)
      mail.deliver
    end
  end

  it "should still send an email if the settings have been set to nil" do
    Mail.defaults do
      delivery_method :exim, :arguments => nil
    end
    
    mail = Mail.new do
      from    'from@test.lindsaar.net'
      to      'marcel@test.lindsaar.net, bob@test.lindsaar.net'
      subject 'invalid RFC2822'
    end
    
    expect(Mail::Exim).to receive(:call).with('/usr/sbin/exim', 
                                              ' -f "from@test.lindsaar.net" --',
                                              '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"', 
                                              mail.encoded)
    mail.deliver!
  end

  it "should escape evil haxxor attemptes" do
    Mail.defaults do
      delivery_method :exim, :arguments => nil
    end
    
    mail = Mail.new do
      from    '"foo\";touch /tmp/PWNED;\""@blah.com'
      to      'marcel@test.lindsaar.net'
      subject 'invalid RFC2822'
    end
    
    expect(Mail::Exim).to receive(:call).with('/usr/sbin/exim', 
                                              " -f \"\\\"foo\\\\\\\"\\;touch /tmp/PWNED\\;\\\\\\\"\\\"@blah.com\" --",
                                              '"marcel@test.lindsaar.net"', 
                                              mail.encoded)
    mail.deliver!
  end

  it "should raise an error if no sender is defined" do
    Mail.defaults do
      delivery_method :test
    end
    expect do
      Mail.deliver do
        to "to@somemail.com"
        subject "Email with no sender"
        body "body"
      end
    end.to raise_error('An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.')
  end

  it "should raise an error if no recipient if defined" do
    Mail.defaults do
      delivery_method :test
    end
    expect do
      Mail.deliver do
        from "from@somemail.com"
        subject "Email with no recipient"
        body "body"
      end
    end.to raise_error('An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.')
  end
end