# 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 = "blah" end part "text/html" do |p| p.body = "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 # This tests body calls accepeting a hash, which is deprecated. def body_ivar(recipient) recipients recipient subject "Body as a local variable" from "test@example.com" body :body => "foo", :bar => "baz" 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 = [] @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.create_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.create_nested_multipart_with_body(@recipient) assert_nothing_raised { created = TestMailer.create_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.create_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.create_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.deliver_signed_up(@recipient) } 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_subject_with_i18n assert_nothing_raised { TestMailer.deliver_subject_with_i18n(@recipient) } assert_equal "Subject with i18n", ActionMailer::Base.deliveries.first.subject.to_s I18n.backend.store_translations('en', :actionmailer => {:test_mailer => {:subject_with_i18n => {:subject => "New Subject!"}}}) assert_nothing_raised { TestMailer.deliver_subject_with_i18n(@recipient) } assert_equal "New Subject!", ActionMailer::Base.deliveries.last.subject.to_s 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.create_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.create_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.create_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.deliver_cancelled_account(@recipient) } 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.create_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.deliver_cc_bcc @recipient 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 ActionMailer::Base.delivery_method = :smtp TestMailer.deliver_from_without_name 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 ActionMailer::Base.delivery_method = :smtp TestMailer.deliver_from_with_name mail = MockSMTP.deliveries.first assert_not_nil mail mail, from, to = mail assert_equal 'system@loudthinking.com', from.addresses.first 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.create_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.deliver_different_reply_to @recipient 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.create_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.deliver_iso_charset @recipient 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.create_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.deliver_unencoded_subject @recipient 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_instances_are_nil assert_nil ActionMailer::Base.new assert_nil TestMailer.new end def test_deliveries_array assert_not_nil ActionMailer::Base.deliveries assert_equal 0, ActionMailer::Base.deliveries.size TestMailer.deliver_signed_up(@recipient) 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.deliver_signed_up(@recipient) assert_equal 0, ActionMailer::Base.deliveries.size ActionMailer::Base.perform_deliveries = true TestMailer.deliver_signed_up(@recipient) 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 TestMailer.delivery_method.expects(:perform_delivery).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) ActionMailer::Base.delivery_method = :sendmail TestMailer.deliver_signed_up(@recipient) 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.create_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.deliver_extended_headers @recipient 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.create_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.create_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.create_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.create_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 ActionMailer::Base.delivery_method = :smtp TestMailer.deliver_cc_bcc(@recipient) 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.deliver_cc_bcc(@recipient) 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.original_filename.dup expected.force_encoding(Encoding::ASCII_8BIT) result.force_encoding(Encoding::ASCII_8BIT) assert_equal expected, result else assert_equal expected, attachment.original_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.create_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.create_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_deliver_with_mail_object mail = TestMailer.create_headers_with_nonalpha_chars(@recipient) assert_nothing_raised { TestMailer.deliver(mail) } assert_equal 1, TestMailer.deliveries.length end def test_multipart_with_template_path_with_dots mail = FunkyPathMailer.create_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.create_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.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 end def test_return_path_with_deliver ActionMailer::Base.delivery_method = :smtp TestMailer.deliver_return_path assert_match %r{^Return-Path: }, MockSMTP.deliveries[0][0] assert_equal "another@somewhere.test", MockSMTP.deliveries[0][1].to_s end def test_body_is_stored_as_an_ivar mail = nil ActiveSupport::Deprecation.silence { mail = TestMailer.create_body_ivar(@recipient) } assert_equal "body: foo\nbar: baz", mail.body.to_s end def test_starttls_is_enabled_if_supported ActionMailer::Base.smtp_settings[: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 TestMailer.deliver_signed_up(@recipient) end def test_starttls_is_disabled_if_not_supported ActionMailer::Base.smtp_settings[: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 TestMailer.deliver_signed_up(@recipient) end def test_starttls_is_not_enabled ActionMailer::Base.smtp_settings[: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 end end class InheritableTemplateRootTest < Test::Unit::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) sub.template_root = 'test/path' assert_equal File.expand_path('test/path'), sub.template_root.to_s assert_equal expected, FunkyPathMailer.template_root.to_s end end class MethodNamingTest < Test::Unit::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 = [] end def teardown restore_delivery_method end def test_send_method assert_nothing_raised do assert_emails 1 do TestMailer.deliver_send 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