Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionmailer/test/mail_service_test.rb

Revision 9218, 31.9 kB (checked in by bitsweat, 8 months ago)

Ruby 1.9 compat: compare with same encoding

  • Property svn:executable set to *
Line 
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 iso_charset(recipient)
44     @recipients = recipient
45     @subject    = "testing isÞ charsets"
46     @from       = "system@loudthinking.com"
47     @sent_on    = Time.local 2004, 12, 12
48     @cc         = "nobody@loudthinking.com"
49     @bcc        = "root@loudthinking.com"
50     @body       = "Nothing to see here."
51     @charset    = "iso-8859-1"
52   end
53
54   def unencoded_subject(recipient)
55     @recipients = recipient
56     @subject    = "testing unencoded subject"
57     @from       = "system@loudthinking.com"
58     @sent_on    = Time.local 2004, 12, 12
59     @cc         = "nobody@loudthinking.com"
60     @bcc        = "root@loudthinking.com"
61     @body       = "Nothing to see here."
62   end
63
64   def extended_headers(recipient)
65     @recipients = recipient
66     @subject    = "testing extended headers"
67     @from       = "GrytÞyr <stian1@example.net>"
68     @sent_on    = Time.local 2004, 12, 12
69     @cc         = "GrytÞyr <stian2@example.net>"
70     @bcc        = "GrytÞyr <stian3@example.net>"
71     @body       = "Nothing to see here."
72     @charset    = "iso-8859-1"
73   end
74
75   def utf8_body(recipient)
76     @recipients = recipient
77     @subject    = "testing utf-8 body"
78     @from       = "Foo áëÎ îÌ <extended@example.net>"
79     @sent_on    = Time.local 2004, 12, 12
80     @cc         = "Foo áëÎ îÌ <extended@example.net>"
81     @bcc        = "Foo áëÎ îÌ <extended@example.net>"
82     @body       = "åœö blah"
83     @charset    = "utf-8"
84   end
85
86   def multipart_with_mime_version(recipient)
87     recipients   recipient
88     subject      "multipart with mime_version"
89     from         "test@example.com"
90     sent_on      Time.local(2004, 12, 12)
91     mime_version "1.1"
92     content_type "multipart/alternative"
93
94     part "text/plain" do |p|
95       p.body = "blah"
96     end
97
98     part "text/html" do |p|
99       p.body = "<b>blah</b>"
100     end
101   end
102
103   def multipart_with_utf8_subject(recipient)
104     recipients   recipient
105     subject      "Foo áëÎ îÌ"
106     from         "test@example.com"
107     charset      "utf-8"
108
109     part "text/plain" do |p|
110       p.body = "blah"
111     end
112
113     part "text/html" do |p|
114       p.body = "<b>blah</b>"
115     end
116   end
117
118   def explicitly_multipart_example(recipient, ct=nil)
119     recipients   recipient
120     subject      "multipart example"
121     from         "test@example.com"
122     sent_on      Time.local(2004, 12, 12)
123     body         "plain text default"
124     content_type ct if ct
125
126     part "text/html" do |p|
127       p.charset = "iso-8859-1"
128       p.body = "blah"
129     end
130
131     attachment :content_type => "image/jpeg", :filename => "foo.jpg",
132       :body => "123456789"
133   end
134
135   def implicitly_multipart_example(recipient, cs = nil, order = nil)
136     @recipients = recipient
137     @subject    = "multipart example"
138     @from       = "test@example.com"
139     @sent_on    = Time.local 2004, 12, 12
140     @body       = { "recipient" => recipient }
141     @charset    = cs if cs
142     @implicit_parts_order = order if order
143   end
144
145   def implicitly_multipart_with_utf8
146     recipients "no.one@nowhere.test"
147     subject    "Foo áëÎ îÌ"
148     from       "some.one@somewhere.test"
149     template   "implicitly_multipart_example"
150     body       ({ "recipient" => "no.one@nowhere.test" })
151   end
152
153   def html_mail(recipient)
154     recipients   recipient
155     subject      "html mail"
156     from         "test@example.com"
157     body         "<em>Emphasize</em> <strong>this</strong>"
158     content_type "text/html"
159   end
160
161   def html_mail_with_underscores(recipient)
162     subject      "html mail with underscores"
163     body         %{<a href="http://google.com" target="_blank">_Google</a>}
164   end
165
166   def custom_template(recipient)
167     recipients recipient
168     subject    "[Signed up] Welcome #{recipient}"
169     from       "system@loudthinking.com"
170     sent_on    Time.local(2004, 12, 12)
171     template   "signed_up"
172
173     body["recipient"] = recipient
174   end
175
176   def custom_templating_extension(recipient)
177     recipients recipient
178     subject    "[Signed up] Welcome #{recipient}"
179     from       "system@loudthinking.com"
180     sent_on    Time.local(2004, 12, 12)
181
182     body["recipient"] = recipient
183   end
184
185   def various_newlines(recipient)
186     recipients   recipient
187     subject      "various newlines"
188     from         "test@example.com"
189     body         "line #1\nline #2\rline #3\r\nline #4\r\r" +
190                  "line #5\n\nline#6\r\n\r\nline #7"
191   end
192
193   def various_newlines_multipart(recipient)
194     recipients   recipient
195     subject      "various newlines multipart"
196     from         "test@example.com"
197     content_type "multipart/alternative"
198     part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r"
199     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"
200   end
201
202   def nested_multipart(recipient)
203     recipients   recipient
204     subject      "nested multipart"
205     from         "test@example.com"
206     content_type "multipart/mixed"
207     part :content_type => "multipart/alternative", :content_disposition => "inline", :headers => { "foo" => "bar" } do |p|
208       p.part :content_type => "text/plain", :body => "test text\nline #2"
209       p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2"
210     end
211     attachment :content_type => "application/octet-stream",:filename => "test.txt", :body => "test abcdefghijklmnopqstuvwxyz"
212   end
213  
214   def nested_multipart_with_body(recipient)
215     recipients   recipient
216     subject      "nested multipart with body"
217     from         "test@example.com"
218     content_type "multipart/mixed"
219     part :content_type => "multipart/alternative", :content_disposition => "inline", :body => "Nothing to see here." do |p|
220       p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>"
221     end
222   end
223
224   def attachment_with_custom_header(recipient)
225     recipients   recipient
226     subject      "custom header in attachment"
227     from         "test@example.com"
228     content_type "multipart/related"
229     part :content_type => "text/html", :body => 'yo'
230     attachment :content_type => "image/jpeg",:filename => "test.jpeg", :body => "i am not a real picture", :headers => { 'Content-ID' => '<test@test.com>' }
231   end
232
233   def unnamed_attachment(recipient)
234     recipients   recipient
235     subject      "nested multipart"
236     from         "test@example.com"
237     content_type "multipart/mixed"
238     part :content_type => "text/plain", :body => "hullo"
239     attachment :content_type => "application/octet-stream", :body => "test abcdefghijklmnopqstuvwxyz"
240   end
241
242   def headers_with_nonalpha_chars(recipient)
243     recipients   recipient
244     subject      "nonalpha chars"
245     from         "One: Two <test@example.com>"
246     cc           "Three: Four <test@example.com>"
247     bcc          "Five: Six <test@example.com>"
248     body         "testing"
249   end
250
251   def custom_content_type_attributes
252     recipients   "no.one@nowhere.test"
253     subject      "custom content types"
254     from         "some.one@somewhere.test"
255     content_type "text/plain; format=flowed"
256     body         "testing"
257   end
258
259   def return_path
260     recipients   "no.one@nowhere.test"
261     subject      "return path test"
262     from         "some.one@somewhere.test"
263     body         "testing"
264     headers      "return-path" => "another@somewhere.test"
265   end
266
267   class <<self
268     attr_accessor :received_body
269   end
270
271   def receive(mail)
272     self.class.received_body = mail.body
273   end
274 end
275
276 uses_mocha 'ActionMailerTest' do
277
278 class ActionMailerTest < Test::Unit::TestCase
279   include ActionMailer::Quoting
280
281   def encode( text, charset="utf-8" )
282     quoted_printable( text, charset )
283   end
284
285   def new_mail( charset="utf-8" )
286     mail = TMail::Mail.new
287     mail.mime_version = "1.0"
288     if charset
289       mail.set_content_type "text", "plain", { "charset" => charset }
290     end
291     mail
292   end
293
294   # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
295   def setup
296     set_delivery_method :test
297     ActionMailer::Base.perform_deliveries = true
298     ActionMailer::Base.raise_delivery_errors = true
299     ActionMailer::Base.deliveries = []
300
301     @original_logger = TestMailer.logger
302     @recipient = 'test@localhost'
303   end
304
305   def teardown
306     TestMailer.logger = @original_logger
307     restore_delivery_method
308   end
309
310   def test_nested_parts
311     created = nil
312     assert_nothing_raised { created = TestMailer.create_nested_multipart(@recipient)}
313     assert_equal 2,created.parts.size
314     assert_equal 2,created.parts.first.parts.size
315    
316     assert_equal "multipart/mixed", created.content_type
317     assert_equal "multipart/alternative", created.parts.first.content_type
318     assert_equal "bar", created.parts.first.header['foo'].to_s
319     assert_equal "text/plain", created.parts.first.parts.first.content_type
320     assert_equal "text/html", created.parts.first.parts[1].content_type
321     assert_equal "application/octet-stream", created.parts[1].content_type
322   end
323
324   def test_nested_parts_with_body
325     created = nil
326     assert_nothing_raised { created = TestMailer.create_nested_multipart_with_body(@recipient)}
327     assert_equal 1,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 "Nothing to see here.", created.parts.first.parts.first.body
333     assert_equal "text/plain", created.parts.first.parts.first.content_type
334     assert_equal "text/html", created.parts.first.parts[1].content_type
335   end
336
337   def test_attachment_with_custom_header
338     created = nil
339     assert_nothing_raised { created = TestMailer.create_attachment_with_custom_header(@recipient)}
340     assert_equal "<test@test.com>", created.parts[1].header['content-id'].to_s
341   end
342
343   def test_signed_up
344     expected = new_mail
345     expected.to      = @recipient
346     expected.subject = "[Signed up] Welcome #{@recipient}"
347     expected.body    = "Hello there, \n\nMr. #{@recipient}"
348     expected.from    = "system@loudthinking.com"
349     expected.date    = Time.local(2004, 12, 12)
350
351     created = nil
352     assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
353     assert_not_nil created
354     assert_equal expected.encoded, created.encoded
355
356     assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
357     assert_not_nil ActionMailer::Base.deliveries.first
358     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
359   end
360  
361   def test_custom_template
362     expected = new_mail
363     expected.to      = @recipient
364     expected.subject = "[Signed up] Welcome #{@recipient}"
365     expected.body    = "Hello there, \n\nMr. #{@recipient}"
366     expected.from    = "system@loudthinking.com"
367     expected.date    = Time.local(2004, 12, 12)
368
369     created = nil
370     assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) }
371     assert_not_nil created
372     assert_equal expected.encoded, created.encoded
373   end
374
375   def test_custom_templating_extension
376     #
377     # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
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     # Stub the render method so no alternative renderers need be present.
386     ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
387    
388     # If the template is not registered, there should be no parts.
389     created = nil
390     assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
391     assert_not_nil created
392     assert_equal 0, created.parts.length
393    
394     ActionMailer::Base.register_template_extension('haml')
395    
396     # Now that the template is registered, there should be one part. The text/plain part.
397     created = nil
398     assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
399     assert_not_nil created
400     assert_equal 2, created.parts.length
401     assert_equal 'text/plain', created.parts[0].content_type
402     assert_equal 'text/html', created.parts[1].content_type
403   end
404
405   def test_cancelled_account
406     expected = new_mail
407     expected.to      = @recipient
408     expected.subject = "[Cancelled] Goodbye #{@recipient}"
409     expected.body    = "Goodbye, Mr. #{@recipient}"
410     expected.from    = "system@loudthinking.com"
411     expected.date    = Time.local(2004, 12, 12)
412
413     created = nil
414     assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) }
415     assert_not_nil created
416     assert_equal expected.encoded, created.encoded
417
418     assert_nothing_raised { TestMailer.deliver_cancelled_account(@recipient) }
419     assert_not_nil ActionMailer::Base.deliveries.first
420     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
421   end
422  
423   def test_cc_bcc
424     expected = new_mail
425     expected.to      = @recipient
426     expected.subject = "testing bcc/cc"
427     expected.body    = "Nothing to see here."
428     expected.from    = "system@loudthinking.com"
429     expected.cc      = "nobody@loudthinking.com"
430     expected.bcc     = "root@loudthinking.com"
431     expected.date    = Time.local 2004, 12, 12
432
433     created = nil
434     assert_nothing_raised do
435       created = TestMailer.create_cc_bcc @recipient
436     end
437     assert_not_nil created
438     assert_equal expected.encoded, created.encoded
439
440     assert_nothing_raised do
441       TestMailer.deliver_cc_bcc @recipient
442     end
443
444     assert_not_nil ActionMailer::Base.deliveries.first
445     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
446   end
447
448   def test_iso_charset
449     expected = new_mail( "iso-8859-1" )
450     expected.to      = @recipient
451     expected.subject = encode "testing isÞ charsets", "iso-8859-1"
452     expected.body    = "Nothing to see here."
453     expected.from    = "system@loudthinking.com"
454     expected.cc      = "nobody@loudthinking.com"
455     expected.bcc     = "root@loudthinking.com"
456     expected.date    = Time.local 2004, 12, 12
457
458     created = nil
459     assert_nothing_raised do
460       created = TestMailer.create_iso_charset @recipient
461     end
462     assert_not_nil created
463     assert_equal expected.encoded, created.encoded
464
465     assert_nothing_raised do
466       TestMailer.deliver_iso_charset @recipient
467     end
468
469     assert_not_nil ActionMailer::Base.deliveries.first
470     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
471   end
472
473   def test_unencoded_subject
474     expected = new_mail
475     expected.to      = @recipient
476     expected.subject = "testing unencoded subject"
477     expected.body    = "Nothing to see here."
478     expected.from    = "system@loudthinking.com"
479     expected.cc      = "nobody@loudthinking.com"
480     expected.bcc     = "root@loudthinking.com"
481     expected.date    = Time.local 2004, 12, 12
482
483     created = nil
484     assert_nothing_raised do
485       created = TestMailer.create_unencoded_subject @recipient
486     end
487     assert_not_nil created
488     assert_equal expected.encoded, created.encoded
489
490     assert_nothing_raised do
491       TestMailer.deliver_unencoded_subject @recipient
492     end
493
494     assert_not_nil ActionMailer::Base.deliveries.first
495     assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
496   end
497
498   def test_instances_are_nil
499     assert_nil ActionMailer::Base.new
500     assert_nil TestMailer.new
501   end
502
503   def test_deliveries_array
504     assert_not_nil ActionMailer::Base.deliveries
505     assert_equal 0, ActionMailer::Base.deliveries.size
506     TestMailer.deliver_signed_up(@recipient)
507     assert_equal 1, ActionMailer::Base.deliveries.size
508     assert_not_nil ActionMailer::Base.deliveries.first
509   end
510
511   def test_perform_deliveries_flag
512     ActionMailer::Base.perform_deliveries = false
513     TestMailer.deliver_signed_up(@recipient)
514     assert_equal 0, ActionMailer::Base.deliveries.size
515     ActionMailer::Base.perform_deliveries = true
516     TestMailer.deliver_signed_up(@recipient)
517     assert_equal 1, ActionMailer::Base.deliveries.size
518   end
519  
520   def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
521     ActionMailer::Base.raise_delivery_errors = false
522     TestMailer.any_instance.expects(:perform_delivery_test).raises(Exception)
523     assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
524   end
525
526   def test_performs_delivery_via_sendmail
527     sm = mock()
528     sm.expects(:print).with(anything)
529     sm.expects(:flush)
530     IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm)
531     ActionMailer::Base.delivery_method = :sendmail
532     TestMailer.deliver_signed_up(@recipient