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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
# encoding: utf-8
# frozen_string_literal: true
require 'spec_helper'
describe "SMTP Delivery Method" 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 => nil,
:enable_starttls_auto => true,
:openssl_verify_mode => nil,
:tls => nil,
:ssl => nil,
:open_timeout => nil,
:read_timeout => nil
}
end
MockSMTP.clear_deliveries
end
describe "general usage" do
it "dot-stuff unterminated last line of the message" do
Mail.deliver do
from 'from@example.com'
to 'to@example.com'
subject 'dot-stuff last line'
body "this is a test\n.\nonly a test\n."
end
message = MockSMTP.deliveries.first
expect(Mail.new(message).decoded).to eq("this is a test\n..\nonly a test\n..")
end
it "should send emails from given settings" do
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
smtp_envelope_from 'smtp_from'
smtp_envelope_to 'smtp_to'
end
expect(MockSMTP.deliveries[0][0]).to eq mail.encoded
expect(MockSMTP.deliveries[0][1]).to eq 'smtp_from'
expect(MockSMTP.deliveries[0][2]).to eq %w(smtp_to)
end
it "should be able to send itself" do
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
end
mail.deliver!
expect(MockSMTP.deliveries[0][0]).to eq mail.encoded
expect(MockSMTP.deliveries[0][1]).to eq mail.from[0]
expect(MockSMTP.deliveries[0][2]).to eq mail.destinations
end
it "should be able to return actual SMTP protocol response" do
Mail.defaults do
delivery_method :smtp, :address => 'smtp.mockup.com', :port => 587, :return_response => true
end
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
end
response = mail.deliver!
expect(response).to eq 'OK'
end
end
describe "enabling tls" do
def redefine_verify_none(new_value)
OpenSSL::SSL.send(:remove_const, :VERIFY_NONE)
OpenSSL::SSL.send(:const_set, :VERIFY_NONE, new_value)
end
it "should use OpenSSL::SSL::VERIFY_NONE if a context" do
# config can't be setup until redefined
redefine_verify_none(OpenSSL::SSL::SSLContext.new)
Mail.defaults do
delivery_method :smtp, :address => 'smtp.mockup.com', :port => 587
end
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
end
expect { mail.deliver! }.not_to raise_error
end
it "should ignore OpenSSL::SSL::VERIFY_NONE if it is 0" do
# config can't be setup until redefined
redefine_verify_none(0)
Mail.defaults do
delivery_method :smtp, :address => 'smtp.mockup.com', :port => 587
end
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
end
expect { mail.deliver! }.not_to raise_error
end
end
describe "enabling ssl" do
def redefine_verify_none(new_value)
OpenSSL::SSL.send(:remove_const, :VERIFY_NONE)
OpenSSL::SSL.send(:const_set, :VERIFY_NONE, new_value)
end
it "should use OpenSSL::SSL::VERIFY_NONE if a context" do
# config can't be setup until redefined
redefine_verify_none(OpenSSL::SSL::SSLContext.new)
Mail.defaults do
delivery_method :smtp, :address => 'smtp.mockup.com', :port => 587, :tls => true
end
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
end
expect { mail.deliver! }.not_to raise_error
end
it "should ignore OpenSSL::SSL::VERIFY_NONE if it is 0" do
# config can't be setup until redefined
redefine_verify_none(0)
Mail.defaults do
delivery_method :smtp, :address => 'smtp.mockup.com', :port => 587, :tls => true
end
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
end
expect { mail.deliver! }.not_to raise_error
end
it "should not set verify mode when none is given" do
context = OpenSSL::SSL::SSLContext.new
allow(Net::SMTP).to receive(:default_ssl_context).and_return(context)
expect(context).to_not receive(:verify_mode=)
Mail.defaults do
delivery_method :smtp, :address => 'smtp.mockup.com', :port => 587, :tls => true
end
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
end
expect { mail.deliver! }.not_to raise_error
end
it "should set verify mode if one is given" do
context = OpenSSL::SSL::SSLContext.new
allow(Net::SMTP).to receive(:default_ssl_context).and_return(context)
expect(context).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER).at_least(1)
Mail.defaults do
delivery_method :smtp,
:address => 'smtp.mockup.com',
:port => 587,
:tls => true,
:openssl_verify_mode => OpenSSL::SSL::VERIFY_PEER
end
mail = Mail.deliver do
from 'roger@moore.com'
to 'marcel@amont.com'
subject 'invalid RFC2822'
end
expect { mail.deliver! }.not_to raise_error
end
end
describe "enabling STARTTLS" do
it 'should default to automatically detecting STARTTLS' do
message = Mail.new do
from 'mikel@test.lindsaar.net'
to 'ada@test.lindsaar.net'
subject 'Re: No way!'
body 'Yeah sure'
end
message.deliver!
expect(MockSMTP.security).to eq :enable_starttls_auto
end
it 'should allow forcing STARTTLS' do
message = Mail.new do
from 'mikel@test.lindsaar.net'
to 'ada@test.lindsaar.net'
subject 'Re: No way!'
body 'Yeah sure'
delivery_method :smtp, { :address => "localhost",
:port => 25,
:domain => 'localhost.localdomain',
:user_name => nil,
:password => nil,
:authentication => nil,
:enable_starttls => true }
end
message.deliver!
expect(MockSMTP.security).to eq :enable_starttls
end
end
describe "SMTP Envelope" do
it "uses the envelope From and To addresses" do
Mail.deliver do
to "to@someemail.com"
from "from@someemail.com"
message_id "<1234@someemail.com>"
body "body"
smtp_envelope_to "smtp_to@someemail.com"
smtp_envelope_from "smtp_from@someemail.com"
end
expect(MockSMTP.deliveries[0][1]).to eq 'smtp_from@someemail.com'
expect(MockSMTP.deliveries[0][2]).to eq %w(smtp_to@someemail.com)
end
it "supports the null sender in the envelope from address" do
Mail.deliver do
to "to@someemail.com"
from "from@someemail.com"
message_id "<1234@someemail.com>"
body "body"
smtp_envelope_to "smtp_to@someemail.com"
smtp_envelope_from Mail::Constants::NULL_SENDER
end
expect(MockSMTP.deliveries[0][1]).to eq '<>'
expect(MockSMTP.deliveries[0][2]).to eq %w(smtp_to@someemail.com)
end
it "should not raise errors if there is no envelope From address" do
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 "should raise an error if no recipient if defined" do
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
it "should raise on SMTP injection via MAIL FROM newlines" do
addr = "inject.from@example.com>\r\nDATA"
mail = Mail.new do
from addr
to "to@somemail.com"
end
expect(mail.smtp_envelope_from).to eq addr
expect do
mail.deliver
end.to raise_error(ArgumentError, "SMTP From address may not contain CR or LF line breaks: #{addr.inspect}")
end
it "should raise on SMTP injection via RCPT TO newlines" do
addr = "inject.to@example.com>\r\nDATA"
mail = Mail.new do
from "from@somemail.com"
to addr
end
expect(mail.smtp_envelope_to).to eq [addr]
expect do
mail.deliver
end.to raise_error(ArgumentError, "SMTP To address may not contain CR or LF line breaks: #{addr.inspect}")
end
it "should raise on SMTP injection via MAIL FROM overflow" do
addr = "inject.from@example.com#{'m' * 2025}DATA"
mail = Mail.new do
from addr
to "to@somemail.com"
end
expect(mail.smtp_envelope_from).to eq addr
expect do
mail.deliver
end.to raise_error(ArgumentError, "SMTP From address may not exceed 2kB: #{addr.inspect}")
end
it "should raise on SMTP injection via RCPT TO overflow" do
addr = "inject.to@example.com#{'m' * 2027}DATA"
mail = Mail.new do
from "from@somemail.com"
to addr
end
expect(mail.smtp_envelope_to).to eq [addr]
expect do
mail.deliver
end.to raise_error(ArgumentError, "SMTP To address may not exceed 2kB: #{addr.inspect}")
end
end
end
|