diff options
Diffstat (limited to 'actionmailer/test')
16 files changed, 159 insertions, 29 deletions
diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index 3a519253f9..4b38d4bd31 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -1,16 +1,4 @@ -# Pathname has a warning, so require it first while silencing -# warnings to shut it up. -# -# Also, in 1.9, Bundler creates warnings due to overriding -# Rubygems methods -begin - old, $VERBOSE = $VERBOSE, nil - require 'pathname' - require File.expand_path('../../../load_paths', __FILE__) -ensure - $VERBOSE = old -end - +require File.expand_path('../../../load_paths', __FILE__) require 'active_support/core_ext/kernel/reporting' # These are the normal settings that will be set up by Railties @@ -20,12 +8,10 @@ silence_warnings do Encoding.default_external = "UTF-8" end -lib = File.expand_path("#{File.dirname(__FILE__)}/../lib") -$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) - require 'minitest/autorun' require 'action_mailer' require 'action_mailer/test_case' +require 'active_support/queueing' silence_warnings do # These external dependencies have warnings :/ diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 65550ab505..4f2af50fdd 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -1,10 +1,15 @@ # encoding: utf-8 require 'abstract_unit' +require 'set' + +require 'action_dispatch' +require 'active_support/queueing' require 'active_support/time' require 'mailers/base_mailer' require 'mailers/proc_mailer' require 'mailers/asset_mailer' +require 'mailers/async_mailer' class BaseTest < ActiveSupport::TestCase def teardown @@ -407,7 +412,7 @@ class BaseTest < ActiveSupport::TestCase BaseMailer.deliveries.clear BaseMailer.expects(:deliver_mail).once mail = BaseMailer.welcome.deliver - assert_instance_of Mail::Message, mail + assert_equal 'The first email on new API!', mail.subject end test "calling deliver on the action should increment the deliveries collection if using the test mailer" do @@ -417,6 +422,17 @@ class BaseTest < ActiveSupport::TestCase assert_equal(1, BaseMailer.deliveries.length) end + test "delivering message asynchronously" do + AsyncMailer.delivery_method = :test + AsyncMailer.deliveries.clear + + AsyncMailer.welcome.deliver + assert_equal 0, AsyncMailer.deliveries.length + + AsyncMailer.queue.drain + assert_equal 1, AsyncMailer.deliveries.length + end + test "calling deliver, ActionMailer should yield back to mail to let it call :do_delivery on itself" do mail = Mail::Message.new mail.expects(:do_delivery).once @@ -431,6 +447,14 @@ class BaseTest < ActiveSupport::TestCase assert_equal("TEXT Implicit Multipart", mail.text_part.body.decoded) end + test "should raise if missing template in implicit render" do + BaseMailer.deliveries.clear + assert_raises ActionView::MissingTemplate do + BaseMailer.implicit_different_template('missing_template').deliver + end + assert_equal(0, BaseMailer.deliveries.length) + end + test "you can specify a different template for explicit render" do mail = BaseMailer.explicit_different_template('explicit_multipart_templates').deliver assert_equal("HTML Explicit Multipart Templates", mail.html_part.body.decoded) @@ -461,14 +485,18 @@ class BaseTest < ActiveSupport::TestCase end test "assets tags should use a Mailer's asset_host settings when available" do - ActionMailer::Base.config.asset_host = "global.com" - ActionMailer::Base.config.assets_dir = "global/" + begin + ActionMailer::Base.config.asset_host = "http://global.com" + ActionMailer::Base.config.assets_dir = "global/" - AssetMailer.asset_host = "http://local.com" + AssetMailer.asset_host = "http://local.com" - mail = AssetMailer.welcome + mail = AssetMailer.welcome - assert_equal(%{<img alt="Dummy" src="http://local.com/images/dummy.png" />}, mail.body.to_s.strip) + assert_equal(%{<img alt="Dummy" src="http://local.com/images/dummy.png" />}, mail.body.to_s.strip) + ensure + AssetMailer.asset_host = ActionMailer::Base.config.asset_host + end end # Before and After hooks @@ -538,11 +566,11 @@ class BaseTest < ActiveSupport::TestCase end test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do - mail1 = ProcMailer.welcome + mail1 = ProcMailer.welcome['X-Proc-Method'] yesterday = 1.day.ago Time.stubs(:now).returns(yesterday) - mail2 = ProcMailer.welcome - assert(mail1['X-Proc-Method'].to_s.to_i > mail2['X-Proc-Method'].to_s.to_i) + mail2 = ProcMailer.welcome['X-Proc-Method'] + assert(mail1.to_s.to_i > mail2.to_s.to_i) end test "we can call other defined methods on the class as needed" do @@ -550,6 +578,52 @@ class BaseTest < ActiveSupport::TestCase assert_equal("Thanks for signing up this afternoon", mail.subject) end + test "modifying the mail message with a before_filter" do + class BeforeFilterMailer < ActionMailer::Base + before_filter :add_special_header! + + def welcome ; mail ; end + + private + def add_special_header! + headers('X-Special-Header' => 'Wow, so special') + end + end + + assert_equal('Wow, so special', BeforeFilterMailer.welcome['X-Special-Header'].to_s) + end + + test "modifying the mail message with an after_filter" do + class AfterFilterMailer < ActionMailer::Base + after_filter :add_special_header! + + def welcome ; mail ; end + + private + def add_special_header! + headers('X-Special-Header' => 'Testing') + end + end + + assert_equal('Testing', AfterFilterMailer.welcome['X-Special-Header'].to_s) + end + + test "adding an inline attachment using a before_filter" do + class DefaultInlineAttachmentMailer < ActionMailer::Base + before_filter :add_inline_attachment! + + def welcome ; mail ; end + + private + def add_inline_attachment! + attachments.inline["footer.jpg"] = 'hey there' + end + end + + mail = DefaultInlineAttachmentMailer.welcome + assert_equal('image/jpeg; filename=footer.jpg', mail.attachments.inline.first['Content-Type'].to_s) + end + test "action methods should be refreshed after defining new method" do class FooMailer < ActionMailer::Base # this triggers action_methods @@ -559,7 +633,33 @@ class BaseTest < ActiveSupport::TestCase end end - assert_equal ["notify"], FooMailer.action_methods + assert_equal Set.new(["notify"]), FooMailer.action_methods + end + + test "mailer can be anonymous" do + mailer = Class.new(ActionMailer::Base) do + def welcome + mail + end + end + + assert_equal "anonymous", mailer.mailer_name + + assert_equal "Welcome", mailer.welcome.subject + assert_equal "Anonymous mailer body", mailer.welcome.body.encoded.strip + end + + test "default_from can be set" do + class DefaultFromMailer < ActionMailer::Base + default :to => 'system@test.lindsaar.net' + self.default_options = {from: "robert.pankowecki@gmail.com"} + + def welcome + mail(subject: "subject", body: "hello world") + end + end + + assert_equal ["robert.pankowecki@gmail.com"], DefaultFromMailer.welcome.from end protected diff --git a/actionmailer/test/delivery_methods_test.rb b/actionmailer/test/delivery_methods_test.rb index 08f84dbf3b..7109f23e4c 100644 --- a/actionmailer/test/delivery_methods_test.rb +++ b/actionmailer/test/delivery_methods_test.rb @@ -4,6 +4,13 @@ require 'mail' class MyCustomDelivery end +class MyOptionedDelivery + attr_reader :options + def initialize(options) + @options = options + end +end + class BogusDelivery def initialize(*) end @@ -115,6 +122,38 @@ class MailDeliveryTest < ActiveSupport::TestCase assert_instance_of Mail::TestMailer, email.delivery_method end + test "delivery method options default to class level options" do + default_options = {a: "b"} + ActionMailer::Base.add_delivery_method :optioned, MyOptionedDelivery, default_options + mail_instance = DeliveryMailer.welcome(:delivery_method => :optioned) + assert_equal default_options, mail_instance.delivery_method.options + end + + test "delivery method options can be overridden per mail instance" do + default_options = {a: "b"} + ActionMailer::Base.add_delivery_method :optioned, MyOptionedDelivery, default_options + overridden_options = {a: "a"} + mail_instance = DeliveryMailer.welcome(:delivery_method => :optioned, :delivery_method_options => overridden_options) + assert_equal overridden_options, mail_instance.delivery_method.options + end + + test "default delivery options can be overridden per mail instance" do + 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 + overridden_options = {user_name: "overridden", :password => "somethingobtuse"} + mail_instance = DeliveryMailer.welcome(:delivery_method_options => overridden_options) + delivery_method_instance = mail_instance.delivery_method + assert_equal "overridden", delivery_method_instance.settings[:user_name] + assert_equal "somethingobtuse", delivery_method_instance.settings[:password] + assert_equal delivery_method_instance.settings.merge(overridden_options), delivery_method_instance.settings + end + test "non registered delivery methods raises errors" do DeliveryMailer.delivery_method = :unknown assert_raise RuntimeError do diff --git a/actionmailer/test/fixtures/anonymous/welcome.erb b/actionmailer/test/fixtures/anonymous/welcome.erb new file mode 100644 index 0000000000..8361da62c4 --- /dev/null +++ b/actionmailer/test/fixtures/anonymous/welcome.erb @@ -0,0 +1 @@ +Anonymous mailer body diff --git a/actionmailer/test/fixtures/async_mailer/welcome.erb b/actionmailer/test/fixtures/async_mailer/welcome.erb new file mode 100644 index 0000000000..01f3f00c63 --- /dev/null +++ b/actionmailer/test/fixtures/async_mailer/welcome.erb @@ -0,0 +1 @@ +Welcome
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/attachment_with_hash.html.erb b/actionmailer/test/fixtures/base_mailer/attachment_with_hash.html.erb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/attachment_with_hash.html.erb diff --git a/actionmailer/test/fixtures/base_mailer/attachment_with_hash_default_encoding.html.erb b/actionmailer/test/fixtures/base_mailer/attachment_with_hash_default_encoding.html.erb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/attachment_with_hash_default_encoding.html.erb diff --git a/actionmailer/test/fixtures/base_mailer/welcome_with_headers.html.erb b/actionmailer/test/fixtures/base_mailer/welcome_with_headers.html.erb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/welcome_with_headers.html.erb diff --git a/actionmailer/test/fixtures/base_test/after_filter_mailer/welcome.html.erb b/actionmailer/test/fixtures/base_test/after_filter_mailer/welcome.html.erb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/actionmailer/test/fixtures/base_test/after_filter_mailer/welcome.html.erb diff --git a/actionmailer/test/fixtures/base_test/before_filter_mailer/welcome.html.erb b/actionmailer/test/fixtures/base_test/before_filter_mailer/welcome.html.erb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/actionmailer/test/fixtures/base_test/before_filter_mailer/welcome.html.erb diff --git a/actionmailer/test/fixtures/base_test/default_inline_attachment_mailer/welcome.html.erb b/actionmailer/test/fixtures/base_test/default_inline_attachment_mailer/welcome.html.erb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/actionmailer/test/fixtures/base_test/default_inline_attachment_mailer/welcome.html.erb diff --git a/actionmailer/test/fixtures/mail_delivery_test/delivery_mailer/welcome.html.erb b/actionmailer/test/fixtures/mail_delivery_test/delivery_mailer/welcome.html.erb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/actionmailer/test/fixtures/mail_delivery_test/delivery_mailer/welcome.html.erb diff --git a/actionmailer/test/fixtures/proc_mailer/welcome.html.erb b/actionmailer/test/fixtures/proc_mailer/welcome.html.erb new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/actionmailer/test/fixtures/proc_mailer/welcome.html.erb diff --git a/actionmailer/test/i18n_with_controller_test.rb b/actionmailer/test/i18n_with_controller_test.rb index 7040ae6f8d..68ed86e0d4 100644 --- a/actionmailer/test/i18n_with_controller_test.rb +++ b/actionmailer/test/i18n_with_controller_test.rb @@ -24,7 +24,7 @@ end class ActionMailerI18nWithControllerTest < ActionDispatch::IntegrationTest Routes = ActionDispatch::Routing::RouteSet.new Routes.draw do - match ':controller(/:action(/:id))' + get ':controller(/:action(/:id))' end def app diff --git a/actionmailer/test/mailers/async_mailer.rb b/actionmailer/test/mailers/async_mailer.rb new file mode 100644 index 0000000000..c21a464f38 --- /dev/null +++ b/actionmailer/test/mailers/async_mailer.rb @@ -0,0 +1,3 @@ +class AsyncMailer < BaseMailer + self.queue = ActiveSupport::TestQueue.new +end diff --git a/actionmailer/test/url_test.rb b/actionmailer/test/url_test.rb index 0536e83098..2ea1723434 100644 --- a/actionmailer/test/url_test.rb +++ b/actionmailer/test/url_test.rb @@ -57,8 +57,8 @@ class ActionMailerUrlTest < ActionMailer::TestCase UrlTestMailer.delivery_method = :test AppRoutes.draw do - match ':controller(/:action(/:id))' - match '/welcome' => "foo#bar", :as => "welcome" + get ':controller(/:action(/:id))' + get '/welcome' => "foo#bar", :as => "welcome" end expected = new_mail |