PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/actionmailer/test/mail_service_test.rb

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