diff options
Diffstat (limited to 'actionmailer/test/base_test.rb')
-rw-r--r-- | actionmailer/test/base_test.rb | 135 |
1 files changed, 54 insertions, 81 deletions
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 7f41f3485c..3b2a072dce 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -3,40 +3,6 @@ require 'abstract_unit' # class Notifier < ActionMailer::Base # delivers_from 'notifications@example.com' -# -# def welcome(user) -# @user = user # available to the view -# mail(:subject => 'Welcome!', :to => user.email_address) -# # auto renders both welcome.text.erb and welcome.html.erb -# end -# -# def goodbye(user) -# headers["X-SPAM"] = 'Not-SPAM' -# mail(:subject => 'Goodbye', :to => user.email_address) do |format| -# format.html { render "shared_template "} -# format.text # goodbye.text.erb -# end -# end -# -# def surprise(user, gift) -# attachments[gift.name] = File.read(gift.path) -# mail(:subject => 'Surprise!', :to => user.email_address) do |format| -# format.html(:charset => "ascii") # surprise.html.erb -# format.text(:transfer_encoding => "base64") # surprise.text.erb -# end -# end -# -# def special_surprise(user, gift) -# attachments[gift.name] = { :content_type => "application/x-gzip", :content => File.read(gift.path) } -# mail(:to => 'special@example.com') # subject not required -# # auto renders both special_surprise.text.erb and special_surprise.html.erb -# end -# end -# -# mail = Notifier.welcome(user) # => returns a Mail object -# mail.deliver -# -# Notifier.welcome(user).deliver # => creates and sends the Mail in one step class BaseTest < ActiveSupport::TestCase DEFAULT_HEADERS = { :to => 'mikel@test.lindsaar.net', @@ -44,8 +10,6 @@ class BaseTest < ActiveSupport::TestCase :subject => 'The first email on new API!' } - # TODO Think on the simple case where I want to send an e-mail - # with attachment and small text (without need to add a template). class BaseMailer < ActionMailer::Base self.mailer_name = "base_mailer" @@ -60,7 +24,7 @@ class BaseTest < ActiveSupport::TestCase end def attachment_with_hash - attachments['invoice.jpg'] = { :content => "you smiling", :mime_type => "image/x-jpg", + attachments['invoice.jpg'] = { :data => "you smiling", :mime_type => "image/x-jpg", :transfer_encoding => "base64" } mail(DEFAULT_HEADERS) end @@ -93,12 +57,12 @@ class BaseTest < ActiveSupport::TestCase end test "method call to mail does not raise error" do - assert_nothing_raised { BaseMailer.deliver_welcome } + assert_nothing_raised { BaseMailer.welcome.deliver } end # Basic mail usage without block test "mail() should set the headers of the mail message" do - email = BaseMailer.deliver_welcome + 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!') @@ -106,13 +70,13 @@ class BaseTest < ActiveSupport::TestCase test "mail() with bcc, cc, content_type, charset, mime_version, reply_to and date" do @time = Time.now - email = BaseMailer.deliver_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', - :date => @time) + 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', + :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') @@ -123,50 +87,50 @@ class BaseTest < ActiveSupport::TestCase end test "mail() renders the template using the method being processed" do - email = BaseMailer.deliver_welcome + email = BaseMailer.welcome.deliver assert_equal("Welcome", email.body.encoded) end test "can pass in :body to the mail method hash" do - email = BaseMailer.deliver_welcome(:body => "Hello there") + email = BaseMailer.welcome(:body => "Hello there").deliver assert_equal("text/plain", email.mime_type) assert_equal("Hello there", email.body.encoded) end # Custom headers test "custom headers" do - email = BaseMailer.deliver_welcome + email = BaseMailer.welcome.deliver assert_equal("Not SPAM", email['X-SPAM'].decoded) end # Attachments test "attachment with content" do - email = BaseMailer.deliver_attachment_with_content + email = BaseMailer.attachment_with_content.deliver 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.deliver_attachment_with_content + email = BaseMailer.attachment_with_content.deliver assert_equal('invoice.pdf', email.attachments[0].filename) end test "attachment with hash" do - email = BaseMailer.deliver_attachment_with_hash + email = BaseMailer.attachment_with_hash.deliver 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.deliver_attachment_with_content + email = BaseMailer.attachment_with_content.deliver assert_equal(1, email.attachments.length) assert_equal("multipart/mixed", email.mime_type) end test "adds the rendered template as part" do - email = BaseMailer.deliver_attachment_with_content + email = BaseMailer.attachment_with_content.deliver assert_equal(2, email.parts.length) assert_equal("multipart/mixed", email.mime_type) assert_equal("text/html", email.parts[0].mime_type) @@ -176,7 +140,7 @@ class BaseTest < ActiveSupport::TestCase end test "adds the given :body as part" do - email = BaseMailer.deliver_attachment_with_content(:body => "I'm the eggman") + email = BaseMailer.attachment_with_content(:body => "I'm the eggman").deliver assert_equal(2, email.parts.length) assert_equal("multipart/mixed", email.mime_type) assert_equal("text/plain", email.parts[0].mime_type) @@ -188,46 +152,46 @@ class BaseTest < ActiveSupport::TestCase # Defaults values test "uses default charset from class" do swap BaseMailer, :default_charset => "US-ASCII" do - email = BaseMailer.deliver_welcome + email = BaseMailer.welcome.deliver assert_equal("US-ASCII", email.charset) - email = BaseMailer.deliver_welcome(:charset => "iso-8559-1") + email = BaseMailer.welcome(:charset => "iso-8559-1").deliver 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.deliver_welcome + email = BaseMailer.welcome.deliver assert_equal("text/html", email.mime_type) - email = BaseMailer.deliver_welcome(:content_type => "text/plain") + email = BaseMailer.welcome(:content_type => "text/plain").deliver 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.deliver_welcome + email = BaseMailer.welcome.deliver assert_equal("2.0", email.mime_version) - email = BaseMailer.deliver_welcome(:mime_version => "1.0") + email = BaseMailer.welcome(:mime_version => "1.0").deliver assert_equal("1.0", email.mime_version) end end test "subject gets default from I18n" do - email = BaseMailer.deliver_welcome(:subject => nil) + email = BaseMailer.welcome(:subject => nil).deliver assert_equal "Welcome", email.subject I18n.backend.store_translations('en', :actionmailer => {:base_mailer => {:welcome => {:subject => "New Subject!"}}}) - email = BaseMailer.deliver_welcome(:subject => nil) + email = BaseMailer.welcome(:subject => nil).deliver assert_equal "New Subject!", email.subject end # Implicit multipart test "implicit multipart" do - email = BaseMailer.deliver_implicit_multipart + email = BaseMailer.implicit_multipart.deliver assert_equal(2, email.parts.size) assert_equal("multipart/alternate", email.mime_type) assert_equal("text/plain", email.parts[0].mime_type) @@ -239,18 +203,18 @@ 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.deliver_implicit_multipart + email = BaseMailer.implicit_multipart.deliver assert_equal("text/html", email.parts[0].mime_type) assert_equal("text/plain", email.parts[1].mime_type) - email = BaseMailer.deliver_implicit_multipart(:parts_order => order.reverse) + email = BaseMailer.implicit_multipart(:parts_order => order.reverse).deliver 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.deliver_implicit_multipart(:attachments => true) + email = BaseMailer.implicit_multipart(:attachments => true).deliver assert_equal("application/pdf", email.parts[0].mime_type) assert_equal("multipart/alternate", email.parts[1].mime_type) assert_equal("text/plain", email.parts[1].parts[0].mime_type) @@ -262,7 +226,7 @@ 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.deliver_implicit_multipart(:attachments => true) + email = BaseMailer.implicit_multipart(:attachments => true).deliver assert_equal("application/pdf", email.parts[0].mime_type) assert_equal("multipart/alternate", email.parts[1].mime_type) assert_equal("text/plain", email.parts[1].parts[1].mime_type) @@ -272,7 +236,7 @@ class BaseTest < ActiveSupport::TestCase # Explicit multipart test "explicit multipart" do - email = BaseMailer.deliver_explicit_multipart + email = BaseMailer.explicit_multipart.deliver assert_equal(2, email.parts.size) assert_equal("multipart/alternate", email.mime_type) assert_equal("text/plain", email.parts[0].mime_type) @@ -284,18 +248,18 @@ 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.deliver_explicit_multipart + email = BaseMailer.explicit_multipart.deliver assert_equal("text/plain", email.parts[0].mime_type) assert_equal("text/html", email.parts[1].mime_type) - email = BaseMailer.deliver_explicit_multipart(:parts_order => order.reverse) + email = BaseMailer.explicit_multipart(:parts_order => order.reverse).deliver 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.deliver_explicit_multipart(:attachments => true) + email = BaseMailer.explicit_multipart(:attachments => true).deliver assert_equal("application/pdf", email.parts[0].mime_type) assert_equal("multipart/alternate", email.parts[1].mime_type) assert_equal("text/plain", email.parts[1].parts[0].mime_type) @@ -305,7 +269,7 @@ class BaseTest < ActiveSupport::TestCase end test "explicit multipart with templates" do - email = BaseMailer.deliver_explicit_multipart_templates + email = BaseMailer.explicit_multipart_templates.deliver assert_equal(2, email.parts.size) assert_equal("multipart/alternate", email.mime_type) assert_equal("text/html", email.parts[0].mime_type) @@ -315,7 +279,7 @@ class BaseTest < ActiveSupport::TestCase end test "explicit multipart with any" do - email = BaseMailer.deliver_explicit_multipart_with_any + email = BaseMailer.explicit_multipart_with_any.deliver assert_equal(2, email.parts.size) assert_equal("multipart/alternate", email.mime_type) assert_equal("text/plain", email.parts[0].mime_type) @@ -324,21 +288,30 @@ class BaseTest < ActiveSupport::TestCase assert_equal("Format with any!", email.parts[1].body.encoded) end - test "ActionMailer should be told when Mail gets delivered" do - BaseMailer.deliveries.clear - BaseMailer.expects(:delivered_email).once - BaseMailer.deliver_welcome - assert_equal(1, BaseMailer.deliveries.length) + # Class level API with method missing + test "should respond to action methods" do + assert BaseMailer.respond_to?(:welcome) + assert BaseMailer.respond_to?(:implicit_multipart) + assert !BaseMailer.respond_to?(:mail) + assert !BaseMailer.respond_to?(:headers) 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 assert_equal(0, BaseMailer.deliveries.length) assert_equal('The first email on new API!', email.subject) 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(:delivered_email).once + BaseMailer.welcome.deliver + assert_equal(1, BaseMailer.deliveries.length) + end + + # Delivery hooks + test "ActionMailer should be told when Mail gets delivered" do BaseMailer.deliveries.clear BaseMailer.expects(:delivered_email).once BaseMailer.welcome.deliver |