Skip to content
Permalink
Browse files

MailExceptions: add tls support

- TODO: use mailer
  • Loading branch information...
gmarik authored and manveru committed Sep 17, 2010
1 parent ea13fab commit e9eb1f5632c1e2e0e644792eef6ee4afa0b3ecf3
Showing with 98 additions and 4 deletions.
  1. +16 −4 lib/rack/contrib/mailexceptions.rb
  2. +8 −0 test/mail_settings.rb
  3. +74 −0 test/spec_rack_mailexceptions.rb
@@ -70,11 +70,23 @@ def send_notification(exception, env)
env['mail.sent'] = true
return if smtp[:server] == 'example.com'

Net::SMTP.start smtp[:server], smtp[:port], smtp[:domain], smtp[:user_name], smtp[:password], smtp[:authentication] do |server|
mail.to.each do |recipient|
server.send_message mail.to_s, mail.from, recipient
end
server = service.new(smtp[:server], smtp[:port])

if smtp[:enable_starttls_auto] == :auto
server.enable_starttls_auto
elsif smtp[:enable_starttls_auto]
server.enable_starttls
end

server.start smtp[:domain], smtp[:user_name], smtp[:password], smtp[:authentication]

mail.to.each do |recipient|
server.send_message mail.to_s, mail.from, recipient
end
end

def service
Net::SMTP
end

def extract_body(env)
@@ -10,3 +10,11 @@
# :user_name => nil,
# :password => nil
# }
#TEST_SMTP_TLS = {
# :server => 'smtp.gmail.com',
# :domain => 'gmail.com',
# :port => 587,
# :authentication => 'plain',
# :user_name => nil,
# :password => nil,
#}
@@ -91,6 +91,80 @@ def test_exception
STDERR.puts 'WARN: Skipping SMTP tests (edit test/mail_settings.rb to enable)'
end

context 'for tls enabled smtp' do
setup do
@smtp_settings.merge!(TEST_SMTP_TLS)
@mailer =
Rack::MailExceptions.new(@app) do |mail|
mail.to TEST_SMTP_TLS.values_at(:user_name, :domain).join('@')
mail.from 'bar@example.org'
mail.subject '[ERROR] %s'
mail.smtp @smtp_settings.merge( :enable_starttls_auto => true)
end
end

describe 'with :enable_starttls_auto set to :auto' do
specify 'sends mail' do
@mailer.smtp.merge(:enable_starttls_auto => :auto)
lambda { @mailer.call(@env) }.should.raise(TestError)
@env['mail.sent'].should.be true
end
end

describe 'with :enable_starttls_auto set to true' do
specify 'sends mail' do
@mailer.smtp.merge(:enable_starttls_auto => true)
lambda { @mailer.call(@env) }.should.raise(TestError)
@env['mail.sent'].should.be true
end
end
end if defined?(TEST_SMTP_TLS)

describe 'for tls enabled fake smtp' do
class FakeSMTP
def initialize(*args); @@_tls = nil; end
def start(*args); end
def enable_starttls_auto; @@_tls = :auto; end
def enable_starttls; @@_tls = true; end
def send_message(*args); end
def self.tls; @@_tls; end
end

setup do
Rack::MailExceptions.class_eval { def service; FakeSMTP; end}
@mailer =
Rack::MailExceptions.new(@app) do |mail|
mail.to 'foo@example.org'
mail.from 'bar@example.org'
mail.subject '[ERROR] %s'
mail.smtp @smtp_settings.merge( :server => 'server.com')
end
end

context 'with :enable_starttls_auto unset' do
specify 'sends mail' do
@mailer.smtp[:enable_starttls_auto] = nil
lambda { @mailer.call(@env) }.should.raise(TestError)
FakeSMTP.tls.should.be nil
end
end

context 'with :enable_starttls_auto set to true' do
specify 'sends mail' do
@mailer.smtp[:enable_starttls_auto] = true
lambda { @mailer.call(@env) }.should.raise(TestError)
FakeSMTP.tls.should == true
end
end

context 'with :enable_starttls_auto set to :auto' do
specify 'sends mail' do
@mailer.smtp[:enable_starttls_auto] = :auto
lambda { @mailer.call(@env) }.should.raise(TestError)
FakeSMTP.tls.should == :auto
end
end
end
end
rescue LoadError => boom
STDERR.puts "WARN: Skipping Rack::MailExceptions tests (tmail not installed)"

0 comments on commit e9eb1f5

Please sign in to comment.
You can’t perform that action at this time.