From bd96614101262e0ad0cc176ed8e2d95a5c17936b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 19:36:42 +0100 Subject: Move old tests to a specific folder and add some delivery method tests. --- actionmailer/test/abstract_unit.rb | 24 +- actionmailer/test/adv_attr_test.rb | 36 - actionmailer/test/asset_host_test.rb | 52 - actionmailer/test/base_test.rb | 8 - actionmailer/test/delivery_methods_test.rb | 112 ++- actionmailer/test/mail_helper_test.rb | 94 -- actionmailer/test/mail_layout_test.rb | 148 --- actionmailer/test/mail_render_test.rb | 157 --- actionmailer/test/mail_service_test.rb | 1192 ----------------------- actionmailer/test/mail_test.rb | 2 - actionmailer/test/old_base/adv_attr_test.rb | 36 + actionmailer/test/old_base/asset_host_test.rb | 52 + actionmailer/test/old_base/mail_helper_test.rb | 94 ++ actionmailer/test/old_base/mail_layout_test.rb | 148 +++ actionmailer/test/old_base/mail_render_test.rb | 157 +++ actionmailer/test/old_base/mail_service_test.rb | 1192 +++++++++++++++++++++++ actionmailer/test/old_base/tmail_compat_test.rb | 25 + actionmailer/test/old_base/url_test.rb | 84 ++ actionmailer/test/tmail_compat_test.rb | 25 - actionmailer/test/url_test.rb | 84 -- 20 files changed, 1888 insertions(+), 1834 deletions(-) delete mode 100644 actionmailer/test/adv_attr_test.rb delete mode 100644 actionmailer/test/asset_host_test.rb delete mode 100644 actionmailer/test/mail_helper_test.rb delete mode 100644 actionmailer/test/mail_layout_test.rb delete mode 100644 actionmailer/test/mail_render_test.rb delete mode 100644 actionmailer/test/mail_service_test.rb create mode 100644 actionmailer/test/old_base/adv_attr_test.rb create mode 100644 actionmailer/test/old_base/asset_host_test.rb create mode 100644 actionmailer/test/old_base/mail_helper_test.rb create mode 100644 actionmailer/test/old_base/mail_layout_test.rb create mode 100644 actionmailer/test/old_base/mail_render_test.rb create mode 100644 actionmailer/test/old_base/mail_service_test.rb create mode 100644 actionmailer/test/old_base/tmail_compat_test.rb create mode 100644 actionmailer/test/old_base/url_test.rb delete mode 100644 actionmailer/test/tmail_compat_test.rb delete mode 100644 actionmailer/test/url_test.rb (limited to 'actionmailer/test') diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index 1fc5ab85e0..781e49ae05 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -8,7 +8,6 @@ $:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) require 'rubygems' require 'test/unit' - require 'action_mailer' # Show backtraces for deprecated behavior for quicker cleanup. @@ -18,15 +17,11 @@ ActiveSupport::Deprecation.debug = true ActionView::Template.register_template_handler :haml, lambda { |template| "Look its HAML!".inspect } ActionView::Template.register_template_handler :bak, lambda { |template| "Lame backup".inspect } -ActionView::Base::DEFAULT_CONFIG = { :assets_dir => '/nowhere' } - -$:.unshift "#{File.dirname(__FILE__)}/fixtures/helpers" +FIXTURE_LOAD_PATH = File.expand_path('fixtures', File.dirname(__FILE__)) +ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH +$:.unshift File.join(FIXTURE_LOAD_PATH, 'helpers') -FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures') -ActionMailer::Base.template_root = FIXTURE_LOAD_PATH - -class MockSMTP - +class MockSMTP def self.deliveries @@deliveries end @@ -42,7 +37,6 @@ class MockSMTP def start(*args) yield self end - end class Net::SMTP @@ -51,14 +45,6 @@ class Net::SMTP end end -def uses_gem(gem_name, test_name, version = '> 0') - gem gem_name.to_s, version - require gem_name.to_s - yield -rescue LoadError - $stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again." -end - def set_delivery_method(method) @old_delivery_method = ActionMailer::Base.delivery_method ActionMailer::Base.delivery_method = method @@ -66,4 +52,4 @@ end def restore_delivery_method ActionMailer::Base.delivery_method = @old_delivery_method -end +end \ No newline at end of file diff --git a/actionmailer/test/adv_attr_test.rb b/actionmailer/test/adv_attr_test.rb deleted file mode 100644 index f22d733bc5..0000000000 --- a/actionmailer/test/adv_attr_test.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'abstract_unit' -require 'action_mailer/adv_attr_accessor' - -class AdvAttrTest < ActiveSupport::TestCase - class Person - cattr_reader :protected_instance_variables - @@protected_instance_variables = [] - - extend ActionMailer::AdvAttrAccessor - adv_attr_accessor :name - end - - def setup - @person = Person.new - end - - def test_adv_attr - assert_nil @person.name - @person.name 'Bob' - assert_equal 'Bob', @person.name - end - - def test_adv_attr_writer - assert_nil @person.name - @person.name = 'Bob' - assert_equal 'Bob', @person.name - end - - def test_raise_an_error_with_multiple_args - assert_raise(ArgumentError) { @person.name('x', 'y') } - end - - def test_ivar_is_added_to_protected_instnace_variables - assert Person.protected_instance_variables.include?('@name') - end -end diff --git a/actionmailer/test/asset_host_test.rb b/actionmailer/test/asset_host_test.rb deleted file mode 100644 index 124032f1d9..0000000000 --- a/actionmailer/test/asset_host_test.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'abstract_unit' - -class AssetHostMailer < ActionMailer::Base - def email_with_asset - recipients 'test@localhost' - subject "testing email containing asset path while asset_host is set" - from "tester@example.com" - end -end - -class AssetHostTest < Test::Unit::TestCase - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries.clear - end - - def teardown - restore_delivery_method - end - - def test_asset_host_as_string - ActionController::Base.asset_host = "http://www.example.com" - mail = AssetHostMailer.email_with_asset - assert_equal "\"Somelogo\"", mail.body.to_s.strip - end - - def test_asset_host_as_one_arguement_proc - ActionController::Base.asset_host = Proc.new { |source| - if source.starts_with?('/images') - "http://images.example.com" - else - "http://assets.example.com" - end - } - mail = AssetHostMailer.email_with_asset - assert_equal "\"Somelogo\"", mail.body.to_s.strip - end - - def test_asset_host_as_two_arguement_proc - ActionController::Base.asset_host = Proc.new {|source,request| - if request && request.ssl? - "https://www.example.com" - else - "http://www.example.com" - end - } - mail = nil - assert_nothing_raised { mail = AssetHostMailer.email_with_asset } - assert_equal "\"Somelogo\"", mail.body.to_s.strip - end -end \ No newline at end of file diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 3b2a072dce..dd17bf868b 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -310,14 +310,6 @@ class BaseTest < ActiveSupport::TestCase 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 - assert_equal(1, BaseMailer.deliveries.length) - end - protected # Execute the block setting the given values and restoring old values after diff --git a/actionmailer/test/delivery_methods_test.rb b/actionmailer/test/delivery_methods_test.rb index de3d54197d..5b2ce61ca9 100644 --- a/actionmailer/test/delivery_methods_test.rb +++ b/actionmailer/test/delivery_methods_test.rb @@ -4,20 +4,17 @@ require 'mail' class MyCustomDelivery end -class DefaultsDeliveryMethodsTest < ActionMailer::TestCase - def setup - set_delivery_method :smtp +class BogusDelivery + def initialize(*) end - def teardown - restore_delivery_method - end - - def test_should_be_the_default_smtp - assert_equal :smtp, ActionMailer::Base.delivery_method + def deliver!(mail) + raise "failed" end +end - def test_should_have_default_smtp_delivery_method_settings +class DefaultsDeliveryMethodsTest < ActiveSupport::TestCase + test "default smtp settings" do settings = { :address => "localhost", :port => 25, :domain => 'localhost.localdomain', @@ -28,45 +25,126 @@ class DefaultsDeliveryMethodsTest < ActionMailer::TestCase assert_equal settings, ActionMailer::Base.smtp_settings end - def test_should_have_default_file_delivery_method_settings + test "default file delivery settings" do settings = {:location => "#{Dir.tmpdir}/mails"} assert_equal settings, ActionMailer::Base.file_settings end - def test_should_have_default_sendmail_delivery_method_settings + test "default sendmail settings" do settings = {:location => '/usr/sbin/sendmail', :arguments => '-i -t'} assert_equal settings, ActionMailer::Base.sendmail_settings end end -class CustomDeliveryMethodsTest < ActionMailer::TestCase +class CustomDeliveryMethodsTest < ActiveSupport::TestCase def setup + @old_delivery_method = ActionMailer::Base.delivery_method ActionMailer::Base.add_delivery_method :custom, MyCustomDelivery end def teardown + ActionMailer::Base.delivery_method = @old_delivery_method ActionMailer::Base.delivery_methods.delete(:custom) end - def test_allow_to_add_a_custom_delivery_method + test "allow to add custom delivery method" do ActionMailer::Base.delivery_method = :custom assert_equal :custom, ActionMailer::Base.delivery_method end - def test_allow_to_customize_custom_settings + test "allow to customize custom settings" do ActionMailer::Base.custom_settings = { :foo => :bar } assert_equal Hash[:foo => :bar], ActionMailer::Base.custom_settings end - def test_respond_to_custom_method_settings + test "respond to custom settings" do assert_respond_to ActionMailer::Base, :custom_settings assert_respond_to ActionMailer::Base, :custom_settings= end - def test_should_not_respond_for_invalid_method_settings + test "does not respond to unknown settings" do assert_raise NoMethodError do ActionMailer::Base.another_settings end end end + +class MailDeliveryTest < ActiveSupport::TestCase + class DeliverMail < ActionMailer::Base + DEFAULT_HEADERS = { + :to => 'mikel@test.lindsaar.net', + :from => 'jose@test.plataformatec.com' + } + + def welcome(hash={}) + mail(DEFAULT_HEADERS.merge(hash)) + end + end + + def setup + ActionMailer::Base.delivery_method = :smtp + end + + def teardown + DeliverMail.delivery_method = :smtp + DeliverMail.perform_deliveries = true + DeliverMail.raise_delivery_errors = true + end + + test "ActionMailer should be told when Mail gets delivered" do + DeliverMail.deliveries.clear + DeliverMail.expects(:delivered_email).once + DeliverMail.welcome.deliver + assert_equal(1, DeliverMail.deliveries.length) + end + + test "delivery method can be customized per instance" do + email = DeliverMail.welcome.deliver + assert_instance_of Mail::SMTP, email.delivery_method + email = DeliverMail.welcome(:delivery_method => :test).deliver + assert_instance_of Mail::TestMailer, email.delivery_method + end + + test "delivery method can be customized in subclasses not changing the parent" do + DeliverMail.delivery_method = :test + assert_equal :smtp, ActionMailer::Base.delivery_method + $BREAK = true + email = DeliverMail.welcome.deliver + assert_instance_of Mail::TestMailer, email.delivery_method + end + + test "non registered delivery methods raises errors" do + DeliverMail.delivery_method = :unknown + assert_raise RuntimeError do + DeliverMail.welcome.deliver + end + end + + test "does not perform deliveries if requested" do + DeliverMail.perform_deliveries = false + DeliverMail.deliveries.clear + DeliverMail.expects(:delivered_email).never + DeliverMail.welcome.deliver + assert_equal(0, DeliverMail.deliveries.length) + end + + test "raise errors on bogus deliveries" do + DeliverMail.delivery_method = BogusDelivery + DeliverMail.deliveries.clear + DeliverMail.expects(:delivered_email).never + assert_raise RuntimeError do + DeliverMail.welcome.deliver + end + assert_equal(0, DeliverMail.deliveries.length) + end + + test "does not raise errors on bogus deliveries if set" do + DeliverMail.delivery_method = BogusDelivery + DeliverMail.raise_delivery_errors = false + DeliverMail.deliveries.clear + DeliverMail.expects(:delivered_email).once + DeliverMail.welcome.deliver + assert_equal(1, DeliverMail.deliveries.length) + end +end \ No newline at end of file diff --git a/actionmailer/test/mail_helper_test.rb b/actionmailer/test/mail_helper_test.rb deleted file mode 100644 index a9b3cd3ce1..0000000000 --- a/actionmailer/test/mail_helper_test.rb +++ /dev/null @@ -1,94 +0,0 @@ -require 'abstract_unit' - -module MailerHelper - def person_name - "Mr. Joe Person" - end -end - -class HelperMailer < ActionMailer::Base - helper MailerHelper - helper :example - - def use_helper - recipients 'test@localhost' - subject "using helpers" - from "tester@example.com" - end - - def use_example_helper - recipients 'test@localhost' - subject "using helpers" - from "tester@example.com" - - @text = "emphasize me!" - end - - def use_mail_helper - recipients 'test@localhost' - subject "using mailing helpers" - from "tester@example.com" - - @text = "But soft! What light through yonder window breaks? It is the east, " + - "and Juliet is the sun. Arise, fair sun, and kill the envious moon, " + - "which is sick and pale with grief that thou, her maid, art far more " + - "fair than she. Be not her maid, for she is envious! Her vestal " + - "livery is but sick and green, and none but fools do wear it. Cast " + - "it off!" - end - - def use_helper_method - recipients 'test@localhost' - subject "using helpers" - from "tester@example.com" - - @text = "emphasize me!" - end - - private - - def name_of_the_mailer_class - self.class.name - end - helper_method :name_of_the_mailer_class -end - -class MailerHelperTest < ActiveSupport::TestCase - def new_mail( charset="utf-8" ) - mail = Mail.new - mail.set_content_type "text", "plain", { "charset" => charset } if charset - mail - end - - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries.clear - end - - def teardown - restore_delivery_method - end - - def test_use_helper - mail = HelperMailer.use_helper - assert_match %r{Mr. Joe Person}, mail.encoded - end - - def test_use_example_helper - mail = HelperMailer.use_example_helper - assert_match %r{emphasize me!}, mail.encoded - end - - def test_use_helper_method - mail = HelperMailer.use_helper_method - assert_match %r{HelperMailer}, mail.encoded - end - - def test_use_mail_helper - mail = HelperMailer.use_mail_helper - assert_match %r{ But soft!}, mail.encoded - assert_match %r{east, and\r\n Juliet}, mail.encoded - end -end - diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/mail_layout_test.rb deleted file mode 100644 index 4038fbf339..0000000000 --- a/actionmailer/test/mail_layout_test.rb +++ /dev/null @@ -1,148 +0,0 @@ -require 'abstract_unit' - -class AutoLayoutMailer < ActionMailer::Base - - def hello - recipients 'test@localhost' - subject "You have a mail" - from "tester@example.com" - end - - def spam - recipients 'test@localhost' - subject "You have a mail" - from "tester@example.com" - - @world = "Earth" - render(:inline => "Hello, <%= @world %>", :layout => 'spam') - end - - def nolayout - recipients 'test@localhost' - subject "You have a mail" - from "tester@example.com" - - @world = "Earth" - render(:inline => "Hello, <%= @world %>", :layout => false) - end - - def multipart(type = nil) - recipients 'test@localhost' - subject "You have a mail" - from "tester@example.com" - - content_type(type) if type - end -end - -class ExplicitLayoutMailer < ActionMailer::Base - layout 'spam', :except => [:logout] - - def signup - recipients 'test@localhost' - subject "You have a mail" - from "tester@example.com" - end - - def logout - recipients 'test@localhost' - subject "You have a mail" - from "tester@example.com" - end -end - -class LayoutMailerTest < Test::Unit::TestCase - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries.clear - end - - def teardown - restore_delivery_method - end - - def test_should_pickup_default_layout - mail = AutoLayoutMailer.hello - assert_equal "Hello from layout Inside", mail.body.to_s.strip - end - - def test_should_pickup_multipart_layout - mail = AutoLayoutMailer.multipart - # CHANGED: content_type returns an object - # assert_equal "multipart/alternative", mail.content_type - assert_equal "multipart/alternative", mail.mime_type - assert_equal 2, mail.parts.size - - # CHANGED: content_type returns an object - # assert_equal 'text/plain', mail.parts.first.content_type - assert_equal 'text/plain', mail.parts.first.mime_type - - # CHANGED: body returns an object - # assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body - assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s - - # CHANGED: content_type returns an object - # assert_equal 'text/html', mail.parts.last.content_type - assert_equal 'text/html', mail.parts.last.mime_type - - # CHANGED: body returns an object - # assert_equal "Hello from layout text/html multipart", mail.parts.last.body - assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s - end - - def test_should_pickup_multipartmixed_layout - mail = AutoLayoutMailer.multipart("multipart/mixed") - # CHANGED: content_type returns an object - # assert_equal "multipart/mixed", mail.content_type - assert_equal "multipart/mixed", mail.mime_type - assert_equal 2, mail.parts.size - - # CHANGED: content_type returns an object - # assert_equal 'text/plain', mail.parts.first.content_type - assert_equal 'text/plain', mail.parts.first.mime_type - # CHANGED: body returns an object - # assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body - assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s - - # CHANGED: content_type returns an object - # assert_equal 'text/html', mail.parts.last.content_type - assert_equal 'text/html', mail.parts.last.mime_type - # CHANGED: body returns an object - # assert_equal "Hello from layout text/html multipart", mail.parts.last.body - assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s - end - - def test_should_fix_multipart_layout - mail = AutoLayoutMailer.multipart("text/plain") - assert_equal "multipart/alternative", mail.mime_type - assert_equal 2, mail.parts.size - - assert_equal 'text/plain', mail.parts.first.mime_type - assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s - - assert_equal 'text/html', mail.parts.last.mime_type - assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s - end - - - def test_should_pickup_layout_given_to_render - mail = AutoLayoutMailer.spam - assert_equal "Spammer layout Hello, Earth", mail.body.to_s.strip - end - - def test_should_respect_layout_false - mail = AutoLayoutMailer.nolayout - assert_equal "Hello, Earth", mail.body.to_s.strip - end - - def test_explicit_class_layout - mail = ExplicitLayoutMailer.signup - assert_equal "Spammer layout We do not spam", mail.body.to_s.strip - end - - def test_explicit_layout_exceptions - mail = ExplicitLayoutMailer.logout - assert_equal "You logged out", mail.body.to_s.strip - end -end diff --git a/actionmailer/test/mail_render_test.rb b/actionmailer/test/mail_render_test.rb deleted file mode 100644 index 804200fd36..0000000000 --- a/actionmailer/test/mail_render_test.rb +++ /dev/null @@ -1,157 +0,0 @@ -require 'abstract_unit' - -class RenderMailer < ActionMailer::Base - def inline_template - recipients 'test@localhost' - subject "using helpers" - from "tester@example.com" - - @world = "Earth" - render :inline => "Hello, <%= @world %>" - end - - def file_template - recipients 'test@localhost' - subject "using helpers" - from "tester@example.com" - - @recipient = 'test@localhost' - render :file => "templates/signed_up" - end - - def implicit_body - recipients 'test@localhost' - subject "using helpers" - from "tester@example.com" - - @recipient = 'test@localhost' - render :template => "templates/signed_up" - end - - def rxml_template - recipients 'test@localhost' - subject "rendering rxml template" - from "tester@example.com" - end - - def included_subtemplate - recipients 'test@localhost' - subject "Including another template in the one being rendered" - from "tester@example.com" - end - - def mailer_accessor - recipients 'test@localhost' - subject "Mailer Accessor" - from "tester@example.com" - - render :inline => "Look, <%= mailer.subject %>!" - end - - def no_instance_variable - recipients 'test@localhost' - subject "No Instance Variable" - from "tester@example.com" - - silence_warnings do - render :inline => "Look, subject.nil? is <%= @subject.nil? %>!" - end - end - - def initialize_defaults(method_name) - super - mailer_name "test_mailer" - end -end - -class FirstMailer < ActionMailer::Base - def share - recipients 'test@localhost' - subject "using helpers" - from "tester@example.com" - end -end - -class SecondMailer < ActionMailer::Base - def share - recipients 'test@localhost' - subject "using helpers" - from "tester@example.com" - end -end - -# CHANGED: Those tests were changed because body returns an object now -# Instead of mail.body.strip, we should mail.body.to_s.strip -class RenderHelperTest < Test::Unit::TestCase - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries.clear - - @recipient = 'test@localhost' - end - - def teardown - restore_delivery_method - end - - def test_implicit_body - mail = RenderMailer.implicit_body - assert_equal "Hello there, \n\nMr. test@localhost", mail.body.to_s.strip - end - - def test_inline_template - mail = RenderMailer.inline_template - assert_equal "Hello, Earth", mail.body.to_s.strip - end - - def test_file_template - mail = RenderMailer.file_template - assert_equal "Hello there, \n\nMr. test@localhost", mail.body.to_s.strip - end - - def test_rxml_template - mail = RenderMailer.rxml_template.deliver - assert_equal "\n", mail.body.to_s.strip - end - - def test_included_subtemplate - mail = RenderMailer.included_subtemplate.deliver - assert_equal "Hey Ho, let's go!", mail.body.to_s.strip - end - - def test_mailer_accessor - mail = RenderMailer.mailer_accessor.deliver - assert_equal "Look, Mailer Accessor!", mail.body.to_s.strip - end - - def test_no_instance_variable - mail = RenderMailer.no_instance_variable.deliver - assert_equal "Look, subject.nil? is true!", mail.body.to_s.strip - end -end - -class FirstSecondHelperTest < Test::Unit::TestCase - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries.clear - - @recipient = 'test@localhost' - end - - def teardown - restore_delivery_method - end - - def test_ordering - mail = FirstMailer.share - assert_equal "first mail", mail.body.to_s.strip - mail = SecondMailer.share - assert_equal "second mail", mail.body.to_s.strip - mail = FirstMailer.share - assert_equal "first mail", mail.body.to_s.strip - mail = SecondMailer.share - assert_equal "second mail", mail.body.to_s.strip - end -end diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb deleted file mode 100644 index aa31c9a803..0000000000 --- a/actionmailer/test/mail_service_test.rb +++ /dev/null @@ -1,1192 +0,0 @@ -# encoding: utf-8 -require 'abstract_unit' - -class FunkyPathMailer < ActionMailer::Base - self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots" - - def multipart_with_template_path_with_dots(recipient) - recipients recipient - subject "This path has dots" - from "Chad Fowler " - attachment :content_type => "text/plain", - :data => "dots dots dots..." - end -end - -class TestMailer < ActionMailer::Base - def signed_up(recipient) - recipients recipient - subject "[Signed up] Welcome #{recipient}" - from "system@loudthinking.com" - - @recipient = recipient - end - - def cancelled_account(recipient) - recipients recipient - subject "[Cancelled] Goodbye #{recipient}" - from "system@loudthinking.com" - sent_on Time.local(2004, 12, 12) - - render :text => "Goodbye, Mr. #{recipient}" - end - - def from_with_name - from "System " - recipients "root@loudthinking.com" - render :text => "Nothing to see here." - end - - def from_without_name - from "system@loudthinking.com" - recipients "root@loudthinking.com" - render :text => "Nothing to see here." - end - - def cc_bcc(recipient) - recipients recipient - subject "testing bcc/cc" - from "system@loudthinking.com" - sent_on Time.local(2004, 12, 12) - cc "nobody@loudthinking.com" - bcc "root@loudthinking.com" - - render :text => "Nothing to see here." - end - - def different_reply_to(recipient) - recipients recipient - subject "testing reply_to" - from "system@loudthinking.com" - sent_on Time.local(2008, 5, 23) - reply_to "atraver@gmail.com" - - render :text => "Nothing to see here." - end - - def iso_charset(recipient) - recipients recipient - subject "testing isø charsets" - from "system@loudthinking.com" - sent_on Time.local(2004, 12, 12) - cc "nobody@loudthinking.com" - bcc "root@loudthinking.com" - charset "iso-8859-1" - - render :text => "Nothing to see here." - end - - def unencoded_subject(recipient) - recipients recipient - subject "testing unencoded subject" - from "system@loudthinking.com" - sent_on Time.local(2004, 12, 12) - cc "nobody@loudthinking.com" - bcc "root@loudthinking.com" - - render :text => "Nothing to see here." - end - - def extended_headers(recipient) - recipients recipient - subject "testing extended headers" - from "Grytøyr " - sent_on Time.local(2004, 12, 12) - cc "Grytøyr " - bcc "Grytøyr " - charset "iso-8859-1" - - render :text => "Nothing to see here." - end - - def utf8_body(recipient) - recipients recipient - subject "testing utf-8 body" - from "Foo áëô îü " - sent_on Time.local(2004, 12, 12) - cc "Foo áëô îü " - bcc "Foo áëô îü " - charset "utf-8" - - render :text => "åœö blah" - end - - def multipart_with_mime_version(recipient) - recipients recipient - subject "multipart with mime_version" - from "test@example.com" - sent_on Time.local(2004, 12, 12) - mime_version "1.1" - content_type "multipart/alternative" - - part "text/plain" do |p| - p.body = render(:text => "blah") - end - - part "text/html" do |p| - p.body = render(:inline => "<%= content_tag(:b, 'blah') %>") - end - end - - def multipart_with_utf8_subject(recipient) - recipients recipient - subject "Foo áëô îü" - from "test@example.com" - charset "utf-8" - - part "text/plain" do |p| - p.body = "blah" - end - - part "text/html" do |p| - p.body = "blah" - end - end - - def explicitly_multipart_example(recipient, ct=nil) - recipients recipient - subject "multipart example" - from "test@example.com" - sent_on Time.local(2004, 12, 12) - content_type ct if ct - - part "text/html" do |p| - p.charset = "iso-8859-1" - p.body = "blah" - end - - attachment :content_type => "image/jpeg", :filename => File.join(File.dirname(__FILE__), "fixtures", "attachments", "foo.jpg"), - :data => "123456789" - - render :text => "plain text default" - end - - def implicitly_multipart_example(recipient, cs = nil, order = nil) - recipients recipient - subject "multipart example" - from "test@example.com" - sent_on Time.local(2004, 12, 12) - - @charset = cs if cs - @recipient = recipient - @implicit_parts_order = order if order - end - - def implicitly_multipart_with_utf8 - recipients "no.one@nowhere.test" - subject "Foo áëô îü" - from "some.one@somewhere.test" - template "implicitly_multipart_example" - - @recipient = "no.one@nowhere.test" - end - - def html_mail(recipient) - recipients recipient - subject "html mail" - from "test@example.com" - content_type "text/html" - - render :text => "Emphasize this" - end - - def html_mail_with_underscores(recipient) - subject "html mail with underscores" - render :text => %{_Google} - end - - def custom_template(recipient) - recipients recipient - subject "[Signed up] Welcome #{recipient}" - from "system@loudthinking.com" - sent_on Time.local(2004, 12, 12) - template "signed_up" - - @recipient = recipient - end - - def custom_templating_extension(recipient) - recipients recipient - subject "[Signed up] Welcome #{recipient}" - from "system@loudthinking.com" - sent_on Time.local(2004, 12, 12) - - @recipient = recipient - end - - def various_newlines(recipient) - recipients recipient - subject "various newlines" - from "test@example.com" - - render :text => "line #1\nline #2\rline #3\r\nline #4\r\r" + - "line #5\n\nline#6\r\n\r\nline #7" - end - - def various_newlines_multipart(recipient) - recipients recipient - subject "various newlines multipart" - from "test@example.com" - content_type "multipart/alternative" - - part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r" - part :content_type => "text/html", :body => "

line #1

\n

line #2

\r

line #3

\r\n

line #4

\r\r" - end - - def nested_multipart(recipient) - recipients recipient - subject "nested multipart" - from "test@example.com" - content_type "multipart/mixed" - - part :content_type => "multipart/alternative", :content_disposition => "inline", "foo" => "bar" do |p| - p.part :content_type => "text/plain", :body => "test text\nline #2" - p.part :content_type => "text/html", :body => "test HTML
\nline #2" - end - - attachment :content_type => "application/octet-stream", :filename => "test.txt", :data => "test abcdefghijklmnopqstuvwxyz" - end - - def nested_multipart_with_body(recipient) - recipients recipient - subject "nested multipart with body" - from "test@example.com" - content_type "multipart/mixed" - - part :content_type => "multipart/alternative", :content_disposition => "inline", :body => "Nothing to see here." do |p| - p.part :content_type => "text/html", :body => "test HTML
" - end - end - - def attachment_with_custom_header(recipient) - recipients recipient - subject "custom header in attachment" - from "test@example.com" - content_type "multipart/related" - part :content_type => "text/html", :body => 'yo' - attachment :content_type => "image/jpeg", :filename => File.join(File.dirname(__FILE__), "fixtures", "attachments", "test.jpg"), :data => "i am not a real picture", 'Content-ID' => '' - end - - def unnamed_attachment(recipient) - recipients recipient - subject "nested multipart" - from "test@example.com" - content_type "multipart/mixed" - part :content_type => "text/plain", :body => "hullo" - attachment :content_type => "application/octet-stream", :data => "test abcdefghijklmnopqstuvwxyz" - end - - def headers_with_nonalpha_chars(recipient) - recipients recipient - subject "nonalpha chars" - from "One: Two " - cc "Three: Four " - bcc "Five: Six " - render :text => "testing" - end - - def custom_content_type_attributes - recipients "no.one@nowhere.test" - subject "custom content types" - from "some.one@somewhere.test" - content_type "text/plain; format=flowed" - render :text => "testing" - end - - def return_path - recipients "no.one@nowhere.test" - subject "return path test" - from "some.one@somewhere.test" - headers["return-path"] = "another@somewhere.test" - render :text => "testing" - end - - def subject_with_i18n(recipient) - recipients recipient - from "system@loudthinking.com" - render :text => "testing" - end - - class << self - attr_accessor :received_body - end - - def receive(mail) - self.class.received_body = mail.body - end -end - -class ActionMailerTest < Test::Unit::TestCase - include ActionMailer::Quoting - - def encode( text, charset="utf-8" ) - quoted_printable( text, charset ) - end - - def new_mail( charset="utf-8" ) - mail = Mail.new - mail.mime_version = "1.0" - if charset - mail.content_type ["text", "plain", { "charset" => charset }] - end - mail - end - - # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3 - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.raise_delivery_errors = true - ActionMailer::Base.deliveries.clear - - @original_logger = TestMailer.logger - @recipient = 'test@localhost' - end - - def teardown - TestMailer.logger = @original_logger - restore_delivery_method - end - - def test_nested_parts - created = nil - assert_nothing_raised { created = TestMailer.nested_multipart(@recipient)} - assert_equal 2, created.parts.size - assert_equal 2, created.parts.first.parts.size - - assert_equal "multipart/mixed", created.mime_type - assert_equal "multipart/alternative", created.parts[0].mime_type - assert_equal "bar", created.parts[0].header['foo'].to_s - assert_nil created.parts[0].charset - assert_equal "text/plain", created.parts[0].parts[0].mime_type - assert_equal "text/html", created.parts[0].parts[1].mime_type - assert_equal "application/octet-stream", created.parts[1].mime_type - - end - - def test_nested_parts_with_body - created = nil - TestMailer.nested_multipart_with_body(@recipient) - assert_nothing_raised { created = TestMailer.nested_multipart_with_body(@recipient)} - - assert_equal 1,created.parts.size - assert_equal 2,created.parts.first.parts.size - - assert_equal "multipart/mixed", created.mime_type - assert_equal "multipart/alternative", created.parts.first.mime_type - assert_equal "text/plain", created.parts.first.parts.first.mime_type - assert_equal "Nothing to see here.", created.parts.first.parts.first.body.to_s - assert_equal "text/html", created.parts.first.parts.second.mime_type - assert_equal "test HTML
", created.parts.first.parts.second.body.to_s - end - - def test_attachment_with_custom_header - created = nil - assert_nothing_raised { created = TestMailer.attachment_with_custom_header(@recipient) } - assert created.parts.any? { |p| p.header['content-id'].to_s == "" } - end - - def test_signed_up - Time.stubs(:now => Time.now) - - expected = new_mail - expected.to = @recipient - expected.subject = "[Signed up] Welcome #{@recipient}" - expected.body = "Hello there, \n\nMr. #{@recipient}" - expected.from = "system@loudthinking.com" - expected.date = Time.now - - created = nil - assert_nothing_raised { created = TestMailer.signed_up(@recipient) } - assert_not_nil created - - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - - assert_equal expected.encoded, created.encoded - - assert_nothing_raised { TestMailer.signed_up(@recipient).deliver } - - delivered = ActionMailer::Base.deliveries.first - assert_not_nil delivered - - expected.message_id = '<123@456>' - delivered.message_id = '<123@456>' - - assert_equal expected.encoded, delivered.encoded - end - - def test_custom_template - expected = new_mail - expected.to = @recipient - expected.subject = "[Signed up] Welcome #{@recipient}" - expected.body = "Hello there, \n\nMr. #{@recipient}" - expected.from = "system@loudthinking.com" - expected.date = Time.local(2004, 12, 12) - - created = nil - assert_nothing_raised { created = TestMailer.custom_template(@recipient) } - assert_not_nil created - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - assert_equal expected.encoded, created.encoded - end - - def test_custom_templating_extension - assert ActionView::Template.template_handler_extensions.include?("haml"), "haml extension was not registered" - - # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory - expected = new_mail - expected.to = @recipient - expected.subject = "[Signed up] Welcome #{@recipient}" - expected.body = "Hello there, \n\nMr. #{@recipient}" - expected.from = "system@loudthinking.com" - expected.date = Time.local(2004, 12, 12) - - # Stub the render method so no alternative renderers need be present. - ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}") - - # Now that the template is registered, there should be one part. The text/plain part. - created = nil - assert_nothing_raised { created = TestMailer.custom_templating_extension(@recipient) } - assert_not_nil created - assert_equal 2, created.parts.length - assert_equal 'text/plain', created.parts[0].mime_type - assert_equal 'text/html', created.parts[1].mime_type - end - - def test_cancelled_account - expected = new_mail - expected.to = @recipient - expected.subject = "[Cancelled] Goodbye #{@recipient}" - expected.body = "Goodbye, Mr. #{@recipient}" - expected.from = "system@loudthinking.com" - expected.date = Time.local(2004, 12, 12) - - created = nil - assert_nothing_raised { created = TestMailer.cancelled_account(@recipient) } - assert_not_nil created - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - assert_equal expected.encoded, created.encoded - - assert_nothing_raised { TestMailer.cancelled_account(@recipient).deliver } - assert_not_nil ActionMailer::Base.deliveries.first - delivered = ActionMailer::Base.deliveries.first - expected.message_id = '<123@456>' - delivered.message_id = '<123@456>' - - assert_equal expected.encoded, delivered.encoded - end - - def test_cc_bcc - expected = new_mail - expected.to = @recipient - expected.subject = "testing bcc/cc" - expected.body = "Nothing to see here." - expected.from = "system@loudthinking.com" - expected.cc = "nobody@loudthinking.com" - expected.bcc = "root@loudthinking.com" - expected.date = Time.local 2004, 12, 12 - - created = nil - assert_nothing_raised do - created = TestMailer.cc_bcc @recipient - end - assert_not_nil created - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - assert_equal expected.encoded, created.encoded - - assert_nothing_raised do - TestMailer.cc_bcc(@recipient).deliver - end - - assert_not_nil ActionMailer::Base.deliveries.first - delivered = ActionMailer::Base.deliveries.first - expected.message_id = '<123@456>' - delivered.message_id = '<123@456>' - - assert_equal expected.encoded, delivered.encoded - end - - def test_from_without_name_for_smtp - TestMailer.delivery_method = :smtp - TestMailer.from_without_name.deliver - - mail = MockSMTP.deliveries.first - assert_not_nil mail - mail, from, to = mail - - assert_equal 'system@loudthinking.com', from.to_s - end - - def test_from_with_name_for_smtp - TestMailer.delivery_method = :smtp - TestMailer.from_with_name.deliver - - mail = MockSMTP.deliveries.first - assert_not_nil mail - mail, from, to = mail - - assert_equal 'system@loudthinking.com', from - end - - def test_reply_to - expected = new_mail - - expected.to = @recipient - expected.subject = "testing reply_to" - expected.body = "Nothing to see here." - expected.from = "system@loudthinking.com" - expected.reply_to = "atraver@gmail.com" - expected.date = Time.local 2008, 5, 23 - - created = nil - assert_nothing_raised do - created = TestMailer.different_reply_to @recipient - end - assert_not_nil created - - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - - assert_equal expected.encoded, created.encoded - - assert_nothing_raised do - TestMailer.different_reply_to(@recipient).deliver - end - - delivered = ActionMailer::Base.deliveries.first - assert_not_nil delivered - - expected.message_id = '<123@456>' - delivered.message_id = '<123@456>' - - assert_equal expected.encoded, delivered.encoded - end - - def test_iso_charset - expected = new_mail( "iso-8859-1" ) - expected.to = @recipient - expected.subject = encode "testing isø charsets", "iso-8859-1" - expected.body = "Nothing to see here." - expected.from = "system@loudthinking.com" - expected.cc = "nobody@loudthinking.com" - expected.bcc = "root@loudthinking.com" - expected.date = Time.local 2004, 12, 12 - - created = nil - assert_nothing_raised do - created = TestMailer.iso_charset @recipient - end - assert_not_nil created - - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - - assert_equal expected.encoded, created.encoded - - assert_nothing_raised do - TestMailer.iso_charset(@recipient).deliver - end - - delivered = ActionMailer::Base.deliveries.first - assert_not_nil delivered - - expected.message_id = '<123@456>' - delivered.message_id = '<123@456>' - - assert_equal expected.encoded, delivered.encoded - end - - def test_unencoded_subject - expected = new_mail - expected.to = @recipient - expected.subject = "testing unencoded subject" - expected.body = "Nothing to see here." - expected.from = "system@loudthinking.com" - expected.cc = "nobody@loudthinking.com" - expected.bcc = "root@loudthinking.com" - expected.date = Time.local 2004, 12, 12 - - created = nil - assert_nothing_raised do - created = TestMailer.unencoded_subject @recipient - end - assert_not_nil created - - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - - assert_equal expected.encoded, created.encoded - - assert_nothing_raised do - TestMailer.unencoded_subject(@recipient).deliver - end - - delivered = ActionMailer::Base.deliveries.first - assert_not_nil delivered - - expected.message_id = '<123@456>' - delivered.message_id = '<123@456>' - - assert_equal expected.encoded, delivered.encoded - end - - def test_deliveries_array - assert_not_nil ActionMailer::Base.deliveries - assert_equal 0, ActionMailer::Base.deliveries.size - TestMailer.signed_up(@recipient).deliver - assert_equal 1, ActionMailer::Base.deliveries.size - assert_not_nil ActionMailer::Base.deliveries.first - end - - def test_perform_deliveries_flag - ActionMailer::Base.perform_deliveries = false - TestMailer.signed_up(@recipient).deliver - assert_equal 0, ActionMailer::Base.deliveries.size - ActionMailer::Base.perform_deliveries = true - TestMailer.signed_up(@recipient).deliver - assert_equal 1, ActionMailer::Base.deliveries.size - end - - def test_doesnt_raise_errors_when_raise_delivery_errors_is_false - ActionMailer::Base.raise_delivery_errors = false - Mail::TestMailer.any_instance.expects(:deliver!).raises(Exception) - assert_nothing_raised { TestMailer.signed_up(@recipient).deliver } - end - - def test_performs_delivery_via_sendmail - IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t -f "system@loudthinking.com" test@localhost', 'w+') - TestMailer.delivery_method = :sendmail - TestMailer.signed_up(@recipient).deliver - end - - def test_unquote_quoted_printable_subject - msg = <" - - expected = new_mail "iso-8859-1" - expected.to = quote_address_if_necessary @recipient, "iso-8859-1" - expected.subject = "testing extended headers" - expected.body = "Nothing to see here." - expected.from = quote_address_if_necessary "Grytøyr ", "iso-8859-1" - expected.cc = quote_address_if_necessary "Grytøyr ", "iso-8859-1" - expected.bcc = quote_address_if_necessary "Grytøyr ", "iso-8859-1" - expected.date = Time.local 2004, 12, 12 - - created = nil - assert_nothing_raised do - created = TestMailer.extended_headers @recipient - end - - assert_not_nil created - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - - assert_equal expected.encoded, created.encoded - - assert_nothing_raised do - TestMailer.extended_headers(@recipient).deliver - end - - delivered = ActionMailer::Base.deliveries.first - assert_not_nil delivered - - expected.message_id = '<123@456>' - delivered.message_id = '<123@456>' - - assert_equal expected.encoded, delivered.encoded - end - - def test_utf8_body_is_not_quoted - @recipient = "Foo áëô îü " - expected = new_mail "utf-8" - expected.to = quote_address_if_necessary @recipient, "utf-8" - expected.subject = "testing utf-8 body" - expected.body = "åœö blah" - expected.from = quote_address_if_necessary @recipient, "utf-8" - expected.cc = quote_address_if_necessary @recipient, "utf-8" - expected.bcc = quote_address_if_necessary @recipient, "utf-8" - expected.date = Time.local 2004, 12, 12 - - created = TestMailer.utf8_body @recipient - assert_match(/åœö blah/, created.encoded) - end - - def test_multiple_utf8_recipients - @recipient = ["\"Foo áëô îü\" ", "\"Example Recipient\" "] - expected = new_mail "utf-8" - expected.to = quote_address_if_necessary @recipient, "utf-8" - expected.subject = "testing utf-8 body" - expected.body = "åœö blah" - expected.from = quote_address_if_necessary @recipient.first, "utf-8" - expected.cc = quote_address_if_necessary @recipient, "utf-8" - expected.bcc = quote_address_if_necessary @recipient, "utf-8" - expected.date = Time.local 2004, 12, 12 - - created = TestMailer.utf8_body @recipient - assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= \r/, created.encoded) - assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= , \r\n\tExample Recipient _Google}, mail.body.to_s - end - - def test_various_newlines - mail = TestMailer.various_newlines(@recipient) - assert_equal("line #1\nline #2\nline #3\nline #4\n\n" + - "line #5\n\nline#6\n\nline #7", mail.body.to_s) - end - - def test_various_newlines_multipart - mail = TestMailer.various_newlines_multipart(@recipient) - assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body.to_s - assert_equal "

line #1

\n

line #2

\n

line #3

\n

line #4

\n\n", mail.parts[1].body.to_s - assert_equal "line #1\r\nline #2\r\nline #3\r\nline #4\r\n\r\n", mail.parts[0].body.encoded - assert_equal "

line #1

\r\n

line #2

\r\n

line #3

\r\n

line #4

\r\n\r\n", mail.parts[1].body.encoded - end - - def test_headers_removed_on_smtp_delivery - TestMailer.delivery_method = :smtp - TestMailer.cc_bcc(@recipient).deliver - assert MockSMTP.deliveries[0][2].include?("root@loudthinking.com") - assert MockSMTP.deliveries[0][2].include?("nobody@loudthinking.com") - assert MockSMTP.deliveries[0][2].include?(@recipient) - assert_match %r{^Cc: nobody@loudthinking.com}, MockSMTP.deliveries[0][0] - assert_match %r{^To: #{@recipient}}, MockSMTP.deliveries[0][0] - assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0] - end - - def test_file_delivery_should_create_a_file - ActionMailer::Base.delivery_method = :file - tmp_location = ActionMailer::Base.file_settings[:location] - - TestMailer.cc_bcc(@recipient).deliver - assert File.exists?(tmp_location) - assert File.directory?(tmp_location) - assert File.exists?(File.join(tmp_location, @recipient)) - assert File.exists?(File.join(tmp_location, 'nobody@loudthinking.com')) - assert File.exists?(File.join(tmp_location, 'root@loudthinking.com')) - end - - def test_recursive_multipart_processing - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7") - mail = Mail.new(fixture) - assert_equal(2, mail.parts.length) - assert_equal(4, mail.parts.first.parts.length) - assert_equal("This is the first part.", mail.parts.first.parts.first.body.to_s) - assert_equal("test.rb", mail.parts.first.parts.second.filename) - assert_equal("flowed", mail.parts.first.parts.fourth.content_type_parameters[:format]) - assert_equal('smime.p7s', mail.parts.second.filename) - end - - def test_decode_encoded_attachment_filename - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8") - mail = Mail.new(fixture) - attachment = mail.attachments.last - - expected = "01 Quien Te Dij\212at. Pitbull.mp3" - - if expected.respond_to?(:force_encoding) - result = attachment.filename.dup - expected.force_encoding(Encoding::ASCII_8BIT) - result.force_encoding(Encoding::ASCII_8BIT) - assert_equal expected, result - else - assert_equal expected, attachment.filename - end - end - - def test_decode_message_with_unknown_charset - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10") - mail = Mail.new(fixture) - assert_nothing_raised { mail.body } - end - - def test_empty_header_values_omitted - result = TestMailer.unnamed_attachment(@recipient).encoded - assert_match %r{Content-Type: application/octet-stream;}, result - assert_match %r{Content-Disposition: attachment;}, result - end - - def test_headers_with_nonalpha_chars - mail = TestMailer.headers_with_nonalpha_chars(@recipient) - assert !mail.from_addrs.empty? - assert !mail.cc_addrs.empty? - assert !mail.bcc_addrs.empty? - assert_match(/:/, mail[:from].decoded) - assert_match(/:/, mail[:cc].decoded) - assert_match(/:/, mail[:bcc].decoded) - end - - def test_with_mail_object_deliver - mail = TestMailer.headers_with_nonalpha_chars(@recipient) - assert_nothing_raised { mail.deliver } - assert_equal 1, TestMailer.deliveries.length - end - - def test_multipart_with_template_path_with_dots - mail = FunkyPathMailer.multipart_with_template_path_with_dots(@recipient) - assert_equal 2, mail.parts.length - assert "text/plain", mail.parts[1].mime_type - assert "utf-8", mail.parts[1].charset - end - - def test_custom_content_type_attributes - mail = TestMailer.custom_content_type_attributes - assert_match %r{format=flowed}, mail.content_type - assert_match %r{charset=utf-8}, mail.content_type - end - - def test_return_path_with_create - mail = TestMailer.return_path - assert_equal "another@somewhere.test", mail.return_path - end - - def test_return_path_with_deliver - TestMailer.delivery_method = :smtp - TestMailer.return_path.deliver - assert_match %r{^Return-Path: }, MockSMTP.deliveries[0][0] - assert_equal "another@somewhere.test", MockSMTP.deliveries[0][1].to_s - end - - def test_starttls_is_enabled_if_supported - TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) - MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(true) - MockSMTP.any_instance.expects(:enable_starttls_auto) - TestMailer.delivery_method = :smtp - TestMailer.signed_up(@recipient).deliver - end - - def test_starttls_is_disabled_if_not_supported - TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) - MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(false) - MockSMTP.any_instance.expects(:enable_starttls_auto).never - TestMailer.delivery_method = :smtp - TestMailer.signed_up(@recipient).deliver - end - - def test_starttls_is_not_enabled - TestMailer.smtp_settings.merge!(:enable_starttls_auto => false) - MockSMTP.any_instance.expects(:respond_to?).never - TestMailer.delivery_method = :smtp - TestMailer.signed_up(@recipient).deliver - ensure - TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) - end -end - -class InheritableTemplateRootTest < ActiveSupport::TestCase - def test_attr - expected = File.expand_path("#{File.dirname(__FILE__)}/fixtures/path.with.dots") - assert_equal expected, FunkyPathMailer.template_root.to_s - - sub = Class.new(FunkyPathMailer) - assert_deprecated do - sub.template_root = 'test/path' - end - - assert_equal File.expand_path('test/path'), sub.template_root.to_s - assert_equal expected, FunkyPathMailer.template_root.to_s - end -end - -class MethodNamingTest < ActiveSupport::TestCase - class TestMailer < ActionMailer::Base - def send - render :text => 'foo' - end - end - - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries.clear - end - - def teardown - restore_delivery_method - end - - def test_send_method - assert_nothing_raised do - assert_emails 1 do - assert_deprecated do - TestMailer.deliver_send - end - end - end - end -end -class RespondToTest < Test::Unit::TestCase - class RespondToMailer < ActionMailer::Base; end - - def setup - set_delivery_method :test - end - - def teardown - restore_delivery_method - end - - def test_should_respond_to_new - assert RespondToMailer.respond_to?(:new) - end - - def test_should_respond_to_create_with_template_suffix - assert RespondToMailer.respond_to?(:create_any_old_template) - end - - def test_should_respond_to_deliver_with_template_suffix - assert RespondToMailer.respond_to?(:deliver_any_old_template) - end - - def test_should_not_respond_to_new_with_template_suffix - assert !RespondToMailer.respond_to?(:new_any_old_template) - end - - def test_should_not_respond_to_create_with_template_suffix_unless_it_is_separated_by_an_underscore - assert !RespondToMailer.respond_to?(:createany_old_template) - end - - def test_should_not_respond_to_deliver_with_template_suffix_unless_it_is_separated_by_an_underscore - assert !RespondToMailer.respond_to?(:deliverany_old_template) - end - - def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_uppercase_letter - assert !RespondToMailer.respond_to?(:create_Any_old_template) - end - - def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_uppercase_letter - assert !RespondToMailer.respond_to?(:deliver_Any_old_template) - end - - def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_digit - assert !RespondToMailer.respond_to?(:create_1_template) - end - - def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_digit - assert !RespondToMailer.respond_to?(:deliver_1_template) - end - - def test_should_not_respond_to_method_where_deliver_is_not_a_suffix - assert !RespondToMailer.respond_to?(:foo_deliver_template) - end - - def test_should_still_raise_exception_with_expected_message_when_calling_an_undefined_method - error = assert_raise NoMethodError do - RespondToMailer.not_a_method - end - - assert_match(/undefined method.*not_a_method/, error.message) - end -end \ No newline at end of file diff --git a/actionmailer/test/mail_test.rb b/actionmailer/test/mail_test.rb index ea6f25d157..f18dfdc156 100644 --- a/actionmailer/test/mail_test.rb +++ b/actionmailer/test/mail_test.rb @@ -8,7 +8,6 @@ class MailTest < Test::Unit::TestCase quoted_body = [expected].pack('*M') m.body = quoted_body assert_equal "something_with_underscores=\r\n", m.body.encoded - # CHANGED: body returns object, not string, Changed m.body to m.body.to_s assert_equal expected, m.body.to_s end @@ -20,5 +19,4 @@ class MailTest < Test::Unit::TestCase assert_equal 1902, mail.attachments.first.decoded.length assert_equal "application/pkcs7-signature", mail.attachments.last.mime_type end - end diff --git a/actionmailer/test/old_base/adv_attr_test.rb b/actionmailer/test/old_base/adv_attr_test.rb new file mode 100644 index 0000000000..f22d733bc5 --- /dev/null +++ b/actionmailer/test/old_base/adv_attr_test.rb @@ -0,0 +1,36 @@ +require 'abstract_unit' +require 'action_mailer/adv_attr_accessor' + +class AdvAttrTest < ActiveSupport::TestCase + class Person + cattr_reader :protected_instance_variables + @@protected_instance_variables = [] + + extend ActionMailer::AdvAttrAccessor + adv_attr_accessor :name + end + + def setup + @person = Person.new + end + + def test_adv_attr + assert_nil @person.name + @person.name 'Bob' + assert_equal 'Bob', @person.name + end + + def test_adv_attr_writer + assert_nil @person.name + @person.name = 'Bob' + assert_equal 'Bob', @person.name + end + + def test_raise_an_error_with_multiple_args + assert_raise(ArgumentError) { @person.name('x', 'y') } + end + + def test_ivar_is_added_to_protected_instnace_variables + assert Person.protected_instance_variables.include?('@name') + end +end diff --git a/actionmailer/test/old_base/asset_host_test.rb b/actionmailer/test/old_base/asset_host_test.rb new file mode 100644 index 0000000000..124032f1d9 --- /dev/null +++ b/actionmailer/test/old_base/asset_host_test.rb @@ -0,0 +1,52 @@ +require 'abstract_unit' + +class AssetHostMailer < ActionMailer::Base + def email_with_asset + recipients 'test@localhost' + subject "testing email containing asset path while asset_host is set" + from "tester@example.com" + end +end + +class AssetHostTest < Test::Unit::TestCase + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries.clear + end + + def teardown + restore_delivery_method + end + + def test_asset_host_as_string + ActionController::Base.asset_host = "http://www.example.com" + mail = AssetHostMailer.email_with_asset + assert_equal "\"Somelogo\"", mail.body.to_s.strip + end + + def test_asset_host_as_one_arguement_proc + ActionController::Base.asset_host = Proc.new { |source| + if source.starts_with?('/images') + "http://images.example.com" + else + "http://assets.example.com" + end + } + mail = AssetHostMailer.email_with_asset + assert_equal "\"Somelogo\"", mail.body.to_s.strip + end + + def test_asset_host_as_two_arguement_proc + ActionController::Base.asset_host = Proc.new {|source,request| + if request && request.ssl? + "https://www.example.com" + else + "http://www.example.com" + end + } + mail = nil + assert_nothing_raised { mail = AssetHostMailer.email_with_asset } + assert_equal "\"Somelogo\"", mail.body.to_s.strip + end +end \ No newline at end of file diff --git a/actionmailer/test/old_base/mail_helper_test.rb b/actionmailer/test/old_base/mail_helper_test.rb new file mode 100644 index 0000000000..a9b3cd3ce1 --- /dev/null +++ b/actionmailer/test/old_base/mail_helper_test.rb @@ -0,0 +1,94 @@ +require 'abstract_unit' + +module MailerHelper + def person_name + "Mr. Joe Person" + end +end + +class HelperMailer < ActionMailer::Base + helper MailerHelper + helper :example + + def use_helper + recipients 'test@localhost' + subject "using helpers" + from "tester@example.com" + end + + def use_example_helper + recipients 'test@localhost' + subject "using helpers" + from "tester@example.com" + + @text = "emphasize me!" + end + + def use_mail_helper + recipients 'test@localhost' + subject "using mailing helpers" + from "tester@example.com" + + @text = "But soft! What light through yonder window breaks? It is the east, " + + "and Juliet is the sun. Arise, fair sun, and kill the envious moon, " + + "which is sick and pale with grief that thou, her maid, art far more " + + "fair than she. Be not her maid, for she is envious! Her vestal " + + "livery is but sick and green, and none but fools do wear it. Cast " + + "it off!" + end + + def use_helper_method + recipients 'test@localhost' + subject "using helpers" + from "tester@example.com" + + @text = "emphasize me!" + end + + private + + def name_of_the_mailer_class + self.class.name + end + helper_method :name_of_the_mailer_class +end + +class MailerHelperTest < ActiveSupport::TestCase + def new_mail( charset="utf-8" ) + mail = Mail.new + mail.set_content_type "text", "plain", { "charset" => charset } if charset + mail + end + + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries.clear + end + + def teardown + restore_delivery_method + end + + def test_use_helper + mail = HelperMailer.use_helper + assert_match %r{Mr. Joe Person}, mail.encoded + end + + def test_use_example_helper + mail = HelperMailer.use_example_helper + assert_match %r{emphasize me!}, mail.encoded + end + + def test_use_helper_method + mail = HelperMailer.use_helper_method + assert_match %r{HelperMailer}, mail.encoded + end + + def test_use_mail_helper + mail = HelperMailer.use_mail_helper + assert_match %r{ But soft!}, mail.encoded + assert_match %r{east, and\r\n Juliet}, mail.encoded + end +end + diff --git a/actionmailer/test/old_base/mail_layout_test.rb b/actionmailer/test/old_base/mail_layout_test.rb new file mode 100644 index 0000000000..4038fbf339 --- /dev/null +++ b/actionmailer/test/old_base/mail_layout_test.rb @@ -0,0 +1,148 @@ +require 'abstract_unit' + +class AutoLayoutMailer < ActionMailer::Base + + def hello + recipients 'test@localhost' + subject "You have a mail" + from "tester@example.com" + end + + def spam + recipients 'test@localhost' + subject "You have a mail" + from "tester@example.com" + + @world = "Earth" + render(:inline => "Hello, <%= @world %>", :layout => 'spam') + end + + def nolayout + recipients 'test@localhost' + subject "You have a mail" + from "tester@example.com" + + @world = "Earth" + render(:inline => "Hello, <%= @world %>", :layout => false) + end + + def multipart(type = nil) + recipients 'test@localhost' + subject "You have a mail" + from "tester@example.com" + + content_type(type) if type + end +end + +class ExplicitLayoutMailer < ActionMailer::Base + layout 'spam', :except => [:logout] + + def signup + recipients 'test@localhost' + subject "You have a mail" + from "tester@example.com" + end + + def logout + recipients 'test@localhost' + subject "You have a mail" + from "tester@example.com" + end +end + +class LayoutMailerTest < Test::Unit::TestCase + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries.clear + end + + def teardown + restore_delivery_method + end + + def test_should_pickup_default_layout + mail = AutoLayoutMailer.hello + assert_equal "Hello from layout Inside", mail.body.to_s.strip + end + + def test_should_pickup_multipart_layout + mail = AutoLayoutMailer.multipart + # CHANGED: content_type returns an object + # assert_equal "multipart/alternative", mail.content_type + assert_equal "multipart/alternative", mail.mime_type + assert_equal 2, mail.parts.size + + # CHANGED: content_type returns an object + # assert_equal 'text/plain', mail.parts.first.content_type + assert_equal 'text/plain', mail.parts.first.mime_type + + # CHANGED: body returns an object + # assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body + assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s + + # CHANGED: content_type returns an object + # assert_equal 'text/html', mail.parts.last.content_type + assert_equal 'text/html', mail.parts.last.mime_type + + # CHANGED: body returns an object + # assert_equal "Hello from layout text/html multipart", mail.parts.last.body + assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s + end + + def test_should_pickup_multipartmixed_layout + mail = AutoLayoutMailer.multipart("multipart/mixed") + # CHANGED: content_type returns an object + # assert_equal "multipart/mixed", mail.content_type + assert_equal "multipart/mixed", mail.mime_type + assert_equal 2, mail.parts.size + + # CHANGED: content_type returns an object + # assert_equal 'text/plain', mail.parts.first.content_type + assert_equal 'text/plain', mail.parts.first.mime_type + # CHANGED: body returns an object + # assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body + assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s + + # CHANGED: content_type returns an object + # assert_equal 'text/html', mail.parts.last.content_type + assert_equal 'text/html', mail.parts.last.mime_type + # CHANGED: body returns an object + # assert_equal "Hello from layout text/html multipart", mail.parts.last.body + assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s + end + + def test_should_fix_multipart_layout + mail = AutoLayoutMailer.multipart("text/plain") + assert_equal "multipart/alternative", mail.mime_type + assert_equal 2, mail.parts.size + + assert_equal 'text/plain', mail.parts.first.mime_type + assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s + + assert_equal 'text/html', mail.parts.last.mime_type + assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s + end + + + def test_should_pickup_layout_given_to_render + mail = AutoLayoutMailer.spam + assert_equal "Spammer layout Hello, Earth", mail.body.to_s.strip + end + + def test_should_respect_layout_false + mail = AutoLayoutMailer.nolayout + assert_equal "Hello, Earth", mail.body.to_s.strip + end + + def test_explicit_class_layout + mail = ExplicitLayoutMailer.signup + assert_equal "Spammer layout We do not spam", mail.body.to_s.strip + end + + def test_explicit_layout_exceptions + mail = ExplicitLayoutMailer.logout + assert_equal "You logged out", mail.body.to_s.strip + end +end diff --git a/actionmailer/test/old_base/mail_render_test.rb b/actionmailer/test/old_base/mail_render_test.rb new file mode 100644 index 0000000000..804200fd36 --- /dev/null +++ b/actionmailer/test/old_base/mail_render_test.rb @@ -0,0 +1,157 @@ +require 'abstract_unit' + +class RenderMailer < ActionMailer::Base + def inline_template + recipients 'test@localhost' + subject "using helpers" + from "tester@example.com" + + @world = "Earth" + render :inline => "Hello, <%= @world %>" + end + + def file_template + recipients 'test@localhost' + subject "using helpers" + from "tester@example.com" + + @recipient = 'test@localhost' + render :file => "templates/signed_up" + end + + def implicit_body + recipients 'test@localhost' + subject "using helpers" + from "tester@example.com" + + @recipient = 'test@localhost' + render :template => "templates/signed_up" + end + + def rxml_template + recipients 'test@localhost' + subject "rendering rxml template" + from "tester@example.com" + end + + def included_subtemplate + recipients 'test@localhost' + subject "Including another template in the one being rendered" + from "tester@example.com" + end + + def mailer_accessor + recipients 'test@localhost' + subject "Mailer Accessor" + from "tester@example.com" + + render :inline => "Look, <%= mailer.subject %>!" + end + + def no_instance_variable + recipients 'test@localhost' + subject "No Instance Variable" + from "tester@example.com" + + silence_warnings do + render :inline => "Look, subject.nil? is <%= @subject.nil? %>!" + end + end + + def initialize_defaults(method_name) + super + mailer_name "test_mailer" + end +end + +class FirstMailer < ActionMailer::Base + def share + recipients 'test@localhost' + subject "using helpers" + from "tester@example.com" + end +end + +class SecondMailer < ActionMailer::Base + def share + recipients 'test@localhost' + subject "using helpers" + from "tester@example.com" + end +end + +# CHANGED: Those tests were changed because body returns an object now +# Instead of mail.body.strip, we should mail.body.to_s.strip +class RenderHelperTest < Test::Unit::TestCase + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries.clear + + @recipient = 'test@localhost' + end + + def teardown + restore_delivery_method + end + + def test_implicit_body + mail = RenderMailer.implicit_body + assert_equal "Hello there, \n\nMr. test@localhost", mail.body.to_s.strip + end + + def test_inline_template + mail = RenderMailer.inline_template + assert_equal "Hello, Earth", mail.body.to_s.strip + end + + def test_file_template + mail = RenderMailer.file_template + assert_equal "Hello there, \n\nMr. test@localhost", mail.body.to_s.strip + end + + def test_rxml_template + mail = RenderMailer.rxml_template.deliver + assert_equal "\n", mail.body.to_s.strip + end + + def test_included_subtemplate + mail = RenderMailer.included_subtemplate.deliver + assert_equal "Hey Ho, let's go!", mail.body.to_s.strip + end + + def test_mailer_accessor + mail = RenderMailer.mailer_accessor.deliver + assert_equal "Look, Mailer Accessor!", mail.body.to_s.strip + end + + def test_no_instance_variable + mail = RenderMailer.no_instance_variable.deliver + assert_equal "Look, subject.nil? is true!", mail.body.to_s.strip + end +end + +class FirstSecondHelperTest < Test::Unit::TestCase + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries.clear + + @recipient = 'test@localhost' + end + + def teardown + restore_delivery_method + end + + def test_ordering + mail = FirstMailer.share + assert_equal "first mail", mail.body.to_s.strip + mail = SecondMailer.share + assert_equal "second mail", mail.body.to_s.strip + mail = FirstMailer.share + assert_equal "first mail", mail.body.to_s.strip + mail = SecondMailer.share + assert_equal "second mail", mail.body.to_s.strip + end +end diff --git a/actionmailer/test/old_base/mail_service_test.rb b/actionmailer/test/old_base/mail_service_test.rb new file mode 100644 index 0000000000..20b0f30b84 --- /dev/null +++ b/actionmailer/test/old_base/mail_service_test.rb @@ -0,0 +1,1192 @@ +# encoding: utf-8 +require 'abstract_unit' + +class FunkyPathMailer < ActionMailer::Base + self.view_paths = "#{File.dirname(__FILE__)}/../fixtures/path.with.dots" + + def multipart_with_template_path_with_dots(recipient) + recipients recipient + subject "This path has dots" + from "Chad Fowler " + attachment :content_type => "text/plain", + :data => "dots dots dots..." + end +end + +class TestMailer < ActionMailer::Base + def signed_up(recipient) + recipients recipient + subject "[Signed up] Welcome #{recipient}" + from "system@loudthinking.com" + + @recipient = recipient + end + + def cancelled_account(recipient) + recipients recipient + subject "[Cancelled] Goodbye #{recipient}" + from "system@loudthinking.com" + sent_on Time.local(2004, 12, 12) + + render :text => "Goodbye, Mr. #{recipient}" + end + + def from_with_name + from "System " + recipients "root@loudthinking.com" + render :text => "Nothing to see here." + end + + def from_without_name + from "system@loudthinking.com" + recipients "root@loudthinking.com" + render :text => "Nothing to see here." + end + + def cc_bcc(recipient) + recipients recipient + subject "testing bcc/cc" + from "system@loudthinking.com" + sent_on Time.local(2004, 12, 12) + cc "nobody@loudthinking.com" + bcc "root@loudthinking.com" + + render :text => "Nothing to see here." + end + + def different_reply_to(recipient) + recipients recipient + subject "testing reply_to" + from "system@loudthinking.com" + sent_on Time.local(2008, 5, 23) + reply_to "atraver@gmail.com" + + render :text => "Nothing to see here." + end + + def iso_charset(recipient) + recipients recipient + subject "testing isø charsets" + from "system@loudthinking.com" + sent_on Time.local(2004, 12, 12) + cc "nobody@loudthinking.com" + bcc "root@loudthinking.com" + charset "iso-8859-1" + + render :text => "Nothing to see here." + end + + def unencoded_subject(recipient) + recipients recipient + subject "testing unencoded subject" + from "system@loudthinking.com" + sent_on Time.local(2004, 12, 12) + cc "nobody@loudthinking.com" + bcc "root@loudthinking.com" + + render :text => "Nothing to see here." + end + + def extended_headers(recipient) + recipients recipient + subject "testing extended headers" + from "Grytøyr " + sent_on Time.local(2004, 12, 12) + cc "Grytøyr " + bcc "Grytøyr " + charset "iso-8859-1" + + render :text => "Nothing to see here." + end + + def utf8_body(recipient) + recipients recipient + subject "testing utf-8 body" + from "Foo áëô îü " + sent_on Time.local(2004, 12, 12) + cc "Foo áëô îü " + bcc "Foo áëô îü " + charset "utf-8" + + render :text => "åœö blah" + end + + def multipart_with_mime_version(recipient) + recipients recipient + subject "multipart with mime_version" + from "test@example.com" + sent_on Time.local(2004, 12, 12) + mime_version "1.1" + content_type "multipart/alternative" + + part "text/plain" do |p| + p.body = render(:text => "blah") + end + + part "text/html" do |p| + p.body = render(:inline => "<%= content_tag(:b, 'blah') %>") + end + end + + def multipart_with_utf8_subject(recipient) + recipients recipient + subject "Foo áëô îü" + from "test@example.com" + charset "utf-8" + + part "text/plain" do |p| + p.body = "blah" + end + + part "text/html" do |p| + p.body = "blah" + end + end + + def explicitly_multipart_example(recipient, ct=nil) + recipients recipient + subject "multipart example" + from "test@example.com" + sent_on Time.local(2004, 12, 12) + content_type ct if ct + + part "text/html" do |p| + p.charset = "iso-8859-1" + p.body = "blah" + end + + attachment :content_type => "image/jpeg", :filename => File.join(File.dirname(__FILE__), "fixtures", "attachments", "foo.jpg"), + :data => "123456789" + + render :text => "plain text default" + end + + def implicitly_multipart_example(recipient, cs = nil, order = nil) + recipients recipient + subject "multipart example" + from "test@example.com" + sent_on Time.local(2004, 12, 12) + + @charset = cs if cs + @recipient = recipient + @implicit_parts_order = order if order + end + + def implicitly_multipart_with_utf8 + recipients "no.one@nowhere.test" + subject "Foo áëô îü" + from "some.one@somewhere.test" + template "implicitly_multipart_example" + + @recipient = "no.one@nowhere.test" + end + + def html_mail(recipient) + recipients recipient + subject "html mail" + from "test@example.com" + content_type "text/html" + + render :text => "Emphasize this" + end + + def html_mail_with_underscores(recipient) + subject "html mail with underscores" + render :text => %{_Google} + end + + def custom_template(recipient) + recipients recipient + subject "[Signed up] Welcome #{recipient}" + from "system@loudthinking.com" + sent_on Time.local(2004, 12, 12) + template "signed_up" + + @recipient = recipient + end + + def custom_templating_extension(recipient) + recipients recipient + subject "[Signed up] Welcome #{recipient}" + from "system@loudthinking.com" + sent_on Time.local(2004, 12, 12) + + @recipient = recipient + end + + def various_newlines(recipient) + recipients recipient + subject "various newlines" + from "test@example.com" + + render :text => "line #1\nline #2\rline #3\r\nline #4\r\r" + + "line #5\n\nline#6\r\n\r\nline #7" + end + + def various_newlines_multipart(recipient) + recipients recipient + subject "various newlines multipart" + from "test@example.com" + content_type "multipart/alternative" + + part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r" + part :content_type => "text/html", :body => "

line #1

\n

line #2

\r

line #3

\r\n

line #4

\r\r" + end + + def nested_multipart(recipient) + recipients recipient + subject "nested multipart" + from "test@example.com" + content_type "multipart/mixed" + + part :content_type => "multipart/alternative", :content_disposition => "inline", "foo" => "bar" do |p| + p.part :content_type => "text/plain", :body => "test text\nline #2" + p.part :content_type => "text/html", :body => "test HTML
\nline #2" + end + + attachment :content_type => "application/octet-stream", :filename => "test.txt", :data => "test abcdefghijklmnopqstuvwxyz" + end + + def nested_multipart_with_body(recipient) + recipients recipient + subject "nested multipart with body" + from "test@example.com" + content_type "multipart/mixed" + + part :content_type => "multipart/alternative", :content_disposition => "inline", :body => "Nothing to see here." do |p| + p.part :content_type => "text/html", :body => "test HTML
" + end + end + + def attachment_with_custom_header(recipient) + recipients recipient + subject "custom header in attachment" + from "test@example.com" + content_type "multipart/related" + part :content_type => "text/html", :body => 'yo' + attachment :content_type => "image/jpeg", :filename => File.join(File.dirname(__FILE__), "fixtures", "attachments", "test.jpg"), :data => "i am not a real picture", 'Content-ID' => '' + end + + def unnamed_attachment(recipient) + recipients recipient + subject "nested multipart" + from "test@example.com" + content_type "multipart/mixed" + part :content_type => "text/plain", :body => "hullo" + attachment :content_type => "application/octet-stream", :data => "test abcdefghijklmnopqstuvwxyz" + end + + def headers_with_nonalpha_chars(recipient) + recipients recipient + subject "nonalpha chars" + from "One: Two " + cc "Three: Four " + bcc "Five: Six " + render :text => "testing" + end + + def custom_content_type_attributes + recipients "no.one@nowhere.test" + subject "custom content types" + from "some.one@somewhere.test" + content_type "text/plain; format=flowed" + render :text => "testing" + end + + def return_path + recipients "no.one@nowhere.test" + subject "return path test" + from "some.one@somewhere.test" + headers["return-path"] = "another@somewhere.test" + render :text => "testing" + end + + def subject_with_i18n(recipient) + recipients recipient + from "system@loudthinking.com" + render :text => "testing" + end + + class << self + attr_accessor :received_body + end + + def receive(mail) + self.class.received_body = mail.body + end +end + +class ActionMailerTest < Test::Unit::TestCase + include ActionMailer::Quoting + + def encode( text, charset="utf-8" ) + quoted_printable( text, charset ) + end + + def new_mail( charset="utf-8" ) + mail = Mail.new + mail.mime_version = "1.0" + if charset + mail.content_type ["text", "plain", { "charset" => charset }] + end + mail + end + + # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3 + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.raise_delivery_errors = true + ActionMailer::Base.deliveries.clear + + @original_logger = TestMailer.logger + @recipient = 'test@localhost' + end + + def teardown + TestMailer.logger = @original_logger + restore_delivery_method + end + + def test_nested_parts + created = nil + assert_nothing_raised { created = TestMailer.nested_multipart(@recipient)} + assert_equal 2, created.parts.size + assert_equal 2, created.parts.first.parts.size + + assert_equal "multipart/mixed", created.mime_type + assert_equal "multipart/alternative", created.parts[0].mime_type + assert_equal "bar", created.parts[0].header['foo'].to_s + assert_nil created.parts[0].charset + assert_equal "text/plain", created.parts[0].parts[0].mime_type + assert_equal "text/html", created.parts[0].parts[1].mime_type + assert_equal "application/octet-stream", created.parts[1].mime_type + + end + + def test_nested_parts_with_body + created = nil + TestMailer.nested_multipart_with_body(@recipient) + assert_nothing_raised { created = TestMailer.nested_multipart_with_body(@recipient)} + + assert_equal 1,created.parts.size + assert_equal 2,created.parts.first.parts.size + + assert_equal "multipart/mixed", created.mime_type + assert_equal "multipart/alternative", created.parts.first.mime_type + assert_equal "text/plain", created.parts.first.parts.first.mime_type + assert_equal "Nothing to see here.", created.parts.first.parts.first.body.to_s + assert_equal "text/html", created.parts.first.parts.second.mime_type + assert_equal "test HTML
", created.parts.first.parts.second.body.to_s + end + + def test_attachment_with_custom_header + created = nil + assert_nothing_raised { created = TestMailer.attachment_with_custom_header(@recipient) } + assert created.parts.any? { |p| p.header['content-id'].to_s == "" } + end + + def test_signed_up + Time.stubs(:now => Time.now) + + expected = new_mail + expected.to = @recipient + expected.subject = "[Signed up] Welcome #{@recipient}" + expected.body = "Hello there, \n\nMr. #{@recipient}" + expected.from = "system@loudthinking.com" + expected.date = Time.now + + created = nil + assert_nothing_raised { created = TestMailer.signed_up(@recipient) } + assert_not_nil created + + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + + assert_equal expected.encoded, created.encoded + + assert_nothing_raised { TestMailer.signed_up(@recipient).deliver } + + delivered = ActionMailer::Base.deliveries.first + assert_not_nil delivered + + expected.message_id = '<123@456>' + delivered.message_id = '<123@456>' + + assert_equal expected.encoded, delivered.encoded + end + + def test_custom_template + expected = new_mail + expected.to = @recipient + expected.subject = "[Signed up] Welcome #{@recipient}" + expected.body = "Hello there, \n\nMr. #{@recipient}" + expected.from = "system@loudthinking.com" + expected.date = Time.local(2004, 12, 12) + + created = nil + assert_nothing_raised { created = TestMailer.custom_template(@recipient) } + assert_not_nil created + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + assert_equal expected.encoded, created.encoded + end + + def test_custom_templating_extension + assert ActionView::Template.template_handler_extensions.include?("haml"), "haml extension was not registered" + + # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory + expected = new_mail + expected.to = @recipient + expected.subject = "[Signed up] Welcome #{@recipient}" + expected.body = "Hello there, \n\nMr. #{@recipient}" + expected.from = "system@loudthinking.com" + expected.date = Time.local(2004, 12, 12) + + # Stub the render method so no alternative renderers need be present. + ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}") + + # Now that the template is registered, there should be one part. The text/plain part. + created = nil + assert_nothing_raised { created = TestMailer.custom_templating_extension(@recipient) } + assert_not_nil created + assert_equal 2, created.parts.length + assert_equal 'text/plain', created.parts[0].mime_type + assert_equal 'text/html', created.parts[1].mime_type + end + + def test_cancelled_account + expected = new_mail + expected.to = @recipient + expected.subject = "[Cancelled] Goodbye #{@recipient}" + expected.body = "Goodbye, Mr. #{@recipient}" + expected.from = "system@loudthinking.com" + expected.date = Time.local(2004, 12, 12) + + created = nil + assert_nothing_raised { created = TestMailer.cancelled_account(@recipient) } + assert_not_nil created + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + assert_equal expected.encoded, created.encoded + + assert_nothing_raised { TestMailer.cancelled_account(@recipient).deliver } + assert_not_nil ActionMailer::Base.deliveries.first + delivered = ActionMailer::Base.deliveries.first + expected.message_id = '<123@456>' + delivered.message_id = '<123@456>' + + assert_equal expected.encoded, delivered.encoded + end + + def test_cc_bcc + expected = new_mail + expected.to = @recipient + expected.subject = "testing bcc/cc" + expected.body = "Nothing to see here." + expected.from = "system@loudthinking.com" + expected.cc = "nobody@loudthinking.com" + expected.bcc = "root@loudthinking.com" + expected.date = Time.local 2004, 12, 12 + + created = nil + assert_nothing_raised do + created = TestMailer.cc_bcc @recipient + end + assert_not_nil created + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + assert_equal expected.encoded, created.encoded + + assert_nothing_raised do + TestMailer.cc_bcc(@recipient).deliver + end + + assert_not_nil ActionMailer::Base.deliveries.first + delivered = ActionMailer::Base.deliveries.first + expected.message_id = '<123@456>' + delivered.message_id = '<123@456>' + + assert_equal expected.encoded, delivered.encoded + end + + def test_from_without_name_for_smtp + TestMailer.delivery_method = :smtp + TestMailer.from_without_name.deliver + + mail = MockSMTP.deliveries.first + assert_not_nil mail + mail, from, to = mail + + assert_equal 'system@loudthinking.com', from.to_s + end + + def test_from_with_name_for_smtp + TestMailer.delivery_method = :smtp + TestMailer.from_with_name.deliver + + mail = MockSMTP.deliveries.first + assert_not_nil mail + mail, from, to = mail + + assert_equal 'system@loudthinking.com', from + end + + def test_reply_to + expected = new_mail + + expected.to = @recipient + expected.subject = "testing reply_to" + expected.body = "Nothing to see here." + expected.from = "system@loudthinking.com" + expected.reply_to = "atraver@gmail.com" + expected.date = Time.local 2008, 5, 23 + + created = nil + assert_nothing_raised do + created = TestMailer.different_reply_to @recipient + end + assert_not_nil created + + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + + assert_equal expected.encoded, created.encoded + + assert_nothing_raised do + TestMailer.different_reply_to(@recipient).deliver + end + + delivered = ActionMailer::Base.deliveries.first + assert_not_nil delivered + + expected.message_id = '<123@456>' + delivered.message_id = '<123@456>' + + assert_equal expected.encoded, delivered.encoded + end + + def test_iso_charset + expected = new_mail( "iso-8859-1" ) + expected.to = @recipient + expected.subject = encode "testing isø charsets", "iso-8859-1" + expected.body = "Nothing to see here." + expected.from = "system@loudthinking.com" + expected.cc = "nobody@loudthinking.com" + expected.bcc = "root@loudthinking.com" + expected.date = Time.local 2004, 12, 12 + + created = nil + assert_nothing_raised do + created = TestMailer.iso_charset @recipient + end + assert_not_nil created + + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + + assert_equal expected.encoded, created.encoded + + assert_nothing_raised do + TestMailer.iso_charset(@recipient).deliver + end + + delivered = ActionMailer::Base.deliveries.first + assert_not_nil delivered + + expected.message_id = '<123@456>' + delivered.message_id = '<123@456>' + + assert_equal expected.encoded, delivered.encoded + end + + def test_unencoded_subject + expected = new_mail + expected.to = @recipient + expected.subject = "testing unencoded subject" + expected.body = "Nothing to see here." + expected.from = "system@loudthinking.com" + expected.cc = "nobody@loudthinking.com" + expected.bcc = "root@loudthinking.com" + expected.date = Time.local 2004, 12, 12 + + created = nil + assert_nothing_raised do + created = TestMailer.unencoded_subject @recipient + end + assert_not_nil created + + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + + assert_equal expected.encoded, created.encoded + + assert_nothing_raised do + TestMailer.unencoded_subject(@recipient).deliver + end + + delivered = ActionMailer::Base.deliveries.first + assert_not_nil delivered + + expected.message_id = '<123@456>' + delivered.message_id = '<123@456>' + + assert_equal expected.encoded, delivered.encoded + end + + def test_deliveries_array + assert_not_nil ActionMailer::Base.deliveries + assert_equal 0, ActionMailer::Base.deliveries.size + TestMailer.signed_up(@recipient).deliver + assert_equal 1, ActionMailer::Base.deliveries.size + assert_not_nil ActionMailer::Base.deliveries.first + end + + def test_perform_deliveries_flag + ActionMailer::Base.perform_deliveries = false + TestMailer.signed_up(@recipient).deliver + assert_equal 0, ActionMailer::Base.deliveries.size + ActionMailer::Base.perform_deliveries = true + TestMailer.signed_up(@recipient).deliver + assert_equal 1, ActionMailer::Base.deliveries.size + end + + def test_doesnt_raise_errors_when_raise_delivery_errors_is_false + ActionMailer::Base.raise_delivery_errors = false + Mail::TestMailer.any_instance.expects(:deliver!).raises(Exception) + assert_nothing_raised { TestMailer.signed_up(@recipient).deliver } + end + + def test_performs_delivery_via_sendmail + IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t -f "system@loudthinking.com" test@localhost', 'w+') + TestMailer.delivery_method = :sendmail + TestMailer.signed_up(@recipient).deliver + end + + def test_unquote_quoted_printable_subject + msg = <" + + expected = new_mail "iso-8859-1" + expected.to = quote_address_if_necessary @recipient, "iso-8859-1" + expected.subject = "testing extended headers" + expected.body = "Nothing to see here." + expected.from = quote_address_if_necessary "Grytøyr ", "iso-8859-1" + expected.cc = quote_address_if_necessary "Grytøyr ", "iso-8859-1" + expected.bcc = quote_address_if_necessary "Grytøyr ", "iso-8859-1" + expected.date = Time.local 2004, 12, 12 + + created = nil + assert_nothing_raised do + created = TestMailer.extended_headers @recipient + end + + assert_not_nil created + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + + assert_equal expected.encoded, created.encoded + + assert_nothing_raised do + TestMailer.extended_headers(@recipient).deliver + end + + delivered = ActionMailer::Base.deliveries.first + assert_not_nil delivered + + expected.message_id = '<123@456>' + delivered.message_id = '<123@456>' + + assert_equal expected.encoded, delivered.encoded + end + + def test_utf8_body_is_not_quoted + @recipient = "Foo áëô îü " + expected = new_mail "utf-8" + expected.to = quote_address_if_necessary @recipient, "utf-8" + expected.subject = "testing utf-8 body" + expected.body = "åœö blah" + expected.from = quote_address_if_necessary @recipient, "utf-8" + expected.cc = quote_address_if_necessary @recipient, "utf-8" + expected.bcc = quote_address_if_necessary @recipient, "utf-8" + expected.date = Time.local 2004, 12, 12 + + created = TestMailer.utf8_body @recipient + assert_match(/åœö blah/, created.encoded) + end + + def test_multiple_utf8_recipients + @recipient = ["\"Foo áëô îü\" ", "\"Example Recipient\" "] + expected = new_mail "utf-8" + expected.to = quote_address_if_necessary @recipient, "utf-8" + expected.subject = "testing utf-8 body" + expected.body = "åœö blah" + expected.from = quote_address_if_necessary @recipient.first, "utf-8" + expected.cc = quote_address_if_necessary @recipient, "utf-8" + expected.bcc = quote_address_if_necessary @recipient, "utf-8" + expected.date = Time.local 2004, 12, 12 + + created = TestMailer.utf8_body @recipient + assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= \r/, created.encoded) + assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= , \r\n\tExample Recipient _Google}, mail.body.to_s + end + + def test_various_newlines + mail = TestMailer.various_newlines(@recipient) + assert_equal("line #1\nline #2\nline #3\nline #4\n\n" + + "line #5\n\nline#6\n\nline #7", mail.body.to_s) + end + + def test_various_newlines_multipart + mail = TestMailer.various_newlines_multipart(@recipient) + assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body.to_s + assert_equal "

line #1

\n

line #2

\n

line #3

\n

line #4

\n\n", mail.parts[1].body.to_s + assert_equal "line #1\r\nline #2\r\nline #3\r\nline #4\r\n\r\n", mail.parts[0].body.encoded + assert_equal "

line #1

\r\n

line #2

\r\n

line #3

\r\n

line #4

\r\n\r\n", mail.parts[1].body.encoded + end + + def test_headers_removed_on_smtp_delivery + TestMailer.delivery_method = :smtp + TestMailer.cc_bcc(@recipient).deliver + assert MockSMTP.deliveries[0][2].include?("root@loudthinking.com") + assert MockSMTP.deliveries[0][2].include?("nobody@loudthinking.com") + assert MockSMTP.deliveries[0][2].include?(@recipient) + assert_match %r{^Cc: nobody@loudthinking.com}, MockSMTP.deliveries[0][0] + assert_match %r{^To: #{@recipient}}, MockSMTP.deliveries[0][0] + assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0] + end + + def test_file_delivery_should_create_a_file + ActionMailer::Base.delivery_method = :file + tmp_location = ActionMailer::Base.file_settings[:location] + + TestMailer.cc_bcc(@recipient).deliver + assert File.exists?(tmp_location) + assert File.directory?(tmp_location) + assert File.exists?(File.join(tmp_location, @recipient)) + assert File.exists?(File.join(tmp_location, 'nobody@loudthinking.com')) + assert File.exists?(File.join(tmp_location, 'root@loudthinking.com')) + end + + def test_recursive_multipart_processing + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email7") + mail = Mail.new(fixture) + assert_equal(2, mail.parts.length) + assert_equal(4, mail.parts.first.parts.length) + assert_equal("This is the first part.", mail.parts.first.parts.first.body.to_s) + assert_equal("test.rb", mail.parts.first.parts.second.filename) + assert_equal("flowed", mail.parts.first.parts.fourth.content_type_parameters[:format]) + assert_equal('smime.p7s', mail.parts.second.filename) + end + + def test_decode_encoded_attachment_filename + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email8") + mail = Mail.new(fixture) + attachment = mail.attachments.last + + expected = "01 Quien Te Dij\212at. Pitbull.mp3" + + if expected.respond_to?(:force_encoding) + result = attachment.filename.dup + expected.force_encoding(Encoding::ASCII_8BIT) + result.force_encoding(Encoding::ASCII_8BIT) + assert_equal expected, result + else + assert_equal expected, attachment.filename + end + end + + def test_decode_message_with_unknown_charset + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email10") + mail = Mail.new(fixture) + assert_nothing_raised { mail.body } + end + + def test_empty_header_values_omitted + result = TestMailer.unnamed_attachment(@recipient).encoded + assert_match %r{Content-Type: application/octet-stream;}, result + assert_match %r{Content-Disposition: attachment;}, result + end + + def test_headers_with_nonalpha_chars + mail = TestMailer.headers_with_nonalpha_chars(@recipient) + assert !mail.from_addrs.empty? + assert !mail.cc_addrs.empty? + assert !mail.bcc_addrs.empty? + assert_match(/:/, mail[:from].decoded) + assert_match(/:/, mail[:cc].decoded) + assert_match(/:/, mail[:bcc].decoded) + end + + def test_with_mail_object_deliver + mail = TestMailer.headers_with_nonalpha_chars(@recipient) + assert_nothing_raised { mail.deliver } + assert_equal 1, TestMailer.deliveries.length + end + + def test_multipart_with_template_path_with_dots + mail = FunkyPathMailer.multipart_with_template_path_with_dots(@recipient) + assert_equal 2, mail.parts.length + assert "text/plain", mail.parts[1].mime_type + assert "utf-8", mail.parts[1].charset + end + + def test_custom_content_type_attributes + mail = TestMailer.custom_content_type_attributes + assert_match %r{format=flowed}, mail.content_type + assert_match %r{charset=utf-8}, mail.content_type + end + + def test_return_path_with_create + mail = TestMailer.return_path + assert_equal "another@somewhere.test", mail.return_path + end + + def test_return_path_with_deliver + TestMailer.delivery_method = :smtp + TestMailer.return_path.deliver + assert_match %r{^Return-Path: }, MockSMTP.deliveries[0][0] + assert_equal "another@somewhere.test", MockSMTP.deliveries[0][1].to_s + end + + def test_starttls_is_enabled_if_supported + TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) + MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(true) + MockSMTP.any_instance.expects(:enable_starttls_auto) + TestMailer.delivery_method = :smtp + TestMailer.signed_up(@recipient).deliver + end + + def test_starttls_is_disabled_if_not_supported + TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) + MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(false) + MockSMTP.any_instance.expects(:enable_starttls_auto).never + TestMailer.delivery_method = :smtp + TestMailer.signed_up(@recipient).deliver + end + + def test_starttls_is_not_enabled + TestMailer.smtp_settings.merge!(:enable_starttls_auto => false) + MockSMTP.any_instance.expects(:respond_to?).never + TestMailer.delivery_method = :smtp + TestMailer.signed_up(@recipient).deliver + ensure + TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) + end +end + +class InheritableTemplateRootTest < ActiveSupport::TestCase + def test_attr + expected = File.expand_path("#{File.dirname(__FILE__)}/../fixtures/path.with.dots") + assert_equal expected, FunkyPathMailer.template_root.to_s + + sub = Class.new(FunkyPathMailer) + assert_deprecated do + sub.template_root = 'test/path' + end + + assert_equal File.expand_path('test/path'), sub.template_root.to_s + assert_equal expected, FunkyPathMailer.template_root.to_s + end +end + +class MethodNamingTest < ActiveSupport::TestCase + class TestMailer < ActionMailer::Base + def send + render :text => 'foo' + end + end + + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries.clear + end + + def teardown + restore_delivery_method + end + + def test_send_method + assert_nothing_raised do + assert_emails 1 do + assert_deprecated do + TestMailer.deliver_send + end + end + end + end +end +class RespondToTest < Test::Unit::TestCase + class RespondToMailer < ActionMailer::Base; end + + def setup + set_delivery_method :test + end + + def teardown + restore_delivery_method + end + + def test_should_respond_to_new + assert RespondToMailer.respond_to?(:new) + end + + def test_should_respond_to_create_with_template_suffix + assert RespondToMailer.respond_to?(:create_any_old_template) + end + + def test_should_respond_to_deliver_with_template_suffix + assert RespondToMailer.respond_to?(:deliver_any_old_template) + end + + def test_should_not_respond_to_new_with_template_suffix + assert !RespondToMailer.respond_to?(:new_any_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_unless_it_is_separated_by_an_underscore + assert !RespondToMailer.respond_to?(:createany_old_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_unless_it_is_separated_by_an_underscore + assert !RespondToMailer.respond_to?(:deliverany_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_uppercase_letter + assert !RespondToMailer.respond_to?(:create_Any_old_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_uppercase_letter + assert !RespondToMailer.respond_to?(:deliver_Any_old_template) + end + + def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_digit + assert !RespondToMailer.respond_to?(:create_1_template) + end + + def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_digit + assert !RespondToMailer.respond_to?(:deliver_1_template) + end + + def test_should_not_respond_to_method_where_deliver_is_not_a_suffix + assert !RespondToMailer.respond_to?(:foo_deliver_template) + end + + def test_should_still_raise_exception_with_expected_message_when_calling_an_undefined_method + error = assert_raise NoMethodError do + RespondToMailer.not_a_method + end + + assert_match(/undefined method.*not_a_method/, error.message) + end +end \ No newline at end of file diff --git a/actionmailer/test/old_base/tmail_compat_test.rb b/actionmailer/test/old_base/tmail_compat_test.rb new file mode 100644 index 0000000000..7c1d9a07c1 --- /dev/null +++ b/actionmailer/test/old_base/tmail_compat_test.rb @@ -0,0 +1,25 @@ +require 'abstract_unit' + +class TmailCompatTest < ActiveSupport::TestCase + + def test_set_content_type_raises_deprecation_warning + mail = Mail.new + assert_deprecated do + assert_nothing_raised do + mail.set_content_type "text/plain" + end + end + assert_equal mail.mime_type, "text/plain" + end + + def test_transfer_encoding_raises_deprecation_warning + mail = Mail.new + assert_deprecated do + assert_nothing_raised do + mail.transfer_encoding "base64" + end + end + assert_equal mail.content_transfer_encoding, "base64" + end + +end diff --git a/actionmailer/test/old_base/url_test.rb b/actionmailer/test/old_base/url_test.rb new file mode 100644 index 0000000000..10b6a36efd --- /dev/null +++ b/actionmailer/test/old_base/url_test.rb @@ -0,0 +1,84 @@ +require 'abstract_unit' + +class WelcomeController < ActionController::Base +end + +class TestMailer < ActionMailer::Base + default_url_options[:host] = 'www.basecamphq.com' + + def signed_up_with_url(recipient) + @recipients = recipient + @subject = "[Signed up] Welcome #{recipient}" + @from = "system@loudthinking.com" + @sent_on = Time.local(2004, 12, 12) + + @recipient = recipient + @welcome_url = url_for :host => "example.com", :controller => "welcome", :action => "greeting" + end + + class < charset }] + end + mail + end + + def setup + set_delivery_method :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries.clear + + @recipient = 'test@localhost' + end + + def teardown + restore_delivery_method + end + + def test_signed_up_with_url + ActionController::Routing::Routes.draw do |map| + map.connect ':controller/:action/:id' + map.welcome 'welcome', :controller=>"foo", :action=>"bar" + end + + expected = new_mail + expected.to = @recipient + expected.subject = "[Signed up] Welcome #{@recipient}" + expected.body = "Hello there, \n\nMr. #{@recipient}. Please see our greeting at http://example.com/welcome/greeting http://www.basecamphq.com/welcome\n\n\"Somelogo\"" + expected.from = "system@loudthinking.com" + expected.date = Time.local(2004, 12, 12) + + created = nil + assert_nothing_raised { created = TestMailer.signed_up_with_url(@recipient) } + assert_not_nil created + + expected.message_id = '<123@456>' + created.message_id = '<123@456>' + assert_equal expected.encoded, created.encoded + + assert_nothing_raised { TestMailer.signed_up_with_url(@recipient).deliver } + assert_not_nil ActionMailer::Base.deliveries.first + delivered = ActionMailer::Base.deliveries.first + + delivered.message_id = '<123@456>' + assert_equal expected.encoded, delivered.encoded + end +end diff --git a/actionmailer/test/tmail_compat_test.rb b/actionmailer/test/tmail_compat_test.rb deleted file mode 100644 index 7c1d9a07c1..0000000000 --- a/actionmailer/test/tmail_compat_test.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'abstract_unit' - -class TmailCompatTest < ActiveSupport::TestCase - - def test_set_content_type_raises_deprecation_warning - mail = Mail.new - assert_deprecated do - assert_nothing_raised do - mail.set_content_type "text/plain" - end - end - assert_equal mail.mime_type, "text/plain" - end - - def test_transfer_encoding_raises_deprecation_warning - mail = Mail.new - assert_deprecated do - assert_nothing_raised do - mail.transfer_encoding "base64" - end - end - assert_equal mail.content_transfer_encoding, "base64" - end - -end diff --git a/actionmailer/test/url_test.rb b/actionmailer/test/url_test.rb deleted file mode 100644 index 10b6a36efd..0000000000 --- a/actionmailer/test/url_test.rb +++ /dev/null @@ -1,84 +0,0 @@ -require 'abstract_unit' - -class WelcomeController < ActionController::Base -end - -class TestMailer < ActionMailer::Base - default_url_options[:host] = 'www.basecamphq.com' - - def signed_up_with_url(recipient) - @recipients = recipient - @subject = "[Signed up] Welcome #{recipient}" - @from = "system@loudthinking.com" - @sent_on = Time.local(2004, 12, 12) - - @recipient = recipient - @welcome_url = url_for :host => "example.com", :controller => "welcome", :action => "greeting" - end - - class < charset }] - end - mail - end - - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries.clear - - @recipient = 'test@localhost' - end - - def teardown - restore_delivery_method - end - - def test_signed_up_with_url - ActionController::Routing::Routes.draw do |map| - map.connect ':controller/:action/:id' - map.welcome 'welcome', :controller=>"foo", :action=>"bar" - end - - expected = new_mail - expected.to = @recipient - expected.subject = "[Signed up] Welcome #{@recipient}" - expected.body = "Hello there, \n\nMr. #{@recipient}. Please see our greeting at http://example.com/welcome/greeting http://www.basecamphq.com/welcome\n\n\"Somelogo\"" - expected.from = "system@loudthinking.com" - expected.date = Time.local(2004, 12, 12) - - created = nil - assert_nothing_raised { created = TestMailer.signed_up_with_url(@recipient) } - assert_not_nil created - - expected.message_id = '<123@456>' - created.message_id = '<123@456>' - assert_equal expected.encoded, created.encoded - - assert_nothing_raised { TestMailer.signed_up_with_url(@recipient).deliver } - assert_not_nil ActionMailer::Base.deliveries.first - delivered = ActionMailer::Base.deliveries.first - - delivered.message_id = '<123@456>' - assert_equal expected.encoded, delivered.encoded - end -end -- cgit v1.2.3