File: sendmail_spec.rb

package info (click to toggle)
ruby-mail 2.7.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,732 kB
  • sloc: ruby: 71,628; makefile: 3
file content (191 lines) | stat: -rw-r--r-- 5,115 bytes parent folder | download | duplicates (3)
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
# encoding: utf-8
# frozen_string_literal: true
require 'spec_helper'

describe Mail::Sendmail do
  let :mail do
    Mail.new do
      from    'roger@test.lindsaar.net'
      to      'marcel@test.lindsaar.net, bob@test.lindsaar.net'
      subject 'some subject'
    end
  end

  before do
    Mail.defaults do
      delivery_method :sendmail
    end
  end

  it 'sends an email using sendmail' do
    expect(described_class).to receive(:call).
      with('/usr/sbin/sendmail',
           '-i -f "roger@test.lindsaar.net" --',
           '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"',
           mail.encoded)

    mail.deliver!
  end

  it 'spawns a sendmail process' do
    expect(described_class).to receive(:popen).
      with('/usr/sbin/sendmail -i -f "roger@test.lindsaar.net" -- "marcel@test.lindsaar.net" "bob@test.lindsaar.net"')

    mail.deliver!
  end

  context 'SMTP From' do
    it 'explicitly passes an envelope From address to sendmail' do
      mail.smtp_envelope_from = 'smtp_from@test.lindsaar.net'

      expect(described_class).to receive(:call).
        with('/usr/sbin/sendmail',
             '-i -f "smtp_from@test.lindsaar.net" --',
             '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"',
             mail.encoded)

      mail.deliver
    end

    it 'escapes the From address' do
      mail.from = '"from+suffix test"@test.lindsaar.net'

      expect(described_class).to receive(:call).
        with('/usr/sbin/sendmail',
             '-i -f "\"from+suffix test\"@test.lindsaar.net" --',
             '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"',
             mail.encoded)

      mail.deliver
    end

    it 'does not escape ~ in From address' do
      mail.from = 'tilde~@test.lindsaar.net'

      expect(described_class).to receive(:call).
        with('/usr/sbin/sendmail',
             '-i -f "tilde~@test.lindsaar.net" --',
             '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"',
             mail.encoded)

      mail.deliver
    end
  end

  context 'SMTP To' do
    it 'explicitly passes envelope To addresses to sendmail' do
      mail.smtp_envelope_to = 'smtp_to@test.lindsaar.net'

      expect(described_class).to receive(:call).
        with('/usr/sbin/sendmail',
             '-i -f "roger@test.lindsaar.net" --',
             '"smtp_to@test.lindsaar.net"',
             mail.encoded)

      mail.deliver
    end

    it 'escapes the To address' do
      mail.to = '"to+suffix test"@test.lindsaar.net'

      expect(described_class).to receive(:call).
        with('/usr/sbin/sendmail',
             '-i -f "roger@test.lindsaar.net" --',
             '"\"to+suffix test\"@test.lindsaar.net"',
             mail.encoded)

      mail.deliver
    end

    it 'does not escape ~ in To address' do
      mail.to = 'tilde~@test.lindsaar.net'

      expect(described_class).to receive(:call).
        with('/usr/sbin/sendmail',
             '-i -f "roger@test.lindsaar.net" --',
             '"tilde~@test.lindsaar.net"',
             mail.encoded)

      mail.deliver
    end

    it 'quotes the destinations to ensure leading -hyphen doesn\'t confuse sendmail' do
      mail.to = '-hyphen@test.lindsaar.net'

      expect(described_class).to receive(:call).
        with('/usr/sbin/sendmail',
             '-i -f "roger@test.lindsaar.net" --',
             '"-hyphen@test.lindsaar.net"',
             mail.encoded)

      mail.deliver
    end
  end

  it 'still sends an email if the arguments setting have been set to nil' do
    Mail.defaults do
      delivery_method :sendmail, :arguments => nil
    end

    expect(described_class).to receive(:call).
      with('/usr/sbin/sendmail',
           ' -f "roger@test.lindsaar.net" --',
           '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"',
           mail.encoded)

    mail.deliver!
  end

  it 'escapes evil haxxor attempts' do
    Mail.defaults do
      delivery_method :sendmail, :arguments => nil
    end

    mail.from = '"foo\";touch /tmp/PWNED;\""@blah.com'
    mail.to   = '"foo\";touch /tmp/PWNED;\""@blah.com'

    expect(described_class).to receive(:call).
      with('/usr/sbin/sendmail',
           " -f \"\\\"foo\\\\\\\"\\;touch /tmp/PWNED\\;\\\\\\\"\\\"@blah.com\" --",
           %("\\\"foo\\\\\\\"\\;touch /tmp/PWNED\\;\\\\\\\"\\\"@blah.com"),
           mail.encoded)

    mail.deliver!
  end

  it 'raises an error if no sender is defined' do
    Mail.defaults do
      delivery_method :sendmail
    end

    mail = Mail.new do
      to "to@somemail.com"
      subject "Email with no sender"
      body "body"
    end

    expect(mail.smtp_envelope_from).to be_nil

    expect do
      mail.deliver
    end.to raise_error('SMTP From address may not be blank: nil')
  end

  it 'raises an error if no recipient is defined' do
    Mail.defaults do
      delivery_method :sendmail
    end

    mail = Mail.new do
      from "from@somemail.com"
      subject "Email with no recipient"
      body "body"
    end

    expect(mail.smtp_envelope_to).to eq([])

    expect do
      mail.deliver!
    end.to raise_error('SMTP To address may not be blank: []')
  end
end