aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer
diff options
context:
space:
mode:
authorJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-24 19:36:42 +0100
committerJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-24 19:36:42 +0100
commitbd96614101262e0ad0cc176ed8e2d95a5c17936b (patch)
tree1612a3baaf634bd180b2b145db12b7eeb3096010 /actionmailer
parent99f960a3d73b62a957988bbee0906264f35afc2e (diff)
downloadrails-bd96614101262e0ad0cc176ed8e2d95a5c17936b.tar.gz
rails-bd96614101262e0ad0cc176ed8e2d95a5c17936b.tar.bz2
rails-bd96614101262e0ad0cc176ed8e2d95a5c17936b.zip
Move old tests to a specific folder and add some delivery method tests.
Diffstat (limited to 'actionmailer')
-rw-r--r--actionmailer/Rakefile4
-rw-r--r--actionmailer/lib/action_mailer/base.rb10
-rw-r--r--actionmailer/lib/action_mailer/delivery_methods.rb16
-rw-r--r--actionmailer/lib/action_mailer/mail_helper.rb2
-rw-r--r--actionmailer/test/abstract_unit.rb24
-rw-r--r--actionmailer/test/base_test.rb8
-rw-r--r--actionmailer/test/delivery_methods_test.rb112
-rw-r--r--actionmailer/test/mail_test.rb2
-rw-r--r--actionmailer/test/old_base/adv_attr_test.rb (renamed from actionmailer/test/adv_attr_test.rb)0
-rw-r--r--actionmailer/test/old_base/asset_host_test.rb (renamed from actionmailer/test/asset_host_test.rb)0
-rw-r--r--actionmailer/test/old_base/mail_helper_test.rb (renamed from actionmailer/test/mail_helper_test.rb)0
-rw-r--r--actionmailer/test/old_base/mail_layout_test.rb (renamed from actionmailer/test/mail_layout_test.rb)0
-rw-r--r--actionmailer/test/old_base/mail_render_test.rb (renamed from actionmailer/test/mail_render_test.rb)0
-rw-r--r--actionmailer/test/old_base/mail_service_test.rb (renamed from actionmailer/test/mail_service_test.rb)26
-rw-r--r--actionmailer/test/old_base/tmail_compat_test.rb (renamed from actionmailer/test/tmail_compat_test.rb)0
-rw-r--r--actionmailer/test/old_base/url_test.rb (renamed from actionmailer/test/url_test.rb)0
16 files changed, 131 insertions, 73 deletions
diff --git a/actionmailer/Rakefile b/actionmailer/Rakefile
index 6c19371514..2619d9359e 100644
--- a/actionmailer/Rakefile
+++ b/actionmailer/Rakefile
@@ -22,14 +22,14 @@ task :default => [ :test ]
# Run the unit tests
Rake::TestTask.new { |t|
t.libs << "test"
- t.pattern = 'test/*_test.rb'
+ t.pattern = 'test/**/*_test.rb'
t.warning = true
}
namespace :test do
task :isolated do
ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
- Dir.glob("test/*_test.rb").all? do |file|
+ Dir.glob("test/**/*_test.rb").all? do |file|
system(ruby, '-Ilib:test', file)
end or raise "Failures"
end
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 7e984124b7..1b81cbf5af 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -256,6 +256,7 @@ module ActionMailer #:nodoc:
include DeliveryMethods, Quoting
abstract!
+ # TODO Add some sanity tests for the included modules
include AbstractController::Logger
include AbstractController::Rendering
include AbstractController::LocalizedCache
@@ -270,12 +271,6 @@ module ActionMailer #:nodoc:
private_class_method :new #:nodoc:
- cattr_accessor :raise_delivery_errors
- @@raise_delivery_errors = true
-
- cattr_accessor :perform_deliveries
- @@perform_deliveries = true
-
extlib_inheritable_accessor :default_charset
self.default_charset = "utf-8"
@@ -295,9 +290,6 @@ module ActionMailer #:nodoc:
self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ]
class << self
- # Provides a list of emails that have been delivered by Mail
- delegate :deliveries, :deliveries=, :to => Mail
-
def mailer_name
@mailer_name ||= name.underscore
end
diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb
index 38325e512f..21909d5d57 100644
--- a/actionmailer/lib/action_mailer/delivery_methods.rb
+++ b/actionmailer/lib/action_mailer/delivery_methods.rb
@@ -1,7 +1,8 @@
require 'tmpdir'
module ActionMailer
- # Provides a DSL for adding delivery methods to ActionMailer.
+ # This modules handles everything related to the delivery, from registering new
+ # delivery methods to configuring the mail object to be send.
module DeliveryMethods
extend ActiveSupport::Concern
@@ -9,6 +10,13 @@ module ActionMailer
extlib_inheritable_accessor :delivery_methods, :delivery_method,
:instance_writer => false
+ # Do not make this inheritable, because we always want it to propagate
+ cattr_accessor :raise_delivery_errors
+ self.raise_delivery_errors = true
+
+ cattr_accessor :perform_deliveries
+ self.perform_deliveries = true
+
self.delivery_methods = {}
self.delivery_method = :smtp
@@ -32,6 +40,9 @@ module ActionMailer
end
module ClassMethods
+ # Provides a list of emails that have been delivered by Mail
+ delegate :deliveries, :deliveries=, :to => Mail
+
# Adds a new delivery method through the given class using the given symbol
# as alias and the default options supplied:
#
@@ -50,7 +61,8 @@ module ActionMailer
self.delivery_methods[symbol.to_sym] = klass
end
- def wrap_delivery_behavior(mail, method=delivery_method) #:nodoc:
+ def wrap_delivery_behavior(mail, method=nil) #:nodoc:
+ method ||= self.delivery_method
mail.register_for_delivery_notification(self)
if method.is_a?(Symbol)
diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb
index adba94cbef..01da954b03 100644
--- a/actionmailer/lib/action_mailer/mail_helper.rb
+++ b/actionmailer/lib/action_mailer/mail_helper.rb
@@ -23,7 +23,7 @@ module ActionMailer
# Access the message instance.
def message #:nodoc:
- @message
+ @_message
end
end
end
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/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_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/adv_attr_test.rb b/actionmailer/test/old_base/adv_attr_test.rb
index f22d733bc5..f22d733bc5 100644
--- a/actionmailer/test/adv_attr_test.rb
+++ b/actionmailer/test/old_base/adv_attr_test.rb
diff --git a/actionmailer/test/asset_host_test.rb b/actionmailer/test/old_base/asset_host_test.rb
index 124032f1d9..124032f1d9 100644
--- a/actionmailer/test/asset_host_test.rb
+++ b/actionmailer/test/old_base/asset_host_test.rb
diff --git a/actionmailer/test/mail_helper_test.rb b/actionmailer/test/old_base/mail_helper_test.rb
index a9b3cd3ce1..a9b3cd3ce1 100644
--- a/actionmailer/test/mail_helper_test.rb
+++ b/actionmailer/test/old_base/mail_helper_test.rb
diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/old_base/mail_layout_test.rb
index 4038fbf339..4038fbf339 100644
--- a/actionmailer/test/mail_layout_test.rb
+++ b/actionmailer/test/old_base/mail_layout_test.rb
diff --git a/actionmailer/test/mail_render_test.rb b/actionmailer/test/old_base/mail_render_test.rb
index 804200fd36..804200fd36 100644
--- a/actionmailer/test/mail_render_test.rb
+++ b/actionmailer/test/old_base/mail_render_test.rb
diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/old_base/mail_service_test.rb
index aa31c9a803..20b0f30b84 100644
--- a/actionmailer/test/mail_service_test.rb
+++ b/actionmailer/test/old_base/mail_service_test.rb
@@ -2,7 +2,7 @@
require 'abstract_unit'
class FunkyPathMailer < ActionMailer::Base
- self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
+ self.view_paths = "#{File.dirname(__FILE__)}/../fixtures/path.with.dots"
def multipart_with_template_path_with_dots(recipient)
recipients recipient
@@ -799,13 +799,13 @@ EOF
end
def test_receive_decodes_base64_encoded_mail
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email")
TestMailer.receive(fixture)
assert_match(/Jamis/, TestMailer.received_body.to_s)
end
def test_receive_attachments
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email2")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email2")
mail = Mail.new(fixture)
attachment = mail.attachments.last
assert_equal "smime.p7s", attachment.filename
@@ -813,21 +813,21 @@ EOF
end
def test_decode_attachment_without_charset
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email3")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email3")
mail = Mail.new(fixture)
attachment = mail.attachments.last
assert_equal 1026, attachment.read.length
end
def test_attachment_using_content_location
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email12")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email12")
mail = Mail.new(fixture)
assert_equal 1, mail.attachments.length
assert_equal "Photo25.jpg", mail.attachments.first.filename
end
def test_attachment_with_text_type
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email13")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email13")
mail = Mail.new(fixture)
assert mail.has_attachments?
assert_equal 1, mail.attachments.length
@@ -835,19 +835,19 @@ EOF
end
def test_decode_part_without_content_type
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email4")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email4")
mail = Mail.new(fixture)
assert_nothing_raised { mail.body }
end
def test_decode_message_without_content_type
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email5")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email5")
mail = Mail.new(fixture)
assert_nothing_raised { mail.body }
end
def test_decode_message_with_incorrect_charset
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email6")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email6")
mail = Mail.new(fixture)
assert_nothing_raised { mail.body }
end
@@ -979,7 +979,7 @@ EOF
end
def test_recursive_multipart_processing
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
+ 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)
@@ -990,7 +990,7 @@ EOF
end
def test_decode_encoded_attachment_filename
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email8")
mail = Mail.new(fixture)
attachment = mail.attachments.last
@@ -1007,7 +1007,7 @@ EOF
end
def test_decode_message_with_unknown_charset
- fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10")
+ fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email10")
mail = Mail.new(fixture)
assert_nothing_raised { mail.body }
end
@@ -1087,7 +1087,7 @@ end
class InheritableTemplateRootTest < ActiveSupport::TestCase
def test_attr
- expected = File.expand_path("#{File.dirname(__FILE__)}/fixtures/path.with.dots")
+ expected = File.expand_path("#{File.dirname(__FILE__)}/../fixtures/path.with.dots")
assert_equal expected, FunkyPathMailer.template_root.to_s
sub = Class.new(FunkyPathMailer)
diff --git a/actionmailer/test/tmail_compat_test.rb b/actionmailer/test/old_base/tmail_compat_test.rb
index 7c1d9a07c1..7c1d9a07c1 100644
--- a/actionmailer/test/tmail_compat_test.rb
+++ b/actionmailer/test/old_base/tmail_compat_test.rb
diff --git a/actionmailer/test/url_test.rb b/actionmailer/test/old_base/url_test.rb
index 10b6a36efd..10b6a36efd 100644
--- a/actionmailer/test/url_test.rb
+++ b/actionmailer/test/old_base/url_test.rb