Froze rails gems
[depot.git] / vendor / rails / actionmailer / test / mail_service_test.rb
1 # encoding: utf-8
2 require 'abstract_unit'
3
4 class FunkyPathMailer < ActionMailer::Base
5 self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
6
7 def multipart_with_template_path_with_dots(recipient)
8 recipients 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"
13 end
14 end
15
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
23 end
24
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}"
31 end
32
33 def cc_bcc(recipient)
34 recipients 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."
41 end
42
43 def different_reply_to(recipient)
44 recipients 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."
50 end
51
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"
61 end
62
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."
71 end
72
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"
82 end
83
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>"
91 @body = "åœö blah"
92 @charset = "utf-8"
93 end
94
95 def multipart_with_mime_version(recipient)
96 recipients recipient
97 subject "multipart with mime_version"
98 from "test@example.com"
99 sent_on Time.local(2004, 12, 12)
100 mime_version "1.1"
101 content_type "multipart/alternative"
102
103 part "text/plain" do |p|
104 p.body = "blah"
105 end
106
107 part "text/html" do |p|
108 p.body = "<b>blah</b>"
109 end
110 end
111
112 def multipart_with_utf8_subject(recipient)
113 recipients recipient
114 subject "Foo áëô îü"
115 from "test@example.com"
116 charset "utf-8"
117
118 part "text/plain" do |p|
119 p.body = "blah"
120 end
121
122 part "text/html" do |p|
123 p.body = "<b>blah</b>"
124 end
125 end
126
127 def explicitly_multipart_example(recipient, ct=nil)
128 recipients recipient
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
134
135 part "text/html" do |p|
136 p.charset = "iso-8859-1"
137 p.body = "blah"
138 end
139
140 attachment :content_type => "image/jpeg", :filename => "foo.jpg",
141 :body => "123456789"
142 end
143
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 }
150 @charset = cs if cs
151 @implicit_parts_order = order if order
152 end
153
154 def implicitly_multipart_with_utf8
155 recipients "no.one@nowhere.test"
156 subject "Foo áëô îü"
157 from "some.one@somewhere.test"
158 template "implicitly_multipart_example"
159 body ({ "recipient" => "no.one@nowhere.test" })
160 end
161
162 def html_mail(recipient)
163 recipients recipient
164 subject "html mail"
165 from "test@example.com"
166 body "<em>Emphasize</em> <strong>this</strong>"
167 content_type "text/html"
168 end
169
170 def html_mail_with_underscores(recipient)
171 subject "html mail with underscores"
172 body %{<a href="http://google.com" target="_blank">_Google</a>}
173 end
174
175 def custom_template(recipient)
176 recipients recipient
177 subject "[Signed up] Welcome #{recipient}"
178 from "system@loudthinking.com"
179 sent_on Time.local(2004, 12, 12)
180 template "signed_up"
181
182 body["recipient"] = recipient
183 end
184
185 def custom_templating_extension(recipient)
186 recipients recipient
187 subject "[Signed up] Welcome #{recipient}"
188 from "system@loudthinking.com"
189 sent_on Time.local(2004, 12, 12)
190
191 body["recipient"] = recipient
192 end
193
194 def various_newlines(recipient)
195 recipients 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"
200 end
201
202 def various_newlines_multipart(recipient)
203 recipients 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"
209 end
210
211 def nested_multipart(recipient)
212 recipients 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"
219 end
220 attachment :content_type => "application/octet-stream",:filename => "test.txt", :body => "test abcdefghijklmnopqstuvwxyz"
221 end
222
223 def nested_multipart_with_body(recipient)
224 recipients 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/>"
230 end
231 end
232
233 def attachment_with_custom_header(recipient)
234 recipients 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>' }
240 end
241
242 def unnamed_attachment(recipient)
243 recipients 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"
249 end
250
251 def headers_with_nonalpha_chars(recipient)
252 recipients 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>"
257 body "testing"
258 end
259
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"
265 body "testing"
266 end
267
268 def return_path
269 recipients "no.one@nowhere.test"
270 subject "return path test"
271 from "some.one@somewhere.test"
272 body "testing"
273 headers "return-path" => "another@somewhere.test"
274 end
275
276 def body_ivar(recipient)
277 recipients recipient
278 subject "Body as a local variable"
279 from "test@example.com"
280 body :body => "foo", :bar => "baz"
281 end
282
283 class <<self
284 attr_accessor :received_body
285 end
286
287 def receive(mail)
288 self.class.received_body = mail.body
289 end
290 end
291
292 uses_mocha 'ActionMailerTest' do
293
294 class ActionMailerTest < Test::Unit::TestCase
295 include ActionMailer::Quoting
296
297 def encode( text, charset="utf-8" )
298 quoted_printable( text, charset )
299 end
300
301 def new_mail( charset="utf-8" )
302 mail = TMail::Mail.new
303 mail.mime_version = "1.0"
304 if charset
305 mail.set_content_type "text", "plain", { "charset" => charset }
306 end
307 mail
308 end
309
310 # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
311 def setup
312 set_delivery_method :test
313 ActionMailer::Base.perform_deliveries = true
314 ActionMailer::Base.raise_delivery_errors = true
315 ActionMailer::Base.deliveries = []
316
317 @original_logger = TestMailer.logger
318 @recipient = 'test@localhost'
319 end
320
321 def teardown
322 TestMailer.logger = @original_logger
323 restore_delivery_method
324 end
325
326 def test_nested_parts
327 created = nil
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
331
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
338 end
339
340 def test_nested_parts_with_body
341 created = nil
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
345
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
351 end
352
353 def test_attachment_with_custom_header
354 created = nil
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
357 end
358
359 def test_signed_up
360 expected = new_mail
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)
366
367 created = nil
368 assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
369 assert_not_nil created
370 assert_equal expected.encoded, created.encoded
371
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
375 end
376
377 def test_custom_template
378 expected = new_mail
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)
384
385 created = nil
386 assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) }
387 assert_not_nil created
388 assert_equal expected.encoded, created.encoded
389 end
390
391 def test_custom_templating_extension
392 # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
393 expected = new_mail
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)
399
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}")
402
403 # Now that the template is registered, there should be one part. The text/plain part.
404 created = nil
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
410 end
411
412 def test_cancelled_account
413 expected = new_mail
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)
419
420 created = nil
421 assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) }
422 assert_not_nil created
423 assert_equal expected.encoded, created.encoded
424
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
428 end
429
430 def test_cc_bcc
431 expected = new_mail
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
439
440 created = nil
441 assert_nothing_raised do
442 created = TestMailer.create_cc_bcc @recipient
443 end
444 assert_not_nil created
445 assert_equal expected.encoded, created.encoded
446
447 assert_nothing_raised do
448 TestMailer.deliver_cc_bcc @recipient
449 end
450
451 assert_not_nil ActionMailer::Base.deliveries.first
452 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
453 end
454
455 def test_reply_to
456 expected = new_mail
457
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
464
465 created = nil
466 assert_nothing_raised do
467 created = TestMailer.create_different_reply_to @recipient
468 end
469 assert_not_nil created
470 assert_equal expected.encoded, created.encoded
471
472 assert_nothing_raised do
473 TestMailer.deliver_different_reply_to @recipient
474 end
475
476 assert_not_nil ActionMailer::Base.deliveries.first
477 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
478 end
479
480 def test_iso_charset
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
489
490 created = nil
491 assert_nothing_raised do
492 created = TestMailer.create_iso_charset @recipient
493 end
494 assert_not_nil created
495 assert_equal expected.encoded, created.encoded
496
497 assert_nothing_raised do
498 TestMailer.deliver_iso_charset @recipient
499 end
500
501 assert_not_nil ActionMailer::Base.deliveries.first
502 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
503 end
504
505 def test_unencoded_subject
506 expected = new_mail
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
514
515 created = nil
516 assert_nothing_raised do
517 created = TestMailer.create_unencoded_subject @recipient
518 end
519 assert_not_nil created
520 assert_equal expected.encoded, created.encoded
521
522 assert_nothing_raised do
523 TestMailer.deliver_unencoded_subject @recipient
524 end
525
526 assert_not_nil ActionMailer::Base.deliveries.first
527 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
528 end
529
530 def test_instances_are_nil
531 assert_nil ActionMailer::Base.new
532 assert_nil TestMailer.new
533 end
534
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
541 end
542
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
550 end
551
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) }
556 end
557
558 def test_performs_delivery_via_sendmail
559 sm = mock()
560 sm.expects(:print).with(anything)
561 sm.expects(:flush)
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)
565 end
566
567 def test_delivery_logs_sent_mail
568 mail = TestMailer.create_signed_up(@recipient)
569 logger = mock()
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)
574 end
575
576 def test_unquote_quoted_printable_subject
577 msg = <<EOF
578 From: me@example.com
579 Subject: =?utf-8?Q?testing_testing_=D6=A4?=
580 Content-Type: text/plain; charset=iso-8859-1
581
582 The body
583 EOF
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
587 end
588
589 def test_unquote_7bit_subject
590 msg = <<EOF
591 From: me@example.com
592 Subject: this == working?
593 Content-Type: text/plain; charset=iso-8859-1
594
595 The body
596 EOF
597 mail = TMail::Mail.parse(msg)
598 assert_equal "this == working?", mail.subject
599 assert_equal "this == working?", mail.quoted_subject
600 end
601
602 def test_unquote_7bit_body
603 msg = <<EOF
604 From: me@example.com
605 Subject: subject
606 Content-Type: text/plain; charset=iso-8859-1
607 Content-Transfer-Encoding: 7bit
608
609 The=3Dbody
610 EOF
611 mail = TMail::Mail.parse(msg)
612 assert_equal "The=3Dbody", mail.body.strip
613 assert_equal "The=3Dbody", mail.quoted_body.strip
614 end
615
616 def test_unquote_quoted_printable_body
617 msg = <<EOF
618 From: me@example.com
619 Subject: subject
620 Content-Type: text/plain; charset=iso-8859-1
621 Content-Transfer-Encoding: quoted-printable
622
623 The=3Dbody
624 EOF
625 mail = TMail::Mail.parse(msg)
626 assert_equal "The=body", mail.body.strip
627 assert_equal "The=3Dbody", mail.quoted_body.strip
628 end
629
630 def test_unquote_base64_body
631 msg = <<EOF
632 From: me@example.com
633 Subject: subject
634 Content-Type: text/plain; charset=iso-8859-1
635 Content-Transfer-Encoding: base64
636
637 VGhlIGJvZHk=
638 EOF
639 mail = TMail::Mail.parse(msg)
640 assert_equal "The body", mail.body.strip
641 assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip
642 end
643
644 def test_extended_headers
645 @recipient = "Grytøyr <test@localhost>"
646
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
655
656 created = nil
657 assert_nothing_raised do
658 created = TestMailer.create_extended_headers @recipient
659 end
660
661 assert_not_nil created
662 assert_equal expected.encoded, created.encoded
663
664 assert_nothing_raised do
665 TestMailer.deliver_extended_headers @recipient
666 end
667
668 assert_not_nil ActionMailer::Base.deliveries.first
669 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
670 end
671
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
682
683 created = TestMailer.create_utf8_body @recipient
684 assert_match(/åœö blah/, created.encoded)
685 end
686
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
697
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)
701 end
702
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)
707 end
708
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
715 end
716
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
722 end
723
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
729 end
730
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
737 end
738
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 }
743 end
744
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 }
749 end
750
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 }
755 end
756
757 def test_multipart_with_mime_version
758 mail = TestMailer.create_multipart_with_mime_version(@recipient)
759 assert_equal "1.1", mail.mime_version
760 end
761
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)
765 end
766
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)
770 end
771
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
777
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
781
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")
787 end
788
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
793 end
794
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
799 end
800
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")
812 end
813
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
820 end
821
822 def test_implicitly_multipart_messages_with_charset
823 mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
824
825 assert_equal "multipart/alternative", mail.header['content-type'].body
826
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")
830 end
831
832 def test_html_mail
833 mail = TestMailer.create_html_mail(@recipient)
834 assert_equal "text/html", mail.content_type
835 end
836
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
840 end
841
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)
846 end
847
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
852 end
853
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]
863 end
864
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
869 end
870
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
875
876 expected = "01 Quien Te Dij\212at. Pitbull.mp3"
877 expected.force_encoding(Encoding::ASCII_8BIT) if expected.respond_to?(:force_encoding)
878
879 assert_equal expected, attachment.original_filename
880 end
881
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) }
885 end
886
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 }
891 end
892
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
897 end
898
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)
907 end
908
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
913 end
914
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
918 end
919
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
924 end
925
926 def test_return_path_with_create
927 mail = TestMailer.create_return_path
928 assert_equal "<another@somewhere.test>", mail['return-path'].to_s
929 end
930
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]
935 end
936
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
940 end
941
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)
947 end
948
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)
954 end
955 end
956
957 end # uses_mocha
958
959 class InheritableTemplateRootTest < Test::Unit::TestCase
960 def test_attr
961 expected = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
962 assert_equal expected, FunkyPathMailer.template_root
963
964 sub = Class.new(FunkyPathMailer)
965 sub.template_root = 'test/path'
966
967 assert_equal 'test/path', sub.template_root
968 assert_equal expected, FunkyPathMailer.template_root
969 end
970 end
971
972 class MethodNamingTest < Test::Unit::TestCase
973 class TestMailer < ActionMailer::Base
974 def send
975 body 'foo'
976 end
977 end
978
979 def setup
980 set_delivery_method :test
981 ActionMailer::Base.perform_deliveries = true
982 ActionMailer::Base.deliveries = []
983 end
984
985 def teardown
986 restore_delivery_method
987 end
988
989 def test_send_method
990 assert_nothing_raised do
991 assert_emails 1 do
992 TestMailer.deliver_send
993 end
994 end
995 end
996 end
997
998 class RespondToTest < Test::Unit::TestCase
999 class RespondToMailer < ActionMailer::Base; end
1000
1001 def setup
1002 set_delivery_method :test
1003 end
1004
1005 def teardown
1006 restore_delivery_method
1007 end
1008
1009 def test_should_respond_to_new
1010 assert RespondToMailer.respond_to?(:new)
1011 end
1012
1013 def test_should_respond_to_create_with_template_suffix
1014 assert RespondToMailer.respond_to?(:create_any_old_template)
1015 end
1016
1017 def test_should_respond_to_deliver_with_template_suffix
1018 assert RespondToMailer.respond_to?(:deliver_any_old_template)
1019 end
1020
1021 def test_should_not_respond_to_new_with_template_suffix
1022 assert !RespondToMailer.respond_to?(:new_any_old_template)
1023 end
1024
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)
1027 end
1028
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)
1031 end
1032
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)
1035 end
1036
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)
1039 end
1040
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)
1043 end
1044
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)
1047 end
1048
1049 def test_should_not_respond_to_method_where_deliver_is_not_a_suffix
1050 assert !RespondToMailer.respond_to?(:foo_deliver_template)
1051 end
1052
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
1056 end
1057
1058 assert_match /undefined method.*not_a_method/, error.message
1059 end
1060 end