aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/test
diff options
context:
space:
mode:
Diffstat (limited to 'actionmailer/test')
-rw-r--r--actionmailer/test/abstract_unit.rb6
-rw-r--r--actionmailer/test/base_test.rb124
-rw-r--r--actionmailer/test/delivery_method_test.rb104
-rw-r--r--actionmailer/test/mail_layout_test.rb1
-rw-r--r--actionmailer/test/mail_service_test.rb28
-rw-r--r--actionmailer/test/test_helper_test.rb2
-rw-r--r--actionmailer/test/tmail_compat_test.rb2
7 files changed, 178 insertions, 89 deletions
diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb
index 50b8a53006..1fc5ab85e0 100644
--- a/actionmailer/test/abstract_unit.rb
+++ b/actionmailer/test/abstract_unit.rb
@@ -26,6 +26,7 @@ FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
class MockSMTP
+
def self.deliveries
@@deliveries
end
@@ -41,6 +42,7 @@ class MockSMTP
def start(*args)
yield self
end
+
end
class Net::SMTP
@@ -57,9 +59,9 @@ rescue LoadError
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
end
-def set_delivery_method(delivery_method)
+def set_delivery_method(method)
@old_delivery_method = ActionMailer::Base.delivery_method
- ActionMailer::Base.delivery_method = delivery_method
+ ActionMailer::Base.delivery_method = method
end
def restore_delivery_method
diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb
new file mode 100644
index 0000000000..f1f5f38482
--- /dev/null
+++ b/actionmailer/test/base_test.rb
@@ -0,0 +1,124 @@
+# encoding: utf-8
+require 'abstract_unit'
+
+# class Notifier < ActionMailer::Base
+# delivers_from 'notifications@example.com'
+#
+# def welcome(user)
+# @user = user # available to the view
+# mail(:subject => 'Welcome!', :to => user.email_address)
+# # auto renders both welcome.text.erb and welcome.html.erb
+# end
+#
+# def goodbye(user)
+# headers["X-SPAM"] = 'Not-SPAM'
+# mail(:subject => 'Goodbye', :to => user.email_address) do |format|
+# format.html { render "shared_template "}
+# format.text # goodbye.text.erb
+# end
+# end
+#
+# def surprise(user, gift)
+# attachments[gift.name] = File.read(gift.path)
+# mail(:subject => 'Surprise!', :to => user.email_address) do |format|
+# format.html(:charset => "ascii") # surprise.html.erb
+# format.text(:transfer_encoding => "base64") # surprise.text.erb
+# end
+# end
+#
+# def special_surprise(user, gift)
+# attachments[gift.name] = { :content_type => "application/x-gzip", :content => File.read(gift.path) }
+# mail(:to => 'special@example.com') # subject not required
+# # auto renders both special_surprise.text.erb and special_surprise.html.erb
+# end
+# end
+#
+# mail = Notifier.welcome(user) # => returns a Mail object
+# mail.deliver
+#
+# Notifier.welcome(user).deliver # => creates and sends the Mail in one step
+class BaseTest < Test::Unit::TestCase
+
+ class TestMailer < ActionMailer::Base
+
+ def welcome(hash = {})
+ hash = {:to => 'mikel@test.lindsaar.net', :from => 'jose@test.plataformatec.com',
+ :subject => 'The first email on new API!'}.merge!(hash)
+ mail(hash)
+ end
+
+ def invoice(hash = {})
+ attachments['invoice.pdf'] = 'This is test File content'
+ hash = {:to => 'mikel@test.lindsaar.net', :from => 'jose@test.plataformatec.com',
+ :subject => 'Your invoice is attached'}.merge!(hash)
+ mail(hash)
+ end
+
+ end
+
+ def test_the_method_call_to_mail_does_not_raise_error
+ assert_nothing_raised { TestMailer.deliver_welcome }
+ end
+
+ def test_should_set_the_headers_of_the_mail_message
+ email = TestMailer.deliver_welcome
+ assert_equal(email.to, ['mikel@test.lindsaar.net'])
+ assert_equal(email.from, ['jose@test.plataformatec.com'])
+ assert_equal(email.subject, 'The first email on new API!')
+ end
+
+ def test_should_allow_all_headers_set
+ @time = Time.now
+ email = TestMailer.deliver_welcome(:bcc => 'bcc@test.lindsaar.net',
+ :cc => 'cc@test.lindsaar.net',
+ :content_type => 'multipart/mixed',
+ :charset => 'iso-8559-1',
+ :mime_version => '2.0',
+ :reply_to => 'reply-to@test.lindsaar.net',
+ :date => @time)
+ assert_equal(email.bcc, ['bcc@test.lindsaar.net'])
+ assert_equal(email.cc, ['cc@test.lindsaar.net'])
+ assert_equal(email.content_type, 'multipart/mixed')
+ assert_equal(email.charset, 'iso-8559-1')
+ assert_equal(email.mime_version, '2.0')
+ assert_equal(email.reply_to, ['reply-to@test.lindsaar.net'])
+ assert_equal(email.date, @time)
+ end
+
+# def test_should_allow_custom_headers_to_be_set
+# email = TestMailer.deliver_welcome
+# assert_equal("Not SPAM", email['X-SPAM'])
+# end
+
+ def test_should_allow_you_to_send_an_attachment
+ assert_nothing_raised { TestMailer.deliver_invoice }
+ end
+
+ def test_should_allow_you_to_send_an_attachment
+ email = TestMailer.deliver_invoice
+ assert_equal(1, email.attachments.length)
+ end
+
+ def test_should_allow_you_to_send_an_attachment
+ email = TestMailer.deliver_invoice
+ assert_equal('invoice.pdf', email.attachments[0].filename)
+ end
+
+ def test_should_allow_you_to_send_an_attachment
+ email = TestMailer.deliver_invoice
+ assert_equal('This is test File content', email.attachments['invoice.pdf'].decoded)
+ end
+
+ def test_should_use_class_defaults
+
+ end
+
+ # def test_that_class_defaults_are_set_on_instantiation
+ # pending
+ # end
+ #
+ # def test_should_set_the_subject_from_i18n
+ # pending
+ # end
+
+end \ No newline at end of file
diff --git a/actionmailer/test/delivery_method_test.rb b/actionmailer/test/delivery_method_test.rb
index 8f8c6b0275..1e7408d6d6 100644
--- a/actionmailer/test/delivery_method_test.rb
+++ b/actionmailer/test/delivery_method_test.rb
@@ -1,101 +1,73 @@
require 'abstract_unit'
+require 'mail'
-class DefaultDeliveryMethodMailer < ActionMailer::Base
+class MyCustomDelivery
end
-class NonDefaultDeliveryMethodMailer < ActionMailer::Base
- self.delivery_method = :sendmail
-end
-
-class FileDeliveryMethodMailer < ActionMailer::Base
- self.delivery_method = :file
-end
-
-class CustomDeliveryMethod
- attr_accessor :custom_deliveries
- def initialize()
- @customer_deliveries = []
- end
-
- def self.perform_delivery(mail)
- self.custom_deliveries << mail
- end
-end
-
-class CustomerDeliveryMailer < ActionMailer::Base
- self.delivery_method = CustomDeliveryMethod.new
-end
-
-class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
+class DefaultsDeliveryMethodsTest < ActionMailer::TestCase
def setup
set_delivery_method :smtp
end
-
+
def teardown
restore_delivery_method
end
def test_should_be_the_default_smtp
- assert_instance_of ActionMailer::DeliveryMethod::Smtp, ActionMailer::Base.delivery_method
+ assert_equal :smtp, ActionMailer::Base.delivery_method
end
-end
-class DefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
- def setup
- set_delivery_method :smtp
- end
-
- def teardown
- restore_delivery_method
+ def test_should_have_default_smtp_delivery_method_settings
+ settings = { :address => "localhost",
+ :port => 25,
+ :domain => 'localhost.localdomain',
+ :user_name => nil,
+ :password => nil,
+ :authentication => nil,
+ :enable_starttls_auto => true }
+ assert_equal settings, ActionMailer::Base.smtp_settings
end
-
- def test_should_be_the_default_smtp
- assert_instance_of ActionMailer::DeliveryMethod::Smtp, DefaultDeliveryMethodMailer.delivery_method
- end
-end
-class NonDefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
- def setup
- set_delivery_method :smtp
- end
-
- def teardown
- restore_delivery_method
+ def test_should_have_default_file_delivery_method_settings
+ settings = {:location => "#{Dir.tmpdir}/mails"}
+ assert_equal settings, ActionMailer::Base.file_settings
end
- def test_should_be_the_set_delivery_method
- assert_instance_of ActionMailer::DeliveryMethod::Sendmail, NonDefaultDeliveryMethodMailer.delivery_method
+ def test_should_have_default_sendmail_delivery_method_settings
+ settings = {:location => '/usr/sbin/sendmail',
+ :arguments => '-i -t'}
+ assert_equal settings, ActionMailer::Base.sendmail_settings
end
end
-class FileDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
+class CustomDeliveryMethodsTest < ActionMailer::TestCase
def setup
- set_delivery_method :smtp
+ ActionMailer::Base.add_delivery_method :custom, MyCustomDelivery
end
def teardown
- restore_delivery_method
- end
-
- def test_should_be_the_set_delivery_method
- assert_instance_of ActionMailer::DeliveryMethod::File, FileDeliveryMethodMailer.delivery_method
+ ActionMailer::Base.delivery_methods.delete(:custom)
+ ActionMailer::Base.delivery_settings.delete(:custom)
end
- def test_should_default_location_to_the_tmpdir
- assert_equal "#{Dir.tmpdir}/mails", ActionMailer::Base.file_settings[:location]
+ def test_allow_to_add_a_custom_delivery_method
+ ActionMailer::Base.delivery_method = :custom
+ assert_equal :custom, ActionMailer::Base.delivery_method
end
-end
-class CustomDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
- def setup
- set_delivery_method :smtp
+ def test_allow_to_customize_custom_settings
+ ActionMailer::Base.custom_settings = { :foo => :bar }
+ assert_equal Hash[:foo => :bar], ActionMailer::Base.custom_settings
end
- def teardown
- restore_delivery_method
+ def test_respond_to_custom_method_settings
+ assert_respond_to ActionMailer::Base, :custom_settings
+ assert_respond_to ActionMailer::Base, :custom_settings=
end
- def test_should_be_the_set_delivery_method
- assert_instance_of CustomDeliveryMethod, CustomerDeliveryMailer.delivery_method
+ def test_should_not_respond_for_invalid_method_settings
+ assert_raise NoMethodError do
+ ActionMailer::Base.another_settings
+ end
end
end
diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/mail_layout_test.rb
index 0877e7b2cb..b9ff075461 100644
--- a/actionmailer/test/mail_layout_test.rb
+++ b/actionmailer/test/mail_layout_test.rb
@@ -1,6 +1,7 @@
require 'abstract_unit'
class AutoLayoutMailer < ActionMailer::Base
+
def hello(recipient)
recipients recipient
subject "You have a mail"
diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb
index f87d9b2e5b..51d722ea00 100644
--- a/actionmailer/test/mail_service_test.rb
+++ b/actionmailer/test/mail_service_test.rb
@@ -545,7 +545,7 @@ class ActionMailerTest < Test::Unit::TestCase
assert_not_nil mail
mail, from, to = mail
- assert_equal 'system@loudthinking.com', from.addresses.first
+ assert_equal 'system@loudthinking.com', from
end
def test_reply_to
@@ -674,15 +674,12 @@ class ActionMailerTest < Test::Unit::TestCase
def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
ActionMailer::Base.raise_delivery_errors = false
- TestMailer.delivery_method.expects(:perform_delivery).raises(Exception)
+ Mail::Message.any_instance.expects(:deliver!).raises(Exception)
assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
end
def test_performs_delivery_via_sendmail
- sm = mock()
- sm.expects(:print).with(anything)
- sm.expects(:flush)
- IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm)
+ IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t -f "system@loudthinking.com" test@localhost', 'w+')
ActionMailer::Base.delivery_method = :sendmail
TestMailer.deliver_signed_up(@recipient)
end
@@ -901,6 +898,7 @@ EOF
assert_equal "iso-8859-1", mail.parts[1].charset
assert_equal "image/jpeg", mail.parts[2].mime_type
+
assert_equal "attachment", mail.parts[2][:content_disposition].disposition_type
assert_equal "foo.jpg", mail.parts[2][:content_disposition].filename
assert_equal "foo.jpg", mail.parts[2][:content_type].filename
@@ -1038,7 +1036,7 @@ EOF
def test_empty_header_values_omitted
result = TestMailer.create_unnamed_attachment(@recipient).encoded
assert_match %r{Content-Type: application/octet-stream;}, result
- assert_match %r{Content-Disposition: attachment[^;]}, result
+ assert_match %r{Content-Disposition: attachment;}, result
end
def test_headers_with_nonalpha_chars
@@ -1072,12 +1070,7 @@ EOF
def test_return_path_with_create
mail = TestMailer.create_return_path
- assert_equal "another@somewhere.test", mail['return-path'].to_s
- end
-
- def test_return_path_with_create
- mail = TestMailer.create_return_path
- assert_equal ["another@somewhere.test"], mail.return_path
+ assert_equal "another@somewhere.test", mail.return_path
end
def test_return_path_with_deliver
@@ -1093,7 +1086,7 @@ EOF
end
def test_starttls_is_enabled_if_supported
- ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
+ ActionMailer::Base.delivery_settings[:smtp].merge!(:enable_starttls_auto => true)
MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(true)
MockSMTP.any_instance.expects(:enable_starttls_auto)
ActionMailer::Base.delivery_method = :smtp
@@ -1101,7 +1094,7 @@ EOF
end
def test_starttls_is_disabled_if_not_supported
- ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
+ ActionMailer::Base.delivery_settings[:smtp].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
ActionMailer::Base.delivery_method = :smtp
@@ -1109,13 +1102,12 @@ EOF
end
def test_starttls_is_not_enabled
- ActionMailer::Base.smtp_settings[:enable_starttls_auto] = false
+ ActionMailer::Base.delivery_settings[:smtp].merge!(:enable_starttls_auto => false)
MockSMTP.any_instance.expects(:respond_to?).never
- MockSMTP.any_instance.expects(:enable_starttls_auto).never
ActionMailer::Base.delivery_method = :smtp
TestMailer.deliver_signed_up(@recipient)
ensure
- ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
+ ActionMailer::Base.delivery_settings[:smtp].merge!(:enable_starttls_auto => true)
end
end
diff --git a/actionmailer/test/test_helper_test.rb b/actionmailer/test/test_helper_test.rb
index 1fed26f78f..48e4433e98 100644
--- a/actionmailer/test/test_helper_test.rb
+++ b/actionmailer/test/test_helper_test.rb
@@ -12,7 +12,7 @@ end
class TestHelperMailerTest < ActionMailer::TestCase
def test_setup_sets_right_action_mailer_options
- assert_instance_of ActionMailer::DeliveryMethod::Test, ActionMailer::Base.delivery_method
+ assert_equal :test, ActionMailer::Base.delivery_method
assert ActionMailer::Base.perform_deliveries
assert_equal [], ActionMailer::Base.deliveries
end
diff --git a/actionmailer/test/tmail_compat_test.rb b/actionmailer/test/tmail_compat_test.rb
index a1ca6a7243..b7fcb3cfea 100644
--- a/actionmailer/test/tmail_compat_test.rb
+++ b/actionmailer/test/tmail_compat_test.rb
@@ -4,7 +4,6 @@ class TmailCompatTest < Test::Unit::TestCase
def test_set_content_type_raises_deprecation_warning
mail = Mail.new
- STDERR.expects(:puts) # Deprecation warning
assert_nothing_raised do
mail.set_content_type "text/plain"
end
@@ -13,7 +12,6 @@ class TmailCompatTest < Test::Unit::TestCase
def test_transfer_encoding_raises_deprecation_warning
mail = Mail.new
- STDERR.expects(:puts) # Deprecation warning
assert_nothing_raised do
mail.transfer_encoding "base64"
end