1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix state leaks in actionmailer/test/base_test.rb.

This commit is contained in:
Zuhao Wan 2014-05-01 23:39:04 +08:00
parent bf44fcc6c4
commit 1401637e3f

View file

@ -10,10 +10,15 @@ require 'mailers/proc_mailer'
require 'mailers/asset_mailer' require 'mailers/asset_mailer'
class BaseTest < ActiveSupport::TestCase class BaseTest < ActiveSupport::TestCase
def teardown setup do
ActionMailer::Base.asset_host = nil @original_asset_host = ActionMailer::Base.asset_host
ActionMailer::Base.assets_dir = nil @original_assets_dir = ActionMailer::Base.assets_dir
ActionMailer::Base.preview_interceptors.clear end
teardown do
ActionMailer::Base.asset_host = @original_asset_host
ActionMailer::Base.assets_dir = @original_assets_dir
BaseMailer.deliveries.clear
end end
test "method call to mail does not raise error" do test "method call to mail does not raise error" do
@ -104,6 +109,7 @@ class BaseTest < ActiveSupport::TestCase
test "attachment gets content type from filename" do test "attachment gets content type from filename" do
email = BaseMailer.attachment_with_content email = BaseMailer.attachment_with_content
assert_equal('invoice.pdf', email.attachments[0].filename) assert_equal('invoice.pdf', email.attachments[0].filename)
assert_equal('application/pdf', email.attachments[0].mime_type)
end end
test "attachment with hash" do test "attachment with hash" do
@ -201,26 +207,30 @@ class BaseTest < ActiveSupport::TestCase
end end
test "subject gets default from I18n" do test "subject gets default from I18n" do
BaseMailer.default subject: nil with_default BaseMailer, subject: nil do
email = BaseMailer.welcome(subject: nil) email = BaseMailer.welcome(subject: nil)
assert_equal "Welcome", email.subject assert_equal "Welcome", email.subject
I18n.backend.store_translations('en', base_mailer: {welcome: {subject: "New Subject!"}}) with_translation 'en', base_mailer: {welcome: {subject: "New Subject!"}} do
email = BaseMailer.welcome(subject: nil) email = BaseMailer.welcome(subject: nil)
assert_equal "New Subject!", email.subject assert_equal "New Subject!", email.subject
end end
end
end
test 'default subject can have interpolations' do test 'default subject can have interpolations' do
I18n.backend.store_translations('en', base_mailer: {with_subject_interpolations: {subject: 'Will the real %{rapper_or_impersonator} please stand up?'}}) with_translation 'en', base_mailer: {with_subject_interpolations: {subject: 'Will the real %{rapper_or_impersonator} please stand up?'}} do
email = BaseMailer.with_subject_interpolations email = BaseMailer.with_subject_interpolations
assert_equal 'Will the real Slim Shady please stand up?', email.subject assert_equal 'Will the real Slim Shady please stand up?', email.subject
end end
end
test "translations are scoped properly" do test "translations are scoped properly" do
I18n.backend.store_translations('en', base_mailer: {email_with_translations: {greet_user: "Hello %{name}!"}}) with_translation 'en', base_mailer: {email_with_translations: {greet_user: "Hello %{name}!"}} do
email = BaseMailer.email_with_translations email = BaseMailer.email_with_translations
assert_equal 'Hello lifo!', email.body.encoded assert_equal 'Hello lifo!', email.body.encoded
end end
end
# Implicit multipart # Implicit multipart
test "implicit multipart" do test "implicit multipart" do
@ -407,14 +417,12 @@ class BaseTest < ActiveSupport::TestCase
end end
test "calling just the action should return the generated mail object" do test "calling just the action should return the generated mail object" do
BaseMailer.deliveries.clear
email = BaseMailer.welcome email = BaseMailer.welcome
assert_equal(0, BaseMailer.deliveries.length) assert_equal(0, BaseMailer.deliveries.length)
assert_equal('The first email on new API!', email.subject) assert_equal('The first email on new API!', email.subject)
end end
test "calling deliver on the action should deliver the mail object" do test "calling deliver on the action should deliver the mail object" do
BaseMailer.deliveries.clear
BaseMailer.expects(:deliver_mail).once BaseMailer.expects(:deliver_mail).once
mail = BaseMailer.welcome.deliver mail = BaseMailer.welcome.deliver
assert_equal 'The first email on new API!', mail.subject assert_equal 'The first email on new API!', mail.subject
@ -422,7 +430,6 @@ class BaseTest < ActiveSupport::TestCase
test "calling deliver on the action should increment the deliveries collection if using the test mailer" do test "calling deliver on the action should increment the deliveries collection if using the test mailer" do
BaseMailer.delivery_method = :test BaseMailer.delivery_method = :test
BaseMailer.deliveries.clear
BaseMailer.welcome.deliver BaseMailer.welcome.deliver
assert_equal(1, BaseMailer.deliveries.length) assert_equal(1, BaseMailer.deliveries.length)
end end
@ -442,7 +449,6 @@ class BaseTest < ActiveSupport::TestCase
end end
test "should raise if missing template in implicit render" do test "should raise if missing template in implicit render" do
BaseMailer.deliveries.clear
assert_raises ActionView::MissingTemplate do assert_raises ActionView::MissingTemplate do
BaseMailer.implicit_different_template('missing_template').deliver BaseMailer.implicit_different_template('missing_template').deliver
end end
@ -479,18 +485,17 @@ class BaseTest < ActiveSupport::TestCase
end end
test "assets tags should use a Mailer's asset_host settings when available" do test "assets tags should use a Mailer's asset_host settings when available" do
begin
ActionMailer::Base.config.asset_host = "http://global.com" ActionMailer::Base.config.asset_host = "http://global.com"
ActionMailer::Base.config.assets_dir = "global/" ActionMailer::Base.config.assets_dir = "global/"
AssetMailer.asset_host = "http://local.com" TempAssetMailer = Class.new(AssetMailer) do
self.mailer_name = "asset_mailer"
self.asset_host = "http://local.com"
end
mail = AssetMailer.welcome mail = TempAssetMailer.welcome
assert_equal(%{<img alt="Dummy" src="http://local.com/images/dummy.png" />}, mail.body.to_s.strip) assert_equal(%{<img alt="Dummy" src="http://local.com/images/dummy.png" />}, mail.body.to_s.strip)
ensure
AssetMailer.asset_host = ActionMailer::Base.config.asset_host
end
end end
test 'the view is not rendered when mail was never called' do test 'the view is not rendered when mail was never called' do
@ -518,33 +523,41 @@ class BaseTest < ActiveSupport::TestCase
end end
test "you can register an observer to the mail object that gets informed on email delivery" do test "you can register an observer to the mail object that gets informed on email delivery" do
mail_side_effects do
ActionMailer::Base.register_observer(MyObserver) ActionMailer::Base.register_observer(MyObserver)
mail = BaseMailer.welcome mail = BaseMailer.welcome
MyObserver.expects(:delivered_email).with(mail) MyObserver.expects(:delivered_email).with(mail)
mail.deliver mail.deliver
end end
end
test "you can register an observer using its stringified name to the mail object that gets informed on email delivery" do test "you can register an observer using its stringified name to the mail object that gets informed on email delivery" do
mail_side_effects do
ActionMailer::Base.register_observer("BaseTest::MyObserver") ActionMailer::Base.register_observer("BaseTest::MyObserver")
mail = BaseMailer.welcome mail = BaseMailer.welcome
MyObserver.expects(:delivered_email).with(mail) MyObserver.expects(:delivered_email).with(mail)
mail.deliver mail.deliver
end end
end
test "you can register an observer using its symbolized underscored name to the mail object that gets informed on email delivery" do test "you can register an observer using its symbolized underscored name to the mail object that gets informed on email delivery" do
mail_side_effects do
ActionMailer::Base.register_observer(:"base_test/my_observer") ActionMailer::Base.register_observer(:"base_test/my_observer")
mail = BaseMailer.welcome mail = BaseMailer.welcome
MyObserver.expects(:delivered_email).with(mail) MyObserver.expects(:delivered_email).with(mail)
mail.deliver mail.deliver
end end
end
test "you can register multiple observers to the mail object that both get informed on email delivery" do test "you can register multiple observers to the mail object that both get informed on email delivery" do
mail_side_effects do
ActionMailer::Base.register_observers("BaseTest::MyObserver", MySecondObserver) ActionMailer::Base.register_observers("BaseTest::MyObserver", MySecondObserver)
mail = BaseMailer.welcome mail = BaseMailer.welcome
MyObserver.expects(:delivered_email).with(mail) MyObserver.expects(:delivered_email).with(mail)
MySecondObserver.expects(:delivered_email).with(mail) MySecondObserver.expects(:delivered_email).with(mail)
mail.deliver mail.deliver
end end
end
class MyInterceptor class MyInterceptor
def self.delivering_email(mail); end def self.delivering_email(mail); end
@ -556,72 +569,41 @@ class BaseTest < ActiveSupport::TestCase
def self.previewing_email(mail); end def self.previewing_email(mail); end
end end
class BaseMailerPreview < ActionMailer::Preview
def welcome
BaseMailer.welcome
end
end
test "you can register an interceptor to the mail object that gets passed the mail object before delivery" do test "you can register an interceptor to the mail object that gets passed the mail object before delivery" do
mail_side_effects do
ActionMailer::Base.register_interceptor(MyInterceptor) ActionMailer::Base.register_interceptor(MyInterceptor)
mail = BaseMailer.welcome mail = BaseMailer.welcome
MyInterceptor.expects(:delivering_email).with(mail) MyInterceptor.expects(:delivering_email).with(mail)
mail.deliver mail.deliver
end end
end
test "you can register an interceptor using its stringified name to the mail object that gets passed the mail object before delivery" do test "you can register an interceptor using its stringified name to the mail object that gets passed the mail object before delivery" do
mail_side_effects do
ActionMailer::Base.register_interceptor("BaseTest::MyInterceptor") ActionMailer::Base.register_interceptor("BaseTest::MyInterceptor")
mail = BaseMailer.welcome mail = BaseMailer.welcome
MyInterceptor.expects(:delivering_email).with(mail) MyInterceptor.expects(:delivering_email).with(mail)
mail.deliver mail.deliver
end end
end
test "you can register an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before delivery" do test "you can register an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before delivery" do
mail_side_effects do
ActionMailer::Base.register_interceptor(:"base_test/my_interceptor") ActionMailer::Base.register_interceptor(:"base_test/my_interceptor")
mail = BaseMailer.welcome mail = BaseMailer.welcome
MyInterceptor.expects(:delivering_email).with(mail) MyInterceptor.expects(:delivering_email).with(mail)
mail.deliver mail.deliver
end end
end
test "you can register multiple interceptors to the mail object that both get passed the mail object before delivery" do test "you can register multiple interceptors to the mail object that both get passed the mail object before delivery" do
mail_side_effects do
ActionMailer::Base.register_interceptors("BaseTest::MyInterceptor", MySecondInterceptor) ActionMailer::Base.register_interceptors("BaseTest::MyInterceptor", MySecondInterceptor)
mail = BaseMailer.welcome mail = BaseMailer.welcome
MyInterceptor.expects(:delivering_email).with(mail) MyInterceptor.expects(:delivering_email).with(mail)
MySecondInterceptor.expects(:delivering_email).with(mail) MySecondInterceptor.expects(:delivering_email).with(mail)
mail.deliver mail.deliver
end end
test "you can register a preview interceptor to the mail object that gets passed the mail object before previewing" do
ActionMailer::Base.register_preview_interceptor(MyInterceptor)
mail = BaseMailer.welcome
BaseMailerPreview.any_instance.stubs(:welcome).returns(mail)
MyInterceptor.expects(:previewing_email).with(mail)
BaseMailerPreview.call(:welcome)
end
test "you can register a preview interceptor using its stringified name to the mail object that gets passed the mail object before previewing" do
ActionMailer::Base.register_preview_interceptor("BaseTest::MyInterceptor")
mail = BaseMailer.welcome
BaseMailerPreview.any_instance.stubs(:welcome).returns(mail)
MyInterceptor.expects(:previewing_email).with(mail)
BaseMailerPreview.call(:welcome)
end
test "you can register an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before previewing" do
ActionMailer::Base.register_preview_interceptor(:"base_test/my_interceptor")
mail = BaseMailer.welcome
BaseMailerPreview.any_instance.stubs(:welcome).returns(mail)
MyInterceptor.expects(:previewing_email).with(mail)
BaseMailerPreview.call(:welcome)
end
test "you can register multiple preview interceptors to the mail object that both get passed the mail object before previewing" do
ActionMailer::Base.register_preview_interceptors("BaseTest::MyInterceptor", MySecondInterceptor)
mail = BaseMailer.welcome
BaseMailerPreview.any_instance.stubs(:welcome).returns(mail)
MyInterceptor.expects(:previewing_email).with(mail)
MySecondInterceptor.expects(:previewing_email).with(mail)
BaseMailerPreview.call(:welcome)
end end
test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do
@ -770,4 +752,77 @@ class BaseTest < ActiveSupport::TestCase
ensure ensure
klass.default_params = old klass.default_params = old
end end
# A simple hack to restore the observers and interceptors for Mail, as it
# does not have an unregister API yet.
def mail_side_effects
old_observers = Mail.class_variable_get(:@@delivery_notification_observers)
old_delivery_interceptors = Mail.class_variable_get(:@@delivery_interceptors)
yield
ensure
Mail.class_variable_set(:@@delivery_notification_observers, old_observers)
Mail.class_variable_set(:@@delivery_interceptors, old_delivery_interceptors)
end
def with_translation(locale, data)
I18n.backend.store_translations(locale, data)
yield
ensure
I18n.backend.reload!
end
end
class BasePreviewInterceptorsTest < ActiveSupport::TestCase
teardown do
ActionMailer::Base.preview_interceptors.clear
end
class BaseMailerPreview < ActionMailer::Preview
def welcome
BaseMailer.welcome
end
end
class MyInterceptor
def self.delivering_email(mail); end
def self.previewing_email(mail); end
end
class MySecondInterceptor
def self.delivering_email(mail); end
def self.previewing_email(mail); end
end
test "you can register a preview interceptor to the mail object that gets passed the mail object before previewing" do
ActionMailer::Base.register_preview_interceptor(MyInterceptor)
mail = BaseMailer.welcome
BaseMailerPreview.any_instance.stubs(:welcome).returns(mail)
MyInterceptor.expects(:previewing_email).with(mail)
BaseMailerPreview.call(:welcome)
end
test "you can register a preview interceptor using its stringified name to the mail object that gets passed the mail object before previewing" do
ActionMailer::Base.register_preview_interceptor("BasePreviewInterceptorsTest::MyInterceptor")
mail = BaseMailer.welcome
BaseMailerPreview.any_instance.stubs(:welcome).returns(mail)
MyInterceptor.expects(:previewing_email).with(mail)
BaseMailerPreview.call(:welcome)
end
test "you can register an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before previewing" do
ActionMailer::Base.register_preview_interceptor(:"base_preview_interceptors_test/my_interceptor")
mail = BaseMailer.welcome
BaseMailerPreview.any_instance.stubs(:welcome).returns(mail)
MyInterceptor.expects(:previewing_email).with(mail)
BaseMailerPreview.call(:welcome)
end
test "you can register multiple preview interceptors to the mail object that both get passed the mail object before previewing" do
ActionMailer::Base.register_preview_interceptors("BasePreviewInterceptorsTest::MyInterceptor", MySecondInterceptor)
mail = BaseMailer.welcome
BaseMailerPreview.any_instance.stubs(:welcome).returns(mail)
MyInterceptor.expects(:previewing_email).with(mail)
MySecondInterceptor.expects(:previewing_email).with(mail)
BaseMailerPreview.call(:welcome)
end
end end