Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionmailer / test / mail_service_test.rb
blob09132ed7e6fba195918c071fb0555ca3f11aed51
1 require "#{File.dirname(__FILE__)}/abstract_unit"
3 class FunkyPathMailer < ActionMailer::Base
4   self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
6   def multipart_with_template_path_with_dots(recipient)
7     recipients recipient
8     subject    "Have a lovely picture"
9     from       "Chad Fowler <chad@chadfowler.com>"
10     attachment :content_type => "image/jpeg",
11       :body => "not really a jpeg, we're only testing, after all"
12   end
13 end
15 class TestMailer < ActionMailer::Base
16   def signed_up(recipient)
17     @recipients   = recipient
18     @subject      = "[Signed up] Welcome #{recipient}"
19     @from         = "system@loudthinking.com"
20     @sent_on      = Time.local(2004, 12, 12)
21     @body["recipient"] = recipient
22   end
24   def cancelled_account(recipient)
25     self.recipients = recipient
26     self.subject    = "[Cancelled] Goodbye #{recipient}"
27     self.from       = "system@loudthinking.com"
28     self.sent_on    = Time.local(2004, 12, 12)
29     self.body       = "Goodbye, Mr. #{recipient}"
30   end
32   def cc_bcc(recipient)
33     recipients recipient
34     subject    "testing bcc/cc"
35     from       "system@loudthinking.com"
36     sent_on    Time.local(2004, 12, 12)
37     cc         "nobody@loudthinking.com"
38     bcc        "root@loudthinking.com"
39     body       "Nothing to see here."
40   end
42   def iso_charset(recipient)
43     @recipients = recipient
44     @subject    = "testing isø charsets"
45     @from       = "system@loudthinking.com"
46     @sent_on    = Time.local 2004, 12, 12
47     @cc         = "nobody@loudthinking.com"
48     @bcc        = "root@loudthinking.com"
49     @body       = "Nothing to see here."
50     @charset    = "iso-8859-1"
51   end
53   def unencoded_subject(recipient)
54     @recipients = recipient
55     @subject    = "testing unencoded subject"
56     @from       = "system@loudthinking.com"
57     @sent_on    = Time.local 2004, 12, 12
58     @cc         = "nobody@loudthinking.com"
59     @bcc        = "root@loudthinking.com"
60     @body       = "Nothing to see here."
61   end
63   def extended_headers(recipient)
64     @recipients = recipient
65     @subject    = "testing extended headers"
66     @from       = "Grytøyr <stian1@example.net>"
67     @sent_on    = Time.local 2004, 12, 12
68     @cc         = "Grytøyr <stian2@example.net>"
69     @bcc        = "Grytøyr <stian3@example.net>"
70     @body       = "Nothing to see here."
71     @charset    = "iso-8859-1"
72   end
74   def utf8_body(recipient)
75     @recipients = recipient
76     @subject    = "testing utf-8 body"
77     @from       = "Foo áëô îü <extended@example.net>"
78     @sent_on    = Time.local 2004, 12, 12
79     @cc         = "Foo áëô îü <extended@example.net>"
80     @bcc        = "Foo áëô îü <extended@example.net>"
81     @body       = "åœö blah"
82     @charset    = "utf-8"
83   end
85   def multipart_with_mime_version(recipient)
86     recipients   recipient
87     subject      "multipart with mime_version"
88     from         "test@example.com"
89     sent_on      Time.local(2004, 12, 12)
90     mime_version "1.1"
91     content_type "multipart/alternative"
93     part "text/plain" do |p|
94       p.body = "blah"
95     end
97     part "text/html" do |p|
98       p.body = "<b>blah</b>"
99     end
100   end
102   def multipart_with_utf8_subject(recipient)
103     recipients   recipient
104     subject      "Foo áëô îü"
105     from         "test@example.com"
106     charset      "utf-8"
108     part "text/plain" do |p|
109       p.body = "blah"
110     end
112     part "text/html" do |p|
113       p.body = "<b>blah</b>"
114     end
115   end
117   def explicitly_multipart_example(recipient, ct=nil)
118     recipients   recipient
119     subject      "multipart example"
120     from         "test@example.com"
121     sent_on      Time.local(2004, 12, 12)
122     body         "plain text default"
123     content_type ct if ct
125     part "text/html" do |p|
126       p.charset = "iso-8859-1"
127       p.body = "blah"
128     end
130     attachment :content_type => "image/jpeg", :filename => "foo.jpg",
131       :body => "123456789"
132   end
134   def implicitly_multipart_example(recipient, cs = nil, order = nil)
135     @recipients = recipient
136     @subject    = "multipart example"
137     @from       = "test@example.com"
138     @sent_on    = Time.local 2004, 12, 12
139     @body       = { "recipient" => recipient }
140     @charset    = cs if cs
141     @implicit_parts_order = order if order
142   end
144   def implicitly_multipart_with_utf8
145     recipients "no.one@nowhere.test"
146     subject    "Foo áëô îü"
147     from       "some.one@somewhere.test"
148     template   "implicitly_multipart_example"
149     body       ({ "recipient" => "no.one@nowhere.test" })
150   end
152   def html_mail(recipient)
153     recipients   recipient
154     subject      "html mail"
155     from         "test@example.com"
156     body         "<em>Emphasize</em> <strong>this</strong>"
157     content_type "text/html"
158   end
160   def html_mail_with_underscores(recipient)
161     subject      "html mail with underscores"
162     body         %{<a href="http://google.com" target="_blank">_Google</a>}
163   end
165   def custom_template(recipient)
166     recipients recipient
167     subject    "[Signed up] Welcome #{recipient}"
168     from       "system@loudthinking.com"
169     sent_on    Time.local(2004, 12, 12)
170     template   "signed_up"
172     body["recipient"] = recipient
173   end
175   def custom_templating_extension(recipient)
176     recipients recipient
177     subject    "[Signed up] Welcome #{recipient}"
178     from       "system@loudthinking.com"
179     sent_on    Time.local(2004, 12, 12)
181     body["recipient"] = recipient
182   end
184   def various_newlines(recipient)
185     recipients   recipient
186     subject      "various newlines"
187     from         "test@example.com"
188     body         "line #1\nline #2\rline #3\r\nline #4\r\r" +
189                  "line #5\n\nline#6\r\n\r\nline #7"
190   end
192   def various_newlines_multipart(recipient)
193     recipients   recipient
194     subject      "various newlines multipart"
195     from         "test@example.com"
196     content_type "multipart/alternative"
197     part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r"
198     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"
199   end
201   def nested_multipart(recipient)
202     recipients   recipient
203     subject      "nested multipart"
204     from         "test@example.com"
205     content_type "multipart/mixed"
206     part :content_type => "multipart/alternative", :content_disposition => "inline", :headers => { "foo" => "bar" } do |p|
207       p.part :content_type => "text/plain", :body => "test text\nline #2"
208       p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2"
209     end
210     attachment :content_type => "application/octet-stream",:filename => "test.txt", :body => "test abcdefghijklmnopqstuvwxyz"
211   end
212   
213   def attachment_with_custom_header(recipient)
214     recipients   recipient
215     subject      "custom header in attachment"
216     from         "test@example.com"
217     content_type "multipart/related"
218     part :content_type => "text/html", :body => 'yo'
219     attachment :content_type => "image/jpeg",:filename => "test.jpeg", :body => "i am not a real picture", :headers => { 'Content-ID' => '<test@test.com>' }
220   end
222   def unnamed_attachment(recipient)
223     recipients   recipient
224     subject      "nested multipart"
225     from         "test@example.com"
226     content_type "multipart/mixed"
227     part :content_type => "text/plain", :body => "hullo"
228     attachment :content_type => "application/octet-stream", :body => "test abcdefghijklmnopqstuvwxyz"
229   end
231   def headers_with_nonalpha_chars(recipient)
232     recipients   recipient
233     subject      "nonalpha chars"
234     from         "One: Two <test@example.com>"
235     cc           "Three: Four <test@example.com>"
236     bcc          "Five: Six <test@example.com>"
237     body         "testing"
238   end
240   def custom_content_type_attributes
241     recipients   "no.one@nowhere.test"
242     subject      "custom content types"
243     from         "some.one@somewhere.test"
244     content_type "text/plain; format=flowed"
245     body         "testing"
246   end
248   def return_path
249     recipients   "no.one@nowhere.test"
250     subject      "return path test"
251     from         "some.one@somewhere.test"
252     body         "testing"
253     headers      "return-path" => "another@somewhere.test"
254   end
256   class <<self
257     attr_accessor :received_body
258   end
260   def receive(mail)
261     self.class.received_body = mail.body
262   end
265 uses_mocha 'ActionMailerTest' do
267 class ActionMailerTest < Test::Unit::TestCase
268   include ActionMailer::Quoting
270   def encode( text, charset="utf-8" )
271     quoted_printable( text, charset )
272   end
274   def new_mail( charset="utf-8" )
275     mail = TMail::Mail.new
276     mail.mime_version = "1.0"
277     if charset
278       mail.set_content_type "text", "plain", { "charset" => charset }
279     end
280     mail
281   end
283   # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
284   def setup
285     set_delivery_method :test
286     ActionMailer::Base.perform_deliveries = true
287     ActionMailer::Base.raise_delivery_errors = true
288     ActionMailer::Base.deliveries = []
290     @original_logger = TestMailer.logger
291     @recipient = 'test@localhost'
292   end
294   def teardown
295     TestMailer.logger = @original_logger
296     restore_delivery_method
297   end
299   def test_nested_parts
300     created = nil
301     assert_nothing_raised { created = TestMailer.create_nested_multipart(@recipient)}
302     assert_equal 2,created.parts.size
303     assert_equal 2,created.parts.first.parts.size
304     
305     assert_equal "multipart/mixed", created.content_type
306     assert_equal "multipart/alternative", created.parts.first.content_type
307     assert_equal "bar", created.parts.first.header['foo'].to_s
308     assert_equal "text/plain", created.parts.first.parts.first.content_type
309     assert_equal "text/html", created.parts.first.parts[1].content_type
310     assert_equal "application/octet-stream", created.parts[1].content_type
311   end
313   def test_attachment_with_custom_header
314     created = nil
315     assert_nothing_raised { created = TestMailer.create_attachment_with_custom_header(@recipient)}
316     assert_equal "<test@test.com>", created.parts[1].header['content-id'].to_s
317   end
319   def test_signed_up
320     expected = new_mail
321     expected.to      = @recipient
322     expected.subject = "[Signed up] Welcome #{@recipient}"
323     expected.body    = "Hello there, \n\nMr. #{@recipient}"
324     expected.from    = "system@loudthinking.com"
325     expected.date    = Time.local(2004, 12, 12)
327     created = nil
328     assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
329     assert_not_nil created
330     assert_equal expected.encoded, created.encoded
332     assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
333     assert_not_nil ActionMailer::Base.deliveries.first
334     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
335   end
336   
337   def test_custom_template
338     expected = new_mail
339     expected.to      = @recipient
340     expected.subject = "[Signed up] Welcome #{@recipient}"
341     expected.body    = "Hello there, \n\nMr. #{@recipient}"
342     expected.from    = "system@loudthinking.com"
343     expected.date    = Time.local(2004, 12, 12)
345     created = nil
346     assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) }
347     assert_not_nil created
348     assert_equal expected.encoded, created.encoded
349   end
351   def test_custom_templating_extension
352     #
353     # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
354     expected = new_mail
355     expected.to      = @recipient
356     expected.subject = "[Signed up] Welcome #{@recipient}"
357     expected.body    = "Hello there, \n\nMr. #{@recipient}"
358     expected.from    = "system@loudthinking.com"
359     expected.date    = Time.local(2004, 12, 12)
360     
361     # Stub the render method so no alternative renderers need be present.
362     ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
363     
364     # If the template is not registered, there should be no parts.
365     created = nil
366     assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
367     assert_not_nil created
368     assert_equal 0, created.parts.length
369     
370     ActionMailer::Base.register_template_extension('haml')
371     
372     # Now that the template is registered, there should be one part. The text/plain part.
373     created = nil
374     assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
375     assert_not_nil created
376     assert_equal 2, created.parts.length
377     assert_equal 'text/plain', created.parts[0].content_type
378     assert_equal 'text/html', created.parts[1].content_type
379   end
381   def test_cancelled_account
382     expected = new_mail
383     expected.to      = @recipient
384     expected.subject = "[Cancelled] Goodbye #{@recipient}"
385     expected.body    = "Goodbye, Mr. #{@recipient}"
386     expected.from    = "system@loudthinking.com"
387     expected.date    = Time.local(2004, 12, 12)
389     created = nil
390     assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) }
391     assert_not_nil created
392     assert_equal expected.encoded, created.encoded
394     assert_nothing_raised { TestMailer.deliver_cancelled_account(@recipient) }
395     assert_not_nil ActionMailer::Base.deliveries.first
396     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
397   end
398   
399   def test_cc_bcc
400     expected = new_mail
401     expected.to      = @recipient
402     expected.subject = "testing bcc/cc"
403     expected.body    = "Nothing to see here."
404     expected.from    = "system@loudthinking.com"
405     expected.cc      = "nobody@loudthinking.com"
406     expected.bcc     = "root@loudthinking.com"
407     expected.date    = Time.local 2004, 12, 12
409     created = nil
410     assert_nothing_raised do
411       created = TestMailer.create_cc_bcc @recipient
412     end
413     assert_not_nil created
414     assert_equal expected.encoded, created.encoded
416     assert_nothing_raised do
417       TestMailer.deliver_cc_bcc @recipient
418     end
420     assert_not_nil ActionMailer::Base.deliveries.first
421     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
422   end
424   def test_iso_charset
425     expected = new_mail( "iso-8859-1" )
426     expected.to      = @recipient
427     expected.subject = encode "testing isø charsets", "iso-8859-1"
428     expected.body    = "Nothing to see here."
429     expected.from    = "system@loudthinking.com"
430     expected.cc      = "nobody@loudthinking.com"
431     expected.bcc     = "root@loudthinking.com"
432     expected.date    = Time.local 2004, 12, 12
434     created = nil
435     assert_nothing_raised do
436       created = TestMailer.create_iso_charset @recipient
437     end
438     assert_not_nil created
439     assert_equal expected.encoded, created.encoded
441     assert_nothing_raised do
442       TestMailer.deliver_iso_charset @recipient
443     end
445     assert_not_nil ActionMailer::Base.deliveries.first
446     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
447   end
449   def test_unencoded_subject
450     expected = new_mail
451     expected.to      = @recipient
452     expected.subject = "testing unencoded subject"
453     expected.body    = "Nothing to see here."
454     expected.from    = "system@loudthinking.com"
455     expected.cc      = "nobody@loudthinking.com"
456     expected.bcc     = "root@loudthinking.com"
457     expected.date    = Time.local 2004, 12, 12
459     created = nil
460     assert_nothing_raised do
461       created = TestMailer.create_unencoded_subject @recipient
462     end
463     assert_not_nil created
464     assert_equal expected.encoded, created.encoded
466     assert_nothing_raised do
467       TestMailer.deliver_unencoded_subject @recipient
468     end
470     assert_not_nil ActionMailer::Base.deliveries.first
471     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
472   end
474   def test_instances_are_nil
475     assert_nil ActionMailer::Base.new
476     assert_nil TestMailer.new
477   end
479   def test_deliveries_array
480     assert_not_nil ActionMailer::Base.deliveries
481     assert_equal 0, ActionMailer::Base.deliveries.size
482     TestMailer.deliver_signed_up(@recipient)
483     assert_equal 1, ActionMailer::Base.deliveries.size
484     assert_not_nil ActionMailer::Base.deliveries.first
485   end
487   def test_perform_deliveries_flag
488     ActionMailer::Base.perform_deliveries = false
489     TestMailer.deliver_signed_up(@recipient)
490     assert_equal 0, ActionMailer::Base.deliveries.size
491     ActionMailer::Base.perform_deliveries = true
492     TestMailer.deliver_signed_up(@recipient)
493     assert_equal 1, ActionMailer::Base.deliveries.size
494   end
495   
496   def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
497     ActionMailer::Base.raise_delivery_errors = false
498     TestMailer.any_instance.expects(:perform_delivery_test).raises(Exception)
499     assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
500   end
502   def test_performs_delivery_via_sendmail
503     sm = mock()
504     sm.expects(:print).with(anything)
505     sm.expects(:flush)
506     IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm)
507     ActionMailer::Base.delivery_method = :sendmail
508     TestMailer.deliver_signed_up(@recipient)
509   end
511   def test_delivery_logs_sent_mail
512     mail = TestMailer.create_signed_up(@recipient)
513     logger = mock()
514     logger.expects(:info).with("Sent mail:\n #{mail.encoded}")
515     TestMailer.logger = logger
516     TestMailer.deliver_signed_up(@recipient)
517   end
519   def test_unquote_quoted_printable_subject
520     msg = <<EOF
521 From: me@example.com
522 Subject: =?utf-8?Q?testing_testing_=D6=A4?=
523 Content-Type: text/plain; charset=iso-8859-1
525 The body
527     mail = TMail::Mail.parse(msg)
528     assert_equal "testing testing \326\244", mail.subject
529     assert_equal "=?utf-8?Q?testing_testing_=D6=A4?=", mail.quoted_subject
530   end
532   def test_unquote_7bit_subject
533     msg = <<EOF
534 From: me@example.com
535 Subject: this == working?
536 Content-Type: text/plain; charset=iso-8859-1
538 The body
540     mail = TMail::Mail.parse(msg)
541     assert_equal "this == working?", mail.subject
542     assert_equal "this == working?", mail.quoted_subject
543   end
545   def test_unquote_7bit_body
546     msg = <<EOF
547 From: me@example.com
548 Subject: subject
549 Content-Type: text/plain; charset=iso-8859-1
550 Content-Transfer-Encoding: 7bit
552 The=3Dbody
554     mail = TMail::Mail.parse(msg)
555     assert_equal "The=3Dbody", mail.body.strip
556     assert_equal "The=3Dbody", mail.quoted_body.strip
557   end
559   def test_unquote_quoted_printable_body
560     msg = <<EOF
561 From: me@example.com
562 Subject: subject
563 Content-Type: text/plain; charset=iso-8859-1
564 Content-Transfer-Encoding: quoted-printable
566 The=3Dbody
568     mail = TMail::Mail.parse(msg)
569     assert_equal "The=body", mail.body.strip
570     assert_equal "The=3Dbody", mail.quoted_body.strip
571   end
573   def test_unquote_base64_body
574     msg = <<EOF
575 From: me@example.com
576 Subject: subject
577 Content-Type: text/plain; charset=iso-8859-1
578 Content-Transfer-Encoding: base64
580 VGhlIGJvZHk=
582     mail = TMail::Mail.parse(msg)
583     assert_equal "The body", mail.body.strip
584     assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip
585   end
587   def test_extended_headers
588     @recipient = "Grytøyr <test@localhost>"
590     expected = new_mail "iso-8859-1"
591     expected.to      = quote_address_if_necessary @recipient, "iso-8859-1"
592     expected.subject = "testing extended headers"
593     expected.body    = "Nothing to see here."
594     expected.from    = quote_address_if_necessary "Grytøyr <stian1@example.net>", "iso-8859-1"
595     expected.cc      = quote_address_if_necessary "Grytøyr <stian2@example.net>", "iso-8859-1"
596     expected.bcc     = quote_address_if_necessary "Grytøyr <stian3@example.net>", "iso-8859-1"
597     expected.date    = Time.local 2004, 12, 12
599     created = nil
600     assert_nothing_raised do
601       created = TestMailer.create_extended_headers @recipient
602     end
604     assert_not_nil created
605     assert_equal expected.encoded, created.encoded
607     assert_nothing_raised do
608       TestMailer.deliver_extended_headers @recipient
609     end
611     assert_not_nil ActionMailer::Base.deliveries.first
612     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
613   end
614   
615   def test_utf8_body_is_not_quoted
616     @recipient = "Foo áëô îü <extended@example.net>"
617     expected = new_mail "utf-8"
618     expected.to      = quote_address_if_necessary @recipient, "utf-8"
619     expected.subject = "testing utf-8 body"
620     expected.body    = "åœö blah"
621     expected.from    = quote_address_if_necessary @recipient, "utf-8"
622     expected.cc      = quote_address_if_necessary @recipient, "utf-8"
623     expected.bcc     = quote_address_if_necessary @recipient, "utf-8"
624     expected.date    = Time.local 2004, 12, 12
626     created = TestMailer.create_utf8_body @recipient
627     assert_match(/åœö blah/, created.encoded)
628   end
630   def test_multiple_utf8_recipients
631     @recipient = ["\"Foo áëô îü\" <extended@example.net>", "\"Example Recipient\" <me@example.com>"]
632     expected = new_mail "utf-8"
633     expected.to      = quote_address_if_necessary @recipient, "utf-8"
634     expected.subject = "testing utf-8 body"
635     expected.body    = "åœö blah"
636     expected.from    = quote_address_if_necessary @recipient.first, "utf-8"
637     expected.cc      = quote_address_if_necessary @recipient, "utf-8"
638     expected.bcc     = quote_address_if_necessary @recipient, "utf-8"
639     expected.date    = Time.local 2004, 12, 12
641     created = TestMailer.create_utf8_body @recipient
642     assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>\r/, created.encoded)
643     assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>, Example Recipient <me/, created.encoded)
644   end
646   def test_receive_decodes_base64_encoded_mail
647     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email")
648     TestMailer.receive(fixture)
649     assert_match(/Jamis/, TestMailer.received_body)
650   end
652   def test_receive_attachments
653     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email2")
654     mail = TMail::Mail.parse(fixture)
655     attachment = mail.attachments.last
656     assert_equal "smime.p7s", attachment.original_filename
657     assert_equal "application/pkcs7-signature", attachment.content_type
658   end
660   def test_decode_attachment_without_charset
661     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email3")
662     mail = TMail::Mail.parse(fixture)
663     attachment = mail.attachments.last
664     assert_equal 1026, attachment.read.length
665   end
667   def test_attachment_using_content_location
668     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email12")
669     mail = TMail::Mail.parse(fixture)
670     assert_equal 1, mail.attachments.length
671     assert_equal "Photo25.jpg", mail.attachments.first.original_filename
672   end
674   def test_attachment_with_text_type
675     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email13")
676     mail = TMail::Mail.parse(fixture)
677     assert mail.has_attachments?
678     assert_equal 1, mail.attachments.length
679     assert_equal "hello.rb", mail.attachments.first.original_filename
680   end
682   def test_decode_part_without_content_type
683     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email4")
684     mail = TMail::Mail.parse(fixture)
685     assert_nothing_raised { mail.body }
686   end
688   def test_decode_message_without_content_type
689     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email5")
690     mail = TMail::Mail.parse(fixture)
691     assert_nothing_raised { mail.body }
692   end
694   def test_decode_message_with_incorrect_charset
695     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email6")
696     mail = TMail::Mail.parse(fixture)
697     assert_nothing_raised { mail.body }
698   end
700   def test_multipart_with_mime_version
701     mail = TestMailer.create_multipart_with_mime_version(@recipient)
702     assert_equal "1.1", mail.mime_version
703   end
704   
705   def test_multipart_with_utf8_subject
706     mail = TestMailer.create_multipart_with_utf8_subject(@recipient)
707     assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded)
708   end
710   def test_implicitly_multipart_with_utf8
711     mail = TestMailer.create_implicitly_multipart_with_utf8
712     assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded)
713   end
715   def test_explicitly_multipart_messages
716     mail = TestMailer.create_explicitly_multipart_example(@recipient)
717     assert_equal 3, mail.parts.length
718     assert_nil mail.content_type
719     assert_equal "text/plain", mail.parts[0].content_type
721     assert_equal "text/html", mail.parts[1].content_type
722     assert_equal "iso-8859-1", mail.parts[1].sub_header("content-type", "charset")
723     assert_equal "inline", mail.parts[1].content_disposition
725     assert_equal "image/jpeg", mail.parts[2].content_type
726     assert_equal "attachment", mail.parts[2].content_disposition
727     assert_equal "foo.jpg", mail.parts[2].sub_header("content-disposition", "filename")
728     assert_equal "foo.jpg", mail.parts[2].sub_header("content-type", "name")
729     assert_nil mail.parts[2].sub_header("content-type", "charset")
730   end
732   def test_explicitly_multipart_with_content_type
733     mail = TestMailer.create_explicitly_multipart_example(@recipient, "multipart/alternative")
734     assert_equal 3, mail.parts.length
735     assert_equal "multipart/alternative", mail.content_type
736   end
738   def test_explicitly_multipart_with_invalid_content_type
739     mail = TestMailer.create_explicitly_multipart_example(@recipient, "text/xml")
740     assert_equal 3, mail.parts.length
741     assert_nil mail.content_type
742   end
744   def test_implicitly_multipart_messages
745     mail = TestMailer.create_implicitly_multipart_example(@recipient)
746     assert_equal 6, mail.parts.length
747     assert_equal "1.0", mail.mime_version
748     assert_equal "multipart/alternative", mail.content_type
749     assert_equal "text/yaml", mail.parts[0].content_type
750     assert_equal "utf-8", mail.parts[0].sub_header("content-type", "charset")
751     assert_equal "text/plain", mail.parts[2].content_type
752     assert_equal "utf-8", mail.parts[2].sub_header("content-type", "charset")
753     assert_equal "text/html", mail.parts[4].content_type
754     assert_equal "utf-8", mail.parts[4].sub_header("content-type", "charset")
755   end
757   def test_implicitly_multipart_messages_with_custom_order
758     mail = TestMailer.create_implicitly_multipart_example(@recipient, nil, ["text/yaml", "text/plain"])
759     assert_equal 6, mail.parts.length
760     assert_equal "text/html", mail.parts[0].content_type
761     assert_equal "text/plain", mail.parts[2].content_type
762     assert_equal "text/yaml", mail.parts[4].content_type
763   end
765   def test_implicitly_multipart_messages_with_charset
766     mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
768     assert_equal "multipart/alternative", mail.header['content-type'].body
769     
770     assert_equal 'iso-8859-1', mail.parts[0].sub_header("content-type", "charset")
771     assert_equal 'iso-8859-1', mail.parts[1].sub_header("content-type", "charset")
772     assert_equal 'iso-8859-1', mail.parts[2].sub_header("content-type", "charset")
773   end
775   def test_html_mail
776     mail = TestMailer.create_html_mail(@recipient)
777     assert_equal "text/html", mail.content_type
778   end
780   def test_html_mail_with_underscores
781     mail = TestMailer.create_html_mail_with_underscores(@recipient)
782     assert_equal %{<a href="http://google.com" target="_blank">_Google</a>}, mail.body
783   end
785   def test_various_newlines
786     mail = TestMailer.create_various_newlines(@recipient)
787     assert_equal("line #1\nline #2\nline #3\nline #4\n\n" +
788                  "line #5\n\nline#6\n\nline #7", mail.body)
789   end
791   def test_various_newlines_multipart
792     mail = TestMailer.create_various_newlines_multipart(@recipient)
793     assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body
794     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
795   end
796   
797   def test_headers_removed_on_smtp_delivery
798     ActionMailer::Base.delivery_method = :smtp
799     TestMailer.deliver_cc_bcc(@recipient)
800     assert MockSMTP.deliveries[0][2].include?("root@loudthinking.com")
801     assert MockSMTP.deliveries[0][2].include?("nobody@loudthinking.com")
802     assert MockSMTP.deliveries[0][2].include?(@recipient)
803     assert_match %r{^Cc: nobody@loudthinking.com}, MockSMTP.deliveries[0][0]
804     assert_match %r{^To: #{@recipient}}, MockSMTP.deliveries[0][0]
805     assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0]
806   end
808   def test_recursive_multipart_processing
809     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
810     mail = TMail::Mail.parse(fixture)
811     assert_equal "This is the first part.\n\nAttachment: test.rb\nAttachment: test.pdf\n\n\nAttachment: smime.p7s\n", mail.body
812   end
814   def test_decode_encoded_attachment_filename
815     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8")
816     mail = TMail::Mail.parse(fixture)
817     attachment = mail.attachments.last
818     assert_equal "01QuienTeDijat.Pitbull.mp3", attachment.original_filename
819   end
821   def test_wrong_mail_header
822     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email9")
823     assert_raise(TMail::SyntaxError) { TMail::Mail.parse(fixture) }
824   end
826   def test_decode_message_with_unknown_charset
827     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10")
828     mail = TMail::Mail.parse(fixture)
829     assert_nothing_raised { mail.body }
830   end
832   def test_decode_message_with_unquoted_atchar_in_header
833     fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email11")
834     mail = TMail::Mail.parse(fixture)
835     assert_not_nil mail.from
836   end
838   def test_empty_header_values_omitted
839     result = TestMailer.create_unnamed_attachment(@recipient).encoded
840     assert_match %r{Content-Type: application/octet-stream[^;]}, result
841     assert_match %r{Content-Disposition: attachment[^;]}, result
842   end
844   def test_headers_with_nonalpha_chars
845     mail = TestMailer.create_headers_with_nonalpha_chars(@recipient)
846     assert !mail.from_addrs.empty?
847     assert !mail.cc_addrs.empty?
848     assert !mail.bcc_addrs.empty?
849     assert_match(/:/, mail.from_addrs.to_s)
850     assert_match(/:/, mail.cc_addrs.to_s)
851     assert_match(/:/, mail.bcc_addrs.to_s)
852   end
854   def test_deliver_with_mail_object
855     mail = TestMailer.create_headers_with_nonalpha_chars(@recipient)
856     assert_nothing_raised { TestMailer.deliver(mail) }
857     assert_equal 1, TestMailer.deliveries.length
858   end
860   def test_multipart_with_template_path_with_dots
861     mail = FunkyPathMailer.create_multipart_with_template_path_with_dots(@recipient)
862     assert_equal 2, mail.parts.length
863   end
865   def test_custom_content_type_attributes
866     mail = TestMailer.create_custom_content_type_attributes
867     assert_match %r{format=flowed}, mail['content-type'].to_s
868     assert_match %r{charset=utf-8}, mail['content-type'].to_s
869   end
871   def test_return_path_with_create
872     mail = TestMailer.create_return_path
873     assert_equal "<another@somewhere.test>", mail['return-path'].to_s
874   end
876   def test_return_path_with_deliver
877     ActionMailer::Base.delivery_method = :smtp
878     TestMailer.deliver_return_path
879     assert_match %r{^Return-Path: <another@somewhere.test>}, MockSMTP.deliveries[0][0]
880   end
883 end # uses_mocha
885 class InheritableTemplateRootTest < Test::Unit::TestCase
886   def test_attr
887     expected = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
888     assert_equal expected, FunkyPathMailer.template_root
890     sub = Class.new(FunkyPathMailer)
891     sub.template_root = 'test/path'
893     assert_equal 'test/path', sub.template_root
894     assert_equal expected, FunkyPathMailer.template_root
895   end
898 class MethodNamingTest < Test::Unit::TestCase
899   class TestMailer < ActionMailer::Base
900     def send
901       body 'foo'
902     end
903   end
905   def setup
906     set_delivery_method :test
907     ActionMailer::Base.perform_deliveries = true
908     ActionMailer::Base.deliveries = []
909   end
911   def teardown
912     restore_delivery_method
913   end
915   def test_send_method
916     assert_nothing_raised do
917       assert_emails 1 do
918         TestMailer.deliver_send
919       end
920     end
921   end