2016-08-06 13:03:39 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "set"
|
2012-03-10 16:13:12 -05:00
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
require "action_dispatch"
|
|
|
|
require "active_support/time"
|
2010-01-20 07:46:59 -05:00
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
require "mailers/base_mailer"
|
|
|
|
require "mailers/proc_mailer"
|
|
|
|
require "mailers/asset_mailer"
|
2010-07-23 08:42:21 -04:00
|
|
|
|
2010-01-22 05:57:54 -05:00
|
|
|
class BaseTest < ActiveSupport::TestCase
|
2014-10-07 19:01:40 -04:00
|
|
|
include Rails::Dom::Testing::Assertions::DomAssertions
|
2014-10-07 18:40:59 -04:00
|
|
|
|
2014-05-01 11:39:04 -04:00
|
|
|
setup do
|
2014-08-13 10:00:19 -04:00
|
|
|
@original_delivery_method = ActionMailer::Base.delivery_method
|
|
|
|
ActionMailer::Base.delivery_method = :test
|
2014-05-01 11:39:04 -04:00
|
|
|
@original_asset_host = ActionMailer::Base.asset_host
|
|
|
|
@original_assets_dir = ActionMailer::Base.assets_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActionMailer::Base.asset_host = @original_asset_host
|
|
|
|
ActionMailer::Base.assets_dir = @original_assets_dir
|
|
|
|
BaseMailer.deliveries.clear
|
2014-08-13 10:00:19 -04:00
|
|
|
ActionMailer::Base.delivery_method = @original_delivery_method
|
2010-05-01 21:30:10 -04:00
|
|
|
end
|
2010-01-20 07:46:59 -05:00
|
|
|
|
2010-01-22 05:57:54 -05:00
|
|
|
test "method call to mail does not raise error" do
|
2010-01-26 08:18:40 -05:00
|
|
|
assert_nothing_raised { BaseMailer.welcome }
|
2010-01-20 07:46:59 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 08:38:41 -05:00
|
|
|
# Basic mail usage without block
|
2010-01-22 05:57:54 -05:00
|
|
|
test "mail() should set the headers of the mail message" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.welcome
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal(["system@test.lindsaar.net"], email.to)
|
|
|
|
assert_equal(["jose@test.plataformatec.com"], email.from)
|
|
|
|
assert_equal("The first email on new API!", email.subject)
|
2010-01-20 07:46:59 -05:00
|
|
|
end
|
2010-01-22 05:57:54 -05:00
|
|
|
|
2010-01-24 18:37:12 -05:00
|
|
|
test "mail() with from overwrites the class level default" do
|
2016-08-06 13:03:39 -04:00
|
|
|
email = BaseMailer.welcome(from: "someone@example.com",
|
|
|
|
to: "another@example.org")
|
|
|
|
assert_equal(["someone@example.com"], email.from)
|
|
|
|
assert_equal(["another@example.org"], email.to)
|
2010-01-24 18:37:12 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 05:57:54 -05:00
|
|
|
test "mail() with bcc, cc, content_type, charset, mime_version, reply_to and date" do
|
2011-03-01 19:55:22 -05:00
|
|
|
time = Time.now.beginning_of_day.to_datetime
|
2016-08-06 13:03:39 -04:00
|
|
|
email = BaseMailer.welcome(bcc: "bcc@test.lindsaar.net",
|
|
|
|
cc: "cc@test.lindsaar.net",
|
|
|
|
content_type: "multipart/mixed",
|
|
|
|
charset: "iso-8559-1",
|
|
|
|
mime_version: "2.0",
|
|
|
|
reply_to: "reply-to@test.lindsaar.net",
|
2012-10-07 13:54:14 -04:00
|
|
|
date: time)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal(["bcc@test.lindsaar.net"], email.bcc)
|
|
|
|
assert_equal(["cc@test.lindsaar.net"], email.cc)
|
|
|
|
assert_equal("multipart/mixed; charset=iso-8559-1", email.content_type)
|
|
|
|
assert_equal("iso-8559-1", email.charset)
|
|
|
|
assert_equal("2.0", email.mime_version)
|
|
|
|
assert_equal(["reply-to@test.lindsaar.net"], email.reply_to)
|
2011-03-01 19:55:22 -05:00
|
|
|
assert_equal(time, email.date)
|
2010-01-20 07:46:59 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 07:27:20 -05:00
|
|
|
test "mail() renders the template using the method being processed" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.welcome
|
2010-01-22 07:27:20 -05:00
|
|
|
assert_equal("Welcome", email.body.encoded)
|
|
|
|
end
|
|
|
|
|
2010-01-23 05:37:34 -05:00
|
|
|
test "can pass in :body to the mail method hash" do
|
2012-10-07 13:54:14 -04:00
|
|
|
email = BaseMailer.welcome(body: "Hello there")
|
2010-01-23 06:20:20 -05:00
|
|
|
assert_equal("text/plain", email.mime_type)
|
2010-01-23 05:37:34 -05:00
|
|
|
assert_equal("Hello there", email.body.encoded)
|
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "should set template content type if mail has only one part" do
|
|
|
|
mail = BaseMailer.html_only
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("text/html", mail.mime_type)
|
2010-03-19 12:20:15 -04:00
|
|
|
mail = BaseMailer.plain_text_only
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("text/plain", mail.mime_type)
|
2010-03-19 12:20:15 -04:00
|
|
|
end
|
|
|
|
|
2010-01-22 08:38:41 -05:00
|
|
|
# Custom headers
|
2010-01-22 05:57:54 -05:00
|
|
|
test "custom headers" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.welcome
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("Not SPAM", email["X-SPAM"].decoded)
|
2010-01-21 04:03:55 -05:00
|
|
|
end
|
|
|
|
|
2010-01-26 10:00:24 -05:00
|
|
|
test "can pass random headers in as a hash to mail" do
|
2016-08-16 03:30:11 -04:00
|
|
|
hash = { "X-Special-Domain-Specific-Header" => "SecretValue",
|
2016-08-06 13:03:39 -04:00
|
|
|
"In-Reply-To" => "1234@mikel.me.com" }
|
2010-02-19 04:51:17 -05:00
|
|
|
mail = BaseMailer.welcome(hash)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("SecretValue", mail["X-Special-Domain-Specific-Header"].decoded)
|
|
|
|
assert_equal("1234@mikel.me.com", mail["In-Reply-To"].decoded)
|
2010-01-26 10:00:24 -05:00
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "can pass random headers in as a hash to headers" do
|
2016-08-16 03:30:11 -04:00
|
|
|
hash = { "X-Special-Domain-Specific-Header" => "SecretValue",
|
2016-08-06 13:03:39 -04:00
|
|
|
"In-Reply-To" => "1234@mikel.me.com" }
|
2010-02-19 04:51:17 -05:00
|
|
|
mail = BaseMailer.welcome_with_headers(hash)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("SecretValue", mail["X-Special-Domain-Specific-Header"].decoded)
|
|
|
|
assert_equal("1234@mikel.me.com", mail["In-Reply-To"].decoded)
|
2010-01-26 10:00:24 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 08:38:41 -05:00
|
|
|
# Attachments
|
2010-01-22 05:57:54 -05:00
|
|
|
test "attachment with content" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.attachment_with_content
|
2010-01-21 04:03:55 -05:00
|
|
|
assert_equal(1, email.attachments.length)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("invoice.pdf", email.attachments[0].filename)
|
|
|
|
assert_equal("This is test File content", email.attachments["invoice.pdf"].decoded)
|
2010-01-21 04:03:55 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 05:57:54 -05:00
|
|
|
test "attachment gets content type from filename" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.attachment_with_content
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("invoice.pdf", email.attachments[0].filename)
|
|
|
|
assert_equal("application/pdf", email.attachments[0].mime_type)
|
2010-01-21 04:03:55 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 05:57:54 -05:00
|
|
|
test "attachment with hash" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.attachment_with_hash
|
2010-01-22 05:57:54 -05:00
|
|
|
assert_equal(1, email.attachments.length)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("invoice.jpg", email.attachments[0].filename)
|
2010-01-27 21:37:01 -05:00
|
|
|
expected = "\312\213\254\232)b"
|
2011-12-25 06:34:58 -05:00
|
|
|
expected.force_encoding(Encoding::BINARY)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal expected, email.attachments["invoice.jpg"].decoded
|
2010-03-28 02:03:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "attachment with hash using default mail encoding" do
|
|
|
|
email = BaseMailer.attachment_with_hash_default_encoding
|
|
|
|
assert_equal(1, email.attachments.length)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("invoice.jpg", email.attachments[0].filename)
|
2010-03-28 02:03:33 -04:00
|
|
|
expected = "\312\213\254\232)b"
|
2011-12-25 06:34:58 -05:00
|
|
|
expected.force_encoding(Encoding::BINARY)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal expected, email.attachments["invoice.jpg"].decoded
|
2010-01-22 05:57:54 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 08:38:41 -05:00
|
|
|
test "sets mime type to multipart/mixed when attachment is included" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.attachment_with_content
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal(1, email.attachments.length)
|
|
|
|
assert_equal("multipart/mixed", email.mime_type)
|
|
|
|
end
|
2010-01-22 07:27:20 -05:00
|
|
|
|
2010-01-22 08:38:41 -05:00
|
|
|
test "adds the rendered template as part" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.attachment_with_content
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal(2, email.parts.length)
|
2010-01-23 06:20:20 -05:00
|
|
|
assert_equal("multipart/mixed", email.mime_type)
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal("text/html", email.parts[0].mime_type)
|
|
|
|
assert_equal("Attachment with content", email.parts[0].body.encoded)
|
|
|
|
assert_equal("application/pdf", email.parts[1].mime_type)
|
|
|
|
assert_equal("VGhpcyBpcyB0ZXN0IEZpbGUgY29udGVudA==\r\n", email.parts[1].body.encoded)
|
|
|
|
end
|
|
|
|
|
2010-01-23 06:20:20 -05:00
|
|
|
test "adds the given :body as part" do
|
2012-10-07 13:54:14 -04:00
|
|
|
email = BaseMailer.attachment_with_content(body: "I'm the eggman")
|
2010-01-23 06:20:20 -05:00
|
|
|
assert_equal(2, email.parts.length)
|
|
|
|
assert_equal("multipart/mixed", email.mime_type)
|
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("I'm the eggman", email.parts[0].body.encoded)
|
|
|
|
assert_equal("application/pdf", email.parts[1].mime_type)
|
|
|
|
assert_equal("VGhpcyBpcyB0ZXN0IEZpbGUgY29udGVudA==\r\n", email.parts[1].body.encoded)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-06-07 21:54:53 -04:00
|
|
|
test "can embed an inline attachment" do
|
|
|
|
email = BaseMailer.inline_attachment
|
|
|
|
# Need to call #encoded to force the JIT sort on parts
|
|
|
|
email.encoded
|
|
|
|
assert_equal(2, email.parts.length)
|
|
|
|
assert_equal("multipart/related", email.mime_type)
|
|
|
|
assert_equal("multipart/alternative", email.parts[0].mime_type)
|
2011-03-06 14:03:06 -05:00
|
|
|
assert_equal("text/plain", email.parts[0].parts[0].mime_type)
|
|
|
|
assert_equal("text/html", email.parts[0].parts[1].mime_type)
|
2010-06-07 21:54:53 -04:00
|
|
|
assert_equal("logo.png", email.parts[1].filename)
|
|
|
|
end
|
2010-01-23 06:20:20 -05:00
|
|
|
|
2010-01-22 08:38:41 -05:00
|
|
|
# Defaults values
|
2010-01-22 05:57:54 -05:00
|
|
|
test "uses default charset from class" do
|
2012-10-07 13:54:14 -04:00
|
|
|
with_default BaseMailer, charset: "US-ASCII" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.welcome
|
2010-01-22 05:57:54 -05:00
|
|
|
assert_equal("US-ASCII", email.charset)
|
|
|
|
|
2012-10-07 13:54:14 -04:00
|
|
|
email = BaseMailer.welcome(charset: "iso-8559-1")
|
2010-01-22 05:57:54 -05:00
|
|
|
assert_equal("iso-8559-1", email.charset)
|
|
|
|
end
|
2010-01-21 04:03:55 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 08:38:41 -05:00
|
|
|
test "uses default content type from class" do
|
2012-10-07 13:54:14 -04:00
|
|
|
with_default BaseMailer, content_type: "text/html" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.welcome
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal("text/html", email.mime_type)
|
|
|
|
|
2012-10-07 13:54:14 -04:00
|
|
|
email = BaseMailer.welcome(content_type: "text/plain")
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal("text/plain", email.mime_type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-22 05:57:54 -05:00
|
|
|
test "uses default mime version from class" do
|
2012-10-07 13:54:14 -04:00
|
|
|
with_default BaseMailer, mime_version: "2.0" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.welcome
|
2010-01-22 05:57:54 -05:00
|
|
|
assert_equal("2.0", email.mime_version)
|
|
|
|
|
2012-10-07 13:54:14 -04:00
|
|
|
email = BaseMailer.welcome(mime_version: "1.0")
|
2010-01-22 05:57:54 -05:00
|
|
|
assert_equal("1.0", email.mime_version)
|
|
|
|
end
|
2010-01-20 07:46:59 -05:00
|
|
|
end
|
|
|
|
|
2010-01-26 08:06:19 -05:00
|
|
|
test "uses random default headers from class" do
|
2010-02-19 04:51:17 -05:00
|
|
|
with_default BaseMailer, "X-Custom" => "Custom" do
|
|
|
|
email = BaseMailer.welcome
|
|
|
|
assert_equal("Custom", email["X-Custom"].decoded)
|
2010-01-26 05:21:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-22 07:27:20 -05:00
|
|
|
test "subject gets default from I18n" do
|
2014-05-01 11:39:04 -04:00
|
|
|
with_default BaseMailer, subject: nil do
|
|
|
|
email = BaseMailer.welcome(subject: nil)
|
|
|
|
assert_equal "Welcome", email.subject
|
2010-01-22 05:57:54 -05:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
with_translation "en", base_mailer: { welcome: { subject: "New Subject!" } } do
|
2014-05-01 11:39:04 -04:00
|
|
|
email = BaseMailer.welcome(subject: nil)
|
|
|
|
assert_equal "New Subject!", email.subject
|
|
|
|
end
|
|
|
|
end
|
2010-01-22 07:27:20 -05:00
|
|
|
end
|
2010-01-22 07:56:06 -05:00
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "default subject can have interpolations" do
|
2016-08-16 03:30:11 -04:00
|
|
|
with_translation "en", base_mailer: { with_subject_interpolations: { subject: "Will the real %{rapper_or_impersonator} please stand up?" } } do
|
2014-05-01 11:39:04 -04:00
|
|
|
email = BaseMailer.with_subject_interpolations
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal "Will the real Slim Shady please stand up?", email.subject
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2013-01-23 13:22:39 -05:00
|
|
|
end
|
|
|
|
|
2010-08-26 14:19:55 -04:00
|
|
|
test "translations are scoped properly" do
|
2016-08-16 03:30:11 -04:00
|
|
|
with_translation "en", base_mailer: { email_with_translations: { greet_user: "Hello %{name}!" } } do
|
2014-05-01 11:39:04 -04:00
|
|
|
email = BaseMailer.email_with_translations
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal "Hello lifo!", email.body.encoded
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2010-08-26 14:19:55 -04:00
|
|
|
end
|
|
|
|
|
2014-07-29 03:21:47 -04:00
|
|
|
test "adding attachments after mail was called raises exception" do
|
|
|
|
class LateAttachmentMailer < ActionMailer::Base
|
|
|
|
def welcome
|
|
|
|
mail body: "yay", from: "welcome@example.com", to: "to@example.com"
|
2016-08-06 13:03:39 -04:00
|
|
|
attachments["invoice.pdf"] = "This is test File content"
|
2014-07-29 03:21:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-13 17:50:38 -04:00
|
|
|
e = assert_raises(RuntimeError) { LateAttachmentMailer.welcome.message }
|
2014-07-29 03:21:47 -04:00
|
|
|
assert_match(/Can't add attachments after `mail` was called./, e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "adding inline attachments after mail was called raises exception" do
|
|
|
|
class LateInlineAttachmentMailer < ActionMailer::Base
|
|
|
|
def welcome
|
|
|
|
mail body: "yay", from: "welcome@example.com", to: "to@example.com"
|
2016-08-06 13:03:39 -04:00
|
|
|
attachments.inline["invoice.pdf"] = "This is test File content"
|
2014-07-29 03:21:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-13 17:50:38 -04:00
|
|
|
e = assert_raises(RuntimeError) { LateInlineAttachmentMailer.welcome.message }
|
2014-07-29 03:21:47 -04:00
|
|
|
assert_match(/Can't add attachments after `mail` was called./, e.message)
|
|
|
|
end
|
|
|
|
|
2014-09-19 04:32:08 -04:00
|
|
|
test "adding inline attachments while rendering mail works" do
|
|
|
|
class LateInlineAttachmentMailer < ActionMailer::Base
|
|
|
|
def on_render
|
|
|
|
mail from: "welcome@example.com", to: "to@example.com"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
mail = LateInlineAttachmentMailer.on_render
|
|
|
|
assert_nothing_raised { mail.message }
|
|
|
|
|
|
|
|
assert_equal ["image/jpeg; filename=controller_attachments.jpg",
|
2016-08-16 03:30:11 -04:00
|
|
|
"image/jpeg; filename=attachments.jpg"], mail.attachments.inline.map { |a| a["Content-Type"].to_s }
|
2014-09-19 04:32:08 -04:00
|
|
|
end
|
|
|
|
|
2014-07-29 03:21:47 -04:00
|
|
|
test "accessing attachments works after mail was called" do
|
|
|
|
class LateAttachmentAccessorMailer < ActionMailer::Base
|
|
|
|
def welcome
|
2016-08-06 13:03:39 -04:00
|
|
|
attachments["invoice.pdf"] = "This is test File content"
|
2014-07-29 03:21:47 -04:00
|
|
|
mail body: "yay", from: "welcome@example.com", to: "to@example.com"
|
|
|
|
|
|
|
|
unless attachments.map(&:filename) == ["invoice.pdf"]
|
|
|
|
raise Minitest::Assertion, "Should allow access to attachments"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-11 14:50:29 -05:00
|
|
|
assert_nothing_raised { LateAttachmentAccessorMailer.welcome.message }
|
2014-07-29 03:21:47 -04:00
|
|
|
end
|
|
|
|
|
2010-01-22 08:38:41 -05:00
|
|
|
# Implicit multipart
|
2010-01-23 06:46:33 -05:00
|
|
|
test "implicit multipart" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.implicit_multipart
|
2010-01-22 07:56:06 -05:00
|
|
|
assert_equal(2, email.parts.size)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
2010-01-22 07:56:06 -05:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal("TEXT Implicit Multipart", email.parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
assert_equal("HTML Implicit Multipart", email.parts[1].body.encoded)
|
|
|
|
end
|
|
|
|
|
2010-01-23 06:46:33 -05:00
|
|
|
test "implicit multipart with sort order" do
|
2010-01-22 08:38:41 -05:00
|
|
|
order = ["text/html", "text/plain"]
|
2012-10-07 13:54:14 -04:00
|
|
|
with_default BaseMailer, parts_order: order do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.implicit_multipart
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal("text/html", email.parts[0].mime_type)
|
|
|
|
assert_equal("text/plain", email.parts[1].mime_type)
|
|
|
|
|
2012-10-07 13:54:14 -04:00
|
|
|
email = BaseMailer.implicit_multipart(parts_order: order.reverse)
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "implicit multipart with attachments creates nested parts" do
|
2012-10-07 13:54:14 -04:00
|
|
|
email = BaseMailer.implicit_multipart(attachments: true)
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal("application/pdf", email.parts[0].mime_type)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.parts[1].mime_type)
|
2010-01-22 08:38:41 -05:00
|
|
|
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
|
|
|
|
assert_equal("TEXT Implicit Multipart", email.parts[1].parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].parts[1].mime_type)
|
|
|
|
assert_equal("HTML Implicit Multipart", email.parts[1].parts[1].body.encoded)
|
2010-01-22 07:56:06 -05:00
|
|
|
end
|
|
|
|
|
2010-01-23 05:37:34 -05:00
|
|
|
test "implicit multipart with attachments and sort order" do
|
|
|
|
order = ["text/html", "text/plain"]
|
2012-10-07 13:54:14 -04:00
|
|
|
with_default BaseMailer, parts_order: order do
|
|
|
|
email = BaseMailer.implicit_multipart(attachments: true)
|
2010-01-23 05:37:34 -05:00
|
|
|
assert_equal("application/pdf", email.parts[0].mime_type)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.parts[1].mime_type)
|
2010-01-23 05:37:34 -05:00
|
|
|
assert_equal("text/plain", email.parts[1].parts[1].mime_type)
|
|
|
|
assert_equal("text/html", email.parts[1].parts[0].mime_type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-24 18:20:38 -05:00
|
|
|
test "implicit multipart with default locale" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.implicit_with_locale
|
2010-01-24 18:20:38 -05:00
|
|
|
assert_equal(2, email.parts.size)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
2010-01-24 18:20:38 -05:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("Implicit with locale TEXT", email.parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
assert_equal("Implicit with locale EN HTML", email.parts[1].body.encoded)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "implicit multipart with other locale" do
|
2012-10-07 13:54:14 -04:00
|
|
|
swap I18n, locale: :pl do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.implicit_with_locale
|
2010-01-24 18:20:38 -05:00
|
|
|
assert_equal(2, email.parts.size)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
2010-01-24 18:20:38 -05:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("Implicit with locale PL TEXT", email.parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
2014-12-29 21:46:55 -05:00
|
|
|
assert_equal("Implicit with locale EN HTML", email.parts[1].body.encoded)
|
2010-01-24 18:20:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-29 21:46:55 -05:00
|
|
|
test "implicit multipart with fallback locale" do
|
|
|
|
fallback_backend = Class.new(I18n::Backend::Simple) do
|
|
|
|
include I18n::Backend::Fallbacks
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
backend = I18n.backend
|
|
|
|
I18n.backend = fallback_backend.new
|
|
|
|
I18n.fallbacks[:"de-AT"] = [:de]
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
swap I18n, locale: "de-AT" do
|
2014-12-29 21:46:55 -05:00
|
|
|
email = BaseMailer.implicit_with_locale
|
|
|
|
assert_equal(2, email.parts.size)
|
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("Implicit with locale DE-AT TEXT", email.parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
assert_equal("Implicit with locale DE HTML", email.parts[1].body.encoded)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
I18n.backend = backend
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-24 18:20:38 -05:00
|
|
|
test "implicit multipart with several view paths uses the first one with template" do
|
2010-01-31 21:32:28 -05:00
|
|
|
old = BaseMailer.view_paths
|
2010-01-24 18:20:38 -05:00
|
|
|
begin
|
2010-01-31 21:32:28 -05:00
|
|
|
BaseMailer.view_paths = [File.join(FIXTURE_LOAD_PATH, "another.path")] + old.dup
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.welcome
|
2010-01-24 18:20:38 -05:00
|
|
|
assert_equal("Welcome from another path", email.body.encoded)
|
|
|
|
ensure
|
2010-01-31 21:32:28 -05:00
|
|
|
BaseMailer.view_paths = old
|
2010-01-24 18:20:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "implicit multipart with inexistent templates uses the next view path" do
|
2010-01-31 21:32:28 -05:00
|
|
|
old = BaseMailer.view_paths
|
2010-01-24 18:20:38 -05:00
|
|
|
begin
|
2010-01-31 21:32:28 -05:00
|
|
|
BaseMailer.view_paths = [File.join(FIXTURE_LOAD_PATH, "unknown")] + old.dup
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.welcome
|
2010-01-24 18:20:38 -05:00
|
|
|
assert_equal("Welcome", email.body.encoded)
|
|
|
|
ensure
|
2010-01-31 21:32:28 -05:00
|
|
|
BaseMailer.view_paths = old
|
2010-01-24 18:20:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-23 06:46:33 -05:00
|
|
|
# Explicit multipart
|
|
|
|
test "explicit multipart" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.explicit_multipart
|
2010-01-23 05:37:34 -05:00
|
|
|
assert_equal(2, email.parts.size)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
2010-01-23 05:37:34 -05:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("TEXT Explicit Multipart", email.parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
assert_equal("HTML Explicit Multipart", email.parts[1].body.encoded)
|
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "explicit multipart have a boundary" do
|
|
|
|
mail = BaseMailer.explicit_multipart
|
|
|
|
assert_not_nil(mail.content_type_parameters[:boundary])
|
|
|
|
end
|
|
|
|
|
2010-01-23 06:20:20 -05:00
|
|
|
test "explicit multipart with attachments creates nested parts" do
|
2012-10-07 13:54:14 -04:00
|
|
|
email = BaseMailer.explicit_multipart(attachments: true)
|
2010-01-23 06:46:33 -05:00
|
|
|
assert_equal("application/pdf", email.parts[0].mime_type)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.parts[1].mime_type)
|
2010-01-23 06:46:33 -05:00
|
|
|
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
|
|
|
|
assert_equal("TEXT Explicit Multipart", email.parts[1].parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].parts[1].mime_type)
|
|
|
|
assert_equal("HTML Explicit Multipart", email.parts[1].parts[1].body.encoded)
|
2010-01-23 06:20:20 -05:00
|
|
|
end
|
2010-01-22 08:38:41 -05:00
|
|
|
|
2010-01-23 17:34:50 -05:00
|
|
|
test "explicit multipart with templates" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.explicit_multipart_templates
|
2010-01-23 17:34:50 -05:00
|
|
|
assert_equal(2, email.parts.size)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
2012-10-27 14:28:42 -04:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("TEXT Explicit Multipart Templates", email.parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
assert_equal("HTML Explicit Multipart Templates", email.parts[1].body.encoded)
|
2010-01-23 17:34:50 -05:00
|
|
|
end
|
2010-01-23 06:46:33 -05:00
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "explicit multipart with format.any" do
|
2010-01-26 08:18:40 -05:00
|
|
|
email = BaseMailer.explicit_multipart_with_any
|
2010-01-23 06:46:33 -05:00
|
|
|
assert_equal(2, email.parts.size)
|
2010-01-25 19:49:32 -05:00
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
2010-01-23 06:46:33 -05:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("Format with any!", email.parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
assert_equal("Format with any!", email.parts[1].body.encoded)
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "explicit without specifying format with format.any" do
|
2015-09-18 13:26:42 -04:00
|
|
|
error = assert_raises(ArgumentError) do
|
|
|
|
BaseMailer.explicit_without_specifying_format_with_any.parts
|
|
|
|
end
|
|
|
|
assert_equal "You have to supply at least one format", error.message
|
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "explicit multipart with format(Hash)" do
|
|
|
|
email = BaseMailer.explicit_multipart_with_options(true)
|
2010-01-26 08:18:40 -05:00
|
|
|
email.ready_to_send!
|
2010-01-25 19:43:41 -05:00
|
|
|
assert_equal(2, email.parts.size)
|
2010-01-25 19:54:23 -05:00
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
2010-01-25 19:43:41 -05:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("base64", email.parts[0].content_transfer_encoding)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
assert_equal("7bit", email.parts[1].content_transfer_encoding)
|
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "explicit multipart with one part is rendered as body and options are merged" do
|
|
|
|
email = BaseMailer.explicit_multipart_with_options
|
2010-01-25 19:43:41 -05:00
|
|
|
assert_equal(0, email.parts.size)
|
|
|
|
assert_equal("text/plain", email.mime_type)
|
|
|
|
assert_equal("base64", email.content_transfer_encoding)
|
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
test "explicit multipart with one template has the expected format" do
|
|
|
|
email = BaseMailer.explicit_multipart_with_one_template
|
|
|
|
assert_equal(2, email.parts.size)
|
|
|
|
assert_equal("multipart/alternative", email.mime_type)
|
2012-10-27 14:28:42 -04:00
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("[:text]", email.parts[0].body.encoded)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
assert_equal("[:html]", email.parts[1].body.encoded)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "explicit multipart with sort order" do
|
|
|
|
order = ["text/html", "text/plain"]
|
|
|
|
with_default BaseMailer, parts_order: order do
|
|
|
|
email = BaseMailer.explicit_multipart
|
|
|
|
assert_equal("text/html", email.parts[0].mime_type)
|
|
|
|
assert_equal("text/plain", email.parts[1].mime_type)
|
|
|
|
|
|
|
|
email = BaseMailer.explicit_multipart(parts_order: order.reverse)
|
|
|
|
assert_equal("text/plain", email.parts[0].mime_type)
|
|
|
|
assert_equal("text/html", email.parts[1].mime_type)
|
|
|
|
end
|
2010-03-19 12:20:15 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 11:31:18 -05:00
|
|
|
# Class level API with method missing
|
|
|
|
test "should respond to action methods" do
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_respond_to BaseMailer, :welcome
|
|
|
|
assert_respond_to BaseMailer, :implicit_multipart
|
2010-01-24 11:31:18 -05:00
|
|
|
assert !BaseMailer.respond_to?(:mail)
|
|
|
|
assert !BaseMailer.respond_to?(:headers)
|
2010-01-24 04:38:53 -05:00
|
|
|
end
|
|
|
|
|
2010-01-24 11:31:18 -05:00
|
|
|
test "calling just the action should return the generated mail object" do
|
2010-01-24 04:38:53 -05:00
|
|
|
email = BaseMailer.welcome
|
|
|
|
assert_equal(0, BaseMailer.deliveries.length)
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("The first email on new API!", email.subject)
|
2010-01-24 04:38:53 -05:00
|
|
|
end
|
|
|
|
|
2010-01-24 11:31:18 -05:00
|
|
|
test "calling deliver on the action should deliver the mail object" do
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called(BaseMailer, :deliver_mail) do
|
|
|
|
mail = BaseMailer.welcome.deliver_now
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal "The first email on new API!", mail.subject
|
2015-08-20 01:57:49 -04:00
|
|
|
end
|
2010-01-24 21:39:48 -05:00
|
|
|
end
|
|
|
|
|
2010-02-07 02:32:52 -05:00
|
|
|
test "calling deliver on the action should increment the deliveries collection if using the test mailer" do
|
2014-08-20 04:34:57 -04:00
|
|
|
BaseMailer.welcome.deliver_now
|
2010-01-24 11:31:18 -05:00
|
|
|
assert_equal(1, BaseMailer.deliveries.length)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-01-25 05:47:03 -05:00
|
|
|
test "calling deliver, ActionMailer should yield back to mail to let it call :do_delivery on itself" do
|
|
|
|
mail = Mail::Message.new
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called(mail, :do_delivery) do
|
|
|
|
assert_called(BaseMailer, :welcome, returns: mail) do
|
|
|
|
BaseMailer.welcome.deliver
|
|
|
|
end
|
|
|
|
end
|
2010-01-25 05:47:03 -05:00
|
|
|
end
|
2010-01-29 10:16:01 -05:00
|
|
|
|
|
|
|
# Rendering
|
2010-02-19 04:51:17 -05:00
|
|
|
test "you can specify a different template for implicit render" do
|
2016-08-06 13:03:39 -04:00
|
|
|
mail = BaseMailer.implicit_different_template("implicit_multipart").deliver_now
|
2010-02-19 04:51:17 -05:00
|
|
|
assert_equal("HTML Implicit Multipart", mail.html_part.body.decoded)
|
|
|
|
assert_equal("TEXT Implicit Multipart", mail.text_part.body.decoded)
|
|
|
|
end
|
|
|
|
|
2012-06-20 15:20:12 -04:00
|
|
|
test "should raise if missing template in implicit render" do
|
|
|
|
assert_raises ActionView::MissingTemplate do
|
2016-08-06 13:03:39 -04:00
|
|
|
BaseMailer.implicit_different_template("missing_template").deliver_now
|
2012-06-20 15:20:12 -04:00
|
|
|
end
|
|
|
|
assert_equal(0, BaseMailer.deliveries.length)
|
|
|
|
end
|
|
|
|
|
2010-02-19 04:51:17 -05:00
|
|
|
test "you can specify a different template for explicit render" do
|
2016-08-06 13:03:39 -04:00
|
|
|
mail = BaseMailer.explicit_different_template("explicit_multipart_templates").deliver_now
|
2010-01-29 03:31:24 -05:00
|
|
|
assert_equal("HTML Explicit Multipart Templates", mail.html_part.body.decoded)
|
|
|
|
assert_equal("TEXT Explicit Multipart Templates", mail.text_part.body.decoded)
|
|
|
|
end
|
2010-01-24 11:31:18 -05:00
|
|
|
|
2010-02-19 04:51:17 -05:00
|
|
|
test "you can specify a different layout" do
|
2016-08-06 13:03:39 -04:00
|
|
|
mail = BaseMailer.different_layout("different_layout").deliver_now
|
2010-01-29 07:00:12 -05:00
|
|
|
assert_equal("HTML -- HTML", mail.html_part.body.decoded)
|
|
|
|
assert_equal("PLAIN -- PLAIN", mail.text_part.body.decoded)
|
|
|
|
end
|
|
|
|
|
2010-02-19 04:51:17 -05:00
|
|
|
test "you can specify the template path for implicit lookup" do
|
2016-08-06 13:03:39 -04:00
|
|
|
mail = BaseMailer.welcome_from_another_path("another.path/base_mailer").deliver_now
|
2010-02-19 04:51:17 -05:00
|
|
|
assert_equal("Welcome from another path", mail.body.encoded)
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
mail = BaseMailer.welcome_from_another_path(["unknown/invalid", "another.path/base_mailer"]).deliver_now
|
2010-02-19 04:51:17 -05:00
|
|
|
assert_equal("Welcome from another path", mail.body.encoded)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-07-23 08:42:21 -04:00
|
|
|
test "assets tags should use ActionMailer's asset_host settings" do
|
|
|
|
ActionMailer::Base.config.asset_host = "http://global.com"
|
|
|
|
ActionMailer::Base.config.assets_dir = "global/"
|
|
|
|
|
|
|
|
mail = AssetMailer.welcome
|
|
|
|
|
2014-10-07 18:40:59 -04:00
|
|
|
assert_dom_equal(%{<img alt="Dummy" src="http://global.com/images/dummy.png" />}, mail.body.to_s.strip)
|
2010-07-23 08:42:21 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-07-23 08:42:21 -04:00
|
|
|
test "assets tags should use a Mailer's asset_host settings when available" do
|
2014-05-01 11:39:04 -04:00
|
|
|
ActionMailer::Base.config.asset_host = "http://global.com"
|
|
|
|
ActionMailer::Base.config.assets_dir = "global/"
|
2010-07-23 08:42:21 -04:00
|
|
|
|
2014-05-01 11:39:04 -04:00
|
|
|
TempAssetMailer = Class.new(AssetMailer) do
|
|
|
|
self.mailer_name = "asset_mailer"
|
|
|
|
self.asset_host = "http://local.com"
|
|
|
|
end
|
2010-07-23 08:42:21 -04:00
|
|
|
|
2014-05-01 11:39:04 -04:00
|
|
|
mail = TempAssetMailer.welcome
|
2010-07-23 08:42:21 -04:00
|
|
|
|
2014-10-07 18:40:59 -04:00
|
|
|
assert_dom_equal(%{<img alt="Dummy" src="http://local.com/images/dummy.png" />}, mail.body.to_s.strip)
|
2010-07-23 08:42:21 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "the view is not rendered when mail was never called" do
|
2012-09-29 16:29:29 -04:00
|
|
|
mail = BaseMailer.without_mail_call
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("", mail.body.to_s.strip)
|
2014-08-20 04:34:57 -04:00
|
|
|
mail.deliver_now
|
2012-09-29 16:29:29 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "the return value of mailer methods is not relevant" do
|
2012-12-07 08:43:49 -05:00
|
|
|
mail = BaseMailer.with_nil_as_return_value
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("Welcome", mail.body.to_s.strip)
|
2014-08-20 04:34:57 -04:00
|
|
|
mail.deliver_now
|
2012-12-07 08:43:49 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 20:17:08 -05:00
|
|
|
# Before and After hooks
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-02-21 20:17:08 -05:00
|
|
|
class MyObserver
|
|
|
|
def self.delivered_email(mail)
|
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
class MySecondObserver
|
|
|
|
def self.delivered_email(mail)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-21 20:17:08 -05:00
|
|
|
test "you can register an observer to the mail object that gets informed on email delivery" do
|
2014-05-01 11:39:04 -04:00
|
|
|
mail_side_effects do
|
|
|
|
ActionMailer::Base.register_observer(MyObserver)
|
|
|
|
mail = BaseMailer.welcome
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called_with(MyObserver, :delivered_email, [mail]) do
|
|
|
|
mail.deliver_now
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2010-02-21 20:17:08 -05:00
|
|
|
end
|
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
test "you can register an observer using its stringified name to the mail object that gets informed on email delivery" do
|
2014-05-01 11:39:04 -04:00
|
|
|
mail_side_effects do
|
|
|
|
ActionMailer::Base.register_observer("BaseTest::MyObserver")
|
|
|
|
mail = BaseMailer.welcome
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called_with(MyObserver, :delivered_email, [mail]) do
|
|
|
|
mail.deliver_now
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2011-04-02 04:51:47 -04:00
|
|
|
end
|
|
|
|
|
2014-01-26 07:03:32 -05:00
|
|
|
test "you can register an observer using its symbolized underscored name to the mail object that gets informed on email delivery" do
|
2014-05-01 11:39:04 -04:00
|
|
|
mail_side_effects do
|
|
|
|
ActionMailer::Base.register_observer(:"base_test/my_observer")
|
|
|
|
mail = BaseMailer.welcome
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called_with(MyObserver, :delivered_email, [mail]) do
|
|
|
|
mail.deliver_now
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2014-01-26 07:03:32 -05:00
|
|
|
end
|
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
test "you can register multiple observers to the mail object that both get informed on email delivery" do
|
2014-05-01 11:39:04 -04:00
|
|
|
mail_side_effects do
|
|
|
|
ActionMailer::Base.register_observers("BaseTest::MyObserver", MySecondObserver)
|
|
|
|
mail = BaseMailer.welcome
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called_with(MyObserver, :delivered_email, [mail]) do
|
|
|
|
assert_called_with(MySecondObserver, :delivered_email, [mail]) do
|
|
|
|
mail.deliver_now
|
|
|
|
end
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2011-04-02 04:51:47 -04:00
|
|
|
end
|
|
|
|
|
2010-02-21 20:17:08 -05:00
|
|
|
class MyInterceptor
|
2014-01-26 06:38:30 -05:00
|
|
|
def self.delivering_email(mail); end
|
|
|
|
def self.previewing_email(mail); end
|
2010-02-21 20:17:08 -05:00
|
|
|
end
|
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
class MySecondInterceptor
|
2014-01-26 06:38:30 -05:00
|
|
|
def self.delivering_email(mail); end
|
|
|
|
def self.previewing_email(mail); end
|
|
|
|
end
|
|
|
|
|
2010-02-21 20:17:08 -05:00
|
|
|
test "you can register an interceptor to the mail object that gets passed the mail object before delivery" do
|
2014-05-01 11:39:04 -04:00
|
|
|
mail_side_effects do
|
|
|
|
ActionMailer::Base.register_interceptor(MyInterceptor)
|
|
|
|
mail = BaseMailer.welcome
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called_with(MyInterceptor, :delivering_email, [mail]) do
|
|
|
|
mail.deliver_now
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2010-02-21 20:17:08 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
test "you can register an interceptor using its stringified name to the mail object that gets passed the mail object before delivery" do
|
2014-05-01 11:39:04 -04:00
|
|
|
mail_side_effects do
|
|
|
|
ActionMailer::Base.register_interceptor("BaseTest::MyInterceptor")
|
|
|
|
mail = BaseMailer.welcome
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called_with(MyInterceptor, :delivering_email, [mail]) do
|
|
|
|
mail.deliver_now
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2011-04-02 04:51:47 -04:00
|
|
|
end
|
|
|
|
|
2014-01-26 07:03:32 -05:00
|
|
|
test "you can register an interceptor using its symbolized underscored name to the mail object that gets passed the mail object before delivery" do
|
2014-05-01 11:39:04 -04:00
|
|
|
mail_side_effects do
|
|
|
|
ActionMailer::Base.register_interceptor(:"base_test/my_interceptor")
|
|
|
|
mail = BaseMailer.welcome
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called_with(MyInterceptor, :delivering_email, [mail]) do
|
|
|
|
mail.deliver_now
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2014-01-26 07:03:32 -05:00
|
|
|
end
|
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
test "you can register multiple interceptors to the mail object that both get passed the mail object before delivery" do
|
2014-05-01 11:39:04 -04:00
|
|
|
mail_side_effects do
|
|
|
|
ActionMailer::Base.register_interceptors("BaseTest::MyInterceptor", MySecondInterceptor)
|
|
|
|
mail = BaseMailer.welcome
|
2015-08-20 01:57:49 -04:00
|
|
|
assert_called_with(MyInterceptor, :delivering_email, [mail]) do
|
|
|
|
assert_called_with(MySecondInterceptor, :delivering_email, [mail]) do
|
|
|
|
mail.deliver_now
|
|
|
|
end
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2014-01-26 06:38:30 -05:00
|
|
|
end
|
|
|
|
|
2010-05-01 21:30:10 -04:00
|
|
|
test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do
|
2016-08-06 13:03:39 -04:00
|
|
|
mail1 = ProcMailer.welcome["X-Proc-Method"]
|
2010-05-01 21:30:10 -04:00
|
|
|
yesterday = 1.day.ago
|
2015-08-20 01:57:49 -04:00
|
|
|
Time.stub(:now, yesterday) do
|
2016-08-06 13:03:39 -04:00
|
|
|
mail2 = ProcMailer.welcome["X-Proc-Method"]
|
2015-08-20 01:57:49 -04:00
|
|
|
assert(mail1.to_s.to_i > mail2.to_s.to_i)
|
|
|
|
end
|
2010-05-01 21:30:10 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "default values which have to_proc (e.g. symbols) should not be considered procs" do
|
|
|
|
assert(ProcMailer.welcome["x-has-to-proc"].to_s == "symbol")
|
2013-07-22 14:23:53 -04:00
|
|
|
end
|
|
|
|
|
2010-05-01 23:16:28 -04:00
|
|
|
test "we can call other defined methods on the class as needed" do
|
|
|
|
mail = ProcMailer.welcome
|
|
|
|
assert_equal("Thanks for signing up this afternoon", mail.subject)
|
|
|
|
end
|
2010-02-19 04:51:17 -05:00
|
|
|
|
2012-12-08 10:20:59 -05:00
|
|
|
test "modifying the mail message with a before_action" do
|
|
|
|
class BeforeActionMailer < ActionMailer::Base
|
|
|
|
before_action :add_special_header!
|
2012-03-10 15:37:04 -05:00
|
|
|
|
|
|
|
def welcome ; mail ; end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def add_special_header!
|
|
|
|
headers("X-Special-Header" => "Wow, so special")
|
|
|
|
end
|
2012-03-10 15:37:04 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("Wow, so special", BeforeActionMailer.welcome["X-Special-Header"].to_s)
|
2012-03-10 15:37:04 -05:00
|
|
|
end
|
|
|
|
|
2012-12-08 10:20:59 -05:00
|
|
|
test "modifying the mail message with an after_action" do
|
|
|
|
class AfterActionMailer < ActionMailer::Base
|
|
|
|
after_action :add_special_header!
|
2012-03-10 15:37:04 -05:00
|
|
|
|
|
|
|
def welcome ; mail ; end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def add_special_header!
|
|
|
|
headers("X-Special-Header" => "Testing")
|
|
|
|
end
|
2012-03-10 15:37:04 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("Testing", AfterActionMailer.welcome["X-Special-Header"].to_s)
|
2012-03-10 15:37:04 -05:00
|
|
|
end
|
|
|
|
|
2012-12-08 10:20:59 -05:00
|
|
|
test "adding an inline attachment using a before_action" do
|
2012-03-10 15:37:04 -05:00
|
|
|
class DefaultInlineAttachmentMailer < ActionMailer::Base
|
2012-12-08 10:20:59 -05:00
|
|
|
before_action :add_inline_attachment!
|
2012-03-10 15:37:04 -05:00
|
|
|
|
|
|
|
def welcome ; mail ; end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def add_inline_attachment!
|
|
|
|
attachments.inline["footer.jpg"] = "hey there"
|
|
|
|
end
|
2012-03-10 15:37:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
mail = DefaultInlineAttachmentMailer.welcome
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal("image/jpeg; filename=footer.jpg", mail.attachments.inline.first["Content-Type"].to_s)
|
2012-03-10 15:37:04 -05:00
|
|
|
end
|
|
|
|
|
2010-08-04 12:58:18 -04:00
|
|
|
test "action methods should be refreshed after defining new method" do
|
|
|
|
class FooMailer < ActionMailer::Base
|
2016-08-07 19:05:28 -04:00
|
|
|
# This triggers action_methods.
|
|
|
|
respond_to?(:foo)
|
2010-08-04 12:58:18 -04:00
|
|
|
|
|
|
|
def notify
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-10 15:48:55 -05:00
|
|
|
assert_equal Set.new(["notify"]), FooMailer.action_methods
|
2010-08-04 12:58:18 -04:00
|
|
|
end
|
|
|
|
|
2012-05-13 07:21:00 -04:00
|
|
|
test "mailer can be anonymous" do
|
|
|
|
mailer = Class.new(ActionMailer::Base) do
|
|
|
|
def welcome
|
|
|
|
mail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "anonymous", mailer.mailer_name
|
|
|
|
|
|
|
|
assert_equal "Welcome", mailer.welcome.subject
|
|
|
|
assert_equal "Anonymous mailer body", mailer.welcome.body.encoded.strip
|
|
|
|
end
|
|
|
|
|
2012-06-15 13:20:47 -04:00
|
|
|
test "default_from can be set" do
|
|
|
|
class DefaultFromMailer < ActionMailer::Base
|
2016-08-06 13:03:39 -04:00
|
|
|
default to: "system@test.lindsaar.net"
|
2016-08-16 03:30:11 -04:00
|
|
|
self.default_options = { from: "robert.pankowecki@gmail.com" }
|
2012-06-15 13:20:47 -04:00
|
|
|
|
|
|
|
def welcome
|
2012-07-06 22:12:48 -04:00
|
|
|
mail(subject: "subject", body: "hello world")
|
2012-06-15 13:20:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from
|
|
|
|
end
|
|
|
|
|
2013-12-02 10:51:13 -05:00
|
|
|
test "mail() without arguments serves as getter for the current mail message" do
|
|
|
|
class MailerWithCallback < ActionMailer::Base
|
|
|
|
after_action :a_callback
|
|
|
|
|
|
|
|
def welcome
|
2016-08-06 13:03:39 -04:00
|
|
|
headers("X-Special-Header" => "special indeed!")
|
2013-12-02 10:51:13 -05:00
|
|
|
mail subject: "subject", body: "hello world", to: ["joe@example.com"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def a_callback
|
|
|
|
mail.to << "jane@example.com"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
mail = MailerWithCallback.welcome
|
|
|
|
assert_equal "subject", mail.subject
|
|
|
|
assert_equal ["joe@example.com", "jane@example.com"], mail.to
|
|
|
|
assert_equal "hello world", mail.body.encoded.strip
|
|
|
|
assert_equal "special indeed!", mail["X-Special-Header"].to_s
|
|
|
|
end
|
|
|
|
|
2010-01-22 05:57:54 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
# Execute the block setting the given values and restoring old values after
|
|
|
|
# the block is executed.
|
2010-01-26 05:21:20 -05:00
|
|
|
def swap(klass, new_values)
|
2010-01-22 05:57:54 -05:00
|
|
|
old_values = {}
|
|
|
|
new_values.each do |key, value|
|
2010-01-26 05:21:20 -05:00
|
|
|
old_values[key] = klass.send key
|
|
|
|
klass.send :"#{key}=", value
|
2010-01-22 05:57:54 -05:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
old_values.each do |key, value|
|
2010-01-26 05:21:20 -05:00
|
|
|
klass.send :"#{key}=", value
|
2010-01-22 05:57:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-26 05:21:20 -05:00
|
|
|
def with_default(klass, new_values)
|
2010-01-31 21:32:28 -05:00
|
|
|
old = klass.default_params
|
|
|
|
klass.default(new_values)
|
2010-01-26 05:21:20 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2010-01-31 21:32:28 -05:00
|
|
|
klass.default_params = old
|
2010-01-26 05:21:20 -05:00
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
|
|
|
|
# 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
|
2015-08-20 01:57:49 -04:00
|
|
|
stub_any_instance(BaseMailerPreview) do |instance|
|
|
|
|
instance.stub(:welcome, mail) do
|
|
|
|
assert_called_with(MyInterceptor, :previewing_email, [mail]) do
|
|
|
|
BaseMailerPreview.call(:welcome)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
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
|
2015-08-20 01:57:49 -04:00
|
|
|
stub_any_instance(BaseMailerPreview) do |instance|
|
|
|
|
instance.stub(:welcome, mail) do
|
|
|
|
assert_called_with(MyInterceptor, :previewing_email, [mail]) do
|
|
|
|
BaseMailerPreview.call(:welcome)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
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
|
2015-08-20 01:57:49 -04:00
|
|
|
stub_any_instance(BaseMailerPreview) do |instance|
|
|
|
|
instance.stub(:welcome, mail) do
|
|
|
|
assert_called_with(MyInterceptor, :previewing_email, [mail]) do
|
|
|
|
BaseMailerPreview.call(:welcome)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
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
|
2015-08-20 01:57:49 -04:00
|
|
|
stub_any_instance(BaseMailerPreview) do |instance|
|
|
|
|
instance.stub(:welcome, mail) do
|
|
|
|
assert_called_with(MyInterceptor, :previewing_email, [mail]) do
|
|
|
|
assert_called_with(MySecondInterceptor, :previewing_email, [mail]) do
|
|
|
|
BaseMailerPreview.call(:welcome)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-01 11:39:04 -04:00
|
|
|
end
|
2010-01-27 21:37:01 -05:00
|
|
|
end
|