2
require 'abstract_unit'
4
class FunkyPathMailer < ActionMailer::Base
5
self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
7
def multipart_with_template_path_with_dots(recipient)
9
subject "Have a lovely picture"
10
from "Chad Fowler <chad@chadfowler.com>"
11
attachment :content_type => "image/jpeg",
12
:body => "not really a jpeg, we're only testing, after all"
16
class TestMailer < ActionMailer::Base
17
def signed_up(recipient)
18
@recipients = recipient
19
@subject = "[Signed up] Welcome #{recipient}"
20
@from = "system@loudthinking.com"
21
@body["recipient"] = recipient
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}"
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."
42
def different_reply_to(recipient)
44
subject "testing reply_to"
45
from "system@loudthinking.com"
46
sent_on Time.local(2008, 5, 23)
47
reply_to "atraver@gmail.com"
48
body "Nothing to see here."
51
def iso_charset(recipient)
52
@recipients = recipient
53
@subject = "testing isø charsets"
54
@from = "system@loudthinking.com"
55
@sent_on = Time.local 2004, 12, 12
56
@cc = "nobody@loudthinking.com"
57
@bcc = "root@loudthinking.com"
58
@body = "Nothing to see here."
59
@charset = "iso-8859-1"
62
def unencoded_subject(recipient)
63
@recipients = recipient
64
@subject = "testing unencoded subject"
65
@from = "system@loudthinking.com"
66
@sent_on = Time.local 2004, 12, 12
67
@cc = "nobody@loudthinking.com"
68
@bcc = "root@loudthinking.com"
69
@body = "Nothing to see here."
72
def extended_headers(recipient)
73
@recipients = recipient
74
@subject = "testing extended headers"
75
@from = "Grytøyr <stian1@example.net>"
76
@sent_on = Time.local 2004, 12, 12
77
@cc = "Grytøyr <stian2@example.net>"
78
@bcc = "Grytøyr <stian3@example.net>"
79
@body = "Nothing to see here."
80
@charset = "iso-8859-1"
83
def utf8_body(recipient)
84
@recipients = recipient
85
@subject = "testing utf-8 body"
86
@from = "Foo áëô îü <extended@example.net>"
87
@sent_on = Time.local 2004, 12, 12
88
@cc = "Foo áëô îü <extended@example.net>"
89
@bcc = "Foo áëô îü <extended@example.net>"
94
def multipart_with_mime_version(recipient)
96
subject "multipart with mime_version"
97
from "test@example.com"
98
sent_on Time.local(2004, 12, 12)
100
content_type "multipart/alternative"
102
part "text/plain" do |p|
106
part "text/html" do |p|
107
p.body = "<b>blah</b>"
111
def multipart_with_utf8_subject(recipient)
113
subject "Foo áëô îü"
114
from "test@example.com"
117
part "text/plain" do |p|
121
part "text/html" do |p|
122
p.body = "<b>blah</b>"
126
def explicitly_multipart_example(recipient, ct=nil)
128
subject "multipart example"
129
from "test@example.com"
130
sent_on Time.local(2004, 12, 12)
131
body "plain text default"
132
content_type ct if ct
134
part "text/html" do |p|
135
p.charset = "iso-8859-1"
139
attachment :content_type => "image/jpeg", :filename => "foo.jpg",
143
def implicitly_multipart_example(recipient, cs = nil, order = nil)
144
@recipients = recipient
145
@subject = "multipart example"
146
@from = "test@example.com"
147
@sent_on = Time.local 2004, 12, 12
148
@body = { "recipient" => recipient }
150
@implicit_parts_order = order if order
153
def implicitly_multipart_with_utf8
154
recipients "no.one@nowhere.test"
155
subject "Foo áëô îü"
156
from "some.one@somewhere.test"
157
template "implicitly_multipart_example"
158
body ({ "recipient" => "no.one@nowhere.test" })
161
def html_mail(recipient)
164
from "test@example.com"
165
body "<em>Emphasize</em> <strong>this</strong>"
166
content_type "text/html"
169
def html_mail_with_underscores(recipient)
170
subject "html mail with underscores"
171
body %{<a href="http://google.com" target="_blank">_Google</a>}
174
def custom_template(recipient)
176
subject "[Signed up] Welcome #{recipient}"
177
from "system@loudthinking.com"
178
sent_on Time.local(2004, 12, 12)
181
body["recipient"] = recipient
184
def custom_templating_extension(recipient)
186
subject "[Signed up] Welcome #{recipient}"
187
from "system@loudthinking.com"
188
sent_on Time.local(2004, 12, 12)
190
body["recipient"] = recipient
193
def various_newlines(recipient)
195
subject "various newlines"
196
from "test@example.com"
197
body "line #1\nline #2\rline #3\r\nline #4\r\r" +
198
"line #5\n\nline#6\r\n\r\nline #7"
201
def various_newlines_multipart(recipient)
203
subject "various newlines multipart"
204
from "test@example.com"
205
content_type "multipart/alternative"
206
part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r"
207
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"
210
def nested_multipart(recipient)
212
subject "nested multipart"
213
from "test@example.com"
214
content_type "multipart/mixed"
215
part :content_type => "multipart/alternative", :content_disposition => "inline", :headers => { "foo" => "bar" } do |p|
216
p.part :content_type => "text/plain", :body => "test text\nline #2"
217
p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2"
219
attachment :content_type => "application/octet-stream",:filename => "test.txt", :body => "test abcdefghijklmnopqstuvwxyz"
222
def nested_multipart_with_body(recipient)
224
subject "nested multipart with body"
225
from "test@example.com"
226
content_type "multipart/mixed"
227
part :content_type => "multipart/alternative", :content_disposition => "inline", :body => "Nothing to see here." do |p|
228
p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>"
232
def attachment_with_custom_header(recipient)
234
subject "custom header in attachment"
235
from "test@example.com"
236
content_type "multipart/related"
237
part :content_type => "text/html", :body => 'yo'
238
attachment :content_type => "image/jpeg",:filename => "test.jpeg", :body => "i am not a real picture", :headers => { 'Content-ID' => '<test@test.com>' }
241
def unnamed_attachment(recipient)
243
subject "nested multipart"
244
from "test@example.com"
245
content_type "multipart/mixed"
246
part :content_type => "text/plain", :body => "hullo"
247
attachment :content_type => "application/octet-stream", :body => "test abcdefghijklmnopqstuvwxyz"
250
def headers_with_nonalpha_chars(recipient)
252
subject "nonalpha chars"
253
from "One: Two <test@example.com>"
254
cc "Three: Four <test@example.com>"
255
bcc "Five: Six <test@example.com>"
259
def custom_content_type_attributes
260
recipients "no.one@nowhere.test"
261
subject "custom content types"
262
from "some.one@somewhere.test"
263
content_type "text/plain; format=flowed"
268
recipients "no.one@nowhere.test"
269
subject "return path test"
270
from "some.one@somewhere.test"
272
headers "return-path" => "another@somewhere.test"
275
def body_ivar(recipient)
277
subject "Body as a local variable"
278
from "test@example.com"
279
body :body => "foo", :bar => "baz"
283
attr_accessor :received_body
287
self.class.received_body = mail.body
291
class ActionMailerTest < Test::Unit::TestCase
292
include ActionMailer::Quoting
294
def encode( text, charset="utf-8" )
295
quoted_printable( text, charset )
298
def new_mail( charset="utf-8" )
299
mail = TMail::Mail.new
300
mail.mime_version = "1.0"
302
mail.set_content_type "text", "plain", { "charset" => charset }
307
# Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
309
set_delivery_method :test
310
ActionMailer::Base.perform_deliveries = true
311
ActionMailer::Base.raise_delivery_errors = true
312
ActionMailer::Base.deliveries = []
314
@original_logger = TestMailer.logger
315
@recipient = 'test@localhost'
319
TestMailer.logger = @original_logger
320
restore_delivery_method
323
def test_nested_parts
325
assert_nothing_raised { created = TestMailer.create_nested_multipart(@recipient)}
326
assert_equal 2,created.parts.size
327
assert_equal 2,created.parts.first.parts.size
329
assert_equal "multipart/mixed", created.content_type
330
assert_equal "multipart/alternative", created.parts.first.content_type
331
assert_equal "bar", created.parts.first.header['foo'].to_s
332
assert_nil created.parts.first.charset
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
assert_equal "application/octet-stream", created.parts[1].content_type
338
def test_nested_parts_with_body
340
assert_nothing_raised { created = TestMailer.create_nested_multipart_with_body(@recipient)}
341
assert_equal 1,created.parts.size
342
assert_equal 2,created.parts.first.parts.size
344
assert_equal "multipart/mixed", created.content_type
345
assert_equal "multipart/alternative", created.parts.first.content_type
346
assert_equal "Nothing to see here.", created.parts.first.parts.first.body
347
assert_equal "text/plain", created.parts.first.parts.first.content_type
348
assert_equal "text/html", created.parts.first.parts[1].content_type
351
def test_attachment_with_custom_header
353
assert_nothing_raised { created = TestMailer.create_attachment_with_custom_header(@recipient)}
354
assert_equal "<test@test.com>", created.parts[1].header['content-id'].to_s
358
Time.stubs(:now => Time.now)
361
expected.to = @recipient
362
expected.subject = "[Signed up] Welcome #{@recipient}"
363
expected.body = "Hello there, \n\nMr. #{@recipient}"
364
expected.from = "system@loudthinking.com"
365
expected.date = Time.now
368
assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
369
assert_not_nil created
370
assert_equal expected.encoded, created.encoded
372
assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
373
assert_not_nil ActionMailer::Base.deliveries.first
374
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
377
def test_custom_template
379
expected.to = @recipient
380
expected.subject = "[Signed up] Welcome #{@recipient}"
381
expected.body = "Hello there, \n\nMr. #{@recipient}"
382
expected.from = "system@loudthinking.com"
383
expected.date = Time.local(2004, 12, 12)
386
assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) }
387
assert_not_nil created
388
assert_equal expected.encoded, created.encoded
391
def test_custom_templating_extension
392
assert ActionView::Template.template_handler_extensions.include?("haml"), "haml extension was not registered"
394
# N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
396
expected.to = @recipient
397
expected.subject = "[Signed up] Welcome #{@recipient}"
398
expected.body = "Hello there, \n\nMr. #{@recipient}"
399
expected.from = "system@loudthinking.com"
400
expected.date = Time.local(2004, 12, 12)
402
# Stub the render method so no alternative renderers need be present.
403
ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
405
# Now that the template is registered, there should be one part. The text/plain part.
407
assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
408
assert_not_nil created
409
assert_equal 2, created.parts.length
410
assert_equal 'text/plain', created.parts[0].content_type
411
assert_equal 'text/html', created.parts[1].content_type
414
def test_cancelled_account
416
expected.to = @recipient
417
expected.subject = "[Cancelled] Goodbye #{@recipient}"
418
expected.body = "Goodbye, Mr. #{@recipient}"
419
expected.from = "system@loudthinking.com"
420
expected.date = Time.local(2004, 12, 12)
423
assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) }
424
assert_not_nil created
425
assert_equal expected.encoded, created.encoded
427
assert_nothing_raised { TestMailer.deliver_cancelled_account(@recipient) }
428
assert_not_nil ActionMailer::Base.deliveries.first
429
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
434
expected.to = @recipient
435
expected.subject = "testing bcc/cc"
436
expected.body = "Nothing to see here."
437
expected.from = "system@loudthinking.com"
438
expected.cc = "nobody@loudthinking.com"
439
expected.bcc = "root@loudthinking.com"
440
expected.date = Time.local 2004, 12, 12
443
assert_nothing_raised do
444
created = TestMailer.create_cc_bcc @recipient
446
assert_not_nil created
447
assert_equal expected.encoded, created.encoded
449
assert_nothing_raised do
450
TestMailer.deliver_cc_bcc @recipient
453
assert_not_nil ActionMailer::Base.deliveries.first
454
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
460
expected.to = @recipient
461
expected.subject = "testing reply_to"
462
expected.body = "Nothing to see here."
463
expected.from = "system@loudthinking.com"
464
expected.reply_to = "atraver@gmail.com"
465
expected.date = Time.local 2008, 5, 23
468
assert_nothing_raised do
469
created = TestMailer.create_different_reply_to @recipient
471
assert_not_nil created
472
assert_equal expected.encoded, created.encoded
474
assert_nothing_raised do
475
TestMailer.deliver_different_reply_to @recipient
478
assert_not_nil ActionMailer::Base.deliveries.first
479
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
483
expected = new_mail( "iso-8859-1" )
484
expected.to = @recipient
485
expected.subject = encode "testing isø charsets", "iso-8859-1"
486
expected.body = "Nothing to see here."
487
expected.from = "system@loudthinking.com"
488
expected.cc = "nobody@loudthinking.com"
489
expected.bcc = "root@loudthinking.com"
490
expected.date = Time.local 2004, 12, 12
493
assert_nothing_raised do
494
created = TestMailer.create_iso_charset @recipient
496
assert_not_nil created
497
assert_equal expected.encoded, created.encoded
499
assert_nothing_raised do
500
TestMailer.deliver_iso_charset @recipient
503
assert_not_nil ActionMailer::Base.deliveries.first
504
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
507
def test_unencoded_subject
509
expected.to = @recipient
510
expected.subject = "testing unencoded subject"
511
expected.body = "Nothing to see here."
512
expected.from = "system@loudthinking.com"
513
expected.cc = "nobody@loudthinking.com"
514
expected.bcc = "root@loudthinking.com"
515
expected.date = Time.local 2004, 12, 12
518
assert_nothing_raised do
519
created = TestMailer.create_unencoded_subject @recipient
521
assert_not_nil created
522
assert_equal expected.encoded, created.encoded
524
assert_nothing_raised do
525
TestMailer.deliver_unencoded_subject @recipient
528
assert_not_nil ActionMailer::Base.deliveries.first
529
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
532
def test_instances_are_nil
533
assert_nil ActionMailer::Base.new
534
assert_nil TestMailer.new
537
def test_deliveries_array
538
assert_not_nil ActionMailer::Base.deliveries
539
assert_equal 0, ActionMailer::Base.deliveries.size
540
TestMailer.deliver_signed_up(@recipient)
541
assert_equal 1, ActionMailer::Base.deliveries.size
542
assert_not_nil ActionMailer::Base.deliveries.first
545
def test_perform_deliveries_flag
546
ActionMailer::Base.perform_deliveries = false
547
TestMailer.deliver_signed_up(@recipient)
548
assert_equal 0, ActionMailer::Base.deliveries.size
549
ActionMailer::Base.perform_deliveries = true
550
TestMailer.deliver_signed_up(@recipient)
551
assert_equal 1, ActionMailer::Base.deliveries.size
554
def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
555
ActionMailer::Base.raise_delivery_errors = false
556
TestMailer.any_instance.expects(:perform_delivery_test).raises(Exception)
557
assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
560
def test_performs_delivery_via_sendmail
562
sm.expects(:print).with(anything)
564
IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm)
565
ActionMailer::Base.delivery_method = :sendmail
566
TestMailer.deliver_signed_up(@recipient)
569
def test_delivery_logs_sent_mail
570
mail = TestMailer.create_signed_up(@recipient)
572
logger.expects(:info).with("Sent mail to #{@recipient}")
573
logger.expects(:debug).with() do |logged_text|
574
logged_text =~ /\[Signed up\] Welcome/
576
TestMailer.logger = logger
577
TestMailer.deliver_signed_up(@recipient)
580
def test_unquote_quoted_printable_subject
583
Subject: =?utf-8?Q?testing_testing_=D6=A4?=
584
Content-Type: text/plain; charset=iso-8859-1
588
mail = TMail::Mail.parse(msg)
589
assert_equal "testing testing \326\244", mail.subject
590
assert_equal "=?utf-8?Q?testing_testing_=D6=A4?=", mail.quoted_subject
593
def test_unquote_7bit_subject
596
Subject: this == working?
597
Content-Type: text/plain; charset=iso-8859-1
601
mail = TMail::Mail.parse(msg)
602
assert_equal "this == working?", mail.subject
603
assert_equal "this == working?", mail.quoted_subject
606
def test_unquote_7bit_body
610
Content-Type: text/plain; charset=iso-8859-1
611
Content-Transfer-Encoding: 7bit
615
mail = TMail::Mail.parse(msg)
616
assert_equal "The=3Dbody", mail.body.strip
617
assert_equal "The=3Dbody", mail.quoted_body.strip
620
def test_unquote_quoted_printable_body
624
Content-Type: text/plain; charset=iso-8859-1
625
Content-Transfer-Encoding: quoted-printable
629
mail = TMail::Mail.parse(msg)
630
assert_equal "The=body", mail.body.strip
631
assert_equal "The=3Dbody", mail.quoted_body.strip
634
def test_unquote_base64_body
638
Content-Type: text/plain; charset=iso-8859-1
639
Content-Transfer-Encoding: base64
643
mail = TMail::Mail.parse(msg)
644
assert_equal "The body", mail.body.strip
645
assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip
648
def test_extended_headers
649
@recipient = "Grytøyr <test@localhost>"
651
expected = new_mail "iso-8859-1"
652
expected.to = quote_address_if_necessary @recipient, "iso-8859-1"
653
expected.subject = "testing extended headers"
654
expected.body = "Nothing to see here."
655
expected.from = quote_address_if_necessary "Grytøyr <stian1@example.net>", "iso-8859-1"
656
expected.cc = quote_address_if_necessary "Grytøyr <stian2@example.net>", "iso-8859-1"
657
expected.bcc = quote_address_if_necessary "Grytøyr <stian3@example.net>", "iso-8859-1"
658
expected.date = Time.local 2004, 12, 12
661
assert_nothing_raised do
662
created = TestMailer.create_extended_headers @recipient
665
assert_not_nil created
666
assert_equal expected.encoded, created.encoded
668
assert_nothing_raised do
669
TestMailer.deliver_extended_headers @recipient
672
assert_not_nil ActionMailer::Base.deliveries.first
673
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
676
def test_utf8_body_is_not_quoted
677
@recipient = "Foo áëô îü <extended@example.net>"
678
expected = new_mail "utf-8"
679
expected.to = quote_address_if_necessary @recipient, "utf-8"
680
expected.subject = "testing utf-8 body"
681
expected.body = "åœö blah"
682
expected.from = quote_address_if_necessary @recipient, "utf-8"
683
expected.cc = quote_address_if_necessary @recipient, "utf-8"
684
expected.bcc = quote_address_if_necessary @recipient, "utf-8"
685
expected.date = Time.local 2004, 12, 12
687
created = TestMailer.create_utf8_body @recipient
688
assert_match(/åœö blah/, created.encoded)
691
def test_multiple_utf8_recipients
692
@recipient = ["\"Foo áëô îü\" <extended@example.net>", "\"Example Recipient\" <me@example.com>"]
693
expected = new_mail "utf-8"
694
expected.to = quote_address_if_necessary @recipient, "utf-8"
695
expected.subject = "testing utf-8 body"
696
expected.body = "åœö blah"
697
expected.from = quote_address_if_necessary @recipient.first, "utf-8"
698
expected.cc = quote_address_if_necessary @recipient, "utf-8"
699
expected.bcc = quote_address_if_necessary @recipient, "utf-8"
700
expected.date = Time.local 2004, 12, 12
702
created = TestMailer.create_utf8_body @recipient
703
assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>\r/, created.encoded)
704
assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>, Example Recipient <me/, created.encoded)
707
def test_receive_decodes_base64_encoded_mail
708
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email")
709
TestMailer.receive(fixture)
710
assert_match(/Jamis/, TestMailer.received_body)
713
def test_receive_attachments
714
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email2")
715
mail = TMail::Mail.parse(fixture)
716
attachment = mail.attachments.last
717
assert_equal "smime.p7s", attachment.original_filename
718
assert_equal "application/pkcs7-signature", attachment.content_type
721
def test_decode_attachment_without_charset
722
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email3")
723
mail = TMail::Mail.parse(fixture)
724
attachment = mail.attachments.last
725
assert_equal 1026, attachment.read.length
728
def test_attachment_using_content_location
729
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email12")
730
mail = TMail::Mail.parse(fixture)
731
assert_equal 1, mail.attachments.length
732
assert_equal "Photo25.jpg", mail.attachments.first.original_filename
735
def test_attachment_with_text_type
736
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email13")
737
mail = TMail::Mail.parse(fixture)
738
assert mail.has_attachments?
739
assert_equal 1, mail.attachments.length
740
assert_equal "hello.rb", mail.attachments.first.original_filename
743
def test_decode_part_without_content_type
744
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email4")
745
mail = TMail::Mail.parse(fixture)
746
assert_nothing_raised { mail.body }
749
def test_decode_message_without_content_type
750
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email5")
751
mail = TMail::Mail.parse(fixture)
752
assert_nothing_raised { mail.body }
755
def test_decode_message_with_incorrect_charset
756
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email6")
757
mail = TMail::Mail.parse(fixture)
758
assert_nothing_raised { mail.body }
761
def test_multipart_with_mime_version
762
mail = TestMailer.create_multipart_with_mime_version(@recipient)
763
assert_equal "1.1", mail.mime_version
766
def test_multipart_with_utf8_subject
767
mail = TestMailer.create_multipart_with_utf8_subject(@recipient)
768
assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded)
771
def test_implicitly_multipart_with_utf8
772
mail = TestMailer.create_implicitly_multipart_with_utf8
773
assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded)
776
def test_explicitly_multipart_messages
777
mail = TestMailer.create_explicitly_multipart_example(@recipient)
778
assert_equal 3, mail.parts.length
779
assert_nil mail.content_type
780
assert_equal "text/plain", mail.parts[0].content_type
782
assert_equal "text/html", mail.parts[1].content_type
783
assert_equal "iso-8859-1", mail.parts[1].sub_header("content-type", "charset")
784
assert_equal "inline", mail.parts[1].content_disposition
786
assert_equal "image/jpeg", mail.parts[2].content_type
787
assert_equal "attachment", mail.parts[2].content_disposition
788
assert_equal "foo.jpg", mail.parts[2].sub_header("content-disposition", "filename")
789
assert_equal "foo.jpg", mail.parts[2].sub_header("content-type", "name")
790
assert_nil mail.parts[2].sub_header("content-type", "charset")
793
def test_explicitly_multipart_with_content_type
794
mail = TestMailer.create_explicitly_multipart_example(@recipient, "multipart/alternative")
795
assert_equal 3, mail.parts.length
796
assert_equal "multipart/alternative", mail.content_type
799
def test_explicitly_multipart_with_invalid_content_type
800
mail = TestMailer.create_explicitly_multipart_example(@recipient, "text/xml")
801
assert_equal 3, mail.parts.length
802
assert_nil mail.content_type
805
def test_implicitly_multipart_messages
806
assert ActionView::Template.template_handler_extensions.include?("bak"), "bak extension was not registered"
808
mail = TestMailer.create_implicitly_multipart_example(@recipient)
809
assert_equal 3, mail.parts.length
810
assert_equal "1.0", mail.mime_version
811
assert_equal "multipart/alternative", mail.content_type
812
assert_equal "text/yaml", mail.parts[0].content_type
813
assert_equal "utf-8", mail.parts[0].sub_header("content-type", "charset")
814
assert_equal "text/plain", mail.parts[1].content_type
815
assert_equal "utf-8", mail.parts[1].sub_header("content-type", "charset")
816
assert_equal "text/html", mail.parts[2].content_type
817
assert_equal "utf-8", mail.parts[2].sub_header("content-type", "charset")
820
def test_implicitly_multipart_messages_with_custom_order
821
assert ActionView::Template.template_handler_extensions.include?("bak"), "bak extension was not registered"
823
mail = TestMailer.create_implicitly_multipart_example(@recipient, nil, ["text/yaml", "text/plain"])
824
assert_equal 3, mail.parts.length
825
assert_equal "text/html", mail.parts[0].content_type
826
assert_equal "text/plain", mail.parts[1].content_type
827
assert_equal "text/yaml", mail.parts[2].content_type
830
def test_implicitly_multipart_messages_with_charset
831
mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
833
assert_equal "multipart/alternative", mail.header['content-type'].body
835
assert_equal 'iso-8859-1', mail.parts[0].sub_header("content-type", "charset")
836
assert_equal 'iso-8859-1', mail.parts[1].sub_header("content-type", "charset")
837
assert_equal 'iso-8859-1', mail.parts[2].sub_header("content-type", "charset")
841
mail = TestMailer.create_html_mail(@recipient)
842
assert_equal "text/html", mail.content_type
845
def test_html_mail_with_underscores
846
mail = TestMailer.create_html_mail_with_underscores(@recipient)
847
assert_equal %{<a href="http://google.com" target="_blank">_Google</a>}, mail.body
850
def test_various_newlines
851
mail = TestMailer.create_various_newlines(@recipient)
852
assert_equal("line #1\nline #2\nline #3\nline #4\n\n" +
853
"line #5\n\nline#6\n\nline #7", mail.body)
856
def test_various_newlines_multipart
857
mail = TestMailer.create_various_newlines_multipart(@recipient)
858
assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body
859
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
862
def test_headers_removed_on_smtp_delivery
863
ActionMailer::Base.delivery_method = :smtp
864
TestMailer.deliver_cc_bcc(@recipient)
865
assert MockSMTP.deliveries[0][2].include?("root@loudthinking.com")
866
assert MockSMTP.deliveries[0][2].include?("nobody@loudthinking.com")
867
assert MockSMTP.deliveries[0][2].include?(@recipient)
868
assert_match %r{^Cc: nobody@loudthinking.com}, MockSMTP.deliveries[0][0]
869
assert_match %r{^To: #{@recipient}}, MockSMTP.deliveries[0][0]
870
assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0]
873
def test_recursive_multipart_processing
874
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
875
mail = TMail::Mail.parse(fixture)
876
assert_equal "This is the first part.\n\nAttachment: test.rb\nAttachment: test.pdf\n\n\nAttachment: smime.p7s\n", mail.body
879
def test_decode_encoded_attachment_filename
880
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8")
881
mail = TMail::Mail.parse(fixture)
882
attachment = mail.attachments.last
884
expected = "01 Quien Te Dij\212at. Pitbull.mp3"
885
expected.force_encoding(Encoding::ASCII_8BIT) if expected.respond_to?(:force_encoding)
887
assert_equal expected, attachment.original_filename
890
def test_wrong_mail_header
891
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email9")
892
assert_raise(TMail::SyntaxError) { TMail::Mail.parse(fixture) }
895
def test_decode_message_with_unknown_charset
896
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10")
897
mail = TMail::Mail.parse(fixture)
898
assert_nothing_raised { mail.body }
901
def test_empty_header_values_omitted
902
result = TestMailer.create_unnamed_attachment(@recipient).encoded
903
assert_match %r{Content-Type: application/octet-stream[^;]}, result
904
assert_match %r{Content-Disposition: attachment[^;]}, result
907
def test_headers_with_nonalpha_chars
908
mail = TestMailer.create_headers_with_nonalpha_chars(@recipient)
909
assert !mail.from_addrs.empty?
910
assert !mail.cc_addrs.empty?
911
assert !mail.bcc_addrs.empty?
912
assert_match(/:/, mail.from_addrs.to_s)
913
assert_match(/:/, mail.cc_addrs.to_s)
914
assert_match(/:/, mail.bcc_addrs.to_s)
917
def test_deliver_with_mail_object
918
mail = TestMailer.create_headers_with_nonalpha_chars(@recipient)
919
assert_nothing_raised { TestMailer.deliver(mail) }
920
assert_equal 1, TestMailer.deliveries.length
923
def test_multipart_with_template_path_with_dots
924
mail = FunkyPathMailer.create_multipart_with_template_path_with_dots(@recipient)
925
assert_equal 2, mail.parts.length
926
assert_equal 'text/plain', mail.parts[0].content_type
927
assert_equal 'utf-8', mail.parts[0].charset
930
def test_custom_content_type_attributes
931
mail = TestMailer.create_custom_content_type_attributes
932
assert_match %r{format=flowed}, mail['content-type'].to_s
933
assert_match %r{charset=utf-8}, mail['content-type'].to_s
936
def test_return_path_with_create
937
mail = TestMailer.create_return_path
938
assert_equal "<another@somewhere.test>", mail['return-path'].to_s
941
def test_return_path_with_deliver
942
ActionMailer::Base.delivery_method = :smtp
943
TestMailer.deliver_return_path
944
assert_match %r{^Return-Path: <another@somewhere.test>}, MockSMTP.deliveries[0][0]
945
assert_equal "another@somewhere.test", MockSMTP.deliveries[0][1].to_s
948
def test_body_is_stored_as_an_ivar
949
mail = TestMailer.create_body_ivar(@recipient)
950
assert_equal "body: foo\nbar: baz", mail.body
953
def test_starttls_is_enabled_if_supported
954
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
955
MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(true)
956
MockSMTP.any_instance.expects(:enable_starttls_auto)
957
ActionMailer::Base.delivery_method = :smtp
958
TestMailer.deliver_signed_up(@recipient)
961
def test_starttls_is_disabled_if_not_supported
962
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
963
MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(false)
964
MockSMTP.any_instance.expects(:enable_starttls_auto).never
965
ActionMailer::Base.delivery_method = :smtp
966
TestMailer.deliver_signed_up(@recipient)
969
def test_starttls_is_not_enabled
970
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = false
971
MockSMTP.any_instance.expects(:respond_to?).never
972
MockSMTP.any_instance.expects(:enable_starttls_auto).never
973
ActionMailer::Base.delivery_method = :smtp
974
TestMailer.deliver_signed_up(@recipient)
976
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
980
class InheritableTemplateRootTest < Test::Unit::TestCase
982
expected = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
983
assert_equal expected, FunkyPathMailer.template_root.to_s
985
sub = Class.new(FunkyPathMailer)
986
sub.template_root = 'test/path'
988
assert_equal 'test/path', sub.template_root.to_s
989
assert_equal expected, FunkyPathMailer.template_root.to_s
993
class MethodNamingTest < Test::Unit::TestCase
994
class TestMailer < ActionMailer::Base
1001
set_delivery_method :test
1002
ActionMailer::Base.perform_deliveries = true
1003
ActionMailer::Base.deliveries = []
1007
restore_delivery_method
1010
def test_send_method
1011
assert_nothing_raised do
1013
TestMailer.deliver_send
1019
class RespondToTest < Test::Unit::TestCase
1020
class RespondToMailer < ActionMailer::Base; end
1023
set_delivery_method :test
1027
restore_delivery_method
1030
def test_should_respond_to_new
1031
assert RespondToMailer.respond_to?(:new)
1034
def test_should_respond_to_create_with_template_suffix
1035
assert RespondToMailer.respond_to?(:create_any_old_template)
1038
def test_should_respond_to_deliver_with_template_suffix
1039
assert RespondToMailer.respond_to?(:deliver_any_old_template)
1042
def test_should_not_respond_to_new_with_template_suffix
1043
assert !RespondToMailer.respond_to?(:new_any_old_template)
1046
def test_should_not_respond_to_create_with_template_suffix_unless_it_is_separated_by_an_underscore
1047
assert !RespondToMailer.respond_to?(:createany_old_template)
1050
def test_should_not_respond_to_deliver_with_template_suffix_unless_it_is_separated_by_an_underscore
1051
assert !RespondToMailer.respond_to?(:deliverany_old_template)
1054
def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_uppercase_letter
1055
assert !RespondToMailer.respond_to?(:create_Any_old_template)
1058
def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_uppercase_letter
1059
assert !RespondToMailer.respond_to?(:deliver_Any_old_template)
1062
def test_should_not_respond_to_create_with_template_suffix_if_it_begins_with_a_digit
1063
assert !RespondToMailer.respond_to?(:create_1_template)
1066
def test_should_not_respond_to_deliver_with_template_suffix_if_it_begins_with_a_digit
1067
assert !RespondToMailer.respond_to?(:deliver_1_template)
1070
def test_should_not_respond_to_method_where_deliver_is_not_a_suffix
1071
assert !RespondToMailer.respond_to?(:foo_deliver_template)
1074
def test_should_still_raise_exception_with_expected_message_when_calling_an_undefined_method
1075
error = assert_raise NoMethodError do
1076
RespondToMailer.not_a_method
1079
assert_match /undefined method.*not_a_method/, error.message