2008-01-05 08:32:22 -05:00
|
|
|
require 'abstract_unit'
|
2010-01-16 06:10:11 -05:00
|
|
|
require 'mail'
|
2007-11-07 11:05:17 -05:00
|
|
|
|
2010-01-19 09:34:58 -05:00
|
|
|
class MyCustomDelivery
|
2007-11-07 11:05:17 -05:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
class BogusDelivery
|
|
|
|
def initialize(*)
|
2007-11-07 11:05:17 -05:00
|
|
|
end
|
2010-01-19 09:34:58 -05:00
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
def deliver!(mail)
|
|
|
|
raise "failed"
|
2010-01-16 06:10:11 -05:00
|
|
|
end
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
2010-01-16 06:10:11 -05:00
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
class DefaultsDeliveryMethodsTest < ActiveSupport::TestCase
|
|
|
|
test "default smtp settings" do
|
2010-01-16 06:10:11 -05:00
|
|
|
settings = { :address => "localhost",
|
|
|
|
:port => 25,
|
|
|
|
:domain => 'localhost.localdomain',
|
|
|
|
:user_name => nil,
|
|
|
|
:password => nil,
|
|
|
|
:authentication => nil,
|
|
|
|
:enable_starttls_auto => true }
|
|
|
|
assert_equal settings, ActionMailer::Base.smtp_settings
|
2007-11-07 11:05:17 -05:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
test "default file delivery settings" do
|
2010-01-19 09:34:58 -05:00
|
|
|
settings = {:location => "#{Dir.tmpdir}/mails"}
|
|
|
|
assert_equal settings, ActionMailer::Base.file_settings
|
2010-01-16 06:10:11 -05:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
test "default sendmail settings" do
|
2010-01-16 06:10:11 -05:00
|
|
|
settings = {:location => '/usr/sbin/sendmail',
|
|
|
|
:arguments => '-i -t'}
|
2010-01-19 09:34:58 -05:00
|
|
|
assert_equal settings, ActionMailer::Base.sendmail_settings
|
2007-11-07 11:05:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
class CustomDeliveryMethodsTest < ActiveSupport::TestCase
|
2009-08-07 23:56:54 -04:00
|
|
|
def setup
|
2010-01-24 13:36:42 -05:00
|
|
|
@old_delivery_method = ActionMailer::Base.delivery_method
|
2010-01-19 09:34:58 -05:00
|
|
|
ActionMailer::Base.add_delivery_method :custom, MyCustomDelivery
|
2009-08-07 23:56:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2010-01-24 13:36:42 -05:00
|
|
|
ActionMailer::Base.delivery_method = @old_delivery_method
|
2010-01-31 21:32:28 -05:00
|
|
|
new = ActionMailer::Base.delivery_methods.dup
|
|
|
|
new.delete(:custom)
|
|
|
|
ActionMailer::Base.delivery_methods = new
|
2009-08-07 23:56:54 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
test "allow to add custom delivery method" do
|
2010-01-19 09:34:58 -05:00
|
|
|
ActionMailer::Base.delivery_method = :custom
|
|
|
|
assert_equal :custom, ActionMailer::Base.delivery_method
|
2009-10-28 05:17:59 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
test "allow to customize custom settings" do
|
2010-01-19 09:34:58 -05:00
|
|
|
ActionMailer::Base.custom_settings = { :foo => :bar }
|
|
|
|
assert_equal Hash[:foo => :bar], ActionMailer::Base.custom_settings
|
2009-10-28 05:17:59 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
test "respond to custom settings" do
|
2010-01-19 09:34:58 -05:00
|
|
|
assert_respond_to ActionMailer::Base, :custom_settings
|
|
|
|
assert_respond_to ActionMailer::Base, :custom_settings=
|
2010-01-16 06:10:11 -05:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
test "does not respond to unknown settings" do
|
2010-01-19 09:34:58 -05:00
|
|
|
assert_raise NoMethodError do
|
|
|
|
ActionMailer::Base.another_settings
|
|
|
|
end
|
2009-10-28 05:17:59 -04:00
|
|
|
end
|
|
|
|
end
|
2010-01-24 13:36:42 -05:00
|
|
|
|
|
|
|
class MailDeliveryTest < ActiveSupport::TestCase
|
2010-01-24 13:52:44 -05:00
|
|
|
class DeliveryMailer < ActionMailer::Base
|
2010-01-24 13:36:42 -05:00
|
|
|
DEFAULT_HEADERS = {
|
|
|
|
:to => 'mikel@test.lindsaar.net',
|
|
|
|
:from => 'jose@test.plataformatec.com'
|
|
|
|
}
|
|
|
|
|
|
|
|
def welcome(hash={})
|
|
|
|
mail(DEFAULT_HEADERS.merge(hash))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
ActionMailer::Base.delivery_method = :smtp
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.delivery_method = :smtp
|
|
|
|
DeliveryMailer.perform_deliveries = true
|
|
|
|
DeliveryMailer.raise_delivery_errors = true
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "ActionMailer should be told when Mail gets delivered" do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.deliveries.clear
|
2010-01-24 21:39:48 -05:00
|
|
|
DeliveryMailer.expects(:deliver_mail).once
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.welcome.deliver
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "delivery method can be customized per instance" do
|
2010-01-24 13:52:44 -05:00
|
|
|
email = DeliveryMailer.welcome.deliver
|
2010-01-24 13:36:42 -05:00
|
|
|
assert_instance_of Mail::SMTP, email.delivery_method
|
2010-01-24 13:52:44 -05:00
|
|
|
email = DeliveryMailer.welcome(:delivery_method => :test).deliver
|
2010-01-24 13:36:42 -05:00
|
|
|
assert_instance_of Mail::TestMailer, email.delivery_method
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delivery method can be customized in subclasses not changing the parent" do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.delivery_method = :test
|
2010-01-24 13:36:42 -05:00
|
|
|
assert_equal :smtp, ActionMailer::Base.delivery_method
|
|
|
|
$BREAK = true
|
2010-01-24 13:52:44 -05:00
|
|
|
email = DeliveryMailer.welcome.deliver
|
2010-01-24 13:36:42 -05:00
|
|
|
assert_instance_of Mail::TestMailer, email.delivery_method
|
|
|
|
end
|
|
|
|
|
|
|
|
test "non registered delivery methods raises errors" do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.delivery_method = :unknown
|
2010-01-24 13:36:42 -05:00
|
|
|
assert_raise RuntimeError do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.welcome.deliver
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not perform deliveries if requested" do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.perform_deliveries = false
|
|
|
|
DeliveryMailer.deliveries.clear
|
2010-01-24 21:39:48 -05:00
|
|
|
Mail::Message.any_instance.expects(:deliver!).never
|
|
|
|
DeliveryMailer.welcome.deliver
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-01-24 21:39:48 -05:00
|
|
|
test "does not append the deliveries collection if told not to perform the delivery" do
|
|
|
|
DeliveryMailer.perform_deliveries = false
|
|
|
|
DeliveryMailer.deliveries.clear
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.welcome.deliver
|
|
|
|
assert_equal(0, DeliveryMailer.deliveries.length)
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "raise errors on bogus deliveries" do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.delivery_method = BogusDelivery
|
|
|
|
DeliveryMailer.deliveries.clear
|
2010-01-24 21:39:48 -05:00
|
|
|
assert_raise RuntimeError do
|
|
|
|
DeliveryMailer.welcome.deliver
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not increment the deliveries collection on error" do
|
|
|
|
DeliveryMailer.delivery_method = BogusDelivery
|
|
|
|
DeliveryMailer.deliveries.clear
|
2010-01-24 13:36:42 -05:00
|
|
|
assert_raise RuntimeError do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.welcome.deliver
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
2010-01-24 13:52:44 -05:00
|
|
|
assert_equal(0, DeliveryMailer.deliveries.length)
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not raise errors on bogus deliveries if set" do
|
2010-01-24 21:39:48 -05:00
|
|
|
DeliveryMailer.delivery_method = BogusDelivery
|
|
|
|
DeliveryMailer.raise_delivery_errors = false
|
|
|
|
assert_nothing_raised do
|
|
|
|
DeliveryMailer.welcome.deliver
|
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-01-25 05:47:03 -05:00
|
|
|
test "does not increment the deliveries collection on bogus deliveries" do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.delivery_method = BogusDelivery
|
|
|
|
DeliveryMailer.raise_delivery_errors = false
|
|
|
|
DeliveryMailer.deliveries.clear
|
|
|
|
DeliveryMailer.welcome.deliver
|
2010-01-25 05:47:03 -05:00
|
|
|
assert_equal(0, DeliveryMailer.deliveries.length)
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-01-31 21:32:28 -05:00
|
|
|
end
|