| 1 | $:.unshift(File.dirname(__FILE__) + "/../lib/") |
|---|
| 2 | |
|---|
| 3 | require 'test/unit' |
|---|
| 4 | require 'action_mailer' |
|---|
| 5 | |
|---|
| 6 | class MockSMTP |
|---|
| 7 | def self.deliveries |
|---|
| 8 | @@deliveries |
|---|
| 9 | end |
|---|
| 10 | |
|---|
| 11 | def initialize |
|---|
| 12 | @@deliveries = [] |
|---|
| 13 | end |
|---|
| 14 | |
|---|
| 15 | def sendmail(mail, from, to) |
|---|
| 16 | @@deliveries << [mail, from, to] |
|---|
| 17 | end |
|---|
| 18 | end |
|---|
| 19 | |
|---|
| 20 | class Net::SMTP |
|---|
| 21 | def self.start(*args) |
|---|
| 22 | yield MockSMTP.new |
|---|
| 23 | end |
|---|
| 24 | end |
|---|
| 25 | |
|---|
| 26 | class FunkyPathMailer < ActionMailer::Base |
|---|
| 27 | self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
|
|---|
| 28 |
|
|---|
| 29 | def multipart_with_template_path_with_dots(recipient) |
|---|
| 30 | recipients recipient |
|---|
| 31 | subject "Have a lovely picture" |
|---|
| 32 | from "Chad Fowler <chad@chadfowler.com>" |
|---|
| 33 | attachment :content_type => "image/jpeg", |
|---|
| 34 | :body => "not really a jpeg, we're only testing, after all" |
|---|
| 35 | end |
|---|
| 36 | |
|---|
| 37 | def template_path |
|---|
| 38 | "#{File.dirname(__FILE__)}/fixtures/path.with.dots" |
|---|
| 39 | end |
|---|
| 40 | end |
|---|
| 41 | |
|---|
| 42 | class TestMailer < ActionMailer::Base |
|---|
| 43 | |
|---|
| 44 | def signed_up(recipient) |
|---|
| 45 | @recipients = recipient |
|---|
| 46 | @subject = "[Signed up] Welcome #{recipient}" |
|---|
| 47 | @from = "system@loudthinking.com" |
|---|
| 48 | @sent_on = Time.local(2004, 12, 12) |
|---|
| 49 | @body["recipient"] = recipient |
|---|
| 50 | end |
|---|
| 51 | |
|---|
| 52 | def cancelled_account(recipient) |
|---|
| 53 | self.recipients = recipient |
|---|
| 54 | self.subject = "[Cancelled] Goodbye #{recipient}" |
|---|
| 55 | self.from = "system@loudthinking.com" |
|---|
| 56 | self.sent_on = Time.local(2004, 12, 12) |
|---|
| 57 | self.body = "Goodbye, Mr. #{recipient}" |
|---|
| 58 | end |
|---|
| 59 | |
|---|
| 60 | def cc_bcc(recipient) |
|---|
| 61 | recipients recipient |
|---|
| 62 | subject "testing bcc/cc" |
|---|
| 63 | from "system@loudthinking.com" |
|---|
| 64 | sent_on Time.local(2004, 12, 12) |
|---|
| 65 | cc "nobody@loudthinking.com" |
|---|
| 66 | bcc "root@loudthinking.com" |
|---|
| 67 | body "Nothing to see here." |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 70 | def iso_charset(recipient) |
|---|
| 71 | @recipients = recipient |
|---|
| 72 | @subject = "testing isø charsets" |
|---|
| 73 | @from = "system@loudthinking.com" |
|---|
| 74 | @sent_on = Time.local 2004, 12, 12 |
|---|
| 75 | @cc = "nobody@loudthinking.com" |
|---|
| 76 | @bcc = "root@loudthinking.com" |
|---|
| 77 | @body = "Nothing to see here." |
|---|
| 78 | @charset = "iso-8859-1" |
|---|
| 79 | end |
|---|
| 80 | |
|---|
| 81 | def unencoded_subject(recipient) |
|---|
| 82 | @recipients = recipient |
|---|
| 83 | @subject = "testing unencoded subject" |
|---|
| 84 | @from = "system@loudthinking.com" |
|---|
| 85 | @sent_on = Time.local 2004, 12, 12 |
|---|
| 86 | @cc = "nobody@loudthinking.com" |
|---|
| 87 | @bcc = "root@loudthinking.com" |
|---|
| 88 | @body = "Nothing to see here." |
|---|
| 89 | end |
|---|
| 90 | |
|---|
| 91 | def extended_headers(recipient) |
|---|
| 92 | @recipients = recipient |
|---|
| 93 | @subject = "testing extended headers" |
|---|
| 94 | @from = "Grytøyr <stian1@example.net>" |
|---|
| 95 | @sent_on = Time.local 2004, 12, 12 |
|---|
| 96 | @cc = "Grytøyr <stian2@example.net>" |
|---|
| 97 | @bcc = "Grytøyr <stian3@example.net>" |
|---|
| 98 | @body = "Nothing to see here." |
|---|
| 99 | @charset = "iso-8859-1" |
|---|
| 100 | end |
|---|
| 101 | |
|---|
| 102 | def utf8_body(recipient) |
|---|
| 103 | @recipients = recipient |
|---|
| 104 | @subject = "testing utf-8 body" |
|---|
| 105 | @from = "Foo áëô îü <extended@example.net>" |
|---|
| 106 | @sent_on = Time.local 2004, 12, 12 |
|---|
| 107 | @cc = "Foo áëô îü <extended@example.net>" |
|---|
| 108 | @bcc = "Foo áëô îü <extended@example.net>" |
|---|
| 109 | @body = "åœö blah" |
|---|
| 110 | @charset = "utf-8" |
|---|
| 111 | end |
|---|
| 112 | |
|---|
| 113 | def multipart_with_mime_version(recipient) |
|---|
| 114 | recipients recipient |
|---|
| 115 | subject "multipart with mime_version" |
|---|
| 116 | from "test@example.com" |
|---|
| 117 | sent_on Time.local(2004, 12, 12) |
|---|
| 118 | mime_version "1.1" |
|---|
| 119 | content_type "multipart/alternative" |
|---|
| 120 | |
|---|
| 121 | part "text/plain" do |p| |
|---|
| 122 | p.body = "blah" |
|---|
| 123 | end |
|---|
| 124 | |
|---|
| 125 | part "text/html" do |p| |
|---|
| 126 | p.body = "<b>blah</b>" |
|---|
| 127 | end |
|---|
| 128 | end |
|---|
| 129 | |
|---|
| 130 | def multipart_with_utf8_subject(recipient) |
|---|
| 131 | recipients recipient |
|---|
| 132 | subject "Foo áëô îü" |
|---|
| 133 | from "test@example.com" |
|---|
| 134 | charset "utf-8" |
|---|
| 135 | |
|---|
| 136 | part "text/plain" do |p| |
|---|
| 137 | p.body = "blah" |
|---|
| 138 | end |
|---|
| 139 | |
|---|
| 140 | part "text/html" do |p| |
|---|
| 141 | p.body = "<b>blah</b>" |
|---|
| 142 | end |
|---|
| 143 | end |
|---|
| 144 | |
|---|
| 145 | def explicitly_multipart_example(recipient, ct=nil) |
|---|
| 146 | recipients recipient |
|---|
| 147 | subject "multipart example" |
|---|
| 148 | from "test@example.com" |
|---|
| 149 | sent_on Time.local(2004, 12, 12) |
|---|
| 150 | body "plain text default" |
|---|
| 151 | content_type ct if ct |
|---|
| 152 | |
|---|
| 153 | part "text/html" do |p| |
|---|
| 154 | p.charset = "iso-8859-1" |
|---|
| 155 | p.body = "blah" |
|---|
| 156 | end |
|---|
| 157 | |
|---|
| 158 | attachment :content_type => "image/jpeg", :filename => "foo.jpg", |
|---|
| 159 | :body => "123456789" |
|---|
| 160 | end |
|---|
| 161 | |
|---|
| 162 | def implicitly_multipart_example(recipient, cs = nil, order = nil) |
|---|
| 163 | @recipients = recipient |
|---|
| 164 | @subject = "multipart example" |
|---|
| 165 | @from = "test@example.com" |
|---|
| 166 | @sent_on = Time.local 2004, 12, 12 |
|---|
| 167 | @body = { "recipient" => recipient } |
|---|
| 168 | @charset = cs if cs |
|---|
| 169 | @implicit_parts_order = order if order |
|---|
| 170 | end |
|---|
| 171 | |
|---|
| 172 | def implicitly_multipart_with_utf8 |
|---|
| 173 | recipients "no.one@nowhere.test" |
|---|
| 174 | subject "Foo áëô îü" |
|---|
| 175 | from "some.one@somewhere.test" |
|---|
| 176 | template "implicitly_multipart_example" |
|---|
| 177 | body ({ "recipient" => "no.one@nowhere.test" }) |
|---|
| 178 | end |
|---|
| 179 | |
|---|
| 180 | def html_mail(recipient) |
|---|
| 181 | recipients recipient |
|---|
| 182 | subject "html mail" |
|---|
| 183 | from "test@example.com" |
|---|
| 184 | body "<em>Emphasize</em> <strong>this</strong>" |
|---|
| 185 | content_type "text/html" |
|---|
| 186 | end |
|---|
| 187 | |
|---|
| 188 | def html_mail_with_underscores(recipient) |
|---|
| 189 | subject "html mail with underscores" |
|---|
| 190 | body %{<a href="http://google.com" target="_blank">_Google</a>} |
|---|
| 191 | end |
|---|
| 192 | |
|---|
| 193 | def custom_template(recipient) |
|---|
| 194 | recipients recipient |
|---|
| 195 | subject "[Signed up] Welcome #{recipient}" |
|---|
| 196 | from "system@loudthinking.com" |
|---|
| 197 | sent_on Time.local(2004, 12, 12) |
|---|
| 198 | template "signed_up" |
|---|
| 199 | |
|---|
| 200 | body["recipient"] = recipient |
|---|
| 201 | end |
|---|
| 202 | |
|---|
| 203 | def various_newlines(recipient) |
|---|
| 204 | recipients recipient |
|---|
| 205 | subject "various newlines" |
|---|
| 206 | from "test@example.com" |
|---|
| 207 | body "line #1\nline #2\rline #3\r\nline #4\r\r" + |
|---|
| 208 | "line #5\n\nline#6\r\n\r\nline #7" |
|---|
| 209 | end |
|---|
| 210 | |
|---|
| 211 | def various_newlines_multipart(recipient) |
|---|
| 212 | recipients recipient |
|---|
| 213 | subject "various newlines multipart" |
|---|
| 214 | from "test@example.com" |
|---|
| 215 | content_type "multipart/alternative" |
|---|
| 216 | part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r" |
|---|
| 217 | 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" |
|---|
| 218 | end |
|---|
| 219 | |
|---|
| 220 | def nested_multipart(recipient) |
|---|
| 221 | recipients recipient |
|---|
| 222 | subject "nested multipart" |
|---|
| 223 | from "test@example.com" |
|---|
| 224 | content_type "multipart/mixed" |
|---|
| 225 | part :content_type => "multipart/alternative", :content_disposition => "inline" do |p| |
|---|
| 226 | p.part :content_type => "text/plain", :body => "test text\nline #2" |
|---|
| 227 | p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2" |
|---|
| 228 | end |
|---|
| 229 | attachment :content_type => "application/octet-stream",:filename => "test.txt", :body => "test abcdefghijklmnopqstuvwxyz" |
|---|
| 230 | end |
|---|
| 231 | |
|---|
| 232 | def attachment_with_custom_header(recipient) |
|---|
| 233 | recipients 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>' } |
|---|
| 239 | end |
|---|
| 240 | |
|---|
| 241 | def unnamed_attachment(recipient) |
|---|
| 242 | recipients 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" |
|---|
| 248 | end |
|---|
| 249 | |
|---|
| 250 | def headers_with_nonalpha_chars(recipient) |
|---|
| 251 | recipients 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>" |
|---|
| 256 | body "testing" |
|---|
| 257 | end |
|---|
| 258 | |
|---|
| 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" |
|---|
| 264 | body "testing" |
|---|
| 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 | TestMailer.template_root = File.dirname(__FILE__) + "/fixtures" |
|---|
| 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 | if charset |
|---|
| 288 | mail.set_content_type "text", "plain", { "charset" => charset } |
|---|
| 289 | end |
|---|
| 290 | mail |
|---|
| 291 | end |
|---|
| 292 | |
|---|
| 293 | def setup |
|---|
| 294 | ActionMailer::Base.delivery_method = :test |
|---|
| 295 | ActionMailer::Base.perform_deliveries = true |
|---|
| 296 | ActionMailer::Base.deliveries = [] |
|---|
| 297 | |
|---|
| 298 | @recipient = 'test@localhost' |
|---|
| 299 | end |
|---|
| 300 | |
|---|
| 301 | def test_nested_parts |
|---|
| 302 | created = nil |
|---|
| 303 | assert_nothing_raised { created = TestMailer.create_nested_multipart(@recipient)} |
|---|
| 304 | assert_equal 2,created.parts.size |
|---|
| 305 | assert_equal 2,created.parts.first.parts.size |
|---|
| 306 | |
|---|
| 307 | assert_equal "multipart/mixed", created.content_type |
|---|
| 308 | assert_equal "multipart/alternative", created.parts.first.content_type |
|---|
| 309 | assert_equal "text/plain", created.parts.first.parts.first.content_type |
|---|
| 310 | assert_equal "text/html", created.parts.first.parts[1].content_type |
|---|
| 311 | assert_equal "application/octet-stream", created.parts[1].content_type |
|---|
| 312 | end |
|---|
| 313 | |
|---|
| 314 | def test_attachment_with_custom_header |
|---|
| 315 | created = nil |
|---|
| 316 | assert_nothing_raised { created = TestMailer.create_attachment_with_custom_header(@recipient)} |
|---|
| 317 | assert_equal "<test@test.com>", created.parts[1].header['content-id'].to_s |
|---|
| 318 | end |
|---|
| 319 | |
|---|
| 320 | def test_signed_up |
|---|
| 321 | expected = new_mail |
|---|
| 322 | expected.to = @recipient |
|---|
| 323 | expected.subject = "[Signed up] Welcome #{@recipient}" |
|---|
| 324 | expected.body = "Hello there, \n\nMr. #{@recipient}" |
|---|
| 325 | expected.from = "system@loudthinking.com" |
|---|
| 326 | expected.date = Time.local(2004, 12, 12) |
|---|
| 327 | expected.mime_version = nil |
|---|
| 328 | |
|---|
| 329 | created = nil |
|---|
| 330 | assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) } |
|---|
| 331 | assert_not_nil created |
|---|
| 332 | assert_equal expected.encoded, created.encoded |
|---|
| 333 | |
|---|
| 334 | assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) } |
|---|
| 335 | assert_not_nil ActionMailer::Base.deliveries.first |
|---|
| 336 | assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded |
|---|
| 337 | end |
|---|
| 338 | |
|---|
| 339 | def test_custom_template |
|---|
| 340 | expected = new_mail |
|---|
| 341 | expected.to = @recipient |
|---|
| 342 | expected.subject = "[Signed up] Welcome #{@recipient}" |
|---|
| 343 | expected.body = "Hello there, \n\nMr. #{@recipient}" |
|---|
| 344 | expected.from = "system@loudthinking.com" |
|---|
| 345 | expected.date = Time.local(2004, 12, 12) |
|---|
| 346 | |
|---|
| 347 | created = nil |
|---|
| 348 | assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) } |
|---|
| 349 | assert_not_nil created |
|---|
| 350 | assert_equal expected.encoded, created.encoded |
|---|
| 351 | end |
|---|
| 352 | |
|---|
| 353 | def test_cancelled_account |
|---|
| 354 | expected = new_mail |
|---|
| 355 | expected.to = @recipient |
|---|
| 356 | expected.subject = "[Cancelled] Goodbye #{@recipient}" |
|---|
| 357 | expected.body = "Goodbye, Mr. #{@recipient}" |
|---|
| 358 | expected.from = "system@loudthinking.com" |
|---|
| 359 | expected.date = Time.local(2004, 12, 12) |
|---|
| 360 | |
|---|
| 361 | created = nil |
|---|
| 362 | assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) } |
|---|
| 363 | assert_not_nil created |
|---|
| 364 | assert_equal expected.encoded, created.encoded |
|---|
| 365 | |
|---|
| 366 | assert_nothing_raised { TestMailer.deliver_cancelled_account(@recipient) } |
|---|
| 367 | assert_not_nil ActionMailer::Base.deliveries.first |
|---|
| 368 | assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded |
|---|
| 369 | end |
|---|
| 370 | |
|---|
| 371 | def test_cc_bcc |
|---|
| 372 | expected = new_mail |
|---|
| 373 | expected.to = @recipient |
|---|
| 374 | expected.subject = "testing bcc/cc" |
|---|
| 375 | expected.body = "Nothing to see here." |
|---|
| 376 | expected.from = "system@loudthinking.com" |
|---|
| 377 | expected.cc = "nobody@loudthinking.com" |
|---|
| 378 | expected.bcc = "root@loudthinking.com" |
|---|
| 379 | expected.date = Time.local 2004, 12, 12 |
|---|
| 380 | |
|---|
| 381 | created = nil |
|---|
| 382 | assert_nothing_raised do |
|---|
| 383 | created = TestMailer.create_cc_bcc @recipient |
|---|
| 384 | end |
|---|
| 385 | assert_not_nil created |
|---|
| 386 | assert_equal expected.encoded, created.encoded |
|---|
| 387 | |
|---|
| 388 | assert_nothing_raised do |
|---|
| 389 | TestMailer.deliver_cc_bcc @recipient |
|---|
| 390 | end |
|---|
| 391 | |
|---|
| 392 | assert_not_nil ActionMailer::Base.deliveries.first |
|---|
| 393 | assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded |
|---|
| 394 | end |
|---|
| 395 | |
|---|
| 396 | def test_iso_charset |
|---|
| 397 | expected = new_mail( "iso-8859-1" ) |
|---|
| 398 | expected.to = @recipient |
|---|
| 399 | expected.subject = encode "testing isø charsets", "iso-8859-1" |
|---|
| 400 | expected.body = "Nothing to see here." |
|---|
| 401 | expected.from = "system@loudthinking.com" |
|---|
| 402 | expected.cc = "nobody@loudthinking.com" |
|---|
| 403 | expected.bcc = "root@loudthinking.com" |
|---|
| 404 | expected.date = Time.local 2004, 12, 12 |
|---|
| 405 | |
|---|
| 406 | created = nil |
|---|
| 407 | assert_nothing_raised do |
|---|
| 408 | created = TestMailer.create_iso_charset @recipient |
|---|
| 409 | end |
|---|
| 410 | assert_not_nil created |
|---|
| 411 | assert_equal expected.encoded, created.encoded |
|---|
| 412 | |
|---|
| 413 | assert_nothing_raised do |
|---|
| 414 | TestMailer.deliver_iso_charset @recipient |
|---|
| 415 | end |
|---|
| 416 | |
|---|
| 417 | assert_not_nil ActionMailer::Base.deliveries.first |
|---|
| 418 | assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded |
|---|
| 419 | end |
|---|
| 420 | |
|---|
| 421 | def test_unencoded_subject |
|---|
| 422 | expected = new_mail |
|---|
| 423 | expected.to = @recipient |
|---|
| 424 | expected.subject = "testing unencoded subject" |
|---|
| 425 | expected.body = "Nothing to see here." |
|---|
| 426 | expected.from = "system@loudthinking.com" |
|---|
| 427 | expected.cc = "nobody@loudthinking.com" |
|---|
| 428 | expected.bcc = "root@loudthinking.com" |
|---|
| 429 | expected.date = Time.local 2004, 12, 12 |
|---|
| 430 | |
|---|
| 431 | created = nil |
|---|
| 432 | assert_nothing_raised do |
|---|
| 433 | created = TestMailer.create_unencoded_subject @recipient |
|---|
| 434 | end |
|---|
| 435 | assert_not_nil created |
|---|
| 436 | assert_equal expected.encoded, created.encoded |
|---|
| 437 | |
|---|
| 438 | assert_nothing_raised do |
|---|
| 439 | TestMailer.deliver_unencoded_subject @recipient |
|---|
| 440 | end |
|---|
| 441 | |
|---|
| 442 | assert_not_nil ActionMailer::Base.deliveries.first |
|---|
| 443 | assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded |
|---|
| 444 | end |
|---|
| 445 | |
|---|
| 446 | def test_instances_are_nil |
|---|
| 447 | assert_nil ActionMailer::Base.new |
|---|
| 448 | assert_nil TestMailer.new |
|---|
| 449 | end |
|---|
| 450 | |
|---|
| 451 | def test_deliveries_array |
|---|
| 452 | assert_not_nil ActionMailer::Base.deliveries |
|---|
| 453 | assert_equal 0, ActionMailer::Base.deliveries.size |
|---|
| 454 | TestMailer.deliver_signed_up(@recipient) |
|---|
| 455 | assert_equal 1, ActionMailer::Base.deliveries.size |
|---|
| 456 | assert_not_nil ActionMailer::Base.deliveries.first |
|---|
| 457 | end |
|---|
| 458 | |
|---|
| 459 | def test_perform_deliveries_flag |
|---|
| 460 | ActionMailer::Base.perform_deliveries = false |
|---|
| 461 | TestMailer.deliver_signed_up(@recipient) |
|---|
| 462 | assert_equal 0, ActionMailer::Base.deliveries.size |
|---|
| 463 | ActionMailer::Base.perform_deliveries = true |
|---|
| 464 | TestMailer.deliver_signed_up(@recipient) |
|---|
| 465 | assert_equal 1, ActionMailer::Base.deliveries.size |
|---|
| 466 | end |
|---|
| 467 | |
|---|
| 468 | def test_unquote_quoted_printable_subject |
|---|
| 469 | msg = <<EOF |
|---|
| 470 | From: me@example.com |
|---|
| 471 | Subject: =?utf-8?Q?testing_testing_=D6=A4?= |
|---|
| 472 | Content-Type: text/plain; charset=iso-8859-1 |
|---|
| 473 | |
|---|
| 474 | The body |
|---|
| 475 | EOF |
|---|
| 476 | mail = TMail::Mail.parse(msg) |
|---|
| 477 | assert_equal "testing testing \326\244", mail.subject |
|---|
| 478 | assert_equal "=?utf-8?Q?testing_testing_=D6=A4?=", mail.quoted_subject |
|---|
| 479 | end |
|---|
| 480 | |
|---|
| 481 | def test_unquote_7bit_subject |
|---|
| 482 | msg = <<EOF |
|---|
| 483 | From: me@example.com |
|---|
| 484 | Subject: this == working? |
|---|
| 485 | Content-Type: text/plain; charset=iso-8859-1 |
|---|
| 486 | |
|---|
| 487 | The body |
|---|
| 488 | EOF |
|---|
| 489 | mail = TMail::Mail.parse(msg) |
|---|
| 490 | assert_equal "this == working?", mail.subject |
|---|
| 491 | assert_equal "this == working?", mail.quoted_subject |
|---|
| 492 | end |
|---|
| 493 | |
|---|
| 494 | def test_unquote_7bit_body |
|---|
| 495 | msg = <<EOF |
|---|
| 496 | From: me@example.com |
|---|
| 497 | Subject: subject |
|---|
| 498 | Content-Type: text/plain; charset=iso-8859-1 |
|---|
| 499 | Content-Transfer-Encoding: 7bit |
|---|
| 500 | |
|---|
| 501 | The=3Dbody |
|---|
| 502 | EOF |
|---|
| 503 | mail = TMail::Mail.parse(msg) |
|---|
| 504 | assert_equal "The=3Dbody", mail.body.strip |
|---|
| 505 | assert_equal "The=3Dbody", mail.quoted_body.strip |
|---|
| 506 | end |
|---|
| 507 | |
|---|
| 508 | def test_unquote_quoted_printable_body |
|---|
| 509 | msg = <<EOF |
|---|
| 510 | From: me@example.com |
|---|
| 511 | Subject: subject |
|---|
| 512 | Content-Type: text/plain; charset=iso-8859-1 |
|---|
| 513 | Content-Transfer-Encoding: quoted-printable |
|---|
| 514 | |
|---|
| 515 | The=3Dbody |
|---|
| 516 | EOF |
|---|
| 517 | mail = TMail::Mail.parse(msg) |
|---|
| 518 | assert_equal "The=body", mail.body.strip |
|---|
| 519 | assert_equal "The=3Dbody", mail.quoted_body.strip |
|---|
| 520 | end |
|---|
| 521 | |
|---|
| 522 | def test_unquote_base64_body |
|---|
| 523 | msg = <<EOF |
|---|
| 524 | From: me@example.com |
|---|
| 525 | Subject: subject |
|---|
| 526 | Content-Type: text/plain; charset=iso-8859-1 |
|---|
| 527 | Content-Transfer-Encoding: base64 |
|---|
| 528 | |
|---|
| 529 | VGhlIGJvZHk= |
|---|
| 530 | EOF |
|---|
| 531 | mail = TMail::Mail.parse(msg) |
|---|
| 532 | assert_equal "The body", mail.body.strip |
|---|
| 533 | assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip |
|---|
| 534 | end |
|---|
| 535 | |
|---|
| 536 | def test_extended_headers |
|---|
| 537 | @recipient = "Grytøyr <test@localhost>" |
|---|
| 538 | |
|---|
| 539 | expected = new_mail "iso-8859-1" |
|---|
| 540 | expected.to = quote_address_if_necessary @recipient, "iso-8859-1" |
|---|
| 541 | expected.subject = "testing extended headers" |
|---|
| 542 | expected.body = "Nothing to see here." |
|---|
| 543 | expected.from = quote_address_if_necessary "Grytøyr <stian1@example.net>", "iso-8859-1" |
|---|
| 544 | expected.cc = quote_address_if_necessary "Grytøyr <stian2@example.net>", "iso-8859-1" |
|---|
| 545 | expected.bcc = quote_address_if_necessary "Grytøyr <stian3@example.net>", "iso-8859-1" |
|---|
| 546 | expected.date = Time.local 2004, 12, 12 |
|---|
| 547 | |
|---|
| 548 | created = nil |
|---|
| 549 | assert_nothing_raised do |
|---|
| 550 | created = TestMailer.create_extended_headers @recipient |
|---|
| 551 | end |
|---|
| 552 | |
|---|
| 553 | assert_not_nil created |
|---|
| 554 | assert_equal expected.encoded, created.encoded |
|---|
| 555 | |
|---|
| 556 | assert_nothing_raised do |
|---|
| 557 | TestMailer.deliver_extended_headers @recipient |
|---|
| 558 | end |
|---|
| 559 | |
|---|
| 560 | assert_not_nil ActionMailer::Base.deliveries.first |
|---|
| 561 | assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded |
|---|
| 562 | end |
|---|
| 563 | |
|---|
| 564 | def test_utf8_body_is_not_quoted |
|---|
| 565 | @recipient = "Foo áëô îü <extended@example.net>" |
|---|
| 566 | expected = new_mail "utf-8" |
|---|
| 567 | expected.to = quote_address_if_necessary @recipient, "utf-8" |
|---|
| 568 | expected.subject = "testing utf-8 body" |
|---|
| 569 | expected.body = "åœö blah" |
|---|
| 570 | expected.from = quote_address_if_necessary @recipient, "utf-8" |
|---|
| 571 | expected.cc = quote_address_if_necessary @recipient, "utf-8" |
|---|
| 572 | expected.bcc = quote_address_if_necessary @recipient, "utf-8" |
|---|
| 573 | expected.date = Time.local 2004, 12, 12 |
|---|
| 574 | |
|---|
| 575 | created = TestMailer.create_utf8_body @recipient |
|---|
| 576 | assert_match(/åœö blah/, created.encoded) |
|---|
| 577 | end |
|---|
| 578 | |
|---|
| 579 | def test_multiple_utf8_recipients |
|---|
| 580 | @recipient = ["\"Foo áëô îü\" <extended@example.net>", "\"Example Recipient\" <me@example.com>"] |
|---|
| 581 | expected = new_mail "utf-8" |
|---|
| 582 | expected.to = quote_address_if_necessary @recipient, "utf-8" |
|---|
| 583 | expected.subject = "testing utf-8 body" |
|---|
| 584 | expected.body = "åœö blah" |
|---|
| 585 | expected.from = quote_address_if_necessary @recipient.first, "utf-8" |
|---|
| 586 | expected.cc = quote_address_if_necessary @recipient, "utf-8" |
|---|
| 587 | expected.bcc = quote_address_if_necessary @recipient, "utf-8" |
|---|
| 588 | expected.date = Time.local 2004, 12, 12 |
|---|
| 589 | |
|---|
| 590 | created = TestMailer.create_utf8_body @recipient |
|---|
| 591 | assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>\r/, created.encoded) |
|---|
| 592 | assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>, Example Recipient <me/, created.encoded) |
|---|
| 593 | end |
|---|
| 594 | |
|---|
| 595 | def test_receive_decodes_base64_encoded_mail |
|---|
| 596 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email") |
|---|
| 597 | TestMailer.receive(fixture) |
|---|
| 598 | assert_match(/Jamis/, TestMailer.received_body) |
|---|
| 599 | end |
|---|
| 600 | |
|---|
| 601 | def test_receive_attachments |
|---|
| 602 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email2") |
|---|
| 603 | mail = TMail::Mail.parse(fixture) |
|---|
| 604 | attachment = mail.attachments.last |
|---|
| 605 | assert_equal "smime.p7s", attachment.original_filename |
|---|
| 606 | assert_equal "application/pkcs7-signature", attachment.content_type |
|---|
| 607 | end |
|---|
| 608 | |
|---|
| 609 | def test_decode_attachment_without_charset |
|---|
| 610 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email3") |
|---|
| 611 | mail = TMail::Mail.parse(fixture) |
|---|
| 612 | attachment = mail.attachments.last |
|---|
| 613 | assert_equal 1026, attachment.read.length |
|---|
| 614 | end |
|---|
| 615 | |
|---|
| 616 | def test_attachment_using_content_location |
|---|
| 617 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email12") |
|---|
| 618 | mail = TMail::Mail.parse(fixture) |
|---|
| 619 | assert_equal 1, mail.attachments.length |
|---|
| 620 | assert_equal "Photo25.jpg", mail.attachments.first.original_filename |
|---|
| 621 | end |
|---|
| 622 | |
|---|
| 623 | def test_attachment_with_text_type |
|---|
| 624 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email13") |
|---|
| 625 | mail = TMail::Mail.parse(fixture) |
|---|
| 626 | assert mail.has_attachments? |
|---|
| 627 | assert_equal 1, mail.attachments.length |
|---|
| 628 | assert_equal "hello.rb", mail.attachments.first.original_filename |
|---|
| 629 | end |
|---|
| 630 | |
|---|
| 631 | def test_decode_part_without_content_type |
|---|
| 632 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email4") |
|---|
| 633 | mail = TMail::Mail.parse(fixture) |
|---|
| 634 | assert_nothing_raised { mail.body } |
|---|
| 635 | end |
|---|
| 636 | |
|---|
| 637 | def test_decode_message_without_content_type |
|---|
| 638 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email5") |
|---|
| 639 | mail = TMail::Mail.parse(fixture) |
|---|
| 640 | assert_nothing_raised { mail.body } |
|---|
| 641 | end |
|---|
| 642 | |
|---|
| 643 | def test_decode_message_with_incorrect_charset |
|---|
| 644 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email6") |
|---|
| 645 | mail = TMail::Mail.parse(fixture) |
|---|
| 646 | assert_nothing_raised { mail.body } |
|---|
| 647 | end |
|---|
| 648 | |
|---|
| 649 | def test_multipart_with_mime_version |
|---|
| 650 | mail = TestMailer.create_multipart_with_mime_version(@recipient) |
|---|
| 651 | assert_equal "1.1", mail.mime_version |
|---|
| 652 | end |
|---|
| 653 | |
|---|
| 654 | def test_multipart_with_utf8_subject |
|---|
| 655 | mail = TestMailer.create_multipart_with_utf8_subject(@recipient) |
|---|
| 656 | assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded) |
|---|
| 657 | end |
|---|
| 658 | |
|---|
| 659 | def test_implicitly_multipart_with_utf8 |
|---|
| 660 | mail = TestMailer.create_implicitly_multipart_with_utf8 |
|---|
| 661 | assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded) |
|---|
| 662 | end |
|---|
| 663 | |
|---|
| 664 | def test_explicitly_multipart_messages |
|---|
| 665 | mail = TestMailer.create_explicitly_multipart_example(@recipient) |
|---|
| 666 | assert_equal 3, mail.parts.length |
|---|
| 667 | assert_nil mail.content_type |
|---|
| 668 | assert_equal "text/plain", mail.parts[0].content_type |
|---|
| 669 | |
|---|
| 670 | assert_equal "text/html", mail.parts[1].content_type |
|---|
| 671 | assert_equal "iso-8859-1", mail.parts[1].sub_header("content-type", "charset") |
|---|
| 672 | assert_equal "inline", mail.parts[1].content_disposition |
|---|
| 673 | |
|---|
| 674 | assert_equal "image/jpeg", mail.parts[2].content_type |
|---|
| 675 | assert_equal "attachment", mail.parts[2].content_disposition |
|---|
| 676 | assert_equal "foo.jpg", mail.parts[2].sub_header("content-disposition", "filename") |
|---|
| 677 | assert_equal "foo.jpg", mail.parts[2].sub_header("content-type", "name") |
|---|
| 678 | assert_nil mail.parts[2].sub_header("content-type", "charset") |
|---|
| 679 | end |
|---|
| 680 | |
|---|
| 681 | def test_explicitly_multipart_with_content_type |
|---|
| 682 | mail = TestMailer.create_explicitly_multipart_example(@recipient, "multipart/alternative") |
|---|
| 683 | assert_equal 3, mail.parts.length |
|---|
| 684 | assert_equal "multipart/alternative", mail.content_type |
|---|
| 685 | end |
|---|
| 686 | |
|---|
| 687 | def test_explicitly_multipart_with_invalid_content_type |
|---|
| 688 | mail = TestMailer.create_explicitly_multipart_example(@recipient, "text/xml") |
|---|
| 689 | assert_equal 3, mail.parts.length |
|---|
| 690 | assert_nil mail.content_type |
|---|
| 691 | end |
|---|
| 692 | |
|---|
| 693 | def test_implicitly_multipart_messages |
|---|
| 694 | mail = TestMailer.create_implicitly_multipart_example(@recipient) |
|---|
| 695 | assert_equal 3, mail.parts.length |
|---|
| 696 | assert_equal "1.0", mail.mime_version |
|---|
| 697 | assert_equal "multipart/alternative", mail.content_type |
|---|
| 698 | assert_equal "text/yaml", mail.parts[0].content_type |
|---|
| 699 | assert_equal "utf-8", mail.parts[0].sub_header("content-type", "charset") |
|---|
| 700 | assert_equal "text/plain", mail.parts[1].content_type |
|---|
| 701 | assert_equal "utf-8", mail.parts[1].sub_header("content-type", "charset") |
|---|
| 702 | assert_equal "text/html", mail.parts[2].content_type |
|---|
| 703 | assert_equal "utf-8", mail.parts[2].sub_header("content-type", "charset") |
|---|
| 704 | end |
|---|
| 705 | |
|---|
| 706 | def test_implicitly_multipart_messages_with_custom_order |
|---|
| 707 | mail = TestMailer.create_implicitly_multipart_example(@recipient, nil, ["text/yaml", "text/plain"]) |
|---|
| 708 | assert_equal 3, mail.parts.length |
|---|
| 709 | assert_equal "text/html", mail.parts[0].content_type |
|---|
| 710 | assert_equal "text/plain", mail.parts[1].content_type |
|---|
| 711 | assert_equal "text/yaml", mail.parts[2].content_type |
|---|
| 712 | end |
|---|
| 713 | |
|---|
| 714 | def test_implicitly_multipart_messages_with_charset |
|---|
| 715 | mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1') |
|---|
| 716 | |
|---|
| 717 | assert_equal "multipart/alternative", mail.header['content-type'].body |
|---|
| 718 | |
|---|
| 719 | assert_equal 'iso-8859-1', mail.parts[0].sub_header("content-type", "charset") |
|---|
| 720 | assert_equal 'iso-8859-1', mail.parts[1].sub_header("content-type", "charset") |
|---|
| 721 | assert_equal 'iso-8859-1', mail.parts[2].sub_header("content-type", "charset") |
|---|
| 722 | end |
|---|
| 723 | |
|---|
| 724 | def test_html_mail |
|---|
| 725 | mail = TestMailer.create_html_mail(@recipient) |
|---|
| 726 | assert_equal "text/html", mail.content_type |
|---|
| 727 | end |
|---|
| 728 | |
|---|
| 729 | def test_html_mail_with_underscores |
|---|
| 730 | mail = TestMailer.create_html_mail_with_underscores(@recipient) |
|---|
| 731 | assert_equal %{<a href="http://google.com" target="_blank">_Google</a>}, mail.body |
|---|
| 732 | end |
|---|
| 733 | |
|---|
| 734 | def test_various_newlines |
|---|
| 735 | mail = TestMailer.create_various_newlines(@recipient) |
|---|
| 736 | assert_equal("line #1\nline #2\nline #3\nline #4\n\n" + |
|---|
| 737 | "line #5\n\nline#6\n\nline #7", mail.body) |
|---|
| 738 | end |
|---|
| 739 | |
|---|
| 740 | def test_various_newlines_multipart |
|---|
| 741 | mail = TestMailer.create_various_newlines_multipart(@recipient) |
|---|
| 742 | assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body |
|---|
| 743 | 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 |
|---|
| 744 | end |
|---|
| 745 | |
|---|
| 746 | def test_headers_removed_on_smtp_delivery |
|---|
| 747 | ActionMailer::Base.delivery_method = :smtp |
|---|
| 748 | TestMailer.deliver_cc_bcc(@recipient) |
|---|
| 749 | assert MockSMTP.deliveries[0][2].include?("root@loudthinking.com") |
|---|
| 750 | assert MockSMTP.deliveries[0][2].include?("nobody@loudthinking.com") |
|---|
| 751 | assert MockSMTP.deliveries[0][2].include?(@recipient) |
|---|
| 752 | assert_match %r{^Cc: nobody@loudthinking.com}, MockSMTP.deliveries[0][0] |
|---|
| 753 | assert_match %r{^To: #{@recipient}}, MockSMTP.deliveries[0][0] |
|---|
| 754 | assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0] |
|---|
| 755 | end |
|---|
| 756 | |
|---|
| 757 | def test_recursive_multipart_processing |
|---|
| 758 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7") |
|---|
| 759 | mail = TMail::Mail.parse(fixture) |
|---|
| 760 | assert_equal "This is the first part.\n\nAttachment: test.rb\nAttachment: test.pdf\n\n\nAttachment: smime.p7s\n", mail.body |
|---|
| 761 | end |
|---|
| 762 | |
|---|
| 763 | def test_decode_encoded_attachment_filename |
|---|
| 764 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8") |
|---|
| 765 | mail = TMail::Mail.parse(fixture) |
|---|
| 766 | attachment = mail.attachments.last |
|---|
| 767 | assert_equal "01QuienTeDijat.Pitbull.mp3", attachment.original_filename |
|---|
| 768 | end |
|---|
| 769 | |
|---|
| 770 | def test_wrong_mail_header |
|---|
| 771 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email9") |
|---|
| 772 | assert_raise(TMail::SyntaxError) { TMail::Mail.parse(fixture) } |
|---|
| 773 | end |
|---|
| 774 | |
|---|
| 775 | def test_decode_message_with_unknown_charset |
|---|
| 776 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10") |
|---|
| 777 | mail = TMail::Mail.parse(fixture) |
|---|
| 778 | assert_nothing_raised { mail.body } |
|---|
| 779 | end |
|---|
| 780 | |
|---|
| 781 | def test_decode_message_with_unquoted_atchar_in_header |
|---|
| 782 | fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email11") |
|---|
| 783 | mail = TMail::Mail.parse(fixture) |
|---|
| 784 | assert_not_nil mail.from |
|---|
| 785 | end |
|---|
| 786 | |
|---|
| 787 | def test_empty_header_values_omitted |
|---|
| 788 | result = TestMailer.create_unnamed_attachment(@recipient).encoded |
|---|
| 789 | assert_match %r{Content-Type: application/octet-stream[^;]}, result |
|---|
| 790 | assert_match %r{Content-Disposition: attachment[^;]}, result |
|---|
| 791 | end |
|---|
| 792 | |
|---|
| 793 | def test_headers_with_nonalpha_chars |
|---|
| 794 | mail = TestMailer.create_headers_with_nonalpha_chars(@recipient) |
|---|
| 795 | assert !mail.from_addrs.empty? |
|---|
| 796 | assert !mail.cc_addrs.empty? |
|---|
| 797 | assert !mail.bcc_addrs.empty? |
|---|
| 798 | assert_match(/:/, mail.from_addrs.to_s) |
|---|
| 799 | assert_match(/:/, mail.cc_addrs.to_s) |
|---|
| 800 | assert_match(/:/, mail.bcc_addrs.to_s) |
|---|
| 801 | end |
|---|
| 802 | |
|---|
| 803 | def test_deliver_with_mail_object |
|---|
| 804 | mail = TestMailer.create_headers_with_nonalpha_chars(@recipient) |
|---|
| 805 | assert_nothing_raised { TestMailer.deliver(mail) } |
|---|
| 806 | assert_equal 1, TestMailer.deliveries.length |
|---|
| 807 | end |
|---|
| 808 | |
|---|
| 809 | def test_multipart_with_template_path_with_dots |
|---|
| 810 | mail = FunkyPathMailer.create_multipart_with_template_path_with_dots(@recipient) |
|---|
| 811 | assert_equal 2, mail.parts.length |
|---|
| 812 | end |
|---|
| 813 | |
|---|
| 814 | def test_custom_content_type_attributes |
|---|
| 815 | mail = TestMailer.create_custom_content_type_attributes |
|---|
| 816 | assert_match %r{format=flowed}, mail['content-type'].to_s |
|---|
| 817 | assert_match %r{charset=utf-8}, mail['content-type'].to_s |
|---|
| 818 | end |
|---|
| 819 | end |
|---|
| 820 | |
|---|
| 821 | class InheritableTemplateRootTest < Test::Unit::TestCase
|
|---|
| 822 | def test_attr
|
|---|
| 823 | expected = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
|
|---|
| 824 | assert_equal expected, FunkyPathMailer.template_root
|
|---|
| 825 |
|
|---|
| 826 | sub = Class.new(FunkyPathMailer)
|
|---|
| 827 | sub.template_root = 'test/path'
|
|---|
| 828 |
|
|---|
| 829 | assert_equal 'test/path', sub.template_root
|
|---|
| 830 | assert_equal expected, FunkyPathMailer.template_root
|
|---|
| 831 | end
|
|---|
| 832 | end
|
|---|