2 require 'abstract_unit'
4 class FunkyPathMailer
< ActionMailer
::Base
5 self.template_root
= "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
7 def multipart_with_template_path_with_dots(recipient
)
9 subject
"Have a lovely picture"
10 from
"Chad Fowler <chad@chadfowler.com>"
11 attachment
:content_type => "image/jpeg",
12 :body => "not really a jpeg, we're only testing, after all"
16 class TestMailer
< ActionMailer
::Base
17 def signed_up(recipient
)
18 @recipients = recipient
19 @subject = "[Signed up] Welcome #{recipient}"
20 @from = "system@loudthinking.com"
21 @sent_on = Time
.local(2004, 12, 12)
22 @body["recipient"] = recipient
25 def cancelled_account(recipient
)
26 self.recipients
= recipient
27 self.subject
= "[Cancelled] Goodbye #{recipient}"
28 self.from
= "system@loudthinking.com"
29 self.sent_on
= Time
.local(2004, 12, 12)
30 self.body
= "Goodbye, Mr. #{recipient}"
35 subject
"testing bcc/cc"
36 from
"system@loudthinking.com"
37 sent_on Time
.local(2004, 12, 12)
38 cc
"nobody@loudthinking.com"
39 bcc
"root@loudthinking.com"
40 body
"Nothing to see here."
43 def different_reply_to(recipient
)
45 subject
"testing reply_to"
46 from
"system@loudthinking.com"
47 sent_on Time
.local(2008, 5, 23)
48 reply_to
"atraver@gmail.com"
49 body
"Nothing to see here."
52 def iso_charset(recipient
)
53 @recipients = recipient
54 @subject = "testing isø charsets"
55 @from = "system@loudthinking.com"
56 @sent_on = Time
.local
2004, 12, 12
57 @cc = "nobody@loudthinking.com"
58 @bcc = "root@loudthinking.com"
59 @body = "Nothing to see here."
60 @charset = "iso-8859-1"
63 def unencoded_subject(recipient
)
64 @recipients = recipient
65 @subject = "testing unencoded subject"
66 @from = "system@loudthinking.com"
67 @sent_on = Time
.local
2004, 12, 12
68 @cc = "nobody@loudthinking.com"
69 @bcc = "root@loudthinking.com"
70 @body = "Nothing to see here."
73 def extended_headers(recipient
)
74 @recipients = recipient
75 @subject = "testing extended headers"
76 @from = "Grytøyr <stian1@example.net>"
77 @sent_on = Time
.local
2004, 12, 12
78 @cc = "Grytøyr <stian2@example.net>"
79 @bcc = "Grytøyr <stian3@example.net>"
80 @body = "Nothing to see here."
81 @charset = "iso-8859-1"
84 def utf8_body(recipient
)
85 @recipients = recipient
86 @subject = "testing utf-8 body"
87 @from = "Foo áëô îü <extended@example.net>"
88 @sent_on = Time
.local
2004, 12, 12
89 @cc = "Foo áëô îü <extended@example.net>"
90 @bcc = "Foo áëô îü <extended@example.net>"
95 def multipart_with_mime_version(recipient
)
97 subject
"multipart with mime_version"
98 from
"test@example.com"
99 sent_on Time
.local(2004, 12, 12)
101 content_type
"multipart/alternative"
103 part
"text/plain" do |p
|
107 part
"text/html" do |p
|
108 p
.body
= "<b>blah</b>"
112 def multipart_with_utf8_subject(recipient
)
115 from
"test@example.com"
118 part
"text/plain" do |p
|
122 part
"text/html" do |p
|
123 p
.body
= "<b>blah</b>"
127 def explicitly_multipart_example(recipient
, ct
=nil)
129 subject
"multipart example"
130 from
"test@example.com"
131 sent_on Time
.local(2004, 12, 12)
132 body
"plain text default"
133 content_type ct
if ct
135 part
"text/html" do |p
|
136 p
.charset
= "iso-8859-1"
140 attachment
:content_type => "image/jpeg", :filename => "foo.jpg",
144 def implicitly_multipart_example(recipient
, cs
= nil, order
= nil)
145 @recipients = recipient
146 @subject = "multipart example"
147 @from = "test@example.com"
148 @sent_on = Time
.local
2004, 12, 12
149 @body = { "recipient" => recipient
}
151 @implicit_parts_order = order
if order
154 def implicitly_multipart_with_utf8
155 recipients
"no.one@nowhere.test"
157 from
"some.one@somewhere.test"
158 template
"implicitly_multipart_example"
159 body ({ "recipient" => "no.one@nowhere.test" })
162 def html_mail(recipient
)
165 from
"test@example.com"
166 body
"<em>Emphasize</em> <strong>this</strong>"
167 content_type
"text/html"
170 def html_mail_with_underscores(recipient
)
171 subject
"html mail with underscores"
172 body %
{<a href
="http://google.com" target
="_blank">_Google
</a
>}
175 def custom_template(recipient
)
177 subject
"[Signed up] Welcome #{recipient}"
178 from
"system@loudthinking.com"
179 sent_on Time
.local(2004, 12, 12)
182 body
["recipient"] = recipient
185 def custom_templating_extension(recipient
)
187 subject
"[Signed up] Welcome #{recipient}"
188 from
"system@loudthinking.com"
189 sent_on Time
.local(2004, 12, 12)
191 body
["recipient"] = recipient
194 def various_newlines(recipient
)
196 subject
"various newlines"
197 from
"test@example.com"
198 body
"line #1\nline #2\rline #3\r\nline #4\r\r" +
199 "line #5\n\nline#6\r\n\r\nline #7"
202 def various_newlines_multipart(recipient
)
204 subject
"various newlines multipart"
205 from
"test@example.com"
206 content_type
"multipart/alternative"
207 part
:content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r"
208 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"
211 def nested_multipart(recipient
)
213 subject
"nested multipart"
214 from
"test@example.com"
215 content_type
"multipart/mixed"
216 part
:content_type => "multipart/alternative", :content_disposition => "inline", :headers => { "foo" => "bar" } do |p
|
217 p
.part
:content_type => "text/plain", :body => "test text\nline #2"
218 p
.part
:content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2"
220 attachment
:content_type => "application/octet-stream",:filename => "test.txt", :body => "test abcdefghijklmnopqstuvwxyz"
223 def nested_multipart_with_body(recipient
)
225 subject
"nested multipart with body"
226 from
"test@example.com"
227 content_type
"multipart/mixed"
228 part
:content_type => "multipart/alternative", :content_disposition => "inline", :body => "Nothing to see here." do |p
|
229 p
.part
:content_type => "text/html", :body => "<b>test</b> HTML<br/>"
233 def attachment_with_custom_header(recipient
)
235 subject
"custom header in attachment"
236 from
"test@example.com"
237 content_type
"multipart/related"
238 part
:content_type => "text/html", :body => 'yo'
239 attachment
:content_type => "image/jpeg",:filename => "test.jpeg", :body => "i am not a real picture", :headers => { 'Content-ID' => '<test@test.com>' }
242 def unnamed_attachment(recipient
)
244 subject
"nested multipart"
245 from
"test@example.com"
246 content_type
"multipart/mixed"
247 part
:content_type => "text/plain", :body => "hullo"
248 attachment
:content_type => "application/octet-stream", :body => "test abcdefghijklmnopqstuvwxyz"
251 def headers_with_nonalpha_chars(recipient
)
253 subject
"nonalpha chars"
254 from
"One: Two <test@example.com>"
255 cc
"Three: Four <test@example.com>"
256 bcc
"Five: Six <test@example.com>"
260 def custom_content_type_attributes
261 recipients
"no.one@nowhere.test"
262 subject
"custom content types"
263 from
"some.one@somewhere.test"
264 content_type
"text/plain; format=flowed"
269 recipients
"no.one@nowhere.test"
270 subject
"return path test"
271 from
"some.one@somewhere.test"
273 headers
"return-path" => "another@somewhere.test"
276 def body_ivar(recipient
)
278 subject
"Body as a local variable"
279 from
"test@example.com"
280 body
:body => "foo", :bar => "baz"
284 attr_accessor :received_body
288 self.class.received_body
= mail
.body
292 class ActionMailerTest
< Test
::Unit::TestCase
293 include ActionMailer
::Quoting
295 def encode( text
, charset
="utf-8" )
296 quoted_printable( text
, charset
)
299 def new_mail( charset
="utf-8" )
300 mail
= TMail
::Mail.new
301 mail
.mime_version
= "1.0"
303 mail
.set_content_type
"text", "plain", { "charset" => charset
}
308 # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
310 set_delivery_method
:test
311 ActionMailer
::Base.perform_deliveries
= true
312 ActionMailer
::Base.raise_delivery_errors
= true
313 ActionMailer
::Base.deliveries
= []
315 @original_logger = TestMailer
.logger
316 @recipient = 'test@localhost'
320 TestMailer
.logger
= @original_logger
321 restore_delivery_method
324 def test_nested_parts
326 assert_nothing_raised
{ created
= TestMailer
.create_nested_multipart(@recipient)}
327 assert_equal
2,created
.parts
.size
328 assert_equal
2,created
.parts
.first
.parts
.size
330 assert_equal
"multipart/mixed", created
.content_type
331 assert_equal
"multipart/alternative", created
.parts
.first
.content_type
332 assert_equal
"bar", created
.parts
.first
.header
['foo'].to_s
333 assert_nil created
.parts
.first
.charset
334 assert_equal
"text/plain", created
.parts
.first
.parts
.first
.content_type
335 assert_equal
"text/html", created
.parts
.first
.parts
[1].content_type
336 assert_equal
"application/octet-stream", created
.parts
[1].content_type
339 def test_nested_parts_with_body
341 assert_nothing_raised
{ created
= TestMailer
.create_nested_multipart_with_body(@recipient)}
342 assert_equal
1,created
.parts
.size
343 assert_equal
2,created
.parts
.first
.parts
.size
345 assert_equal
"multipart/mixed", created
.content_type
346 assert_equal
"multipart/alternative", created
.parts
.first
.content_type
347 assert_equal
"Nothing to see here.", created
.parts
.first
.parts
.first
.body
348 assert_equal
"text/plain", created
.parts
.first
.parts
.first
.content_type
349 assert_equal
"text/html", created
.parts
.first
.parts
[1].content_type
352 def test_attachment_with_custom_header
354 assert_nothing_raised
{ created
= TestMailer
.create_attachment_with_custom_header(@recipient)}
355 assert_equal
"<test@test.com>", created
.parts
[1].header
['content-id'].to_s
360 expected
.to
= @recipient
361 expected
.subject
= "[Signed up] Welcome #{@recipient}"
362 expected
.body
= "Hello there, \n\nMr. #{@recipient}"
363 expected
.from
= "system@loudthinking.com"
364 expected
.date
= Time
.local(2004, 12, 12)
367 assert_nothing_raised
{ created
= TestMailer
.create_signed_up(@recipient) }
368 assert_not_nil created
369 assert_equal expected
.encoded
, created
.encoded
371 assert_nothing_raised
{ TestMailer
.deliver_signed_up(@recipient) }
372 assert_not_nil ActionMailer
::Base.deliveries
.first
373 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
376 def test_custom_template
378 expected
.to
= @recipient
379 expected
.subject
= "[Signed up] Welcome #{@recipient}"
380 expected
.body
= "Hello there, \n\nMr. #{@recipient}"
381 expected
.from
= "system@loudthinking.com"
382 expected
.date
= Time
.local(2004, 12, 12)
385 assert_nothing_raised
{ created
= TestMailer
.create_custom_template(@recipient) }
386 assert_not_nil created
387 assert_equal expected
.encoded
, created
.encoded
390 def test_custom_templating_extension
391 assert ActionView
::Template.template_handler_extensions
.include?("haml"), "haml extension was not registered"
393 # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
395 expected
.to
= @recipient
396 expected
.subject
= "[Signed up] Welcome #{@recipient}"
397 expected
.body
= "Hello there, \n\nMr. #{@recipient}"
398 expected
.from
= "system@loudthinking.com"
399 expected
.date
= Time
.local(2004, 12, 12)
401 # Stub the render method so no alternative renderers need be present.
402 ActionView
::Base.any_instance
.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
404 # Now that the template is registered, there should be one part. The text/plain part.
406 assert_nothing_raised
{ created
= TestMailer
.create_custom_templating_extension(@recipient) }
407 assert_not_nil created
408 assert_equal
2, created
.parts
.length
409 assert_equal
'text/plain', created
.parts
[0].content_type
410 assert_equal
'text/html', created
.parts
[1].content_type
413 def test_cancelled_account
415 expected
.to
= @recipient
416 expected
.subject
= "[Cancelled] Goodbye #{@recipient}"
417 expected
.body
= "Goodbye, Mr. #{@recipient}"
418 expected
.from
= "system@loudthinking.com"
419 expected
.date
= Time
.local(2004, 12, 12)
422 assert_nothing_raised
{ created
= TestMailer
.create_cancelled_account(@recipient) }
423 assert_not_nil created
424 assert_equal expected
.encoded
, created
.encoded
426 assert_nothing_raised
{ TestMailer
.deliver_cancelled_account(@recipient) }
427 assert_not_nil ActionMailer
::Base.deliveries
.first
428 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
433 expected
.to
= @recipient
434 expected
.subject
= "testing bcc/cc"
435 expected
.body
= "Nothing to see here."
436 expected
.from
= "system@loudthinking.com"
437 expected
.cc
= "nobody@loudthinking.com"
438 expected
.bcc
= "root@loudthinking.com"
439 expected
.date
= Time
.local
2004, 12, 12
442 assert_nothing_raised
do
443 created
= TestMailer
.create_cc_bcc
@recipient
445 assert_not_nil created
446 assert_equal expected
.encoded
, created
.encoded
448 assert_nothing_raised
do
449 TestMailer
.deliver_cc_bcc
@recipient
452 assert_not_nil ActionMailer
::Base.deliveries
.first
453 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
459 expected
.to
= @recipient
460 expected
.subject
= "testing reply_to"
461 expected
.body
= "Nothing to see here."
462 expected
.from
= "system@loudthinking.com"
463 expected
.reply_to
= "atraver@gmail.com"
464 expected
.date
= Time
.local
2008, 5, 23
467 assert_nothing_raised
do
468 created
= TestMailer
.create_different_reply_to
@recipient
470 assert_not_nil created
471 assert_equal expected
.encoded
, created
.encoded
473 assert_nothing_raised
do
474 TestMailer
.deliver_different_reply_to
@recipient
477 assert_not_nil ActionMailer
::Base.deliveries
.first
478 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
482 expected
= new_mail( "iso-8859-1" )
483 expected
.to
= @recipient
484 expected
.subject
= encode
"testing isø charsets", "iso-8859-1"
485 expected
.body
= "Nothing to see here."
486 expected
.from
= "system@loudthinking.com"
487 expected
.cc
= "nobody@loudthinking.com"
488 expected
.bcc
= "root@loudthinking.com"
489 expected
.date
= Time
.local
2004, 12, 12
492 assert_nothing_raised
do
493 created
= TestMailer
.create_iso_charset
@recipient
495 assert_not_nil created
496 assert_equal expected
.encoded
, created
.encoded
498 assert_nothing_raised
do
499 TestMailer
.deliver_iso_charset
@recipient
502 assert_not_nil ActionMailer
::Base.deliveries
.first
503 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
506 def test_unencoded_subject
508 expected
.to
= @recipient
509 expected
.subject
= "testing unencoded subject"
510 expected
.body
= "Nothing to see here."
511 expected
.from
= "system@loudthinking.com"
512 expected
.cc
= "nobody@loudthinking.com"
513 expected
.bcc
= "root@loudthinking.com"
514 expected
.date
= Time
.local
2004, 12, 12
517 assert_nothing_raised
do
518 created
= TestMailer
.create_unencoded_subject
@recipient
520 assert_not_nil created
521 assert_equal expected
.encoded
, created
.encoded
523 assert_nothing_raised
do
524 TestMailer
.deliver_unencoded_subject
@recipient
527 assert_not_nil ActionMailer
::Base.deliveries
.first
528 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
531 def test_instances_are_nil
532 assert_nil ActionMailer
::Base.new
533 assert_nil TestMailer
.new
536 def test_deliveries_array
537 assert_not_nil ActionMailer
::Base.deliveries
538 assert_equal
0, ActionMailer
::Base.deliveries
.size
539 TestMailer
.deliver_signed_up(@recipient)
540 assert_equal
1, ActionMailer
::Base.deliveries
.size
541 assert_not_nil ActionMailer
::Base.deliveries
.first
544 def test_perform_deliveries_flag
545 ActionMailer
::Base.perform_deliveries
= false
546 TestMailer
.deliver_signed_up(@recipient)
547 assert_equal
0, ActionMailer
::Base.deliveries
.size
548 ActionMailer
::Base.perform_deliveries
= true
549 TestMailer
.deliver_signed_up(@recipient)
550 assert_equal
1, ActionMailer
::Base.deliveries
.size
553 def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
554 ActionMailer
::Base.raise_delivery_errors
= false
555 TestMailer
.any_instance
.expects(:perform_delivery_test).raises(Exception
)
556 assert_nothing_raised
{ TestMailer
.deliver_signed_up(@recipient) }
559 def test_performs_delivery_via_sendmail
561 sm
.expects(:print).with(anything
)
563 IO
.expects(:popen).once
.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm
)
564 ActionMailer
::Base.delivery_method
= :sendmail
565 TestMailer
.deliver_signed_up(@recipient)
568 def test_delivery_logs_sent_mail
569 mail
= TestMailer
.create_signed_up(@recipient)
571 logger
.expects(:info).with("Sent mail to #{@recipient}")
572 logger
.expects(:debug).with("\n#{mail.encoded}")
573 TestMailer
.logger
= logger
574 TestMailer
.deliver_signed_up(@recipient)
577 def test_unquote_quoted_printable_subject
580 Subject: =?utf-8?Q?testing_testing_=D6=A4?=
581 Content-Type: text/plain; charset=iso-8859-1
585 mail
= TMail
::Mail.parse(msg
)
586 assert_equal
"testing testing \326\244", mail
.subject
587 assert_equal
"=?utf-8?Q?testing_testing_=D6=A4?=", mail
.quoted_subject
590 def test_unquote_7bit_subject
593 Subject: this == working?
594 Content-Type: text/plain; charset=iso-8859-1
598 mail
= TMail
::Mail.parse(msg
)
599 assert_equal
"this == working?", mail
.subject
600 assert_equal
"this == working?", mail
.quoted_subject
603 def test_unquote_7bit_body
607 Content-Type: text/plain; charset=iso-8859-1
608 Content-Transfer-Encoding: 7bit
612 mail
= TMail
::Mail.parse(msg
)
613 assert_equal
"The=3Dbody", mail
.body
.strip
614 assert_equal
"The=3Dbody", mail
.quoted_body
.strip
617 def test_unquote_quoted_printable_body
621 Content-Type: text/plain; charset=iso-8859-1
622 Content-Transfer-Encoding: quoted-printable
626 mail
= TMail
::Mail.parse(msg
)
627 assert_equal
"The=body", mail
.body
.strip
628 assert_equal
"The=3Dbody", mail
.quoted_body
.strip
631 def test_unquote_base64_body
635 Content-Type: text/plain; charset=iso-8859-1
636 Content-Transfer-Encoding: base64
640 mail
= TMail
::Mail.parse(msg
)
641 assert_equal
"The body", mail
.body
.strip
642 assert_equal
"VGhlIGJvZHk=", mail
.quoted_body
.strip
645 def test_extended_headers
646 @recipient = "Grytøyr <test@localhost>"
648 expected
= new_mail
"iso-8859-1"
649 expected
.to
= quote_address_if_necessary
@recipient, "iso-8859-1"
650 expected
.subject
= "testing extended headers"
651 expected
.body
= "Nothing to see here."
652 expected
.from
= quote_address_if_necessary
"Grytøyr <stian1@example.net>", "iso-8859-1"
653 expected
.cc
= quote_address_if_necessary
"Grytøyr <stian2@example.net>", "iso-8859-1"
654 expected
.bcc
= quote_address_if_necessary
"Grytøyr <stian3@example.net>", "iso-8859-1"
655 expected
.date
= Time
.local
2004, 12, 12
658 assert_nothing_raised
do
659 created
= TestMailer
.create_extended_headers
@recipient
662 assert_not_nil created
663 assert_equal expected
.encoded
, created
.encoded
665 assert_nothing_raised
do
666 TestMailer
.deliver_extended_headers
@recipient
669 assert_not_nil ActionMailer
::Base.deliveries
.first
670 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
673 def test_utf8_body_is_not_quoted
674 @recipient = "Foo áëô îü <extended@example.net>"
675 expected
= new_mail
"utf-8"
676 expected
.to
= quote_address_if_necessary
@recipient, "utf-8"
677 expected
.subject
= "testing utf-8 body"
678 expected
.body
= "åœö blah"
679 expected
.from
= quote_address_if_necessary
@recipient, "utf-8"
680 expected
.cc
= quote_address_if_necessary
@recipient, "utf-8"
681 expected
.bcc
= quote_address_if_necessary
@recipient, "utf-8"
682 expected
.date
= Time
.local
2004, 12, 12
684 created
= TestMailer
.create_utf8_body
@recipient
685 assert_match(/åœö blah/, created
.encoded
)
688 def test_multiple_utf8_recipients
689 @recipient = ["\"Foo áëô îü\" <extended@example.net>", "\"Example Recipient\" <me@example.com>"]
690 expected
= new_mail
"utf-8"
691 expected
.to
= quote_address_if_necessary
@recipient, "utf-8"
692 expected
.subject
= "testing utf-8 body"
693 expected
.body
= "åœö blah"
694 expected
.from
= quote_address_if_necessary
@recipient.first
, "utf-8"
695 expected
.cc
= quote_address_if_necessary
@recipient, "utf-8"
696 expected
.bcc
= quote_address_if_necessary
@recipient, "utf-8"
697 expected
.date
= Time
.local
2004, 12, 12
699 created
= TestMailer
.create_utf8_body
@recipient
700 assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>\r/, created
.encoded
)
701 assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>, Example Recipient <me/, created
.encoded
)
704 def test_receive_decodes_base64_encoded_mail
705 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email")
706 TestMailer
.receive(fixture
)
707 assert_match(/Jamis/, TestMailer
.received_body
)
710 def test_receive_attachments
711 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email2")
712 mail
= TMail
::Mail.parse(fixture
)
713 attachment
= mail
.attachments
.last
714 assert_equal
"smime.p7s", attachment
.original_filename
715 assert_equal
"application/pkcs7-signature", attachment
.content_type
718 def test_decode_attachment_without_charset
719 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email3")
720 mail
= TMail
::Mail.parse(fixture
)
721 attachment
= mail
.attachments
.last
722 assert_equal
1026, attachment
.read
.length
725 def test_attachment_using_content_location
726 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email12")
727 mail
= TMail
::Mail.parse(fixture
)
728 assert_equal
1, mail
.attachments
.length
729 assert_equal
"Photo25.jpg", mail
.attachments
.first
.original_filename
732 def test_attachment_with_text_type
733 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email13")
734 mail
= TMail
::Mail.parse(fixture
)
735 assert mail
.has_attachments
?
736 assert_equal
1, mail
.attachments
.length
737 assert_equal
"hello.rb", mail
.attachments
.first
.original_filename
740 def test_decode_part_without_content_type
741 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email4")
742 mail
= TMail
::Mail.parse(fixture
)
743 assert_nothing_raised
{ mail
.body
}
746 def test_decode_message_without_content_type
747 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email5")
748 mail
= TMail
::Mail.parse(fixture
)
749 assert_nothing_raised
{ mail
.body
}
752 def test_decode_message_with_incorrect_charset
753 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email6")
754 mail
= TMail
::Mail.parse(fixture
)
755 assert_nothing_raised
{ mail
.body
}
758 def test_multipart_with_mime_version
759 mail
= TestMailer
.create_multipart_with_mime_version(@recipient)
760 assert_equal
"1.1", mail
.mime_version
763 def test_multipart_with_utf8_subject
764 mail
= TestMailer
.create_multipart_with_utf8_subject(@recipient)
765 assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail
.encoded
)
768 def test_implicitly_multipart_with_utf8
769 mail
= TestMailer
.create_implicitly_multipart_with_utf8
770 assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail
.encoded
)
773 def test_explicitly_multipart_messages
774 mail
= TestMailer
.create_explicitly_multipart_example(@recipient)
775 assert_equal
3, mail
.parts
.length
776 assert_nil mail
.content_type
777 assert_equal
"text/plain", mail
.parts
[0].content_type
779 assert_equal
"text/html", mail
.parts
[1].content_type
780 assert_equal
"iso-8859-1", mail
.parts
[1].sub_header("content-type", "charset")
781 assert_equal
"inline", mail
.parts
[1].content_disposition
783 assert_equal
"image/jpeg", mail
.parts
[2].content_type
784 assert_equal
"attachment", mail
.parts
[2].content_disposition
785 assert_equal
"foo.jpg", mail
.parts
[2].sub_header("content-disposition", "filename")
786 assert_equal
"foo.jpg", mail
.parts
[2].sub_header("content-type", "name")
787 assert_nil mail
.parts
[2].sub_header("content-type", "charset")
790 def test_explicitly_multipart_with_content_type
791 mail
= TestMailer
.create_explicitly_multipart_example(@recipient, "multipart/alternative")
792 assert_equal
3, mail
.parts
.length
793 assert_equal
"multipart/alternative", mail
.content_type
796 def test_explicitly_multipart_with_invalid_content_type
797 mail
= TestMailer
.create_explicitly_multipart_example(@recipient, "text/xml")
798 assert_equal
3, mail
.parts
.length
799 assert_nil mail
.content_type
802 def test_implicitly_multipart_messages
803 assert ActionView
::Template.template_handler_extensions
.include?("bak"), "bak extension was not registered"
805 mail
= TestMailer
.create_implicitly_multipart_example(@recipient)
806 assert_equal
3, mail
.parts
.length
807 assert_equal
"1.0", mail
.mime_version
808 assert_equal
"multipart/alternative", mail
.content_type
809 assert_equal
"text/yaml", mail
.parts
[0].content_type
810 assert_equal
"utf-8", mail
.parts
[0].sub_header("content-type", "charset")
811 assert_equal
"text/plain", mail
.parts
[1].content_type
812 assert_equal
"utf-8", mail
.parts
[1].sub_header("content-type", "charset")
813 assert_equal
"text/html", mail
.parts
[2].content_type
814 assert_equal
"utf-8", mail
.parts
[2].sub_header("content-type", "charset")
817 def test_implicitly_multipart_messages_with_custom_order
818 assert ActionView
::Template.template_handler_extensions
.include?("bak"), "bak extension was not registered"
820 mail
= TestMailer
.create_implicitly_multipart_example(@recipient, nil, ["text/yaml", "text/plain"])
821 assert_equal
3, mail
.parts
.length
822 assert_equal
"text/html", mail
.parts
[0].content_type
823 assert_equal
"text/plain", mail
.parts
[1].content_type
824 assert_equal
"text/yaml", mail
.parts
[2].content_type
827 def test_implicitly_multipart_messages_with_charset
828 mail
= TestMailer
.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
830 assert_equal
"multipart/alternative", mail
.header
['content-type'].body
832 assert_equal
'iso-8859-1', mail
.parts
[0].sub_header("content-type", "charset")
833 assert_equal
'iso-8859-1', mail
.parts
[1].sub_header("content-type", "charset")
834 assert_equal
'iso-8859-1', mail
.parts
[2].sub_header("content-type", "charset")
838 mail
= TestMailer
.create_html_mail(@recipient)
839 assert_equal
"text/html", mail
.content_type
842 def test_html_mail_with_underscores
843 mail
= TestMailer
.create_html_mail_with_underscores(@recipient)
844 assert_equal %
{<a href
="http://google.com" target
="_blank">_Google
</a
>}, mail
.body
847 def test_various_newlines
848 mail
= TestMailer
.create_various_newlines(@recipient)
849 assert_equal("line #1\nline #2\nline #3\nline #4\n\n" +
850 "line #5\n\nline#6\n\nline #7", mail
.body
)
853 def test_various_newlines_multipart
854 mail
= TestMailer
.create_various_newlines_multipart(@recipient)
855 assert_equal
"line #1\nline #2\nline #3\nline #4\n\n", mail
.parts
[0].body
856 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
859 def test_headers_removed_on_smtp_delivery
860 ActionMailer
::Base.delivery_method
= :smtp
861 TestMailer
.deliver_cc_bcc(@recipient)
862 assert MockSMTP
.deliveries
[0][2].include?("root@loudthinking.com")
863 assert MockSMTP
.deliveries
[0][2].include?("nobody@loudthinking.com")
864 assert MockSMTP
.deliveries
[0][2].include?(@recipient)
865 assert_match
%r
{^Cc
: nobody
@loudthinking.com
}, MockSMTP
.deliveries
[0][0]
866 assert_match
%r
{^To
: #{@recipient}}, MockSMTP.deliveries[0][0]
867 assert_no_match
%r
{^Bcc
: root
@loudthinking.com
}, MockSMTP
.deliveries
[0][0]
870 def test_recursive_multipart_processing
871 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email7")
872 mail
= TMail
::Mail.parse(fixture
)
873 assert_equal
"This is the first part.\n\nAttachment: test.rb\nAttachment: test.pdf\n\n\nAttachment: smime.p7s\n", mail
.body
876 def test_decode_encoded_attachment_filename
877 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email8")
878 mail
= TMail
::Mail.parse(fixture
)
879 attachment
= mail
.attachments
.last
881 expected
= "01 Quien Te Dij\212at. Pitbull.mp3"
882 expected
.force_encoding(Encoding
::ASCII_8BIT) if expected
.respond_to
?(:force_encoding)
884 assert_equal expected
, attachment
.original_filename
887 def test_wrong_mail_header
888 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email9")
889 assert_raise(TMail
::SyntaxError) { TMail
::Mail.parse(fixture
) }
892 def test_decode_message_with_unknown_charset
893 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email10")
894 mail
= TMail
::Mail.parse(fixture
)
895 assert_nothing_raised
{ mail
.body
}
898 def test_empty_header_values_omitted
899 result
= TestMailer
.create_unnamed_attachment(@recipient).encoded
900 assert_match
%r
{Content-Type
: application
/octet-stream
[^
;]}, result
901 assert_match
%r
{Content-Disposition
: attachment
[^
;]}, result
904 def test_headers_with_nonalpha_chars
905 mail
= TestMailer
.create_headers_with_nonalpha_chars(@recipient)
906 assert
!mail
.from_addrs
.empty
?
907 assert
!mail
.cc_addrs
.empty
?
908 assert
!mail
.bcc_addrs
.empty
?
909 assert_match(/:/, mail
.from_addrs
.to_s
)
910 assert_match(/:/, mail
.cc_addrs
.to_s
)
911 assert_match(/:/, mail
.bcc_addrs
.to_s
)
914 def test_deliver_with_mail_object
915 mail
= TestMailer
.create_headers_with_nonalpha_chars(@recipient)
916 assert_nothing_raised
{ TestMailer
.deliver(mail
) }
917 assert_equal
1, TestMailer
.deliveries
.length
920 def test_multipart_with_template_path_with_dots
921 mail
= FunkyPathMailer
.create_multipart_with_template_path_with_dots(@recipient)
922 assert_equal
2, mail
.parts
.length
923 assert_equal
'text/plain', mail
.parts
[0].content_type
924 assert_equal
'utf-8', mail
.parts
[0].charset
927 def test_custom_content_type_attributes
928 mail
= TestMailer
.create_custom_content_type_attributes
929 assert_match
%r
{format
=flowed
}, mail
['content-type'].to_s
930 assert_match
%r
{charset
=utf-8
}, mail
['content-type'].to_s
933 def test_return_path_with_create
934 mail
= TestMailer
.create_return_path
935 assert_equal
"<another@somewhere.test>", mail
['return-path'].to_s
938 def test_return_path_with_deliver
939 ActionMailer
::Base.delivery_method
= :smtp
940 TestMailer
.deliver_return_path
941 assert_match
%r
{^Return-Path
: <another
@somewhere.test
>}, MockSMTP
.deliveries
[0][0]
942 assert_equal
"another@somewhere.test", MockSMTP
.deliveries
[0][1].to_s
945 def test_body_is_stored_as_an_ivar
946 mail
= TestMailer
.create_body_ivar(@recipient)
947 assert_equal
"body: foo\nbar: baz", mail
.body
950 def test_starttls_is_enabled_if_supported
951 ActionMailer
::Base.smtp_settings
[:enable_starttls_auto] = true
952 MockSMTP
.any_instance
.expects(:respond_to?).with(:enable_starttls_auto).returns(true)
953 MockSMTP
.any_instance
.expects(:enable_starttls_auto)
954 ActionMailer
::Base.delivery_method
= :smtp
955 TestMailer
.deliver_signed_up(@recipient)
958 def test_starttls_is_disabled_if_not_supported
959 ActionMailer
::Base.smtp_settings
[:enable_starttls_auto] = true
960 MockSMTP
.any_instance
.expects(:respond_to?).with(:enable_starttls_auto).returns(false)
961 MockSMTP
.any_instance
.expects(:enable_starttls_auto).never
962 ActionMailer
::Base.delivery_method
= :smtp
963 TestMailer
.deliver_signed_up(@recipient)
966 def test_starttls_is_not_enabled
967 ActionMailer
::Base.smtp_settings
[:enable_starttls_auto] = false
968 MockSMTP
.any_instance
.expects(:respond_to?).never
969 MockSMTP
.any_instance
.expects(:enable_starttls_auto).never
970 ActionMailer
::Base.delivery_method
= :smtp
971 TestMailer
.deliver_signed_up(@recipient)
973 ActionMailer
::Base.smtp_settings
[:enable_starttls_auto] = true
977 class InheritableTemplateRootTest
< Test
::Unit::TestCase
979 expected
= "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
980 assert_equal expected
, FunkyPathMailer
.template_root
.to_s
982 sub
= Class
.new(FunkyPathMailer
)
983 sub
.template_root
= 'test/path'
985 assert_equal
'test/path', sub
.template_root
.to_s
986 assert_equal expected
, FunkyPathMailer
.template_root
.to_s
990 class MethodNamingTest
< Test
::Unit::TestCase
991 class TestMailer
< ActionMailer
::Base
998 set_delivery_method
:test
999 ActionMailer
::Base.perform_deliveries
= true
1000 ActionMailer
::Base.deliveries
= []
1004 restore_delivery_method
1007 def test_send_method
1008 assert_nothing_raised
do
1010 TestMailer
.deliver_send
1016 class RespondToTest
< Test
::Unit::TestCase
1017 class RespondToMailer
< ActionMailer
::Base; end
1020 set_delivery_method
:test
1024 restore_delivery_method
1027 def test_should_respond_to_new
1028 assert RespondToMailer
.respond_to
?(:new)
1031 def test_should_respond_to_create_with_template_suffix
1032 assert RespondToMailer
.respond_to
?(:create_any_old_template)
1035 def test_should_respond_to_deliver_with_template_suffix
1036 assert RespondToMailer
.respond_to
?(:deliver_any_old_template)
1039 def test_should_not_respond_to_new_with_template_suffix
1040 assert
!RespondToMailer
.respond_to
?(:new_any_old_template)
1043 def test_should_not_respond_to_create_with_template_suffix_unless_it_is_separated_by_an_underscore
1044 assert
!RespondToMailer
.respond_to
?(:createany_old_template)
1047 def test_should_not_respond_to_deliver_with_template_suffix_unless_it_is_separated_by_an_underscore
1048 assert
!RespondToMailer
.respond_to
?(:deliverany_old_template)
1051 def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_uppercase_letter
1052 assert
!RespondToMailer
.respond_to
?(:create_Any_old_template)
1055 def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_uppercase_letter
1056 assert
!RespondToMailer
.respond_to
?(:deliver_Any_old_template)
1059 def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_digit
1060 assert
!RespondToMailer
.respond_to
?(:create_1_template)
1063 def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_digit
1064 assert
!RespondToMailer
.respond_to
?(:deliver_1_template)
1067 def test_should_not_respond_to_method_where_deliver_is_not_a_suffix
1068 assert
!RespondToMailer
.respond_to
?(:foo_deliver_template)
1071 def test_should_still_raise_exception_with_expected_message_when_calling_an_undefined_method
1072 error
= assert_raise NoMethodError
do
1073 RespondToMailer
.not_a_method
1076 assert_match
/undefined method.*not_a_method/, error
.message