Move old tests to a specific folder and add some delivery method tests.

This commit is contained in:
José Valim and Mikel Lindsaar 2010-01-24 19:36:42 +01:00
parent 99f960a3d7
commit bd96614101
16 changed files with 131 additions and 73 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -23,7 +23,7 @@ module ActionMailer
# Access the message instance.
def message #:nodoc:
@message
@_message
end
end
end

View File

@ -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' }
FIXTURE_LOAD_PATH = File.expand_path('fixtures', File.dirname(__FILE__))
ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH
$:.unshift File.join(FIXTURE_LOAD_PATH, 'helpers')
$:.unshift "#{File.dirname(__FILE__)}/fixtures/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

View File

@ -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

View File

@ -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
def deliver!(mail)
raise "failed"
end
end
def test_should_be_the_default_smtp
assert_equal :smtp, ActionMailer::Base.delivery_method
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

View File

@ -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

View File

@ -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)