2008-03-31 01:31:37 -04:00
# encoding: utf-8
2008-01-05 08:32:22 -05:00
require 'abstract_unit'
2004-11-23 20:04:44 -05:00
2006-01-28 21:25:15 -05:00
class FunkyPathMailer < ActionMailer :: Base
2006-06-30 01:27:05 -04:00
self . template_root = " #{ File . dirname ( __FILE__ ) } /fixtures/path.with.dots "
2006-01-28 21:25:15 -05:00
def multipart_with_template_path_with_dots ( recipient )
recipients recipient
2009-05-27 13:05:27 -04:00
subject " This path has dots "
2006-01-28 21:25:15 -05:00
from " Chad Fowler <chad@chadfowler.com> "
2009-05-27 13:05:27 -04:00
attachment :content_type = > " text/plain " ,
:body = > " dots dots dots... "
2006-01-28 21:25:15 -05:00
end
end
2004-11-23 20:04:44 -05:00
class TestMailer < ActionMailer :: Base
def signed_up ( recipient )
2009-10-21 19:26:10 -04:00
recipients recipient
subject " [Signed up] Welcome #{ recipient } "
from " system@loudthinking.com "
@recipient = recipient
2004-11-23 20:04:44 -05:00
end
def cancelled_account ( recipient )
2009-10-21 19:26:10 -04:00
recipients recipient
subject " [Cancelled] Goodbye #{ recipient } "
from " system@loudthinking.com "
sent_on Time . local ( 2004 , 12 , 12 )
render :text = > " Goodbye, Mr. #{ recipient } "
2004-11-23 20:04:44 -05:00
end
2005-01-13 08:05:51 -05:00
def cc_bcc ( recipient )
2005-05-29 12:36:22 -04:00
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 "
2009-10-21 19:26:10 -04:00
render :text = > " Nothing to see here. "
2005-01-13 08:05:51 -05:00
end
2008-05-23 13:40:36 -04:00
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 "
2009-10-21 19:26:10 -04:00
render :text = > " Nothing to see here. "
2008-05-23 13:40:36 -04:00
end
2005-02-19 16:51:16 -05:00
def iso_charset ( recipient )
2009-10-21 19:26:10 -04:00
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. "
2005-02-19 16:51:16 -05:00
end
def unencoded_subject ( recipient )
2009-10-21 19:26:10 -04:00
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. "
2005-04-11 06:29:15 -04:00
end
def extended_headers ( recipient )
2009-10-21 19:26:10 -04:00
recipients recipient
subject " testing extended headers "
from " Grytøyr <stian1@example.net> "
sent_on Time . local ( 2004 , 12 , 12 )
cc " Grytøyr <stian2@example.net> "
bcc " Grytøyr <stian3@example.net> "
charset " iso-8859-1 "
render :text = > " Nothing to see here. "
2005-02-19 16:51:16 -05:00
end
2005-04-11 07:46:12 -04:00
def utf8_body ( recipient )
2009-10-21 19:26:10 -04:00
recipients recipient
subject " testing utf-8 body "
from " Foo áëô îü <extended@example.net> "
sent_on Time . local ( 2004 , 12 , 12 )
cc " Foo áëô îü <extended@example.net> "
bcc " Foo áëô îü <extended@example.net> "
charset " utf-8 "
render :text = > " åœö blah "
2005-04-11 07:46:12 -04:00
end
2005-05-02 13:15:21 -04:00
2005-08-22 19:53:04 -04:00
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 = " <b>blah</b> "
end
end
2005-11-06 08:41:12 -05:00
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 = " <b>blah</b> "
end
end
2005-06-24 08:15:45 -04:00
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
2005-05-29 12:36:22 -04:00
part " text/html " do | p |
p . charset = " iso-8859-1 "
p . body = " blah "
end
2009-11-20 05:19:10 -05:00
attachment :content_type = > " image/jpeg " , :filename = > File . join ( File . dirname ( __FILE__ ) , " fixtures " , " attachments " , " foo.jpg " ) ,
2005-05-29 12:36:22 -04:00
:body = > " 123456789 "
2009-10-21 19:26:10 -04:00
render :text = > " plain text default "
2005-05-29 12:36:22 -04:00
end
2005-09-01 12:00:08 -04:00
def implicitly_multipart_example ( recipient , cs = nil , order = nil )
2009-10-21 19:26:10 -04:00
recipients recipient
subject " multipart example "
from " test@example.com "
sent_on Time . local ( 2004 , 12 , 12 )
@charset = cs if cs
@recipient = recipient
2005-09-01 12:00:08 -04:00
@implicit_parts_order = order if order
2005-05-29 12:36:22 -04:00
end
2006-03-18 19:18:50 -05:00
def implicitly_multipart_with_utf8
recipients " no.one@nowhere.test "
subject " Foo áëô îü "
from " some.one@somewhere.test "
template " implicitly_multipart_example "
2009-10-21 19:26:10 -04:00
@recipient = " no.one@nowhere.test "
2006-03-18 19:18:50 -05:00
end
2005-06-03 06:57:06 -04:00
def html_mail ( recipient )
recipients recipient
subject " html mail "
from " test@example.com "
content_type " text/html "
2009-10-21 19:26:10 -04:00
render :text = > " <em>Emphasize</em> <strong>this</strong> "
2005-06-03 06:57:06 -04:00
end
2005-09-01 10:45:10 -04:00
def html_mail_with_underscores ( recipient )
subject " html mail with underscores "
2009-10-21 19:26:10 -04:00
render :text = > %{ <a href="http://google.com" target="_blank">_Google</a> }
2005-09-01 10:45:10 -04:00
end
2005-06-30 17:21:54 -04:00
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 "
2009-10-21 19:26:10 -04:00
@recipient = recipient
2005-06-30 17:21:54 -04:00
end
2007-06-07 19:42:47 -04:00
def custom_templating_extension ( recipient )
recipients recipient
subject " [Signed up] Welcome #{ recipient } "
from " system@loudthinking.com "
sent_on Time . local ( 2004 , 12 , 12 )
2009-10-21 19:26:10 -04:00
@recipient = recipient
2007-06-07 19:42:47 -04:00
end
2005-06-30 17:40:43 -04:00
def various_newlines ( recipient )
recipients recipient
subject " various newlines "
from " test@example.com "
2009-10-21 19:26:10 -04:00
render :text = > " line # 1 \n line # 2 \r line # 3 \r \n line # 4 \r \r " +
" line # 5 \n \n line # 6 \r \n \r \n line # 7 "
2005-06-30 17:40:43 -04:00
end
2005-07-06 05:53:34 -04:00
def various_newlines_multipart ( recipient )
recipients recipient
subject " various newlines multipart "
from " test@example.com "
content_type " multipart/alternative "
2009-10-21 19:26:10 -04:00
2005-07-06 05:53:34 -04:00
part :content_type = > " text/plain " , :body = > " line # 1 \n line # 2 \r line # 3 \r \n line # 4 \r \r "
part :content_type = > " text/html " , :body = > " <p>line # 1</p> \n <p>line # 2</p> \r <p>line # 3</p> \r \n <p>line # 4</p> \r \r "
end
2005-07-01 09:17:44 -04:00
def nested_multipart ( recipient )
recipients recipient
subject " nested multipart "
from " test@example.com "
content_type " multipart/mixed "
2009-10-21 19:26:10 -04:00
2006-09-02 15:35:55 -04:00
part :content_type = > " multipart/alternative " , :content_disposition = > " inline " , :headers = > { " foo " = > " bar " } do | p |
2005-07-01 09:17:44 -04:00
p . part :content_type = > " text/plain " , :body = > " test text \n line # 2 "
p . part :content_type = > " text/html " , :body = > " <b>test</b> HTML<br/> \n line # 2 "
end
2009-10-21 19:26:10 -04:00
2005-07-01 09:17:44 -04:00
attachment :content_type = > " application/octet-stream " , :filename = > " test.txt " , :body = > " test abcdefghijklmnopqstuvwxyz "
2005-07-04 12:11:22 -04:00
end
2008-08-06 21:08:27 -04:00
2007-11-28 21:52:49 -05:00
def nested_multipart_with_body ( recipient )
recipients recipient
subject " nested multipart with body "
from " test@example.com "
content_type " multipart/mixed "
2009-10-21 19:26:10 -04:00
2007-11-28 21:52:49 -05:00
part :content_type = > " multipart/alternative " , :content_disposition = > " inline " , :body = > " Nothing to see here. " do | p |
p . part :content_type = > " text/html " , :body = > " <b>test</b> HTML<br/> "
end
end
2006-03-18 18:06:25 -05:00
def attachment_with_custom_header ( recipient )
recipients recipient
subject " custom header in attachment "
from " test@example.com "
content_type " multipart/related "
2009-11-19 22:10:57 -05:00
part :content_type = > " text/html " , :body = > 'yo'
attachment :content_type = > " image/jpeg " , :filename = > File . join ( File . dirname ( __FILE__ ) , " fixtures " , " attachments " , " test.jpg " ) , :body = > " i am not a real picture " , :headers = > { 'Content-ID' = > '<test@test.com>' }
2006-03-18 18:06:25 -05:00
end
2005-07-04 12:11:22 -04:00
def unnamed_attachment ( recipient )
recipients recipient
subject " nested multipart "
from " test@example.com "
2005-07-04 12:21:11 -04:00
content_type " multipart/mixed "
2005-07-04 12:11:22 -04:00
part :content_type = > " text/plain " , :body = > " hullo "
attachment :content_type = > " application/octet-stream " , :body = > " test abcdefghijklmnopqstuvwxyz "
2005-07-01 09:17:44 -04:00
end
2005-07-04 12:21:11 -04:00
def headers_with_nonalpha_chars ( recipient )
recipients recipient
subject " nonalpha chars "
from " One: Two <test@example.com> "
cc " Three: Four <test@example.com> "
bcc " Five: Six <test@example.com> "
2009-10-21 19:26:10 -04:00
render :text = > " testing "
2005-07-04 12:21:11 -04:00
end
2009-11-20 05:19:10 -05:00
2006-03-18 18:53:07 -05:00
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 "
2009-10-21 19:26:10 -04:00
render :text = > " testing "
2006-03-18 18:53:07 -05:00
end
2007-09-22 13:32:39 -04:00
def return_path
recipients " no.one@nowhere.test "
subject " return path test "
from " some.one@somewhere.test "
headers " return-path " = > " another@somewhere.test "
2009-10-21 19:26:10 -04:00
render :text = > " testing "
2007-09-22 13:32:39 -04:00
end
2008-09-28 13:31:45 -04:00
def body_ivar ( recipient )
recipients recipient
subject " Body as a local variable "
from " test@example.com "
body :body = > " foo " , :bar = > " baz "
end
2009-10-28 07:22:07 -04:00
def subject_with_i18n ( recipient )
recipients recipient
from " system@loudthinking.com "
render :text = > " testing "
end
2009-10-18 18:20:14 -04:00
class << self
2005-05-02 13:15:21 -04:00
attr_accessor :received_body
end
def receive ( mail )
self . class . received_body = mail . body
end
2004-11-23 20:04:44 -05:00
end
class ActionMailerTest < Test :: Unit :: TestCase
2005-05-29 12:36:22 -04:00
include ActionMailer :: Quoting
2005-01-13 08:05:51 -05:00
2005-02-19 16:51:16 -05:00
def encode ( text , charset = " utf-8 " )
2005-05-29 12:36:22 -04:00
quoted_printable ( text , charset )
2005-02-19 16:51:16 -05:00
end
def new_mail ( charset = " utf-8 " )
2009-11-12 00:08:50 -05:00
mail = Mail . new
2006-09-09 17:56:38 -04:00
mail . mime_version = " 1.0 "
2005-02-19 16:51:16 -05:00
if charset
2009-11-19 22:10:57 -05:00
mail . content_type [ " text " , " plain " , { " charset " = > charset } ]
2005-02-19 16:51:16 -05:00
end
mail
end
2007-01-21 18:20:44 -05:00
# Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
2004-11-23 20:04:44 -05:00
def setup
2007-11-07 11:05:17 -05:00
set_delivery_method :test
2004-11-23 20:04:44 -05:00
ActionMailer :: Base . perform_deliveries = true
2007-06-20 02:50:43 -04:00
ActionMailer :: Base . raise_delivery_errors = true
2004-11-23 20:04:44 -05:00
ActionMailer :: Base . deliveries = [ ]
2007-01-21 18:20:44 -05:00
@original_logger = TestMailer . logger
2004-11-23 20:04:44 -05:00
@recipient = 'test@localhost'
end
2007-11-07 11:05:17 -05:00
2007-01-21 18:20:44 -05:00
def teardown
TestMailer . logger = @original_logger
2007-11-07 11:05:17 -05:00
restore_delivery_method
2007-01-21 18:20:44 -05:00
end
2004-11-23 20:04:44 -05:00
2005-07-01 09:17:44 -04:00
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
2008-08-06 21:08:27 -04:00
2005-07-01 09:17:44 -04:00
assert_equal " multipart/mixed " , created . content_type
assert_equal " multipart/alternative " , created . parts . first . content_type
2006-09-02 15:35:55 -04:00
assert_equal " bar " , created . parts . first . header [ 'foo' ] . to_s
2009-03-01 15:08:16 -05:00
assert_nil created . parts . first . charset
2005-07-01 09:17:44 -04:00
assert_equal " text/plain " , created . parts . first . parts . first . content_type
assert_equal " text/html " , created . parts . first . parts [ 1 ] . content_type
assert_equal " application/octet-stream " , created . parts [ 1 ] . content_type
end
2007-11-28 21:52:49 -05:00
def test_nested_parts_with_body
created = nil
2009-04-22 20:16:28 -04:00
TestMailer . create_nested_multipart_with_body ( @recipient )
2007-11-28 21:52:49 -05:00
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 . content_type
assert_equal " multipart/alternative " , created . parts . first . content_type
assert_equal " Nothing to see here. " , created . parts . first . parts . first . body
assert_equal " text/plain " , created . parts . first . parts . first . content_type
assert_equal " text/html " , created . parts . first . parts [ 1 ] . content_type
end
2006-03-18 18:06:25 -05:00
def test_attachment_with_custom_header
created = nil
2009-04-22 20:16:28 -04:00
assert_nothing_raised { created = TestMailer . create_attachment_with_custom_header ( @recipient ) }
2009-11-19 22:10:57 -05:00
created . encoded
2009-04-22 20:16:28 -04:00
assert created . parts . any? { | p | p . header [ 'content-id' ] . to_s == " <test@test.com> " }
2006-03-18 18:06:25 -05:00
end
2004-11-23 20:04:44 -05:00
def test_signed_up
2009-08-07 21:37:21 -04:00
Time . stubs ( :now = > Time . now )
2005-02-19 16:51:16 -05:00
expected = new_mail
2004-11-23 20:04:44 -05:00
expected . to = @recipient
2005-04-11 06:29:15 -04:00
expected . subject = " [Signed up] Welcome #{ @recipient } "
2004-11-23 20:04:44 -05:00
expected . body = " Hello there, \n \n Mr. #{ @recipient } "
expected . from = " system@loudthinking.com "
2009-08-07 21:37:21 -04:00
expected . date = Time . now
2004-11-23 20:04:44 -05:00
created = nil
assert_nothing_raised { created = TestMailer . create_signed_up ( @recipient ) }
assert_not_nil created
2009-11-20 05:19:10 -05:00
expected . message_id = '<123@456>'
created . message_id = '<123@456>'
2004-11-23 20:04:44 -05:00
assert_equal expected . encoded , created . encoded
assert_nothing_raised { TestMailer . deliver_signed_up ( @recipient ) }
2009-11-20 05:19:10 -05:00
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
2004-11-23 20:04:44 -05:00
end
2008-08-06 21:08:27 -04:00
2009-10-28 07:22:07 -04:00
def test_subject_with_i18n
assert_nothing_raised { TestMailer . deliver_subject_with_i18n ( @recipient ) }
2009-11-12 00:08:50 -05:00
assert_equal " Subject with i18n " , ActionMailer :: Base . deliveries . first . subject . decoded
2009-10-28 07:22:07 -04:00
I18n . backend . store_translations ( 'en' , :actionmailer = > { :subjects = > { :test_mailer = > { :subject_with_i18n = > " New Subject! " } } } )
assert_nothing_raised { TestMailer . deliver_subject_with_i18n ( @recipient ) }
2009-11-12 00:08:50 -05:00
assert_equal " New Subject! " , ActionMailer :: Base . deliveries . last . subject . decoded
2009-10-28 07:22:07 -04:00
end
2005-06-30 17:21:54 -04:00
def test_custom_template
expected = new_mail
expected . to = @recipient
expected . subject = " [Signed up] Welcome #{ @recipient } "
expected . body = " Hello there, \n \n Mr. #{ @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
2009-11-20 05:19:10 -05:00
expected . message_id = '<123@456>'
created . message_id = '<123@456>'
2005-06-30 17:21:54 -04:00
assert_equal expected . encoded , created . encoded
end
2007-06-07 19:42:47 -04:00
def test_custom_templating_extension
2008-12-03 14:58:47 -05:00
assert ActionView :: Template . template_handler_extensions . include? ( " haml " ) , " haml extension was not registered "
2007-06-07 19:42:47 -04:00
# 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 \n Mr. #{ @recipient } "
expected . from = " system@loudthinking.com "
expected . date = Time . local ( 2004 , 12 , 12 )
2008-08-06 21:08:27 -04:00
2007-06-07 19:42:47 -04:00
# Stub the render method so no alternative renderers need be present.
ActionView :: Base . any_instance . stubs ( :render ) . returns ( " Hello there, \n \n Mr. #{ @recipient } " )
2008-08-06 21:08:27 -04:00
2007-06-07 19:42:47 -04:00
# 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
2009-11-20 05:19:10 -05:00
assert_equal 'text/plain' , created . parts [ 0 ] . content_type . string
assert_equal 'text/html' , created . parts [ 1 ] . content_type . string
2007-06-07 19:42:47 -04:00
end
2004-11-23 20:04:44 -05:00
def test_cancelled_account
2009-11-20 05:19:10 -05:00
expected = new_mail
2004-11-23 20:04:44 -05:00
expected . to = @recipient
2005-04-11 06:29:15 -04:00
expected . subject = " [Cancelled] Goodbye #{ @recipient } "
2004-11-23 20:04:44 -05:00
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
2009-11-19 22:10:57 -05:00
expected . message_id = '<123@456>'
created . message_id = '<123@456>'
2004-11-23 20:04:44 -05:00
assert_equal expected . encoded , created . encoded
assert_nothing_raised { TestMailer . deliver_cancelled_account ( @recipient ) }
assert_not_nil ActionMailer :: Base . deliveries . first
2009-11-19 22:10:57 -05:00
delivered = ActionMailer :: Base . deliveries . first
expected . message_id = '<123@456>'
delivered . message_id = '<123@456>'
assert_equal expected . encoded , delivered . encoded
2004-11-23 20:04:44 -05:00
end
2008-08-06 21:08:27 -04:00
2005-01-13 08:05:51 -05:00
def test_cc_bcc
2009-11-20 05:19:10 -05:00
expected = new_mail
2005-01-13 08:05:51 -05:00
expected . to = @recipient
2005-04-11 06:29:15 -04:00
expected . subject = " testing bcc/cc "
2005-01-13 08:05:51 -05:00
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
2009-11-19 22:10:57 -05:00
expected . message_id = '<123@456>'
created . message_id = '<123@456>'
2005-01-13 08:05:51 -05:00
assert_equal expected . encoded , created . encoded
assert_nothing_raised do
TestMailer . deliver_cc_bcc @recipient
end
assert_not_nil ActionMailer :: Base . deliveries . first
2009-11-19 22:10:57 -05:00
delivered = ActionMailer :: Base . deliveries . first
expected . message_id = '<123@456>'
delivered . message_id = '<123@456>'
assert_equal expected . encoded , delivered . encoded
2005-01-13 08:05:51 -05:00
end
2008-05-23 13:40:36 -04:00
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
2009-11-20 05:19:10 -05:00
expected . message_id = '<123@456>'
created . message_id = '<123@456>'
2008-05-23 13:40:36 -04:00
assert_equal expected . encoded , created . encoded
assert_nothing_raised do
TestMailer . deliver_different_reply_to @recipient
end
2009-11-20 05:19:10 -05:00
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
2008-05-23 13:40:36 -04:00
end
2005-02-19 16:51:16 -05:00
def test_iso_charset
expected = new_mail ( " iso-8859-1 " )
expected . to = @recipient
2005-04-11 06:29:15 -04:00
expected . subject = encode " testing isø charsets " , " iso-8859-1 "
2005-02-19 16:51:16 -05:00
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
2009-11-20 05:19:10 -05:00
expected . message_id = '<123@456>'
created . message_id = '<123@456>'
2009-11-20 06:01:40 -05:00
2005-02-19 16:51:16 -05:00
assert_equal expected . encoded , created . encoded
assert_nothing_raised do
TestMailer . deliver_iso_charset @recipient
end
2009-11-20 05:19:10 -05:00
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
2005-02-19 16:51:16 -05:00
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
2009-11-20 06:01:40 -05:00
expected . message_id = '<123@456>'
created . message_id = '<123@456>'
2005-02-19 16:51:16 -05:00
assert_equal expected . encoded , created . encoded
assert_nothing_raised do
TestMailer . deliver_unencoded_subject @recipient
end
2009-11-20 06:01:40 -05:00
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
2005-02-19 16:51:16 -05:00
end
2004-11-23 20:04:44 -05:00
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
2008-08-06 21:08:27 -04:00
2007-01-21 18:20:44 -05:00
def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
ActionMailer :: Base . raise_delivery_errors = false
2009-10-28 05:17:59 -04:00
TestMailer . delivery_method . expects ( :perform_delivery ) . raises ( Exception )
2007-01-21 18:20:44 -05:00
assert_nothing_raised { TestMailer . deliver_signed_up ( @recipient ) }
end
2007-06-20 02:50:43 -04:00
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
2009-01-22 17:18:10 -05:00
class FakeLogger
attr_reader :info_contents , :debug_contents
def initialize
@info_contents , @debug_contents = " " , " "
end
2009-08-07 02:18:45 -04:00
def info ( str = nil , & blk )
@info_contents << str if str
@info_contents << blk . call if block_given?
2009-01-22 17:18:10 -05:00
end
2009-08-07 02:18:45 -04:00
def debug ( str = nil , & blk )
@debug_contents << str if str
@debug_contents << blk . call if block_given?
2009-01-22 17:18:10 -05:00
end
end
2007-01-21 18:20:44 -05:00
def test_delivery_logs_sent_mail
mail = TestMailer . create_signed_up ( @recipient )
2009-01-22 17:18:10 -05:00
# logger = mock()
# logger.expects(:info).with("Sent mail to #{@recipient}")
# logger.expects(:debug).with("\n#{mail.encoded}")
TestMailer . logger = FakeLogger . new
2007-01-21 18:20:44 -05:00
TestMailer . deliver_signed_up ( @recipient )
2009-01-22 17:18:10 -05:00
assert ( TestMailer . logger . info_contents =~ / Sent mail to #{ @recipient } / )
2009-11-20 05:19:10 -05:00
expected = TestMailer . logger . debug_contents
actual = " \n #{ mail . encoded } "
expected . gsub! ( / Message-ID:.* \ r \ n / , " Message-ID: <123@456> \r \n " )
actual . gsub! ( / Message-ID:.* \ r \ n / , " Message-ID: <123@456> \r \n " )
assert_equal ( expected , actual )
2007-01-21 18:20:44 -05:00
end
2005-01-13 08:05:51 -05:00
2005-05-06 06:39:00 -04:00
def test_unquote_quoted_printable_subject
2005-03-21 07:10:47 -05:00
msg = <<EOF
From : me @example . com
Subject : = ? utf - 8 ? Q? testing_testing_ = D6 = A4 ?=
Content - Type : text / plain ; charset = iso - 8859 - 1
2005-05-06 06:39:00 -04:00
The body
2005-03-21 07:10:47 -05:00
EOF
2009-11-19 22:10:57 -05:00
mail = Mail . new ( msg )
2009-11-12 00:08:50 -05:00
assert_equal " testing testing \326 \244 " , mail . subject . decoded
2009-11-21 08:04:36 -05:00
assert_equal " Subject: =?utf-8?Q?testing_testing_=D6=A4?= \r \n " , mail . subject . encoded
2005-05-06 06:39:00 -04:00
end
def test_unquote_7bit_subject
msg = <<EOF
From : me @example . com
Subject : this == working?
Content - Type : text / plain ; charset = iso - 8859 - 1
The body
EOF
2009-11-19 22:10:57 -05:00
mail = Mail . new ( msg )
2009-11-12 00:08:50 -05:00
assert_equal " this == working? " , mail . subject . decoded
2009-11-21 07:55:53 -05:00
assert_equal " Subject: this == working? \r \n " , mail . subject . encoded
2005-05-06 06:39:00 -04:00
end
def test_unquote_7bit_body
msg = <<EOF
From : me @example . com
Subject : subject
Content - Type : text / plain ; charset = iso - 8859 - 1
Content - Transfer - Encoding : 7 bit
The = 3 Dbody
EOF
2009-11-19 22:10:57 -05:00
mail = Mail . new ( msg )
2009-11-12 00:08:50 -05:00
assert_equal " The=3Dbody " , mail . body . decoded . strip
2009-11-21 07:55:53 -05:00
assert_equal " The=3Dbody " , mail . body . encoded . strip
2005-05-06 06:39:00 -04:00
end
def test_unquote_quoted_printable_body
msg = <<EOF
From : me @example . com
Subject : subject
Content - Type : text / plain ; charset = iso - 8859 - 1
Content - Transfer - Encoding : quoted - printable
The = 3 Dbody
EOF
2009-11-19 22:10:57 -05:00
mail = Mail . new ( msg )
2009-11-12 00:08:50 -05:00
assert_equal " The=body " , mail . body . decoded . strip
2009-11-21 07:55:53 -05:00
assert_equal " The=3Dbody " , mail . body . encoded . strip
2005-05-06 06:39:00 -04:00
end
def test_unquote_base64_body
msg = <<EOF
From : me @example . com
Subject : subject
Content - Type : text / plain ; charset = iso - 8859 - 1
Content - Transfer - Encoding : base64
VGhlIGJvZHk =
EOF
2009-11-19 22:10:57 -05:00
mail = Mail . new ( msg )
2009-11-12 00:08:50 -05:00
assert_equal " The body " , mail . body . decoded . strip
2009-11-21 07:55:53 -05:00
assert_equal " VGhlIGJvZHk= " , mail . body . encoded . strip
2005-03-21 07:10:47 -05:00
end
2005-04-11 06:29:15 -04:00
def test_extended_headers
@recipient = " Grytøyr <test@localhost> "
expected = new_mail " iso-8859-1 "
2005-05-29 12:36:22 -04:00
expected . to = quote_address_if_necessary @recipient , " iso-8859-1 "
2005-04-11 06:29:15 -04:00
expected . subject = " testing extended headers "
expected . body = " Nothing to see here. "
2005-05-29 12:36:22 -04:00
expected . from = quote_address_if_necessary " Grytøyr <stian1@example.net> " , " iso-8859-1 "
expected . cc = quote_address_if_necessary " Grytøyr <stian2@example.net> " , " iso-8859-1 "
expected . bcc = quote_address_if_necessary " Grytøyr <stian3@example.net> " , " iso-8859-1 "
2005-04-11 06:29:15 -04:00
expected . date = Time . local 2004 , 12 , 12
created = nil
assert_nothing_raised do
created = TestMailer . create_extended_headers @recipient
end
assert_not_nil created
2009-11-20 05:19:10 -05:00
expected . message_id = '<123@456>'
created . message_id = '<123@456>'
2005-04-11 06:29:15 -04:00
assert_equal expected . encoded , created . encoded
assert_nothing_raised do
TestMailer . deliver_extended_headers @recipient
end
2009-11-20 05:19:10 -05:00
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
2005-04-11 06:29:15 -04:00
end
2008-08-06 21:08:27 -04:00
2005-04-11 07:46:12 -04:00
def test_utf8_body_is_not_quoted
2005-04-13 13:49:24 -04:00
@recipient = " Foo áëô îü <extended@example.net> "
2005-04-11 07:46:12 -04:00
expected = new_mail " utf-8 "
2005-05-29 12:36:22 -04:00
expected . to = quote_address_if_necessary @recipient , " utf-8 "
2005-04-11 07:46:12 -04:00
expected . subject = " testing utf-8 body "
2005-04-13 13:49:24 -04:00
expected . body = " åœö blah "
2005-05-29 12:36:22 -04:00
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 "
2005-04-11 07:46:12 -04:00
expected . date = Time . local 2004 , 12 , 12
created = TestMailer . create_utf8_body @recipient
2005-04-13 13:49:24 -04:00
assert_match ( / åœö blah / , created . encoded )
2005-04-13 00:44:58 -04:00
end
def test_multiple_utf8_recipients
2005-04-13 13:49:24 -04:00
@recipient = [ " \" Foo áëô îü \" <extended@example.net> " , " \" Example Recipient \" <me@example.com> " ]
2005-04-13 00:44:58 -04:00
expected = new_mail " utf-8 "
2005-05-29 12:36:22 -04:00
expected . to = quote_address_if_necessary @recipient , " utf-8 "
2005-04-13 00:44:58 -04:00
expected . subject = " testing utf-8 body "
2005-04-13 13:49:24 -04:00
expected . body = " åœö blah "
2005-05-29 12:36:22 -04:00
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 "
2005-04-13 00:44:58 -04:00
expected . date = Time . local 2004 , 12 , 12
created = TestMailer . create_utf8_body @recipient
2005-04-13 13:49:24 -04:00
assert_match ( / \ nFrom: = \ ?utf-8 \ ?Q \ ?Foo_.*? \ ?= <extended@example.net> \ r / , created . encoded )
assert_match ( / \ nTo: = \ ?utf-8 \ ?Q \ ?Foo_.*? \ ?= <extended@example.net>, Example Recipient <me / , created . encoded )
2005-04-13 00:44:58 -04:00
end
2005-04-11 06:29:15 -04:00
2005-05-02 13:15:21 -04:00
def test_receive_decodes_base64_encoded_mail
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email " )
TestMailer . receive ( fixture )
2009-11-21 07:55:53 -05:00
assert_match ( / Jamis / , TestMailer . received_body . decoded )
2005-05-02 13:15:21 -04:00
end
2005-05-10 09:40:26 -04:00
def test_receive_attachments
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email2 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2005-05-10 09:40:26 -04:00
attachment = mail . attachments . last
assert_equal " smime.p7s " , attachment . original_filename
2009-11-21 07:55:53 -05:00
assert_equal " application/pkcs7-signature " , mail . parts . last . content_type . string
2005-05-10 09:40:26 -04:00
end
def test_decode_attachment_without_charset
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email3 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2005-05-10 09:40:26 -04:00
attachment = mail . attachments . last
assert_equal 1026 , attachment . read . length
end
2005-08-22 16:43:43 -04:00
def test_attachment_using_content_location
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email12 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2005-08-22 16:43:43 -04:00
assert_equal 1 , mail . attachments . length
assert_equal " Photo25.jpg " , mail . attachments . first . original_filename
end
2006-01-23 11:37:46 -05:00
def test_attachment_with_text_type
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email13 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2006-02-25 18:38:48 -05:00
assert mail . has_attachments?
2006-01-23 11:37:46 -05:00
assert_equal 1 , mail . attachments . length
assert_equal " hello.rb " , mail . attachments . first . original_filename
end
2005-05-10 11:30:18 -04:00
def test_decode_part_without_content_type
2005-05-10 09:40:26 -04:00
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email4 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2005-05-10 09:40:26 -04:00
assert_nothing_raised { mail . body }
end
2005-05-10 11:30:18 -04:00
def test_decode_message_without_content_type
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email5 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2005-05-10 11:30:18 -04:00
assert_nothing_raised { mail . body }
end
2005-05-30 19:09:50 -04:00
def test_decode_message_with_incorrect_charset
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email6 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2005-05-30 19:09:50 -04:00
assert_nothing_raised { mail . body }
end
2005-08-22 19:53:04 -04:00
def test_multipart_with_mime_version
mail = TestMailer . create_multipart_with_mime_version ( @recipient )
2009-11-20 05:19:10 -05:00
assert_equal " 1.1 " , mail . mime_version . version
2005-08-22 19:53:04 -04:00
end
2008-08-06 21:08:27 -04:00
2005-11-06 08:41:12 -05:00
def test_multipart_with_utf8_subject
mail = TestMailer . create_multipart_with_utf8_subject ( @recipient )
assert_match ( / \ nSubject: = \ ?utf-8 \ ?Q \ ?Foo_.*? \ ?= / , mail . encoded )
end
2005-08-22 19:53:04 -04:00
2006-03-18 19:18:50 -05:00
def test_implicitly_multipart_with_utf8
mail = TestMailer . create_implicitly_multipart_with_utf8
assert_match ( / \ nSubject: = \ ?utf-8 \ ?Q \ ?Foo_.*? \ ?= / , mail . encoded )
end
2005-05-29 12:36:22 -04:00
def test_explicitly_multipart_messages
mail = TestMailer . create_explicitly_multipart_example ( @recipient )
assert_equal 3 , mail . parts . length
2005-06-24 08:15:45 -04:00
assert_nil mail . content_type
2009-11-20 05:19:10 -05:00
assert_equal " text/plain " , mail . parts [ 0 ] . content_type . string
2005-05-29 12:36:22 -04:00
2009-11-20 05:19:10 -05:00
assert_equal " text/html " , mail . parts [ 1 ] . content_type . string
assert_equal " iso-8859-1 " , mail . parts [ 1 ] . charset
2005-05-29 12:36:22 -04:00
assert_equal " inline " , mail . parts [ 1 ] . content_disposition
2009-11-20 05:19:10 -05:00
assert_equal " image/jpeg " , mail . parts [ 2 ] . content_type . string
2005-05-29 12:36:22 -04:00
assert_equal " attachment " , mail . parts [ 2 ] . content_disposition
assert_equal " foo.jpg " , mail . parts [ 2 ] . sub_header ( " content-disposition " , " filename " )
assert_equal " foo.jpg " , mail . parts [ 2 ] . sub_header ( " content-type " , " name " )
2005-06-03 05:05:06 -04:00
assert_nil mail . parts [ 2 ] . sub_header ( " content-type " , " charset " )
2005-05-29 12:36:22 -04:00
end
2005-06-24 08:15:45 -04:00
def test_explicitly_multipart_with_content_type
2005-09-01 12:00:08 -04:00
mail = TestMailer . create_explicitly_multipart_example ( @recipient , " multipart/alternative " )
2005-06-24 08:15:45 -04:00
assert_equal 3 , mail . parts . length
2009-11-20 05:19:10 -05:00
assert_equal " multipart/alternative " , mail . content_type . string
2005-06-24 08:15:45 -04:00
end
def test_explicitly_multipart_with_invalid_content_type
mail = TestMailer . create_explicitly_multipart_example ( @recipient , " text/xml " )
assert_equal 3 , mail . parts . length
assert_nil mail . content_type
end
2005-05-29 12:36:22 -04:00
def test_implicitly_multipart_messages
2008-12-03 14:58:47 -05:00
assert ActionView :: Template . template_handler_extensions . include? ( " bak " ) , " bak extension was not registered "
2005-05-29 12:36:22 -04:00
mail = TestMailer . create_implicitly_multipart_example ( @recipient )
2008-02-07 18:10:01 -05:00
assert_equal 3 , mail . parts . length
2009-11-19 22:10:57 -05:00
assert_equal " 1.0 " , mail . mime_version . decoded
2009-11-20 05:19:10 -05:00
assert_equal " multipart/alternative " , mail . content_type . string
assert_equal " application/x-yaml " , mail . parts [ 0 ] . content_type . string
assert_equal " utf-8 " , mail . parts [ 0 ] . charset
assert_equal " text/plain " , mail . parts [ 1 ] . content_type . string
assert_equal " utf-8 " , mail . parts [ 1 ] . charset
assert_equal " text/html " , mail . parts [ 2 ] . content_type . string
assert_equal " utf-8 " , mail . parts [ 2 ] . charset
2005-07-01 16:43:40 -04:00
end
def test_implicitly_multipart_messages_with_custom_order
2008-12-03 14:58:47 -05:00
assert ActionView :: Template . template_handler_extensions . include? ( " bak " ) , " bak extension was not registered "
2009-04-22 20:16:28 -04:00
mail = TestMailer . create_implicitly_multipart_example ( @recipient , nil , [ " application/x-yaml " , " text/plain " ] )
2008-02-07 18:10:01 -05:00
assert_equal 3 , mail . parts . length
2009-11-20 05:19:10 -05:00
assert_equal " text/html " , mail . parts [ 0 ] . content_type . string
assert_equal " text/plain " , mail . parts [ 1 ] . content_type . string
assert_equal " application/x-yaml " , mail . parts [ 2 ] . content_type . string
2005-05-29 12:36:22 -04:00
end
2005-09-01 12:00:08 -04:00
def test_implicitly_multipart_messages_with_charset
mail = TestMailer . create_implicitly_multipart_example ( @recipient , 'iso-8859-1' )
2009-11-19 22:10:57 -05:00
assert_equal " multipart/alternative " , mail . header [ 'content-type' ] . content_type
2008-08-06 21:08:27 -04:00
2009-11-19 22:10:57 -05:00
assert_equal 'iso-8859-1' , mail . parts [ 0 ] . content_type . parameters [ :charset ]
assert_equal 'iso-8859-1' , mail . parts [ 1 ] . content_type . parameters [ :charset ]
assert_equal 'iso-8859-1' , mail . parts [ 2 ] . content_type . parameters [ :charset ]
2005-09-01 12:00:08 -04:00
end
2005-06-03 06:57:06 -04:00
def test_html_mail
mail = TestMailer . create_html_mail ( @recipient )
2009-11-20 05:19:10 -05:00
assert_equal " text/html " , mail . content_type . string
2005-06-03 06:57:06 -04:00
end
2005-09-01 10:45:10 -04:00
def test_html_mail_with_underscores
mail = TestMailer . create_html_mail_with_underscores ( @recipient )
2009-11-12 00:08:50 -05:00
assert_equal %{ <a href="http://google.com" target="_blank">_Google</a> } , mail . body . decoded
2005-09-01 10:45:10 -04:00
end
2005-06-30 17:40:43 -04:00
def test_various_newlines
mail = TestMailer . create_various_newlines ( @recipient )
assert_equal ( " line # 1 \n line # 2 \n line # 3 \n line # 4 \n \n " +
2009-11-12 00:08:50 -05:00
" line # 5 \n \n line # 6 \n \n line # 7 " , mail . body . decoded )
2005-06-30 17:40:43 -04:00
end
2005-07-06 05:53:34 -04:00
def test_various_newlines_multipart
mail = TestMailer . create_various_newlines_multipart ( @recipient )
2009-11-12 00:08:50 -05:00
assert_equal " line # 1 \n line # 2 \n line # 3 \n line # 4 \n \n " , mail . parts [ 0 ] . body . decoded
assert_equal " <p>line # 1</p> \n <p>line # 2</p> \n <p>line # 3</p> \n <p>line # 4</p> \n \n " , mail . parts [ 1 ] . body . decoded
2009-11-20 05:19:10 -05:00
assert_equal " line # 1 \r \n line # 2 \r \n line # 3 \r \n line # 4 \r \n \r \n " , mail . parts [ 0 ] . body . encoded
assert_equal " <p>line # 1</p> \r \n <p>line # 2</p> \r \n <p>line # 3</p> \r \n <p>line # 4</p> \r \n \r \n " , mail . parts [ 1 ] . body . encoded
2005-07-06 05:53:34 -04:00
end
2008-08-06 21:08:27 -04:00
2005-06-06 05:32:52 -04:00
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
2005-06-06 12:06:26 -04:00
2009-08-07 23:56:54 -04:00
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 )
2009-08-09 11:00:53 -04:00
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' ) )
2009-08-07 23:56:54 -04:00
end
2005-06-06 12:06:26 -04:00
def test_recursive_multipart_processing
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email7 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2009-11-12 00:08:50 -05:00
assert_equal " This is the first part. \n \n Attachment: test.rb \n Attachment: test.pdf \n \n \n Attachment: smime.p7s \n " , mail . body . decoded
2005-06-06 12:06:26 -04:00
end
2005-06-06 12:12:38 -04:00
def test_decode_encoded_attachment_filename
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email8 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2005-06-06 12:12:38 -04:00
attachment = mail . attachments . last
2008-04-03 13:28:05 -04:00
expected = " 01 Quien Te Dij \212 at. Pitbull.mp3 "
expected . force_encoding ( Encoding :: ASCII_8BIT ) if expected . respond_to? ( :force_encoding )
assert_equal expected , attachment . original_filename
2005-06-06 12:12:38 -04:00
end
2005-06-06 12:47:20 -04:00
2005-06-08 06:18:26 -04:00
def test_decode_message_with_unknown_charset
fixture = File . read ( File . dirname ( __FILE__ ) + " /fixtures/raw_email10 " )
2009-11-19 22:10:57 -05:00
mail = Mail . new ( fixture )
2005-06-08 06:18:26 -04:00
assert_nothing_raised { mail . body }
end
2005-06-22 05:59:14 -04:00
2005-07-04 12:11:22 -04:00
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
2005-07-04 12:21:11 -04:00
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_addrs . to_s )
assert_match ( / : / , mail . cc_addrs . to_s )
assert_match ( / : / , mail . bcc_addrs . to_s )
end
2005-07-31 04:26:32 -04:00
def test_deliver_with_mail_object
2006-01-28 21:25:15 -05:00
mail = TestMailer . create_headers_with_nonalpha_chars ( @recipient )
2005-07-31 04:26:32 -04:00
assert_nothing_raised { TestMailer . deliver ( mail ) }
assert_equal 1 , TestMailer . deliveries . length
end
2006-01-28 21:25:15 -05:00
def test_multipart_with_template_path_with_dots
mail = FunkyPathMailer . create_multipart_with_template_path_with_dots ( @recipient )
assert_equal 2 , mail . parts . length
2009-11-20 05:19:10 -05:00
assert " text/plain " , mail . parts [ 1 ] . content_type . string
2009-11-12 00:08:50 -05:00
assert " utf-8 " , mail . parts [ 1 ] . content_type [ 'charset' ]
2006-01-28 21:25:15 -05:00
end
2006-03-18 18:53:07 -05:00
def test_custom_content_type_attributes
mail = TestMailer . create_custom_content_type_attributes
2009-11-20 05:19:10 -05:00
assert_match %r{ format="flowed" } , mail . content_type . encoded
assert_match %r{ charset="utf-8" } , mail . content_type . encoded
2006-03-18 18:53:07 -05:00
end
2007-09-22 13:32:39 -04:00
def test_return_path_with_create
mail = TestMailer . create_return_path
2009-11-12 00:08:50 -05:00
assert_equal " another@somewhere.test " , mail [ 'return-path' ] . decoded . to_s
2007-09-22 13:32:39 -04:00
end
def test_return_path_with_deliver
ActionMailer :: Base . delivery_method = :smtp
TestMailer . deliver_return_path
assert_match %r{ ^Return-Path: <another@somewhere.test> } , MockSMTP . deliveries [ 0 ] [ 0 ]
2009-02-03 14:58:20 -05:00
assert_equal " another@somewhere.test " , MockSMTP . deliveries [ 0 ] [ 1 ] . to_s
2007-09-22 13:32:39 -04:00
end
2008-09-28 13:31:45 -04:00
def test_body_is_stored_as_an_ivar
2009-10-21 19:05:55 -04:00
mail = TestMailer . create_body_ivar ( @recipient )
2009-11-20 06:01:40 -05:00
assert_equal " body: foo \n bar: baz " , mail . body . decoded
2008-09-28 13:31:45 -04:00
end
2008-11-05 22:54:37 -05:00
def test_starttls_is_enabled_if_supported
2009-01-11 05:02:54 -05:00
ActionMailer :: Base . smtp_settings [ :enable_starttls_auto ] = true
2008-11-05 22:54:37 -05:00
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
2009-01-11 05:02:54 -05:00
ActionMailer :: Base . smtp_settings [ :enable_starttls_auto ] = true
2008-11-05 22:54:37 -05:00
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
2009-01-11 05:02:54 -05:00
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
2004-11-23 20:04:44 -05:00
end
2005-01-13 08:05:51 -05:00
2006-06-30 01:27:05 -04:00
class InheritableTemplateRootTest < Test :: Unit :: TestCase
def test_attr
2009-04-27 15:34:25 -04:00
expected = File . expand_path ( " #{ File . dirname ( __FILE__ ) } /fixtures/path.with.dots " )
2008-12-09 14:39:45 -05:00
assert_equal expected , FunkyPathMailer . template_root . to_s
2006-06-30 01:27:05 -04:00
sub = Class . new ( FunkyPathMailer )
sub . template_root = 'test/path'
2009-04-27 15:34:25 -04:00
assert_equal File . expand_path ( 'test/path' ) , sub . template_root . to_s
2008-12-09 14:39:45 -05:00
assert_equal expected , FunkyPathMailer . template_root . to_s
2006-06-30 01:27:05 -04:00
end
end
2006-11-13 00:03:48 -05:00
class MethodNamingTest < Test :: Unit :: TestCase
class TestMailer < ActionMailer :: Base
def send
2009-10-21 19:26:10 -04:00
render :text = > 'foo'
2006-11-13 00:03:48 -05:00
end
end
def setup
2007-11-07 11:05:17 -05:00
set_delivery_method :test
2006-11-13 00:03:48 -05:00
ActionMailer :: Base . perform_deliveries = true
ActionMailer :: Base . deliveries = [ ]
end
2007-11-07 11:05:17 -05:00
def teardown
restore_delivery_method
end
2006-11-13 00:03:48 -05:00
def test_send_method
assert_nothing_raised do
assert_emails 1 do
TestMailer . deliver_send
end
end
end
end
2008-08-29 16:08:16 -04:00
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
2008-11-11 10:39:50 -05:00
2008-11-11 10:45:53 -05:00
def test_should_not_respond_to_method_where_deliver_is_not_a_suffix
assert ! RespondToMailer . respond_to? ( :foo_deliver_template )
end
2008-11-11 10:39:50 -05:00
def test_should_still_raise_exception_with_expected_message_when_calling_an_undefined_method
2009-03-08 16:11:58 -04:00
error = assert_raise NoMethodError do
2008-11-11 10:39:50 -05:00
RespondToMailer . not_a_method
end
assert_match / undefined method.*not_a_method / , error . message
end
2008-08-29 16:08:16 -04:00
end