aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/test
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-01-26 16:00:57 +0100
committerJosé Valim <jose.valim@gmail.com>2010-01-26 16:00:57 +0100
commit1078677575b44ff2700cb6232b50d9c887e73c2b (patch)
tree47963e11e0eade3d92383b80b04783adae7d5275 /actionmailer/test
parentd7de1c76cad28f52fc62693d415a07abf6c86ef5 (diff)
parent05c4ad9d3f6ff8d0017f4bca9b52e552629c4813 (diff)
downloadrails-1078677575b44ff2700cb6232b50d9c887e73c2b.tar.gz
rails-1078677575b44ff2700cb6232b50d9c887e73c2b.tar.bz2
rails-1078677575b44ff2700cb6232b50d9c887e73c2b.zip
Merge remote branch 'mikel/master'
Diffstat (limited to 'actionmailer/test')
-rw-r--r--actionmailer/test/base_test.rb198
1 files changed, 123 insertions, 75 deletions
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
index 4b8427fb92..4a65363e3e 100644
--- a/actionmailer/test/base_test.rb
+++ b/actionmailer/test/base_test.rb
@@ -2,63 +2,70 @@
require 'abstract_unit'
class BaseTest < ActiveSupport::TestCase
- DEFAULT_HEADERS = {
- :to => 'mikel@test.lindsaar.net',
- :subject => 'The first email on new API!'
- }
-
class BaseMailer < ActionMailer::Base
- delivers_from 'jose@test.plataformatec.com'
self.mailer_name = "base_mailer"
+ defaults :to => 'system@test.lindsaar.net',
+ :from => 'jose@test.plataformatec.com',
+ :reply_to => 'mikel@test.lindsaar.net'
+
def welcome(hash = {})
headers['X-SPAM'] = "Not SPAM"
- mail(DEFAULT_HEADERS.merge(hash))
+ mail({:subject => "The first email on new API!"}.merge!(hash))
+ end
+
+ def simple(hash = {})
+ mail(hash)
+ end
+
+ def simple_with_headers(hash = {})
+ headers hash
+ mail
end
def attachment_with_content(hash = {})
attachments['invoice.pdf'] = 'This is test File content'
- mail(DEFAULT_HEADERS.merge(hash))
+ mail(hash)
end
def attachment_with_hash
attachments['invoice.jpg'] = { :data => "you smiling", :mime_type => "image/x-jpg",
:transfer_encoding => "base64" }
- mail(DEFAULT_HEADERS)
+ mail
end
def implicit_multipart(hash = {})
attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments)
- mail(DEFAULT_HEADERS.merge(hash))
+ mail(hash)
end
def implicit_with_locale(hash = {})
- mail(DEFAULT_HEADERS.merge(hash))
+ mail(hash)
end
def explicit_multipart(hash = {})
attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments)
- mail(DEFAULT_HEADERS.merge(hash)) do |format|
+ mail(hash) do |format|
format.text { render :text => "TEXT Explicit Multipart" }
format.html { render :text => "HTML Explicit Multipart" }
end
end
def explicit_multipart_templates(hash = {})
- mail(DEFAULT_HEADERS.merge(hash)) do |format|
+ mail(hash) do |format|
format.html
format.text
end
end
def explicit_multipart_with_any(hash = {})
- mail(DEFAULT_HEADERS.merge(hash)) do |format|
+ mail(hash) do |format|
format.any(:text, :html){ render :text => "Format with any!" }
end
end
def custom_block(include_html=false)
- mail(DEFAULT_HEADERS) do |format|
+ mail do |format|
format.text(:content_transfer_encoding => "base64"){ render "welcome" }
format.html{ render "welcome" } if include_html
end
@@ -66,20 +73,22 @@ class BaseTest < ActiveSupport::TestCase
end
test "method call to mail does not raise error" do
- assert_nothing_raised { BaseMailer.welcome.deliver }
+ assert_nothing_raised { BaseMailer.welcome }
end
# Basic mail usage without block
test "mail() should set the headers of the mail message" do
- email = BaseMailer.welcome.deliver
- assert_equal(email.to, ['mikel@test.lindsaar.net'])
- assert_equal(email.from, ['jose@test.plataformatec.com'])
- assert_equal(email.subject, 'The first email on new API!')
+ email = BaseMailer.welcome
+ 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)
end
test "mail() with from overwrites the class level default" do
- email = BaseMailer.welcome(:from => 'someone@else.com').deliver
- assert_equal(email.from, ['someone@else.com'])
+ 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)
end
test "mail() with bcc, cc, content_type, charset, mime_version, reply_to and date" do
@@ -90,61 +99,77 @@ class BaseTest < ActiveSupport::TestCase
:charset => 'iso-8559-1',
:mime_version => '2.0',
:reply_to => 'reply-to@test.lindsaar.net',
- :date => @time).deliver
- assert_equal(email.bcc, ['bcc@test.lindsaar.net'])
- assert_equal(email.cc, ['cc@test.lindsaar.net'])
- assert_equal(email.content_type, 'multipart/mixed')
- assert_equal(email.charset, 'iso-8559-1')
- assert_equal(email.mime_version, '2.0')
- assert_equal(email.reply_to, ['reply-to@test.lindsaar.net'])
- assert_equal(email.date, @time)
+ :date => @time)
+ assert_equal(['bcc@test.lindsaar.net'], email.bcc)
+ assert_equal(['cc@test.lindsaar.net'], email.cc)
+ assert_equal('multipart/mixed', 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)
+ assert_equal(@time, email.date)
end
test "mail() renders the template using the method being processed" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("Welcome", email.body.encoded)
end
test "can pass in :body to the mail method hash" do
- email = BaseMailer.welcome(:body => "Hello there").deliver
+ email = BaseMailer.welcome(:body => "Hello there")
assert_equal("text/plain", email.mime_type)
assert_equal("Hello there", email.body.encoded)
end
# Custom headers
test "custom headers" do
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("Not SPAM", email['X-SPAM'].decoded)
end
+ test "can pass random headers in as a hash to mail" do
+ hash = {'X-Special-Domain-Specific-Header' => "SecretValue",
+ 'In-Reply-To' => '1234@mikel.me.com' }
+ mail = BaseMailer.simple(hash)
+ assert_equal('SecretValue', mail['X-Special-Domain-Specific-Header'].decoded)
+ assert_equal('1234@mikel.me.com', mail['In-Reply-To'].decoded)
+ end
+
+ test "can pass random headers in as a hash" do
+ hash = {'X-Special-Domain-Specific-Header' => "SecretValue",
+ 'In-Reply-To' => '1234@mikel.me.com' }
+ mail = BaseMailer.simple_with_headers(hash)
+ assert_equal('SecretValue', mail['X-Special-Domain-Specific-Header'].decoded)
+ assert_equal('1234@mikel.me.com', mail['In-Reply-To'].decoded)
+ end
+
# Attachments
test "attachment with content" do
- email = BaseMailer.attachment_with_content.deliver
+ email = BaseMailer.attachment_with_content
assert_equal(1, email.attachments.length)
assert_equal('invoice.pdf', email.attachments[0].filename)
assert_equal('This is test File content', email.attachments['invoice.pdf'].decoded)
end
test "attachment gets content type from filename" do
- email = BaseMailer.attachment_with_content.deliver
+ email = BaseMailer.attachment_with_content
assert_equal('invoice.pdf', email.attachments[0].filename)
end
test "attachment with hash" do
- email = BaseMailer.attachment_with_hash.deliver
+ email = BaseMailer.attachment_with_hash
assert_equal(1, email.attachments.length)
assert_equal('invoice.jpg', email.attachments[0].filename)
assert_equal("\312\213\254\232)b", email.attachments['invoice.jpg'].decoded)
end
test "sets mime type to multipart/mixed when attachment is included" do
- email = BaseMailer.attachment_with_content.deliver
+ email = BaseMailer.attachment_with_content
assert_equal(1, email.attachments.length)
assert_equal("multipart/mixed", email.mime_type)
end
test "adds the rendered template as part" do
- email = BaseMailer.attachment_with_content.deliver
+ email = BaseMailer.attachment_with_content
assert_equal(2, email.parts.length)
assert_equal("multipart/mixed", email.mime_type)
assert_equal("text/html", email.parts[0].mime_type)
@@ -154,7 +179,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "adds the given :body as part" do
- email = BaseMailer.attachment_with_content(:body => "I'm the eggman").deliver
+ email = BaseMailer.attachment_with_content(:body => "I'm the eggman")
assert_equal(2, email.parts.length)
assert_equal("multipart/mixed", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -165,47 +190,55 @@ class BaseTest < ActiveSupport::TestCase
# Defaults values
test "uses default charset from class" do
- swap BaseMailer, :default_charset => "US-ASCII" do
- email = BaseMailer.welcome.deliver
+ with_default BaseMailer, :charset => "US-ASCII" do
+ email = BaseMailer.welcome
assert_equal("US-ASCII", email.charset)
- email = BaseMailer.welcome(:charset => "iso-8559-1").deliver
+ email = BaseMailer.welcome(:charset => "iso-8559-1")
assert_equal("iso-8559-1", email.charset)
end
end
test "uses default content type from class" do
- swap BaseMailer, :default_content_type => "text/html" do
- email = BaseMailer.welcome.deliver
+ with_default BaseMailer, :content_type => "text/html" do
+ email = BaseMailer.welcome
assert_equal("text/html", email.mime_type)
- email = BaseMailer.welcome(:content_type => "text/plain").deliver
+ email = BaseMailer.welcome(:content_type => "text/plain")
assert_equal("text/plain", email.mime_type)
end
end
test "uses default mime version from class" do
- swap BaseMailer, :default_mime_version => "2.0" do
- email = BaseMailer.welcome.deliver
+ with_default BaseMailer, :mime_version => "2.0" do
+ email = BaseMailer.welcome
assert_equal("2.0", email.mime_version)
- email = BaseMailer.welcome(:mime_version => "1.0").deliver
+ email = BaseMailer.welcome(:mime_version => "1.0")
assert_equal("1.0", email.mime_version)
end
end
+ test "uses random default headers from class" do
+ with_default BaseMailer, "X-SPAM" => "Not spam" do
+ email = BaseMailer.simple
+ assert_equal("Not spam", email["X-SPAM"].decoded)
+ end
+ end
+
test "subject gets default from I18n" do
- email = BaseMailer.welcome(:subject => nil).deliver
+ BaseMailer.defaults[:subject] = nil
+ email = BaseMailer.welcome(:subject => nil)
assert_equal "Welcome", email.subject
I18n.backend.store_translations('en', :actionmailer => {:base_mailer => {:welcome => {:subject => "New Subject!"}}})
- email = BaseMailer.welcome(:subject => nil).deliver
+ email = BaseMailer.welcome(:subject => nil)
assert_equal "New Subject!", email.subject
end
# Implicit multipart
test "implicit multipart" do
- email = BaseMailer.implicit_multipart.deliver
+ email = BaseMailer.implicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -216,19 +249,19 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with sort order" do
order = ["text/html", "text/plain"]
- swap BaseMailer, :default_implicit_parts_order => order do
- email = BaseMailer.implicit_multipart.deliver
+ with_default BaseMailer, :parts_order => order do
+ email = BaseMailer.implicit_multipart
assert_equal("text/html", email.parts[0].mime_type)
assert_equal("text/plain", email.parts[1].mime_type)
- email = BaseMailer.implicit_multipart(:parts_order => order.reverse).deliver
+ email = BaseMailer.implicit_multipart(:parts_order => order.reverse)
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
- email = BaseMailer.implicit_multipart(:attachments => true).deliver
+ email = BaseMailer.implicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
@@ -239,8 +272,8 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with attachments and sort order" do
order = ["text/html", "text/plain"]
- swap BaseMailer, :default_implicit_parts_order => order do
- email = BaseMailer.implicit_multipart(:attachments => true).deliver
+ with_default BaseMailer, :parts_order => order do
+ email = BaseMailer.implicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[1].mime_type)
@@ -249,7 +282,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "implicit multipart with default locale" do
- email = BaseMailer.implicit_with_locale.deliver
+ 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)
@@ -260,7 +293,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with other locale" do
swap I18n, :locale => :pl do
- email = BaseMailer.implicit_with_locale.deliver
+ 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)
@@ -273,7 +306,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with several view paths uses the first one with template" do
begin
BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "another.path"))
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("Welcome from another path", email.body.encoded)
ensure
BaseMailer.view_paths.shift
@@ -283,7 +316,7 @@ class BaseTest < ActiveSupport::TestCase
test "implicit multipart with inexistent templates uses the next view path" do
begin
BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "unknown"))
- email = BaseMailer.welcome.deliver
+ email = BaseMailer.welcome
assert_equal("Welcome", email.body.encoded)
ensure
BaseMailer.view_paths.shift
@@ -292,7 +325,7 @@ class BaseTest < ActiveSupport::TestCase
# Explicit multipart
test "explicit multipart" do
- email = BaseMailer.explicit_multipart.deliver
+ email = BaseMailer.explicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -303,19 +336,19 @@ class BaseTest < ActiveSupport::TestCase
test "explicit multipart does not sort order" do
order = ["text/html", "text/plain"]
- swap BaseMailer, :default_implicit_parts_order => order do
- email = BaseMailer.explicit_multipart.deliver
+ with_default BaseMailer, :parts_order => order do
+ email = BaseMailer.explicit_multipart
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("text/html", email.parts[1].mime_type)
- email = BaseMailer.explicit_multipart(:parts_order => order.reverse).deliver
+ 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
end
test "explicit multipart with attachments creates nested parts" do
- email = BaseMailer.explicit_multipart(:attachments => true).deliver
+ email = BaseMailer.explicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
@@ -325,7 +358,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with templates" do
- email = BaseMailer.explicit_multipart_templates.deliver
+ email = BaseMailer.explicit_multipart_templates
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/html", email.parts[0].mime_type)
@@ -335,7 +368,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with any" do
- email = BaseMailer.explicit_multipart_with_any.deliver
+ email = BaseMailer.explicit_multipart_with_any
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -345,7 +378,8 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with options" do
- email = BaseMailer.custom_block(true).deliver
+ email = BaseMailer.custom_block(true)
+ email.ready_to_send!
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
@@ -355,7 +389,7 @@ class BaseTest < ActiveSupport::TestCase
end
test "explicit multipart with one part is rendered as body" do
- email = BaseMailer.custom_block.deliver
+ email = BaseMailer.custom_block
assert_equal(0, email.parts.size)
assert_equal("text/plain", email.mime_type)
assert_equal("base64", email.content_transfer_encoding)
@@ -379,7 +413,8 @@ class BaseTest < ActiveSupport::TestCase
test "calling deliver on the action should deliver the mail object" do
BaseMailer.deliveries.clear
BaseMailer.expects(:deliver_mail).once
- BaseMailer.welcome.deliver
+ mail = BaseMailer.welcome.deliver
+ assert_instance_of Mail::Message, mail
end
test "calling deliver on the action should increment the deliveries collection" do
@@ -404,17 +439,30 @@ class BaseTest < ActiveSupport::TestCase
# Execute the block setting the given values and restoring old values after
# the block is executed.
- def swap(object, new_values)
+ def swap(klass, new_values)
old_values = {}
new_values.each do |key, value|
- old_values[key] = object.send key
- object.send :"#{key}=", value
+ old_values[key] = klass.send key
+ klass.send :"#{key}=", value
end
yield
ensure
old_values.each do |key, value|
- object.send :"#{key}=", value
+ klass.send :"#{key}=", value
end
end
+ def with_default(klass, new_values)
+ hash = klass.defaults
+ old_values = {}
+ new_values.each do |key, value|
+ old_values[key] = hash[key]
+ hash[key] = value
+ end
+ yield
+ ensure
+ old_values.each do |key, value|
+ hash[key] = value
+ end
+ end
end \ No newline at end of file