2016-08-06 13:03:39 -04:00
|
|
|
require "abstract_unit"
|
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
|
|
|
|
|
2012-08-20 09:22:12 -04:00
|
|
|
class MyOptionedDelivery
|
|
|
|
attr_reader :options
|
|
|
|
def initialize(options)
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
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
|
2012-10-07 13:54:14 -04:00
|
|
|
settings = { address: "localhost",
|
|
|
|
port: 25,
|
2016-08-06 13:03:39 -04:00
|
|
|
domain: "localhost.localdomain",
|
2012-10-07 13:54:14 -04:00
|
|
|
user_name: nil,
|
|
|
|
password: nil,
|
|
|
|
authentication: nil,
|
|
|
|
enable_starttls_auto: true }
|
2010-01-16 06:10:11 -05:00
|
|
|
assert_equal settings, ActionMailer::Base.smtp_settings
|
2007-11-07 11:05:17 -05:00
|
|
|
end
|
|
|
|
|
2015-11-04 16:32:53 -05:00
|
|
|
test "default file delivery settings (with Rails.root)" do
|
2016-08-16 03:30:11 -04:00
|
|
|
settings = { location: "#{Rails.root}/tmp/mails" }
|
2010-01-19 09:34:58 -05:00
|
|
|
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
|
2014-02-09 13:41:41 -05:00
|
|
|
settings = {
|
2016-08-06 13:03:39 -04:00
|
|
|
location: "/usr/sbin/sendmail",
|
|
|
|
arguments: "-i"
|
2014-02-09 13:41:41 -05:00
|
|
|
}
|
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
|
2014-05-12 23:01:11 -04:00
|
|
|
setup do
|
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
|
|
|
|
|
2014-05-12 23:01:11 -04:00
|
|
|
teardown do
|
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
|
2012-10-07 13:54:14 -04: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 = {
|
2016-08-06 13:03:39 -04:00
|
|
|
to: "mikel@test.lindsaar.net",
|
|
|
|
from: "jose@test.plataformatec.com"
|
2010-01-24 13:36:42 -05:00
|
|
|
}
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def welcome(hash = {})
|
2010-01-24 13:36:42 -05:00
|
|
|
mail(DEFAULT_HEADERS.merge(hash))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-12 23:01:11 -04:00
|
|
|
setup do
|
|
|
|
@old_delivery_method = DeliveryMailer.delivery_method
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
2014-05-12 23:01:11 -04:00
|
|
|
teardown do
|
|
|
|
DeliveryMailer.delivery_method = @old_delivery_method
|
|
|
|
DeliveryMailer.deliveries.clear
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "ActionMailer should be told when Mail gets delivered" do
|
2015-08-20 01:57:49 -04:00
|
|
|
DeliveryMailer.delivery_method = :test
|
|
|
|
assert_called(DeliveryMailer, :deliver_mail) do
|
|
|
|
DeliveryMailer.welcome.deliver_now
|
|
|
|
end
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "delivery method can be customized per instance" do
|
2015-08-20 01:57:49 -04:00
|
|
|
stub_any_instance(Mail::SMTP, instance: Mail::SMTP.new({})) do |instance|
|
|
|
|
assert_called(instance, :deliver!) do
|
|
|
|
email = DeliveryMailer.welcome.deliver_now
|
|
|
|
assert_instance_of Mail::SMTP, email.delivery_method
|
|
|
|
email = DeliveryMailer.welcome(delivery_method: :test).deliver_now
|
|
|
|
assert_instance_of Mail::TestMailer, email.delivery_method
|
|
|
|
end
|
|
|
|
end
|
2010-01-24 13:36:42 -05:00
|
|
|
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
|
2014-08-20 04:34:57 -04:00
|
|
|
email = DeliveryMailer.welcome.deliver_now
|
2010-01-24 13:36:42 -05:00
|
|
|
assert_instance_of Mail::TestMailer, email.delivery_method
|
|
|
|
end
|
|
|
|
|
2012-08-20 09:22:12 -04:00
|
|
|
test "delivery method options default to class level options" do
|
2016-08-16 03:30:11 -04:00
|
|
|
default_options = { a: "b" }
|
2012-08-20 09:22:12 -04:00
|
|
|
ActionMailer::Base.add_delivery_method :optioned, MyOptionedDelivery, default_options
|
2012-10-07 13:54:14 -04:00
|
|
|
mail_instance = DeliveryMailer.welcome(delivery_method: :optioned)
|
2012-08-20 09:22:12 -04:00
|
|
|
assert_equal default_options, mail_instance.delivery_method.options
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delivery method options can be overridden per mail instance" do
|
2016-08-16 03:30:11 -04:00
|
|
|
default_options = { a: "b" }
|
2012-08-20 09:22:12 -04:00
|
|
|
ActionMailer::Base.add_delivery_method :optioned, MyOptionedDelivery, default_options
|
2016-08-16 03:30:11 -04:00
|
|
|
overridden_options = { a: "a" }
|
2012-10-07 13:54:14 -04:00
|
|
|
mail_instance = DeliveryMailer.welcome(delivery_method: :optioned, delivery_method_options: overridden_options)
|
2012-08-20 09:22:12 -04:00
|
|
|
assert_equal overridden_options, mail_instance.delivery_method.options
|
|
|
|
end
|
|
|
|
|
|
|
|
test "default delivery options can be overridden per mail instance" do
|
2014-02-09 13:41:41 -05:00
|
|
|
settings = {
|
|
|
|
address: "localhost",
|
|
|
|
port: 25,
|
2016-08-06 13:03:39 -04:00
|
|
|
domain: "localhost.localdomain",
|
2014-02-09 13:41:41 -05:00
|
|
|
user_name: nil,
|
|
|
|
password: nil,
|
|
|
|
authentication: nil,
|
|
|
|
enable_starttls_auto: true
|
|
|
|
}
|
2012-08-20 09:22:12 -04:00
|
|
|
assert_equal settings, ActionMailer::Base.smtp_settings
|
2016-08-16 03:30:11 -04:00
|
|
|
overridden_options = { user_name: "overridden", password: "somethingobtuse" }
|
2012-10-07 13:54:14 -04:00
|
|
|
mail_instance = DeliveryMailer.welcome(delivery_method_options: overridden_options)
|
2012-08-20 09:22:12 -04:00
|
|
|
delivery_method_instance = mail_instance.delivery_method
|
|
|
|
assert_equal "overridden", delivery_method_instance.settings[:user_name]
|
|
|
|
assert_equal "somethingobtuse", delivery_method_instance.settings[:password]
|
|
|
|
assert_equal delivery_method_instance.settings.merge(overridden_options), delivery_method_instance.settings
|
2013-07-25 15:00:56 -04:00
|
|
|
|
|
|
|
# make sure that overriding delivery method options per mail instance doesn't affect the Base setting
|
|
|
|
assert_equal settings, ActionMailer::Base.smtp_settings
|
2012-08-20 09:22:12 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
test "non registered delivery methods raises errors" do
|
2010-01-24 13:52:44 -05:00
|
|
|
DeliveryMailer.delivery_method = :unknown
|
2015-09-22 09:41:03 -04:00
|
|
|
error = assert_raise RuntimeError do
|
2014-08-20 04:34:57 -04:00
|
|
|
DeliveryMailer.welcome.deliver_now
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
2015-09-22 09:41:03 -04:00
|
|
|
assert_equal "Invalid delivery method :unknown", error.message
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
2014-02-09 13:41:41 -05:00
|
|
|
test "undefined delivery methods raises errors" do
|
|
|
|
DeliveryMailer.delivery_method = nil
|
2015-09-22 09:41:03 -04:00
|
|
|
error = assert_raise RuntimeError do
|
2014-08-20 04:34:57 -04:00
|
|
|
DeliveryMailer.welcome.deliver_now
|
2014-02-09 13:41:41 -05:00
|
|
|
end
|
2015-09-22 09:41:03 -04:00
|
|
|
assert_equal "Delivery method cannot be nil", error.message
|
2014-02-09 13:41:41 -05:00
|
|
|
end
|
|
|
|
|
2010-01-24 13:36:42 -05:00
|
|
|
test "does not perform deliveries if requested" do
|
2014-05-12 23:01:11 -04:00
|
|
|
old_perform_deliveries = DeliveryMailer.perform_deliveries
|
|
|
|
begin
|
|
|
|
DeliveryMailer.perform_deliveries = false
|
2015-08-20 01:57:49 -04:00
|
|
|
stub_any_instance(Mail::Message) do |instance|
|
|
|
|
assert_not_called(instance, :deliver!) do
|
|
|
|
DeliveryMailer.welcome.deliver_now
|
|
|
|
end
|
|
|
|
end
|
2014-05-12 23:01:11 -04:00
|
|
|
ensure
|
|
|
|
DeliveryMailer.perform_deliveries = old_perform_deliveries
|
|
|
|
end
|
2010-01-24 21:39:48 -05:00
|
|
|
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
|
2014-05-12 23:01:11 -04:00
|
|
|
old_perform_deliveries = DeliveryMailer.perform_deliveries
|
|
|
|
begin
|
|
|
|
DeliveryMailer.perform_deliveries = false
|
2014-08-20 04:34:57 -04:00
|
|
|
DeliveryMailer.welcome.deliver_now
|
2014-05-12 23:01:11 -04:00
|
|
|
assert_equal [], DeliveryMailer.deliveries
|
|
|
|
ensure
|
|
|
|
DeliveryMailer.perform_deliveries = old_perform_deliveries
|
|
|
|
end
|
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
|
2010-01-24 21:39:48 -05:00
|
|
|
assert_raise RuntimeError do
|
2014-08-20 04:34:57 -04:00
|
|
|
DeliveryMailer.welcome.deliver_now
|
2010-01-24 21:39:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not increment the deliveries collection on error" do
|
|
|
|
DeliveryMailer.delivery_method = BogusDelivery
|
2010-01-24 13:36:42 -05:00
|
|
|
assert_raise RuntimeError do
|
2014-08-20 04:34:57 -04:00
|
|
|
DeliveryMailer.welcome.deliver_now
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
2014-05-12 23:01:11 -04:00
|
|
|
assert_equal [], DeliveryMailer.deliveries
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not raise errors on bogus deliveries if set" do
|
2014-05-12 23:01:11 -04:00
|
|
|
old_raise_delivery_errors = DeliveryMailer.raise_delivery_errors
|
|
|
|
begin
|
|
|
|
DeliveryMailer.delivery_method = BogusDelivery
|
|
|
|
DeliveryMailer.raise_delivery_errors = false
|
|
|
|
assert_nothing_raised do
|
2014-08-20 04:34:57 -04:00
|
|
|
DeliveryMailer.welcome.deliver_now
|
2014-05-12 23:01:11 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
DeliveryMailer.raise_delivery_errors = old_raise_delivery_errors
|
2010-01-24 21:39:48 -05:00
|
|
|
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
|
2014-05-12 23:01:11 -04:00
|
|
|
old_raise_delivery_errors = DeliveryMailer.raise_delivery_errors
|
|
|
|
begin
|
|
|
|
DeliveryMailer.delivery_method = BogusDelivery
|
|
|
|
DeliveryMailer.raise_delivery_errors = false
|
2014-08-20 04:34:57 -04:00
|
|
|
DeliveryMailer.welcome.deliver_now
|
2014-05-12 23:01:11 -04:00
|
|
|
assert_equal [], DeliveryMailer.deliveries
|
|
|
|
ensure
|
|
|
|
DeliveryMailer.raise_delivery_errors = old_raise_delivery_errors
|
|
|
|
end
|
2010-01-24 13:36:42 -05:00
|
|
|
end
|
2010-01-31 21:32:28 -05:00
|
|
|
end
|