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 uses_mocha
'ActionMailerTest' do
294 class ActionMailerTest
< Test
::Unit::TestCase
295 include ActionMailer
::Quoting
297 def encode( text
, charset
="utf-8" )
298 quoted_printable( text
, charset
)
301 def new_mail( charset
="utf-8" )
302 mail
= TMail
::Mail.new
303 mail
.mime_version
= "1.0"
305 mail
.set_content_type
"text", "plain", { "charset" => charset
}
310 # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
312 set_delivery_method
:test
313 ActionMailer
::Base.perform_deliveries
= true
314 ActionMailer
::Base.raise_delivery_errors
= true
315 ActionMailer
::Base.deliveries
= []
317 @original_logger = TestMailer
.logger
318 @recipient = 'test@localhost'
322 TestMailer
.logger
= @original_logger
323 restore_delivery_method
326 def test_nested_parts
328 assert_nothing_raised
{ created
= TestMailer
.create_nested_multipart(@recipient)}
329 assert_equal
2,created
.parts
.size
330 assert_equal
2,created
.parts
.first
.parts
.size
332 assert_equal
"multipart/mixed", created
.content_type
333 assert_equal
"multipart/alternative", created
.parts
.first
.content_type
334 assert_equal
"bar", created
.parts
.first
.header
['foo'].to_s
335 assert_equal
"text/plain", created
.parts
.first
.parts
.first
.content_type
336 assert_equal
"text/html", created
.parts
.first
.parts
[1].content_type
337 assert_equal
"application/octet-stream", created
.parts
[1].content_type
340 def test_nested_parts_with_body
342 assert_nothing_raised
{ created
= TestMailer
.create_nested_multipart_with_body(@recipient)}
343 assert_equal
1,created
.parts
.size
344 assert_equal
2,created
.parts
.first
.parts
.size
346 assert_equal
"multipart/mixed", created
.content_type
347 assert_equal
"multipart/alternative", created
.parts
.first
.content_type
348 assert_equal
"Nothing to see here.", created
.parts
.first
.parts
.first
.body
349 assert_equal
"text/plain", created
.parts
.first
.parts
.first
.content_type
350 assert_equal
"text/html", created
.parts
.first
.parts
[1].content_type
353 def test_attachment_with_custom_header
355 assert_nothing_raised
{ created
= TestMailer
.create_attachment_with_custom_header(@recipient)}
356 assert_equal
"<test@test.com>", created
.parts
[1].header
['content-id'].to_s
361 expected
.to
= @recipient
362 expected
.subject
= "[Signed up] Welcome #{@recipient}"
363 expected
.body
= "Hello there, \n\nMr. #{@recipient}"
364 expected
.from
= "system@loudthinking.com"
365 expected
.date
= Time
.local(2004, 12, 12)
368 assert_nothing_raised
{ created
= TestMailer
.create_signed_up(@recipient) }
369 assert_not_nil created
370 assert_equal expected
.encoded
, created
.encoded
372 assert_nothing_raised
{ TestMailer
.deliver_signed_up(@recipient) }
373 assert_not_nil ActionMailer
::Base.deliveries
.first
374 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
377 def test_custom_template
379 expected
.to
= @recipient
380 expected
.subject
= "[Signed up] Welcome #{@recipient}"
381 expected
.body
= "Hello there, \n\nMr. #{@recipient}"
382 expected
.from
= "system@loudthinking.com"
383 expected
.date
= Time
.local(2004, 12, 12)
386 assert_nothing_raised
{ created
= TestMailer
.create_custom_template(@recipient) }
387 assert_not_nil created
388 assert_equal expected
.encoded
, created
.encoded
391 def test_custom_templating_extension
392 # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
394 expected
.to
= @recipient
395 expected
.subject
= "[Signed up] Welcome #{@recipient}"
396 expected
.body
= "Hello there, \n\nMr. #{@recipient}"
397 expected
.from
= "system@loudthinking.com"
398 expected
.date
= Time
.local(2004, 12, 12)
400 # Stub the render method so no alternative renderers need be present.
401 ActionView
::Base.any_instance
.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
403 # Now that the template is registered, there should be one part. The text/plain part.
405 assert_nothing_raised
{ created
= TestMailer
.create_custom_templating_extension(@recipient) }
406 assert_not_nil created
407 assert_equal
2, created
.parts
.length
408 assert_equal
'text/plain', created
.parts
[0].content_type
409 assert_equal
'text/html', created
.parts
[1].content_type
412 def test_cancelled_account
414 expected
.to
= @recipient
415 expected
.subject
= "[Cancelled] Goodbye #{@recipient}"
416 expected
.body
= "Goodbye, Mr. #{@recipient}"
417 expected
.from
= "system@loudthinking.com"
418 expected
.date
= Time
.local(2004, 12, 12)
421 assert_nothing_raised
{ created
= TestMailer
.create_cancelled_account(@recipient) }
422 assert_not_nil created
423 assert_equal expected
.encoded
, created
.encoded
425 assert_nothing_raised
{ TestMailer
.deliver_cancelled_account(@recipient) }
426 assert_not_nil ActionMailer
::Base.deliveries
.first
427 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
432 expected
.to
= @recipient
433 expected
.subject
= "testing bcc/cc"
434 expected
.body
= "Nothing to see here."
435 expected
.from
= "system@loudthinking.com"
436 expected
.cc
= "nobody@loudthinking.com"
437 expected
.bcc
= "root@loudthinking.com"
438 expected
.date
= Time
.local
2004, 12, 12
441 assert_nothing_raised
do
442 created
= TestMailer
.create_cc_bcc
@recipient
444 assert_not_nil created
445 assert_equal expected
.encoded
, created
.encoded
447 assert_nothing_raised
do
448 TestMailer
.deliver_cc_bcc
@recipient
451 assert_not_nil ActionMailer
::Base.deliveries
.first
452 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
458 expected
.to
= @recipient
459 expected
.subject
= "testing reply_to"
460 expected
.body
= "Nothing to see here."
461 expected
.from
= "system@loudthinking.com"
462 expected
.reply_to
= "atraver@gmail.com"
463 expected
.date
= Time
.local
2008, 5, 23
466 assert_nothing_raised
do
467 created
= TestMailer
.create_different_reply_to
@recipient
469 assert_not_nil created
470 assert_equal expected
.encoded
, created
.encoded
472 assert_nothing_raised
do
473 TestMailer
.deliver_different_reply_to
@recipient
476 assert_not_nil ActionMailer
::Base.deliveries
.first
477 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
481 expected
= new_mail( "iso-8859-1" )
482 expected
.to
= @recipient
483 expected
.subject
= encode
"testing isø charsets", "iso-8859-1"
484 expected
.body
= "Nothing to see here."
485 expected
.from
= "system@loudthinking.com"
486 expected
.cc
= "nobody@loudthinking.com"
487 expected
.bcc
= "root@loudthinking.com"
488 expected
.date
= Time
.local
2004, 12, 12
491 assert_nothing_raised
do
492 created
= TestMailer
.create_iso_charset
@recipient
494 assert_not_nil created
495 assert_equal expected
.encoded
, created
.encoded
497 assert_nothing_raised
do
498 TestMailer
.deliver_iso_charset
@recipient
501 assert_not_nil ActionMailer
::Base.deliveries
.first
502 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
505 def test_unencoded_subject
507 expected
.to
= @recipient
508 expected
.subject
= "testing unencoded subject"
509 expected
.body
= "Nothing to see here."
510 expected
.from
= "system@loudthinking.com"
511 expected
.cc
= "nobody@loudthinking.com"
512 expected
.bcc
= "root@loudthinking.com"
513 expected
.date
= Time
.local
2004, 12, 12
516 assert_nothing_raised
do
517 created
= TestMailer
.create_unencoded_subject
@recipient
519 assert_not_nil created
520 assert_equal expected
.encoded
, created
.encoded
522 assert_nothing_raised
do
523 TestMailer
.deliver_unencoded_subject
@recipient
526 assert_not_nil ActionMailer
::Base.deliveries
.first
527 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
530 def test_instances_are_nil
531 assert_nil ActionMailer
::Base.new
532 assert_nil TestMailer
.new
535 def test_deliveries_array
536 assert_not_nil ActionMailer
::Base.deliveries
537 assert_equal
0, ActionMailer
::Base.deliveries
.size
538 TestMailer
.deliver_signed_up(@recipient)
539 assert_equal
1, ActionMailer
::Base.deliveries
.size
540 assert_not_nil ActionMailer
::Base.deliveries
.first
543 def test_perform_deliveries_flag
544 ActionMailer
::Base.perform_deliveries
= false
545 TestMailer
.deliver_signed_up(@recipient)
546 assert_equal
0, ActionMailer
::Base.deliveries
.size
547 ActionMailer
::Base.perform_deliveries
= true
548 TestMailer
.deliver_signed_up(@recipient)
549 assert_equal
1, ActionMailer
::Base.deliveries
.size
552 def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
553 ActionMailer
::Base.raise_delivery_errors
= false
554 TestMailer
.any_instance
.expects(:perform_delivery_test).raises(Exception
)
555 assert_nothing_raised
{ TestMailer
.deliver_signed_up(@recipient) }
558 def test_performs_delivery_via_sendmail
560 sm
.expects(:print).with(anything
)
562 IO
.expects(:popen).once
.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm
)
563 ActionMailer
::Base.delivery_method
= :sendmail
564 TestMailer
.deliver_signed_up(@recipient)
567 def test_delivery_logs_sent_mail
568 mail
= TestMailer
.create_signed_up(@recipient)
570 logger
.expects(:info).with("Sent mail to #{@recipient}")
571 logger
.expects(:debug).with("\n#{mail.encoded}")
572 TestMailer
.logger
= logger
573 TestMailer
.deliver_signed_up(@recipient)
576 def test_unquote_quoted_printable_subject
579 Subject: =?utf-8?Q?testing_testing_=D6=A4?=
580 Content-Type: text/plain; charset=iso-8859-1
584 mail
= TMail
::Mail.parse(msg
)
585 assert_equal
"testing testing \326\244", mail
.subject
586 assert_equal
"=?utf-8?Q?testing_testing_=D6=A4?=", mail
.quoted_subject
589 def test_unquote_7bit_subject
592 Subject: this == working?
593 Content-Type: text/plain; charset=iso-8859-1
597 mail
= TMail
::Mail.parse(msg
)
598 assert_equal
"this == working?", mail
.subject
599 assert_equal
"this == working?", mail
.quoted_subject
602 def test_unquote_7bit_body
606 Content-Type: text/plain; charset=iso-8859-1
607 Content-Transfer-Encoding: 7bit
611 mail
= TMail
::Mail.parse(msg
)
612 assert_equal
"The=3Dbody", mail
.body
.strip
613 assert_equal
"The=3Dbody", mail
.quoted_body
.strip
616 def test_unquote_quoted_printable_body
620 Content-Type: text/plain; charset=iso-8859-1
621 Content-Transfer-Encoding: quoted-printable
625 mail
= TMail
::Mail.parse(msg
)
626 assert_equal
"The=body", mail
.body
.strip
627 assert_equal
"The=3Dbody", mail
.quoted_body
.strip
630 def test_unquote_base64_body
634 Content-Type: text/plain; charset=iso-8859-1
635 Content-Transfer-Encoding: base64
639 mail
= TMail
::Mail.parse(msg
)
640 assert_equal
"The body", mail
.body
.strip
641 assert_equal
"VGhlIGJvZHk=", mail
.quoted_body
.strip
644 def test_extended_headers
645 @recipient = "Grytøyr <test@localhost>"
647 expected
= new_mail
"iso-8859-1"
648 expected
.to
= quote_address_if_necessary
@recipient, "iso-8859-1"
649 expected
.subject
= "testing extended headers"
650 expected
.body
= "Nothing to see here."
651 expected
.from
= quote_address_if_necessary
"Grytøyr <stian1@example.net>", "iso-8859-1"
652 expected
.cc
= quote_address_if_necessary
"Grytøyr <stian2@example.net>", "iso-8859-1"
653 expected
.bcc
= quote_address_if_necessary
"Grytøyr <stian3@example.net>", "iso-8859-1"
654 expected
.date
= Time
.local
2004, 12, 12
657 assert_nothing_raised
do
658 created
= TestMailer
.create_extended_headers
@recipient
661 assert_not_nil created
662 assert_equal expected
.encoded
, created
.encoded
664 assert_nothing_raised
do
665 TestMailer
.deliver_extended_headers
@recipient
668 assert_not_nil ActionMailer
::Base.deliveries
.first
669 assert_equal expected
.encoded
, ActionMailer
::Base.deliveries
.first
.encoded
672 def test_utf8_body_is_not_quoted
673 @recipient = "Foo áëô îü <extended@example.net>"
674 expected
= new_mail
"utf-8"
675 expected
.to
= quote_address_if_necessary
@recipient, "utf-8"
676 expected
.subject
= "testing utf-8 body"
677 expected
.body
= "åœö blah"
678 expected
.from
= quote_address_if_necessary
@recipient, "utf-8"
679 expected
.cc
= quote_address_if_necessary
@recipient, "utf-8"
680 expected
.bcc
= quote_address_if_necessary
@recipient, "utf-8"
681 expected
.date
= Time
.local
2004, 12, 12
683 created
= TestMailer
.create_utf8_body
@recipient
684 assert_match(/åœö blah/, created
.encoded
)
687 def test_multiple_utf8_recipients
688 @recipient = ["\"Foo áëô îü\" <extended@example.net>", "\"Example Recipient\" <me@example.com>"]
689 expected
= new_mail
"utf-8"
690 expected
.to
= quote_address_if_necessary
@recipient, "utf-8"
691 expected
.subject
= "testing utf-8 body"
692 expected
.body
= "åœö blah"
693 expected
.from
= quote_address_if_necessary
@recipient.first
, "utf-8"
694 expected
.cc
= quote_address_if_necessary
@recipient, "utf-8"
695 expected
.bcc
= quote_address_if_necessary
@recipient, "utf-8"
696 expected
.date
= Time
.local
2004, 12, 12
698 created
= TestMailer
.create_utf8_body
@recipient
699 assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>\r/, created
.encoded
)
700 assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>, Example Recipient <me/, created
.encoded
)
703 def test_receive_decodes_base64_encoded_mail
704 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email")
705 TestMailer
.receive(fixture
)
706 assert_match(/Jamis/, TestMailer
.received_body
)
709 def test_receive_attachments
710 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email2")
711 mail
= TMail
::Mail.parse(fixture
)
712 attachment
= mail
.attachments
.last
713 assert_equal
"smime.p7s", attachment
.original_filename
714 assert_equal
"application/pkcs7-signature", attachment
.content_type
717 def test_decode_attachment_without_charset
718 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email3")
719 mail
= TMail
::Mail.parse(fixture
)
720 attachment
= mail
.attachments
.last
721 assert_equal
1026, attachment
.read
.length
724 def test_attachment_using_content_location
725 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email12")
726 mail
= TMail
::Mail.parse(fixture
)
727 assert_equal
1, mail
.attachments
.length
728 assert_equal
"Photo25.jpg", mail
.attachments
.first
.original_filename
731 def test_attachment_with_text_type
732 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email13")
733 mail
= TMail
::Mail.parse(fixture
)
734 assert mail
.has_attachments
?
735 assert_equal
1, mail
.attachments
.length
736 assert_equal
"hello.rb", mail
.attachments
.first
.original_filename
739 def test_decode_part_without_content_type
740 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email4")
741 mail
= TMail
::Mail.parse(fixture
)
742 assert_nothing_raised
{ mail
.body
}
745 def test_decode_message_without_content_type
746 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email5")
747 mail
= TMail
::Mail.parse(fixture
)
748 assert_nothing_raised
{ mail
.body
}
751 def test_decode_message_with_incorrect_charset
752 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email6")
753 mail
= TMail
::Mail.parse(fixture
)
754 assert_nothing_raised
{ mail
.body
}
757 def test_multipart_with_mime_version
758 mail
= TestMailer
.create_multipart_with_mime_version(@recipient)
759 assert_equal
"1.1", mail
.mime_version
762 def test_multipart_with_utf8_subject
763 mail
= TestMailer
.create_multipart_with_utf8_subject(@recipient)
764 assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail
.encoded
)
767 def test_implicitly_multipart_with_utf8
768 mail
= TestMailer
.create_implicitly_multipart_with_utf8
769 assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail
.encoded
)
772 def test_explicitly_multipart_messages
773 mail
= TestMailer
.create_explicitly_multipart_example(@recipient)
774 assert_equal
3, mail
.parts
.length
775 assert_nil mail
.content_type
776 assert_equal
"text/plain", mail
.parts
[0].content_type
778 assert_equal
"text/html", mail
.parts
[1].content_type
779 assert_equal
"iso-8859-1", mail
.parts
[1].sub_header("content-type", "charset")
780 assert_equal
"inline", mail
.parts
[1].content_disposition
782 assert_equal
"image/jpeg", mail
.parts
[2].content_type
783 assert_equal
"attachment", mail
.parts
[2].content_disposition
784 assert_equal
"foo.jpg", mail
.parts
[2].sub_header("content-disposition", "filename")
785 assert_equal
"foo.jpg", mail
.parts
[2].sub_header("content-type", "name")
786 assert_nil mail
.parts
[2].sub_header("content-type", "charset")
789 def test_explicitly_multipart_with_content_type
790 mail
= TestMailer
.create_explicitly_multipart_example(@recipient, "multipart/alternative")
791 assert_equal
3, mail
.parts
.length
792 assert_equal
"multipart/alternative", mail
.content_type
795 def test_explicitly_multipart_with_invalid_content_type
796 mail
= TestMailer
.create_explicitly_multipart_example(@recipient, "text/xml")
797 assert_equal
3, mail
.parts
.length
798 assert_nil mail
.content_type
801 def test_implicitly_multipart_messages
802 mail
= TestMailer
.create_implicitly_multipart_example(@recipient)
803 assert_equal
3, mail
.parts
.length
804 assert_equal
"1.0", mail
.mime_version
805 assert_equal
"multipart/alternative", mail
.content_type
806 assert_equal
"text/yaml", mail
.parts
[0].content_type
807 assert_equal
"utf-8", mail
.parts
[0].sub_header("content-type", "charset")
808 assert_equal
"text/plain", mail
.parts
[1].content_type
809 assert_equal
"utf-8", mail
.parts
[1].sub_header("content-type", "charset")
810 assert_equal
"text/html", mail
.parts
[2].content_type
811 assert_equal
"utf-8", mail
.parts
[2].sub_header("content-type", "charset")
814 def test_implicitly_multipart_messages_with_custom_order
815 mail
= TestMailer
.create_implicitly_multipart_example(@recipient, nil, ["text/yaml", "text/plain"])
816 assert_equal
3, mail
.parts
.length
817 assert_equal
"text/html", mail
.parts
[0].content_type
818 assert_equal
"text/plain", mail
.parts
[1].content_type
819 assert_equal
"text/yaml", mail
.parts
[2].content_type
822 def test_implicitly_multipart_messages_with_charset
823 mail
= TestMailer
.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
825 assert_equal
"multipart/alternative", mail
.header
['content-type'].body
827 assert_equal
'iso-8859-1', mail
.parts
[0].sub_header("content-type", "charset")
828 assert_equal
'iso-8859-1', mail
.parts
[1].sub_header("content-type", "charset")
829 assert_equal
'iso-8859-1', mail
.parts
[2].sub_header("content-type", "charset")
833 mail
= TestMailer
.create_html_mail(@recipient)
834 assert_equal
"text/html", mail
.content_type
837 def test_html_mail_with_underscores
838 mail
= TestMailer
.create_html_mail_with_underscores(@recipient)
839 assert_equal %
{<a href
="http://google.com" target
="_blank">_Google
</a
>}, mail
.body
842 def test_various_newlines
843 mail
= TestMailer
.create_various_newlines(@recipient)
844 assert_equal("line #1\nline #2\nline #3\nline #4\n\n" +
845 "line #5\n\nline#6\n\nline #7", mail
.body
)
848 def test_various_newlines_multipart
849 mail
= TestMailer
.create_various_newlines_multipart(@recipient)
850 assert_equal
"line #1\nline #2\nline #3\nline #4\n\n", mail
.parts
[0].body
851 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
854 def test_headers_removed_on_smtp_delivery
855 ActionMailer
::Base.delivery_method
= :smtp
856 TestMailer
.deliver_cc_bcc(@recipient)
857 assert MockSMTP
.deliveries
[0][2].include?("root@loudthinking.com")
858 assert MockSMTP
.deliveries
[0][2].include?("nobody@loudthinking.com")
859 assert MockSMTP
.deliveries
[0][2].include?(@recipient)
860 assert_match
%r
{^Cc
: nobody
@loudthinking.com
}, MockSMTP
.deliveries
[0][0]
861 assert_match
%r
{^To
: #{@recipient}}, MockSMTP.deliveries[0][0]
862 assert_no_match
%r
{^Bcc
: root
@loudthinking.com
}, MockSMTP
.deliveries
[0][0]
865 def test_recursive_multipart_processing
866 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email7")
867 mail
= TMail
::Mail.parse(fixture
)
868 assert_equal
"This is the first part.\n\nAttachment: test.rb\nAttachment: test.pdf\n\n\nAttachment: smime.p7s\n", mail
.body
871 def test_decode_encoded_attachment_filename
872 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email8")
873 mail
= TMail
::Mail.parse(fixture
)
874 attachment
= mail
.attachments
.last
876 expected
= "01 Quien Te Dij\212at. Pitbull.mp3"
877 expected
.force_encoding(Encoding
::ASCII_8BIT) if expected
.respond_to
?(:force_encoding)
879 assert_equal expected
, attachment
.original_filename
882 def test_wrong_mail_header
883 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email9")
884 assert_raise(TMail
::SyntaxError) { TMail
::Mail.parse(fixture
) }
887 def test_decode_message_with_unknown_charset
888 fixture
= File
.read(File
.dirname(__FILE__
) + "/fixtures/raw_email10")
889 mail
= TMail
::Mail.parse(fixture
)
890 assert_nothing_raised
{ mail
.body
}
893 def test_empty_header_values_omitted
894 result
= TestMailer
.create_unnamed_attachment(@recipient).encoded
895 assert_match
%r
{Content-Type
: application
/octet-stream
[^
;]}, result
896 assert_match
%r
{Content-Disposition
: attachment
[^
;]}, result
899 def test_headers_with_nonalpha_chars
900 mail
= TestMailer
.create_headers_with_nonalpha_chars(@recipient)
901 assert
!mail
.from_addrs
.empty
?
902 assert
!mail
.cc_addrs
.empty
?
903 assert
!mail
.bcc_addrs
.empty
?
904 assert_match(/:/, mail
.from_addrs
.to_s
)
905 assert_match(/:/, mail
.cc_addrs
.to_s
)
906 assert_match(/:/, mail
.bcc_addrs
.to_s
)
909 def test_deliver_with_mail_object
910 mail
= TestMailer
.create_headers_with_nonalpha_chars(@recipient)
911 assert_nothing_raised
{ TestMailer
.deliver(mail
) }
912 assert_equal
1, TestMailer
.deliveries
.length
915 def test_multipart_with_template_path_with_dots
916 mail
= FunkyPathMailer
.create_multipart_with_template_path_with_dots(@recipient)
917 assert_equal
2, mail
.parts
.length
920 def test_custom_content_type_attributes
921 mail
= TestMailer
.create_custom_content_type_attributes
922 assert_match
%r
{format
=flowed
}, mail
['content-type'].to_s
923 assert_match
%r
{charset
=utf-8
}, mail
['content-type'].to_s
926 def test_return_path_with_create
927 mail
= TestMailer
.create_return_path
928 assert_equal
"<another@somewhere.test>", mail
['return-path'].to_s
931 def test_return_path_with_deliver
932 ActionMailer
::Base.delivery_method
= :smtp
933 TestMailer
.deliver_return_path
934 assert_match
%r
{^Return-Path
: <another
@somewhere.test
>}, MockSMTP
.deliveries
[0][0]
937 def test_body_is_stored_as_an_ivar
938 mail
= TestMailer
.create_body_ivar(@recipient)
939 assert_equal
"body: foo\nbar: baz", mail
.body
942 def test_starttls_is_enabled_if_supported
943 MockSMTP
.any_instance
.expects(:respond_to?).with(:enable_starttls_auto).returns(true)
944 MockSMTP
.any_instance
.expects(:enable_starttls_auto)
945 ActionMailer
::Base.delivery_method
= :smtp
946 TestMailer
.deliver_signed_up(@recipient)
949 def test_starttls_is_disabled_if_not_supported
950 MockSMTP
.any_instance
.expects(:respond_to?).with(:enable_starttls_auto).returns(false)
951 MockSMTP
.any_instance
.expects(:enable_starttls_auto).never
952 ActionMailer
::Base.delivery_method
= :smtp
953 TestMailer
.deliver_signed_up(@recipient)
959 class InheritableTemplateRootTest
< Test
::Unit::TestCase
961 expected
= "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
962 assert_equal expected
, FunkyPathMailer
.template_root
964 sub
= Class
.new(FunkyPathMailer
)
965 sub
.template_root
= 'test/path'
967 assert_equal
'test/path', sub
.template_root
968 assert_equal expected
, FunkyPathMailer
.template_root
972 class MethodNamingTest
< Test
::Unit::TestCase
973 class TestMailer
< ActionMailer
::Base
980 set_delivery_method
:test
981 ActionMailer
::Base.perform_deliveries
= true
982 ActionMailer
::Base.deliveries
= []
986 restore_delivery_method
990 assert_nothing_raised
do
992 TestMailer
.deliver_send
998 class RespondToTest
< Test
::Unit::TestCase
999 class RespondToMailer
< ActionMailer
::Base; end
1002 set_delivery_method
:test
1006 restore_delivery_method
1009 def test_should_respond_to_new
1010 assert RespondToMailer
.respond_to
?(:new)
1013 def test_should_respond_to_create_with_template_suffix
1014 assert RespondToMailer
.respond_to
?(:create_any_old_template)
1017 def test_should_respond_to_deliver_with_template_suffix
1018 assert RespondToMailer
.respond_to
?(:deliver_any_old_template)
1021 def test_should_not_respond_to_new_with_template_suffix
1022 assert
!RespondToMailer
.respond_to
?(:new_any_old_template)
1025 def test_should_not_respond_to_create_with_template_suffix_unless_it_is_separated_by_an_underscore
1026 assert
!RespondToMailer
.respond_to
?(:createany_old_template)
1029 def test_should_not_respond_to_deliver_with_template_suffix_unless_it_is_separated_by_an_underscore
1030 assert
!RespondToMailer
.respond_to
?(:deliverany_old_template)
1033 def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_uppercase_letter
1034 assert
!RespondToMailer
.respond_to
?(:create_Any_old_template)
1037 def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_uppercase_letter
1038 assert
!RespondToMailer
.respond_to
?(:deliver_Any_old_template)
1041 def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_digit
1042 assert
!RespondToMailer
.respond_to
?(:create_1_template)
1045 def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_digit
1046 assert
!RespondToMailer
.respond_to
?(:deliver_1_template)
1049 def test_should_not_respond_to_method_where_deliver_is_not_a_suffix
1050 assert
!RespondToMailer
.respond_to
?(:foo_deliver_template)
1053 def test_should_still_raise_exception_with_expected_message_when_calling_an_undefined_method
1054 error
= assert_raises NoMethodError
do
1055 RespondToMailer
.not_a_method
1058 assert_match
/undefined method.*not_a_method/, error
.message