Updated README.rdoc again
[feedcatcher.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 class ActionMailerTest < Test::Unit::TestCase
293 include ActionMailer::Quoting
294
295 def encode( text, charset="utf-8" )
296 quoted_printable( text, charset )
297 end
298
299 def new_mail( charset="utf-8" )
300 mail = TMail::Mail.new
301 mail.mime_version = "1.0"
302 if charset
303 mail.set_content_type "text", "plain", { "charset" => charset }
304 end
305 mail
306 end
307
308 # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
309 def setup
310 set_delivery_method :test
311 ActionMailer::Base.perform_deliveries = true
312 ActionMailer::Base.raise_delivery_errors = true
313 ActionMailer::Base.deliveries = []
314
315 @original_logger = TestMailer.logger
316 @recipient = 'test@localhost'
317 end
318
319 def teardown
320 TestMailer.logger = @original_logger
321 restore_delivery_method
322 end
323
324 def test_nested_parts
325 created = nil
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
329
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
337 end
338
339 def test_nested_parts_with_body
340 created = nil
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
344
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
350 end
351
352 def test_attachment_with_custom_header
353 created = nil
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
356 end
357
358 def test_signed_up
359 expected = new_mail
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)
365
366 created = nil
367 assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
368 assert_not_nil created
369 assert_equal expected.encoded, created.encoded
370
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
374 end
375
376 def test_custom_template
377 expected = new_mail
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)
383
384 created = nil
385 assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) }
386 assert_not_nil created
387 assert_equal expected.encoded, created.encoded
388 end
389
390 def test_custom_templating_extension
391 assert ActionView::Template.template_handler_extensions.include?("haml"), "haml extension was not registered"
392
393 # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
394 expected = new_mail
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)
400
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}")
403
404 # Now that the template is registered, there should be one part. The text/plain part.
405 created = nil
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
411 end
412
413 def test_cancelled_account
414 expected = new_mail
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)
420
421 created = nil
422 assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) }
423 assert_not_nil created
424 assert_equal expected.encoded, created.encoded
425
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
429 end
430
431 def test_cc_bcc
432 expected = new_mail
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
440
441 created = nil
442 assert_nothing_raised do
443 created = TestMailer.create_cc_bcc @recipient
444 end
445 assert_not_nil created
446 assert_equal expected.encoded, created.encoded
447
448 assert_nothing_raised do
449 TestMailer.deliver_cc_bcc @recipient
450 end
451
452 assert_not_nil ActionMailer::Base.deliveries.first
453 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
454 end
455
456 def test_reply_to
457 expected = new_mail
458
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
465
466 created = nil
467 assert_nothing_raised do
468 created = TestMailer.create_different_reply_to @recipient
469 end
470 assert_not_nil created
471 assert_equal expected.encoded, created.encoded
472
473 assert_nothing_raised do
474 TestMailer.deliver_different_reply_to @recipient
475 end
476
477 assert_not_nil ActionMailer::Base.deliveries.first
478 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
479 end
480
481 def test_iso_charset
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
490
491 created = nil
492 assert_nothing_raised do
493 created = TestMailer.create_iso_charset @recipient
494 end
495 assert_not_nil created
496 assert_equal expected.encoded, created.encoded
497
498 assert_nothing_raised do
499 TestMailer.deliver_iso_charset @recipient
500 end
501
502 assert_not_nil ActionMailer::Base.deliveries.first
503 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
504 end
505
506 def test_unencoded_subject
507 expected = new_mail
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
515
516 created = nil
517 assert_nothing_raised do
518 created = TestMailer.create_unencoded_subject @recipient
519 end
520 assert_not_nil created
521 assert_equal expected.encoded, created.encoded
522
523 assert_nothing_raised do
524 TestMailer.deliver_unencoded_subject @recipient
525 end
526
527 assert_not_nil ActionMailer::Base.deliveries.first
528 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
529 end
530
531 def test_instances_are_nil
532 assert_nil ActionMailer::Base.new
533 assert_nil TestMailer.new
534 end
535
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
542 end
543
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
551 end
552
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) }
557 end
558
559 def test_performs_delivery_via_sendmail
560 sm = mock()
561 sm.expects(:print).with(anything)
562 sm.expects(:flush)
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)
566 end
567
568 def test_delivery_logs_sent_mail
569 mail = TestMailer.create_signed_up(@recipient)
570 logger = mock()
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)
575 end
576
577 def test_unquote_quoted_printable_subject
578 msg = <<EOF
579 From: me@example.com
580 Subject: =?utf-8?Q?testing_testing_=D6=A4?=
581 Content-Type: text/plain; charset=iso-8859-1
582
583 The body
584 EOF
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
588 end
589
590 def test_unquote_7bit_subject
591 msg = <<EOF
592 From: me@example.com
593 Subject: this == working?
594 Content-Type: text/plain; charset=iso-8859-1
595
596 The body
597 EOF
598 mail = TMail::Mail.parse(msg)
599 assert_equal "this == working?", mail.subject
600 assert_equal "this == working?", mail.quoted_subject
601 end
602
603 def test_unquote_7bit_body
604 msg = <<EOF
605 From: me@example.com
606 Subject: subject
607 Content-Type: text/plain; charset=iso-8859-1
608 Content-Transfer-Encoding: 7bit
609
610 The=3Dbody
611 EOF
612 mail = TMail::Mail.parse(msg)
613 assert_equal "The=3Dbody", mail.body.strip
614 assert_equal "The=3Dbody", mail.quoted_body.strip
615 end
616
617 def test_unquote_quoted_printable_body
618 msg = <<EOF
619 From: me@example.com
620 Subject: subject
621 Content-Type: text/plain; charset=iso-8859-1
622 Content-Transfer-Encoding: quoted-printable
623
624 The=3Dbody
625 EOF
626 mail = TMail::Mail.parse(msg)
627 assert_equal "The=body", mail.body.strip
628 assert_equal "The=3Dbody", mail.quoted_body.strip
629 end
630
631 def test_unquote_base64_body
632 msg = <<EOF
633 From: me@example.com
634 Subject: subject
635 Content-Type: text/plain; charset=iso-8859-1
636 Content-Transfer-Encoding: base64
637
638 VGhlIGJvZHk=
639 EOF
640 mail = TMail::Mail.parse(msg)
641 assert_equal "The body", mail.body.strip
642 assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip
643 end
644
645 def test_extended_headers
646 @recipient = "Grytøyr <test@localhost>"
647
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
656
657 created = nil
658 assert_nothing_raised do
659 created = TestMailer.create_extended_headers @recipient
660 end
661
662 assert_not_nil created
663 assert_equal expected.encoded, created.encoded
664
665 assert_nothing_raised do
666 TestMailer.deliver_extended_headers @recipient
667 end
668
669 assert_not_nil ActionMailer::Base.deliveries.first
670 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
671 end
672
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
683
684 created = TestMailer.create_utf8_body @recipient
685 assert_match(/åœö blah/, created.encoded)
686 end
687
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
698
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)
702 end
703
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)
708 end
709
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
716 end
717
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
723 end
724
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
730 end
731
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
738 end
739
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 }
744 end
745
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 }
750 end
751
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 }
756 end
757
758 def test_multipart_with_mime_version
759 mail = TestMailer.create_multipart_with_mime_version(@recipient)
760 assert_equal "1.1", mail.mime_version
761 end
762
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)
766 end
767
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)
771 end
772
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
778
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
782
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")
788 end
789
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
794 end
795
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
800 end
801
802 def test_implicitly_multipart_messages
803 assert ActionView::Template.template_handler_extensions.include?("bak"), "bak extension was not registered"
804
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")
815 end
816
817 def test_implicitly_multipart_messages_with_custom_order
818 assert ActionView::Template.template_handler_extensions.include?("bak"), "bak extension was not registered"
819
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
825 end
826
827 def test_implicitly_multipart_messages_with_charset
828 mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
829
830 assert_equal "multipart/alternative", mail.header['content-type'].body
831
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")
835 end
836
837 def test_html_mail
838 mail = TestMailer.create_html_mail(@recipient)
839 assert_equal "text/html", mail.content_type
840 end
841
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
845 end
846
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)
851 end
852
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
857 end
858
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]
868 end
869
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
874 end
875
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
880
881 expected = "01 Quien Te Dij\212at. Pitbull.mp3"
882 expected.force_encoding(Encoding::ASCII_8BIT) if expected.respond_to?(:force_encoding)
883
884 assert_equal expected, attachment.original_filename
885 end
886
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) }
890 end
891
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 }
896 end
897
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
902 end
903
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)
912 end
913
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
918 end
919
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
925 end
926
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
931 end
932
933 def test_return_path_with_create
934 mail = TestMailer.create_return_path
935 assert_equal "<another@somewhere.test>", mail['return-path'].to_s
936 end
937
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
943 end
944
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
948 end
949
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)
956 end
957
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)
964 end
965
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)
972 ensure
973 ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
974 end
975 end
976
977 class InheritableTemplateRootTest < Test::Unit::TestCase
978 def test_attr
979 expected = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
980 assert_equal expected, FunkyPathMailer.template_root.to_s
981
982 sub = Class.new(FunkyPathMailer)
983 sub.template_root = 'test/path'
984
985 assert_equal 'test/path', sub.template_root.to_s
986 assert_equal expected, FunkyPathMailer.template_root.to_s
987 end
988 end
989
990 class MethodNamingTest < Test::Unit::TestCase
991 class TestMailer < ActionMailer::Base
992 def send
993 body 'foo'
994 end
995 end
996
997 def setup
998 set_delivery_method :test
999 ActionMailer::Base.perform_deliveries = true
1000 ActionMailer::Base.deliveries = []
1001 end
1002
1003 def teardown
1004 restore_delivery_method
1005 end
1006
1007 def test_send_method
1008 assert_nothing_raised do
1009 assert_emails 1 do
1010 TestMailer.deliver_send
1011 end
1012 end
1013 end
1014 end
1015
1016 class RespondToTest < Test::Unit::TestCase
1017 class RespondToMailer < ActionMailer::Base; end
1018
1019 def setup
1020 set_delivery_method :test
1021 end
1022
1023 def teardown
1024 restore_delivery_method
1025 end
1026
1027 def test_should_respond_to_new
1028 assert RespondToMailer.respond_to?(:new)
1029 end
1030
1031 def test_should_respond_to_create_with_template_suffix
1032 assert RespondToMailer.respond_to?(:create_any_old_template)
1033 end
1034
1035 def test_should_respond_to_deliver_with_template_suffix
1036 assert RespondToMailer.respond_to?(:deliver_any_old_template)
1037 end
1038
1039 def test_should_not_respond_to_new_with_template_suffix
1040 assert !RespondToMailer.respond_to?(:new_any_old_template)
1041 end
1042
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)
1045 end
1046
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)
1049 end
1050
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)
1053 end
1054
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)
1057 end
1058
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)
1061 end
1062
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)
1065 end
1066
1067 def test_should_not_respond_to_method_where_deliver_is_not_a_suffix
1068 assert !RespondToMailer.respond_to?(:foo_deliver_template)
1069 end
1070
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
1074 end
1075
1076 assert_match /undefined method.*not_a_method/, error.message
1077 end
1078 end