You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mailer_test.rb 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../test_helper'
  19. class MailerTest < ActiveSupport::TestCase
  20. include Redmine::I18n
  21. include Rails::Dom::Testing::Assertions
  22. fixtures :projects, :enabled_modules, :issues, :users, :email_addresses, :user_preferences, :members,
  23. :member_roles, :roles, :documents, :attachments, :news,
  24. :tokens, :journals, :journal_details, :changesets,
  25. :trackers, :projects_trackers,
  26. :custom_fields, :custom_fields_trackers,
  27. :issue_statuses, :enumerations, :messages, :boards, :repositories,
  28. :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
  29. :versions,
  30. :comments,
  31. :groups_users, :watchers
  32. def setup
  33. ActionMailer::Base.deliveries.clear
  34. Setting.plain_text_mail = '0'
  35. Setting.default_language = 'en'
  36. User.current = nil
  37. end
  38. def test_generated_links_in_emails
  39. with_settings :host_name => 'mydomain.foo', :protocol => 'https' do
  40. journal = Journal.find(3)
  41. assert Mailer.deliver_issue_edit(journal)
  42. end
  43. mail = last_email
  44. assert_select_email do
  45. # link to the main ticket on issue id
  46. assert_select 'a[href=?]',
  47. 'https://mydomain.foo/issues/2#change-3',
  48. :text => '#2'
  49. # link to the main ticket
  50. assert_select 'a[href=?]',
  51. 'https://mydomain.foo/issues/2#change-3',
  52. :text => 'Feature request #2: Add ingredients categories'
  53. # link to a referenced ticket
  54. assert_select 'a[href=?][title=?]',
  55. 'https://mydomain.foo/issues/1',
  56. "Bug: Cannot print recipes (New)",
  57. :text => '#1'
  58. # link to a changeset
  59. assert_select 'a[href=?][title=?]',
  60. 'https://mydomain.foo/projects/ecookbook/repository/10/revisions/2',
  61. 'This commit fixes #1, #2 and references #1 & #3',
  62. :text => 'r2'
  63. # link to a description diff
  64. assert_select 'a[href^=?][title=?]',
  65. # should be https://mydomain.foo/journals/diff/3?detail_id=4
  66. # but the Rails 4.2 DOM assertion doesn't handle the ? in the
  67. # attribute value
  68. 'https://mydomain.foo/journals/3/diff',
  69. 'View differences',
  70. :text => 'diff'
  71. # link to an attachment
  72. assert_select 'a[href=?]',
  73. 'https://mydomain.foo/attachments/download/4/source.rb',
  74. :text => 'source.rb'
  75. end
  76. end
  77. def test_generated_links_with_prefix
  78. relative_url_root = Redmine::Utils.relative_url_root
  79. with_settings :host_name => 'mydomain.foo/rdm', :protocol => 'http' do
  80. journal = Journal.find(3)
  81. assert Mailer.deliver_issue_edit(journal)
  82. end
  83. mail = last_email
  84. assert_select_email do
  85. # link to the main ticket
  86. assert_select 'a[href=?]',
  87. 'http://mydomain.foo/rdm/issues/2#change-3',
  88. :text => 'Feature request #2: Add ingredients categories'
  89. # link to a referenced ticket
  90. assert_select 'a[href=?][title=?]',
  91. 'http://mydomain.foo/rdm/issues/1',
  92. "Bug: Cannot print recipes (New)",
  93. :text => '#1'
  94. # link to a changeset
  95. assert_select 'a[href=?][title=?]',
  96. 'http://mydomain.foo/rdm/projects/ecookbook/repository/10/revisions/2',
  97. 'This commit fixes #1, #2 and references #1 & #3',
  98. :text => 'r2'
  99. # link to a description diff
  100. assert_select 'a[href^=?][title=?]',
  101. # should be http://mydomain.foo/rdm/journals/diff/3?detail_id=4
  102. # but the Rails 4.2 DOM assertion doesn't handle the ? in the
  103. # attribute value
  104. 'http://mydomain.foo/rdm/journals/3/diff',
  105. 'View differences',
  106. :text => 'diff'
  107. # link to an attachment
  108. assert_select 'a[href=?]',
  109. 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
  110. :text => 'source.rb'
  111. end
  112. end
  113. def test_generated_links_with_port_and_prefix
  114. with_settings :host_name => '10.0.0.1:81/redmine', :protocol => 'http' do
  115. Mailer.test_email(User.find(1)).deliver_now
  116. mail = last_email
  117. assert_include 'http://10.0.0.1:81/redmine', mail_body(mail)
  118. end
  119. end
  120. def test_generated_links_with_port
  121. with_settings :host_name => '10.0.0.1:81', :protocol => 'http' do
  122. Mailer.test_email(User.find(1)).deliver_now
  123. mail = last_email
  124. assert_include 'http://10.0.0.1:81', mail_body(mail)
  125. end
  126. end
  127. def test_issue_edit_should_generate_url_with_hostname_for_relations
  128. journal = Journal.new(:journalized => Issue.find(1), :user => User.find(1), :created_on => Time.now)
  129. journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'label_relates_to', :value => 2)
  130. journal.save
  131. Mailer.deliver_issue_edit(journal)
  132. assert_not_nil last_email
  133. assert_select_email do
  134. assert_select 'a[href=?]', 'http://localhost:3000/issues/2', :text => 'Feature request #2'
  135. end
  136. end
  137. def test_generated_links_with_prefix_and_no_relative_url_root
  138. relative_url_root = Redmine::Utils.relative_url_root
  139. Redmine::Utils.relative_url_root = nil
  140. with_settings :host_name => 'mydomain.foo/rdm', :protocol => 'http' do
  141. journal = Journal.find(3)
  142. assert Mailer.deliver_issue_edit(journal)
  143. end
  144. mail = last_email
  145. assert_select_email do
  146. # link to the main ticket
  147. assert_select 'a[href=?]',
  148. 'http://mydomain.foo/rdm/issues/2#change-3',
  149. :text => 'Feature request #2: Add ingredients categories'
  150. # link to a referenced ticket
  151. assert_select 'a[href=?][title=?]',
  152. 'http://mydomain.foo/rdm/issues/1',
  153. "Bug: Cannot print recipes (New)",
  154. :text => '#1'
  155. # link to a changeset
  156. assert_select 'a[href=?][title=?]',
  157. 'http://mydomain.foo/rdm/projects/ecookbook/repository/10/revisions/2',
  158. 'This commit fixes #1, #2 and references #1 & #3',
  159. :text => 'r2'
  160. # link to a description diff
  161. assert_select 'a[href^=?][title=?]',
  162. # should be http://mydomain.foo/rdm/journals/diff/3?detail_id=4
  163. # but the Rails 4.2 DOM assertion doesn't handle the ? in the
  164. # attribute value
  165. 'http://mydomain.foo/rdm/journals/3/diff',
  166. 'View differences',
  167. :text => 'diff'
  168. # link to an attachment
  169. assert_select 'a[href=?]',
  170. 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
  171. :text => 'source.rb'
  172. end
  173. ensure
  174. # restore it
  175. Redmine::Utils.relative_url_root = relative_url_root
  176. end
  177. def test_link_to_user_in_email
  178. issue = Issue.generate!(:description => '@jsmith')
  179. assert Mailer.deliver_issue_add(issue)
  180. assert_select_email do
  181. assert_select "a[href=?]", "http://localhost:3000/users/2", :text => '@John Smith'
  182. end
  183. end
  184. def test_thumbnail_macro_in_email
  185. set_tmp_attachments_directory
  186. issue = Issue.generate!(:description => '{{thumbnail(image.png)}}')
  187. issue.attachments << Attachment.new(:file => mock_file_with_options(:original_filename => 'image.png'), :author => User.find(1))
  188. issue.save!
  189. assert Mailer.deliver_issue_add(issue)
  190. assert_select_email do
  191. assert_select 'img[alt="image.png"]'
  192. end
  193. end
  194. def test_email_headers
  195. with_settings :mail_from => 'Redmine <redmine@example.net>' do
  196. issue = Issue.find(1)
  197. Mailer.deliver_issue_add(issue)
  198. end
  199. mail = last_email
  200. assert_equal 'All', mail.header['X-Auto-Response-Suppress'].to_s
  201. assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
  202. # List-Id should not include the display name "Redmine"
  203. assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
  204. assert_equal 'Bug', mail.header['X-Redmine-Issue-Tracker'].to_s
  205. assert_equal 'Low', mail.header['X-Redmine-Issue-Priority'].to_s
  206. end
  207. def test_email_headers_should_include_sender
  208. issue = Issue.find(1)
  209. Mailer.deliver_issue_add(issue)
  210. mail = last_email
  211. assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
  212. end
  213. def test_email_headers_should_not_include_assignee_when_not_assigned
  214. issue = Issue.find(6)
  215. issue.init_journal(User.current)
  216. issue.update(:status_id => 4)
  217. issue.update(:assigned_to_id => nil)
  218. mail = last_email
  219. assert_not mail.header['X-Redmine-Issue-Assignee']
  220. end
  221. def test_email_headers_should_include_assignee_when_assigned
  222. issue = Issue.find(6)
  223. issue.init_journal(User.current)
  224. issue.update(:assigned_to_id => 2)
  225. mail = last_email
  226. assert_equal 'jsmith', mail.header['X-Redmine-Issue-Assignee'].to_s
  227. end
  228. def test_email_headers_should_include_assignee_if_assigned_to_group
  229. issue = Issue.find(6)
  230. with_settings :issue_group_assignment => 1 do
  231. issue.init_journal(User.current)
  232. issue.update(:assigned_to_id => 10)
  233. end
  234. mail = last_email
  235. assert_equal 'Group (A Team)', mail.header['X-Redmine-Issue-Assignee'].to_s
  236. end
  237. def test_plain_text_mail
  238. Setting.plain_text_mail = 1
  239. journal = Journal.find(2)
  240. Mailer.deliver_issue_edit(journal)
  241. mail = last_email
  242. assert_equal "text/plain; charset=UTF-8", mail.content_type
  243. assert_equal 0, mail.parts.size
  244. assert !mail.encoded.include?('href')
  245. end
  246. def test_html_mail
  247. Setting.plain_text_mail = 0
  248. journal = Journal.find(2)
  249. Mailer.deliver_issue_edit(journal)
  250. mail = last_email
  251. assert_equal 2, mail.parts.size
  252. assert mail.encoded.include?('href')
  253. end
  254. def test_from_header
  255. with_settings :mail_from => 'redmine@example.net' do
  256. Mailer.deliver_test_email(User.find(1))
  257. end
  258. mail = last_email
  259. assert_equal 'redmine@example.net', mail.from_addrs.first
  260. end
  261. def test_from_header_with_phrase
  262. with_settings :mail_from => 'Redmine app <redmine@example.net>' do
  263. Mailer.deliver_test_email(User.find(1))
  264. end
  265. mail = last_email
  266. assert_equal 'redmine@example.net', mail.from_addrs.first
  267. assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
  268. end
  269. def test_from_header_with_rfc_non_compliant_phrase
  270. # Send out the email instead of raising an exception
  271. # no matter if the emission email address is not RFC compliant
  272. assert_nothing_raised do
  273. with_settings :mail_from => '[Redmine app] <redmine@example.net>' do
  274. Mailer.deliver_test_email(User.find(1))
  275. end
  276. end
  277. mail = last_email
  278. assert_match /<redmine@example\.net>/, mail.from_addrs.first
  279. assert_equal '[Redmine app] <redmine@example.net>', mail.header['From'].to_s
  280. end
  281. def test_from_header_with_author_name
  282. # Use the author's name or Setting.app_title as a display name
  283. # when Setting.mail_from does not include a display name
  284. with_settings :mail_from => 'redmine@example.net', :app_title => 'Foo' do
  285. # Use @author.name as a display name
  286. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
  287. :subject => 'Issue created by Dave Lopper', :author_id => 3)
  288. mail = last_email
  289. assert_equal 'redmine@example.net', mail.from_addrs.first
  290. assert_equal 'Dave Lopper <redmine@example.net>', mail.header['From'].to_s
  291. # Use app_title if @author is nil or AnonymousUser
  292. Mailer.deliver_test_email(User.find(1))
  293. mail = last_email
  294. assert_equal 'redmine@example.net', mail.from_addrs.first
  295. assert_equal "Foo <redmine@example.net>", mail.header['From'].to_s
  296. end
  297. end
  298. def test_should_not_send_email_without_recipient
  299. news = News.first
  300. user = news.author
  301. # Remove members except news author
  302. news.project.memberships.each {|m| m.destroy unless m.user == user}
  303. user.pref.no_self_notified = false
  304. user.pref.save
  305. User.current = user
  306. Mailer.deliver_news_added(news.reload)
  307. assert_equal 1, last_email.to.size
  308. # nobody to notify
  309. user.pref.no_self_notified = true
  310. user.pref.save
  311. User.current = user
  312. ActionMailer::Base.deliveries.clear
  313. Mailer.deliver_news_added(news.reload)
  314. assert ActionMailer::Base.deliveries.empty?
  315. end
  316. def test_issue_add_message_id
  317. issue = Issue.find(2)
  318. Mailer.deliver_issue_add(issue)
  319. mail = last_email
  320. uid = destination_user(mail).id
  321. assert_include "redmine.issue-2.20060719190421.#{uid}@example.net", mail.message_id
  322. assert_include "redmine.issue-2.20060719190421.#{uid}@example.net", mail.references
  323. end
  324. def test_issue_edit_message_id
  325. journal = Journal.find(3)
  326. journal.issue = Issue.find(2)
  327. Mailer.deliver_issue_edit(journal)
  328. mail = last_email
  329. uid = destination_user(mail).id
  330. assert_match /^redmine\.journal-3\.\d+\.#{uid}@example\.net/, mail.message_id
  331. assert_include "redmine.issue-2.20060719190421.#{uid}@example.net", mail.references
  332. assert_select_email do
  333. # link to the update
  334. assert_select "a[href=?]",
  335. "http://localhost:3000/issues/#{journal.journalized_id}#change-#{journal.id}"
  336. end
  337. end
  338. def test_message_posted_message_id
  339. message = Message.find(1)
  340. attachment = message.attachments.first
  341. Mailer.deliver_message_posted(message)
  342. mail = last_email
  343. uid = destination_user(mail).id
  344. assert_include "redmine.message-1.20070512151532.#{uid}@example.net", mail.message_id
  345. assert_include "redmine.message-1.20070512151532.#{uid}@example.net", mail.references
  346. assert_select_email do
  347. # link to the message
  348. assert_select "a[href=?]",
  349. "http://localhost:3000/boards/#{message.board.id}/topics/#{message.id}",
  350. :text => message.subject
  351. # link to the attachments download
  352. assert_select 'fieldset.attachments' do
  353. assert_select 'a[href=?]',
  354. "http://localhost:3000/attachments/download/#{attachment.id}/#{attachment.filename}",
  355. :text => attachment.filename
  356. end
  357. end
  358. end
  359. def test_reply_posted_message_id
  360. set_tmp_attachments_directory
  361. message = Message.find(3)
  362. attachment = Attachment.generate!(
  363. :container => message,
  364. :file => uploaded_test_file('testfile.txt', 'text/plain')
  365. )
  366. Mailer.deliver_message_posted(message)
  367. mail = last_email
  368. uid = destination_user(mail).id
  369. assert_include "redmine.message-3.20070512151802.#{uid}@example.net", mail.message_id
  370. assert_include "redmine.message-1.20070512151532.#{uid}@example.net", mail.references
  371. assert_select_email do
  372. # link to the reply
  373. assert_select "a[href=?]",
  374. "http://localhost:3000/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
  375. :text => message.subject
  376. # link to the attachments download
  377. assert_select 'fieldset.attachments' do
  378. assert_select 'a[href=?]',
  379. "http://localhost:3000/attachments/download/#{attachment.id}/testfile.txt",
  380. :text => 'testfile.txt'
  381. end
  382. end
  383. end
  384. def test_timestamp_in_message_id_should_be_utc
  385. zone_was = Time.zone
  386. issue = Issue.find(3)
  387. user = User.find(1)
  388. %w(UTC Paris Tokyo).each do |zone|
  389. Time.use_zone(zone) do
  390. assert_match /^redmine\.issue-3\.20060719190727\.1@example\.net/, Mailer.token_for(issue, user)
  391. end
  392. end
  393. end
  394. test "#issue_add should notify project members" do
  395. issue = Issue.find(1)
  396. assert Mailer.deliver_issue_add(issue)
  397. assert_include 'dlopper@somenet.foo', recipients
  398. end
  399. def test_issue_add_should_send_mail_to_all_user_email_address
  400. EmailAddress.create!(:user_id => 3, :address => 'otheremail@somenet.foo')
  401. issue = Issue.find(1)
  402. assert Mailer.deliver_issue_add(issue)
  403. assert mail = ActionMailer::Base.deliveries.find {|m| m.to.include?('dlopper@somenet.foo')}
  404. assert mail.to.include?('otheremail@somenet.foo')
  405. end
  406. test "#issue_add should not notify project members that are not allow to view the issue" do
  407. issue = Issue.find(1)
  408. Role.find(2).remove_permission!(:view_issues)
  409. assert Mailer.deliver_issue_add(issue)
  410. assert_not_include 'dlopper@somenet.foo', recipients
  411. end
  412. test "#issue_add should notify issue watchers" do
  413. issue = Issue.find(1)
  414. user = User.find(9)
  415. # minimal email notification options
  416. user.pref.no_self_notified = '1'
  417. user.pref.save
  418. user.mail_notification = false
  419. user.save
  420. Watcher.create!(:watchable => issue, :user => user)
  421. assert Mailer.deliver_issue_add(issue)
  422. assert_include user.mail, recipients
  423. end
  424. test "#issue_add should not notify watchers not allowed to view the issue" do
  425. issue = Issue.find(1)
  426. user = User.find(9)
  427. Watcher.create!(:watchable => issue, :user => user)
  428. Role.non_member.remove_permission!(:view_issues)
  429. assert Mailer.deliver_issue_add(issue)
  430. assert_not_include user.mail, recipients
  431. end
  432. def test_issue_add_should_notify_mentioned_users_in_issue_description
  433. User.find(1).mail_notification = 'only_my_events'
  434. issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper and @admin.')
  435. assert Mailer.deliver_issue_add(issue)
  436. # @jsmith and @dlopper are members of the project
  437. # admin is mentioned
  438. # @dlopper won't receive duplicated notifications
  439. assert_equal 3, ActionMailer::Base.deliveries.size
  440. assert_include User.find(1).mail, recipients
  441. end
  442. def test_issue_add_should_include_enabled_fields
  443. issue = Issue.find(2)
  444. assert Mailer.deliver_issue_add(issue)
  445. assert_mail_body_match '* Target version: 1.0', last_email
  446. assert_select_email do
  447. assert_select 'li', :text => 'Target version: 1.0'
  448. end
  449. end
  450. def test_issue_add_should_not_include_disabled_fields
  451. issue = Issue.find(2)
  452. tracker = issue.tracker
  453. tracker.core_fields -= ['fixed_version_id', 'start_date']
  454. tracker.save!
  455. assert Mailer.deliver_issue_add(issue)
  456. assert_mail_body_no_match 'Target version', last_email
  457. assert_mail_body_no_match 'Start date', last_email
  458. assert_select_email do
  459. assert_select 'li', :text => /Target version/, :count => 0
  460. assert_select 'li', :text => /Start date/, :count => 0
  461. end
  462. end
  463. def test_issue_add_subject_should_include_status_if_setting_is_enabled
  464. with_settings :show_status_changes_in_mail_subject => 1 do
  465. issue = Issue.find(2)
  466. Mailer.deliver_issue_add(issue)
  467. mail = last_email
  468. assert_equal "[eCookbook - Feature request #2] (Assigned) Add ingredients categories", mail.subject
  469. end
  470. end
  471. def test_issue_add_subject_should_not_include_status_if_setting_is_disabled
  472. with_settings :show_status_changes_in_mail_subject => 0 do
  473. issue = Issue.find(2)
  474. Mailer.deliver_issue_add(issue)
  475. mail = last_email
  476. assert_equal "[eCookbook - Feature request #2] Add ingredients categories", mail.subject
  477. end
  478. end
  479. def test_issue_add_should_include_issue_status_type_badge
  480. issue = Issue.find(1)
  481. Mailer.deliver_issue_add(issue)
  482. mail = last_email
  483. assert_select_email do
  484. assert_select 'span.badge.badge-status-open', text: 'open'
  485. end
  486. end
  487. def test_issue_edit_subject_should_include_status_changes_if_setting_is_enabled
  488. with_settings :show_status_changes_in_mail_subject => 1 do
  489. issue = Issue.find(2)
  490. issue.init_journal(User.current)
  491. issue.update(:status_id => 4)
  492. journal = issue.journals.last
  493. Mailer.deliver_issue_edit(journal)
  494. assert journal.new_value_for('status_id')
  495. mail = last_email
  496. assert_equal "[eCookbook - Feature request #2] (Feedback) Add ingredients categories", mail.subject
  497. end
  498. end
  499. def test_issue_edit_subject_should_not_include_status_changes_if_setting_is_disabled
  500. with_settings :show_status_changes_in_mail_subject => 0 do
  501. issue = Issue.find(2)
  502. issue.init_journal(User.current)
  503. issue.update(:status_id => 4)
  504. journal = issue.journals.last
  505. Mailer.deliver_issue_edit(journal)
  506. assert journal.new_value_for('status_id')
  507. mail = last_email
  508. assert_equal "[eCookbook - Feature request #2] Add ingredients categories", mail.subject
  509. end
  510. end
  511. def test_issue_edit_should_send_private_notes_to_users_with_permission_only
  512. journal = Journal.find(1)
  513. journal.private_notes = true
  514. journal.save!
  515. Role.find(2).add_permission! :view_private_notes
  516. assert_difference 'ActionMailer::Base.deliveries.size', 2 do
  517. Mailer.deliver_issue_edit(journal)
  518. end
  519. assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), recipients
  520. ActionMailer::Base.deliveries.clear
  521. Role.find(2).remove_permission! :view_private_notes
  522. assert_difference 'ActionMailer::Base.deliveries.size', 1 do
  523. Mailer.deliver_issue_edit(journal)
  524. end
  525. assert_equal %w(jsmith@somenet.foo), recipients
  526. end
  527. def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
  528. Issue.find(1).set_watcher(User.find_by_login('someone'))
  529. journal = Journal.find(1)
  530. journal.private_notes = true
  531. journal.save!
  532. Role.non_member.add_permission! :view_private_notes
  533. Mailer.deliver_issue_edit(journal)
  534. assert_include 'someone@foo.bar', recipients
  535. ActionMailer::Base.deliveries.clear
  536. Role.non_member.remove_permission! :view_private_notes
  537. Mailer.deliver_issue_edit(journal)
  538. assert_not_include 'someone@foo.bar', recipients
  539. end
  540. def test_issue_edit_should_mark_private_notes
  541. journal = Journal.find(2)
  542. journal.private_notes = true
  543. journal.save!
  544. with_settings :default_language => 'en' do
  545. Mailer.deliver_issue_edit(journal)
  546. end
  547. assert_mail_body_match '(Private notes)', last_email
  548. end
  549. def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
  550. issue = Issue.generate!
  551. issue.init_journal(User.find(1))
  552. private_issue = Issue.generate!(:is_private => true)
  553. IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
  554. issue.reload
  555. assert_equal 1, issue.journals.size
  556. journal = issue.journals.first
  557. ActionMailer::Base.deliveries.clear
  558. Mailer.deliver_issue_edit(journal)
  559. recipients.each do |email|
  560. user = User.find_by_mail(email)
  561. assert private_issue.visible?(user), "Issue was not visible to #{user}"
  562. end
  563. end
  564. def test_issue_edit_should_notify_mentioned_users_in_issue_updated_description
  565. User.find(1).mail_notification = 'only_my_events'
  566. issue = Issue.find(3)
  567. issue.init_journal(User.current)
  568. issue.update(description: "Hello @admin")
  569. journal = issue.journals.last
  570. ActionMailer::Base.deliveries.clear
  571. Mailer.deliver_issue_edit(journal)
  572. # @jsmith and @dlopper are members of the project
  573. # admin is mentioned in the updated description
  574. # @dlopper won't receive duplicated notifications
  575. assert_equal 3, ActionMailer::Base.deliveries.size
  576. assert_include User.find(1).mail, recipients
  577. end
  578. def test_issue_edit_should_notify_mentioned_users_in_notes
  579. User.find(1).mail_notification = 'only_my_events'
  580. journal = Journal.generate!(journalized: Issue.find(3), user: User.find(1), notes: 'Hello @admin.')
  581. ActionMailer::Base.deliveries.clear
  582. Mailer.deliver_issue_edit(journal)
  583. # @jsmith and @dlopper are members of the project
  584. # admin is mentioned in the notes
  585. # @dlopper won't receive duplicated notifications
  586. assert_equal 3, ActionMailer::Base.deliveries.size
  587. assert_include User.find(1).mail, recipients
  588. end
  589. def test_issue_should_send_email_notification_with_suppress_empty_fields
  590. ActionMailer::Base.deliveries.clear
  591. with_settings :notified_events => %w(issue_added) do
  592. cf = IssueCustomField.generate!
  593. issue = Issue.generate!(:parent => Issue.find(1))
  594. Mailer.deliver_issue_add(issue)
  595. assert_not_equal 0, ActionMailer::Base.deliveries.size
  596. mail = last_email
  597. assert_mail_body_match /^\* Author: /, mail
  598. assert_mail_body_match /^\* Status: /, mail
  599. assert_mail_body_match /^\* Priority: /, mail
  600. assert_mail_body_match /^\* Parent task: /, mail
  601. assert_mail_body_no_match /^\* Assignee: /, mail
  602. assert_mail_body_no_match /^\* Category: /, mail
  603. assert_mail_body_no_match /^\* Target version: /, mail
  604. assert_mail_body_no_match /^\* #{cf.name}: /, mail
  605. end
  606. end
  607. def test_locked_user_in_group_watcher_should_not_be_notified
  608. locked_user = users(:users_005)
  609. group = Group.generate!
  610. group.users << locked_user
  611. issue = Issue.generate!
  612. Watcher.create!(:watchable => issue, :user => group)
  613. ActionMailer::Base.deliveries.clear
  614. assert Mailer.deliver_issue_add(issue)
  615. assert_not_include locked_user.mail, recipients
  616. journal = issue.init_journal(User.current)
  617. issue.update(:status_id => 4)
  618. ActionMailer::Base.deliveries.clear
  619. Mailer.deliver_issue_edit(journal)
  620. assert_not_include locked_user.mail, recipients
  621. end
  622. def test_version_file_added
  623. attachements = [Attachment.find_by_container_type('Version')]
  624. assert Mailer.deliver_attachments_added(attachements)
  625. assert_not_nil last_email.to
  626. assert last_email.to.any?
  627. assert_select_email do
  628. assert_select "a[href=?]", "http://localhost:3000/projects/ecookbook/files"
  629. end
  630. end
  631. def test_project_file_added
  632. attachements = [Attachment.find_by_container_type('Project')]
  633. assert Mailer.deliver_attachments_added(attachements)
  634. assert_not_nil last_email.to
  635. assert last_email.to.any?
  636. assert_select_email do
  637. assert_select "a[href=?]", "http://localhost:3000/projects/ecookbook/files"
  638. end
  639. end
  640. def test_news_added_should_notify_project_news_watchers
  641. set_tmp_attachments_directory
  642. user1 = User.generate!
  643. user2 = User.generate!
  644. news = News.find(1)
  645. news.project.enabled_module('news').add_watcher(user1)
  646. attachment = Attachment.generate!(
  647. :container => news,
  648. :file => uploaded_test_file('testfile.txt', 'text/plain')
  649. )
  650. Mailer.deliver_news_added(news)
  651. assert_include user1.mail, recipients
  652. assert_not_include user2.mail, recipients
  653. assert_select_email do
  654. # link to the attachments download
  655. assert_select 'fieldset.attachments' do
  656. assert_select 'a[href=?]',
  657. "http://localhost:3000/attachments/download/#{attachment.id}/testfile.txt",
  658. :text => 'testfile.txt'
  659. end
  660. end
  661. end
  662. def test_wiki_content_added
  663. content = WikiContent.find(1)
  664. assert_difference 'ActionMailer::Base.deliveries.size', 2 do
  665. assert Mailer.deliver_wiki_content_added(content)
  666. assert_select_email do
  667. assert_select 'a[href=?]',
  668. 'http://localhost:3000/projects/ecookbook/wiki/CookBook_documentation',
  669. :text => 'CookBook documentation'
  670. end
  671. end
  672. end
  673. def test_wiki_content_added_should_notify_mentioned_users_in_content
  674. content = WikiContent.new(text: 'Hello @admin.', author_id: 1, page_id: 1)
  675. content.save!
  676. ActionMailer::Base.deliveries.clear
  677. Mailer.deliver_wiki_content_added(content)
  678. # @jsmith and @dlopper are members of the project
  679. # admin is mentioned in the notes
  680. # @dlopper won't receive duplicated notifications
  681. assert_equal 3, ActionMailer::Base.deliveries.size
  682. assert_include User.find(1).mail, recipients
  683. end
  684. def test_wiki_content_updated
  685. content = WikiContent.find(1)
  686. assert Mailer.deliver_wiki_content_updated(content)
  687. assert_select_email do
  688. assert_select 'a[href=?]',
  689. 'http://localhost:3000/projects/ecookbook/wiki/CookBook_documentation',
  690. :text => 'CookBook documentation'
  691. end
  692. end
  693. def test_wiki_content_updated_should_notify_mentioned_users_in_updated_content
  694. content = WikiContent.find(1)
  695. content.update(text: 'Hello @admin.')
  696. content.save!
  697. ActionMailer::Base.deliveries.clear
  698. Mailer.deliver_wiki_content_updated(content)
  699. # @jsmith and @dlopper are members of the project
  700. # admin is mentioned in the notes
  701. # @dlopper won't receive duplicated notifications
  702. assert_equal 3, ActionMailer::Base.deliveries.size
  703. assert_include User.find(1).mail, recipients
  704. end
  705. def test_register
  706. token = Token.find(1)
  707. assert Mailer.deliver_register(token.user, token)
  708. assert_select_email do
  709. assert_select "a[href=?]",
  710. "http://localhost:3000/account/activate?token=#{token.value}",
  711. :text => "http://localhost:3000/account/activate?token=#{token.value}"
  712. end
  713. end
  714. def test_test_email_later
  715. user = User.find(1)
  716. assert Mailer.test_email(user).deliver_later
  717. assert_equal 1, ActionMailer::Base.deliveries.size
  718. end
  719. def test_reminders
  720. users(:users_003).pref.update_attribute :time_zone, 'UTC' # dlopper
  721. days = 42
  722. Mailer.reminders(:days => days)
  723. assert_equal 1, ActionMailer::Base.deliveries.size
  724. mail = last_email
  725. assert mail.to.include?('dlopper@somenet.foo')
  726. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe (5 days late)', mail
  727. assert_mail_body_match 'View all issues (2 open)', mail
  728. url =
  729. "http://localhost:3000/issues?f%5B%5D=status_id&f%5B%5D=assigned_to_id" \
  730. "&f%5B%5D=due_date&op%5Bassigned_to_id%5D=%3D&op%5Bdue_date%5D=%3Ct%2B&op%5B" \
  731. "status_id%5D=o&set_filter=1&sort=due_date%3Aasc&v%5B" \
  732. "assigned_to_id%5D%5B%5D=me&v%5Bdue_date%5D%5B%5D=#{days}"
  733. assert_select_email do
  734. assert_select 'a[href=?]',
  735. url,
  736. :text => '1'
  737. assert_select 'a[href=?]',
  738. 'http://localhost:3000/issues?assigned_to_id=me&set_filter=1&sort=due_date%3Aasc',
  739. :text => 'View all issues'
  740. assert_select '/p:nth-last-of-type(1)', :text => 'View all issues (2 open)'
  741. end
  742. assert_equal "1 issue(s) due in the next #{days} days", mail.subject
  743. end
  744. def test_reminders_language_auto
  745. with_settings :default_language => 'fr' do
  746. user = User.find(3)
  747. user.update_attribute :language, ''
  748. user.pref.update_attribute :time_zone, 'UTC'
  749. Mailer.reminders(:days => 42)
  750. assert_equal 1, ActionMailer::Base.deliveries.size
  751. mail = last_email
  752. assert mail.to.include?('dlopper@somenet.foo')
  753. assert_mail_body_match(
  754. 'Bug #3: Error 281 when updating a recipe (En retard de 5 jours)',
  755. mail
  756. )
  757. assert_equal "1 demande(s) arrivent à échéance (42)", mail.subject
  758. end
  759. end
  760. def test_reminders_should_not_include_closed_issues
  761. with_settings :default_language => 'en' do
  762. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
  763. :subject => 'Closed issue', :assigned_to_id => 3,
  764. :due_date => 5.days.from_now,
  765. :author_id => 2)
  766. ActionMailer::Base.deliveries.clear
  767. Mailer.reminders(:days => 42)
  768. assert_equal 1, ActionMailer::Base.deliveries.size
  769. mail = last_email
  770. assert mail.to.include?('dlopper@somenet.foo')
  771. assert_mail_body_no_match 'Closed issue', mail
  772. end
  773. end
  774. def test_reminders_for_users
  775. users(:users_003).pref.update_attribute :time_zone, 'UTC' # dlopper
  776. Mailer.reminders(:days => 42, :users => ['5'])
  777. assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
  778. Mailer.reminders(:days => 42, :users => ['3'])
  779. assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
  780. mail = last_email
  781. assert mail.to.include?('dlopper@somenet.foo')
  782. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe (5 days late)', mail
  783. end
  784. def test_reminder_should_include_issues_assigned_to_groups
  785. with_settings :default_language => 'en', :issue_group_assignment => '1' do
  786. group = Group.generate!
  787. Member.create!(:project_id => 1, :principal => group, :role_ids => [1])
  788. [users(:users_002), users(:users_003)].each do |user| # jsmith, dlopper
  789. group.users << user
  790. user.pref.update_attribute :time_zone, 'UTC'
  791. end
  792. Issue.update_all(:assigned_to_id => nil)
  793. due_date = 10.days.from_now
  794. Issue.update(1, :due_date => due_date, :assigned_to_id => 3)
  795. Issue.update(2, :due_date => due_date, :assigned_to_id => group.id)
  796. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
  797. :subject => 'Assigned to group', :assigned_to => group,
  798. :due_date => 5.days.from_now,
  799. :author_id => 2)
  800. ActionMailer::Base.deliveries.clear
  801. Mailer.reminders(:days => 7)
  802. assert_equal 2, ActionMailer::Base.deliveries.size
  803. assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), recipients
  804. ActionMailer::Base.deliveries.each do |mail|
  805. assert_mail_body_match(
  806. '1 issue(s) that are assigned to you are due in the next 7 days::',
  807. mail
  808. )
  809. assert_mail_body_match 'Assigned to group (Due in 5 days)', mail
  810. assert_mail_body_match(
  811. "View all issues (#{mail.to.include?('dlopper@somenet.foo') ? 3 : 2} open)",
  812. mail
  813. )
  814. end
  815. end
  816. end
  817. def test_reminders_with_version_option
  818. with_settings :default_language => 'en' do
  819. version = Version.generate!(:name => 'Acme', :project_id => 1)
  820. Issue.generate!(:assigned_to => User.find(2), :due_date => 5.days.from_now)
  821. Issue.generate!(:assigned_to => User.find(3), :due_date => 5.days.from_now,
  822. :fixed_version => version)
  823. ActionMailer::Base.deliveries.clear
  824. Mailer.reminders(:days => 42, :version => 'acme')
  825. assert_equal 1, ActionMailer::Base.deliveries.size
  826. assert_include 'dlopper@somenet.foo', recipients
  827. end
  828. end
  829. def test_reminders_should_only_include_issues_the_user_can_see
  830. with_settings :default_language => 'en' do
  831. user = User.find(3)
  832. member = Member.create!(:project_id => 2, :principal => user, :role_ids => [1])
  833. Issue.create!(:project_id => 2, :tracker_id => 1, :status_id => 1,
  834. :subject => 'Issue dlopper should not see', :assigned_to_id => 3,
  835. :due_date => 5.days.from_now,
  836. :author_id => 2)
  837. member.destroy
  838. ActionMailer::Base.deliveries.clear
  839. Mailer.reminders(:days => 42)
  840. assert_equal 1, ActionMailer::Base.deliveries.size
  841. assert_include 'dlopper@somenet.foo', recipients
  842. mail = last_email
  843. assert_mail_body_no_match 'Issue dlopper should not see', mail
  844. end
  845. end
  846. def test_reminders_should_sort_issues_by_due_date
  847. user = User.find(2)
  848. user.pref.update_attribute :time_zone, 'UTC'
  849. Issue.generate!(:assigned_to => user, :due_date => 2.days.from_now, :subject => 'quux')
  850. Issue.generate!(:assigned_to => user, :due_date => 0.days.from_now, :subject => 'baz')
  851. Issue.generate!(:assigned_to => user, :due_date => 1.days.from_now, :subject => 'qux')
  852. Issue.generate!(:assigned_to => user, :due_date => -1.days.from_now, :subject => 'foo')
  853. Issue.generate!(:assigned_to => user, :due_date => -1.days.from_now, :subject => 'bar')
  854. ActionMailer::Base.deliveries.clear
  855. Mailer.reminders(:days => 7, :users => [user.id])
  856. assert_equal 1, ActionMailer::Base.deliveries.size
  857. assert_select_email do
  858. assert_select 'li', 5
  859. assert_select 'li:nth-child(1)', /foo \(1 day late\)/
  860. assert_select 'li:nth-child(2)', /bar \(1 day late\)/
  861. assert_select 'li:nth-child(3)', /baz \(Due in 0 days\)/
  862. assert_select 'li:nth-child(4)', /qux \(Due in 1 day\)/
  863. assert_select 'li:nth-child(5)', /quux \(Due in 2 days\)/
  864. end
  865. end
  866. def test_security_notification
  867. set_language_if_valid User.find(1).language
  868. with_settings :emails_footer => "footer without link" do
  869. sender = User.find(2)
  870. sender.remote_ip = '192.168.1.1'
  871. assert(
  872. Mailer.deliver_security_notification(
  873. User.find(1),
  874. sender,
  875. :message => :notice_account_password_updated
  876. )
  877. )
  878. mail = last_email
  879. assert_mail_body_match sender.login, mail
  880. assert_mail_body_match '192.168.1.1', mail
  881. assert_mail_body_match I18n.t(:notice_account_password_updated), mail
  882. assert_select_email do
  883. assert_select "h1", false
  884. assert_select "a", false
  885. end
  886. end
  887. end
  888. def test_security_notification_with_overridden_remote_ip
  889. set_language_if_valid User.find(1).language
  890. with_settings :emails_footer => "footer without link" do
  891. sender = User.find(2)
  892. sender.remote_ip = '192.168.1.1'
  893. assert(
  894. Mailer.deliver_security_notification(
  895. User.find(1),
  896. sender,
  897. :message => :notice_account_password_updated,
  898. :remote_ip => '10.0.0.42'
  899. )
  900. )
  901. mail = last_email
  902. assert_mail_body_match '10.0.0.42', mail
  903. end
  904. end
  905. def test_security_notification_should_include_title
  906. set_language_if_valid User.find(2).language
  907. with_settings :emails_footer => "footer without link" do
  908. assert(
  909. Mailer.deliver_security_notification(
  910. User.find(2), User.find(2),
  911. :message => :notice_account_password_updated,
  912. :title => :label_my_account
  913. )
  914. )
  915. assert_select_email do
  916. assert_select "a", false
  917. assert_select "h1", :text => I18n.t(:label_my_account)
  918. end
  919. end
  920. end
  921. def test_security_notification_should_include_link
  922. set_language_if_valid User.find(3).language
  923. with_settings :emails_footer => "footer without link" do
  924. assert(
  925. Mailer.deliver_security_notification(
  926. User.find(3), User.find(3),
  927. :message => :notice_account_password_updated,
  928. :title => :label_my_account,
  929. :url => {:controller => 'my', :action => 'account'}
  930. )
  931. )
  932. assert_select_email do
  933. assert_select "h1", false
  934. assert_select 'a[href=?]', 'http://localhost:3000/my/account', :text => I18n.t(:label_my_account)
  935. end
  936. end
  937. end
  938. def test_mailer_should_not_change_locale
  939. # Set current language to italian
  940. set_language_if_valid 'it'
  941. # Send an email to a french user
  942. user = User.find(1)
  943. user.update_attribute :language, 'fr'
  944. Mailer.deliver_account_activated(user)
  945. mail = last_email
  946. assert_mail_body_match 'Votre compte', mail
  947. assert_equal :it, current_language
  948. end
  949. def test_with_deliveries_off
  950. Mailer.with_deliveries false do
  951. Mailer.test_email(User.find(1)).deliver_now
  952. end
  953. assert ActionMailer::Base.deliveries.empty?
  954. # should restore perform_deliveries
  955. assert ActionMailer::Base.perform_deliveries
  956. end
  957. def test_token_for_should_strip_trailing_gt_from_address_with_full_name
  958. with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do
  959. assert_match /\Aredmine.issue-\d+\.\d+\.3@redmine.org\z/,
  960. Mailer.token_for(Issue.generate!, User.find(3))
  961. end
  962. end
  963. def test_layout_should_include_the_emails_header
  964. with_settings :emails_header => '*Header content*', :text_formatting => 'textile' do
  965. with_settings :plain_text_mail => 0 do
  966. assert Mailer.test_email(User.find(1)).deliver_now
  967. assert_select_email do
  968. assert_select ".header" do
  969. assert_select "strong", :text => "Header content"
  970. end
  971. end
  972. end
  973. with_settings :plain_text_mail => 1 do
  974. assert Mailer.test_email(User.find(1)).deliver_now
  975. mail = last_email
  976. assert_include "*Header content*", mail.body.decoded
  977. end
  978. end
  979. end
  980. def test_layout_should_not_include_empty_emails_header
  981. with_settings :emails_header => "", :plain_text_mail => 0 do
  982. assert Mailer.test_email(User.find(1)).deliver_now
  983. assert_select_email do
  984. assert_select ".header", false
  985. end
  986. end
  987. end
  988. def test_layout_should_include_the_emails_footer
  989. with_settings :emails_footer => '*Footer content*', :text_formatting => 'textile' do
  990. with_settings :plain_text_mail => 0 do
  991. assert Mailer.test_email(User.find(1)).deliver_now
  992. assert_select_email do
  993. assert_select ".footer" do
  994. assert_select "strong", :text => "Footer content"
  995. end
  996. end
  997. end
  998. with_settings :plain_text_mail => 1 do
  999. assert Mailer.test_email(User.find(1)).deliver_now
  1000. mail = last_email
  1001. assert_include "\n-- \n", mail.body.decoded
  1002. assert_include "*Footer content*", mail.body.decoded
  1003. end
  1004. end
  1005. end
  1006. def test_layout_should_not_include_empty_emails_footer
  1007. with_settings :emails_footer => "" do
  1008. with_settings :plain_text_mail => 0 do
  1009. assert Mailer.test_email(User.find(1)).deliver_now
  1010. assert_select_email do
  1011. assert_select ".footer", false
  1012. end
  1013. end
  1014. with_settings :plain_text_mail => 1 do
  1015. assert Mailer.test_email(User.find(1)).deliver_now
  1016. mail = last_email
  1017. assert_not_include "\n-- \n", mail.body.decoded
  1018. end
  1019. end
  1020. end
  1021. def test_should_escape_html_templates_only
  1022. Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>', :notify => true)
  1023. mail = last_email
  1024. assert_equal 2, mail.parts.size
  1025. assert_include '<tag>', text_part.body.encoded
  1026. assert_include '&lt;tag&gt;', html_part.body.encoded
  1027. end
  1028. def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
  1029. mail = Mailer.test_email(User.find(1))
  1030. mail.delivery_method.stubs(:deliver!).raises(StandardError.new("delivery error"))
  1031. ActionMailer::Base.raise_delivery_errors = true
  1032. assert_raise StandardError, "delivery error" do
  1033. mail.deliver
  1034. end
  1035. ensure
  1036. ActionMailer::Base.raise_delivery_errors = false
  1037. end
  1038. def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
  1039. mail = Mailer.test_email(User.find(1))
  1040. mail.delivery_method.stubs(:deliver!).raises(StandardError.new("delivery error"))
  1041. Rails.logger.expects(:error).with("Email delivery error: delivery error")
  1042. ActionMailer::Base.raise_delivery_errors = false
  1043. assert_nothing_raised do
  1044. mail.deliver
  1045. end
  1046. end
  1047. def test_with_synched_deliveries_should_yield_with_synced_deliveries
  1048. ActionMailer::MailDeliveryJob.queue_adapter = ActiveJob::QueueAdapters::AsyncAdapter.new
  1049. Mailer.with_synched_deliveries do
  1050. assert_kind_of ActiveJob::QueueAdapters::InlineAdapter, ActionMailer::MailDeliveryJob.queue_adapter
  1051. end
  1052. assert_kind_of ActiveJob::QueueAdapters::AsyncAdapter, ActionMailer::MailDeliveryJob.queue_adapter
  1053. ensure
  1054. ActionMailer::MailDeliveryJob.queue_adapter = ActiveJob::QueueAdapters::InlineAdapter.new
  1055. end
  1056. def test_email_addresses_should_keep_addresses
  1057. assert_equal ["foo@example.net"],
  1058. Mailer.email_addresses("foo@example.net")
  1059. assert_equal ["foo@example.net", "bar@example.net"],
  1060. Mailer.email_addresses(["foo@example.net", "bar@example.net"])
  1061. end
  1062. def test_email_addresses_should_replace_users_with_their_email_addresses
  1063. assert_equal ["admin@somenet.foo"],
  1064. Mailer.email_addresses(User.find(1))
  1065. assert_equal ["admin@somenet.foo", "jsmith@somenet.foo"],
  1066. Mailer.email_addresses(User.where(:id => [1, 2])).sort
  1067. end
  1068. def test_email_addresses_should_include_notified_emails_addresses_only
  1069. EmailAddress.create!(:user_id => 2, :address => "another@somenet.foo", :notify => false)
  1070. EmailAddress.create!(:user_id => 2, :address => "another2@somenet.foo")
  1071. assert_equal ["another2@somenet.foo", "jsmith@somenet.foo"],
  1072. Mailer.email_addresses(User.find(2)).sort
  1073. end
  1074. private
  1075. # Returns an array of email addresses to which emails were sent
  1076. def recipients
  1077. ActionMailer::Base.deliveries.map(&:to).flatten.sort
  1078. end
  1079. def last_email
  1080. mail = ActionMailer::Base.deliveries.last
  1081. assert_not_nil mail
  1082. mail
  1083. end
  1084. def text_part
  1085. last_email.parts.detect {|part| part.content_type.include?('text/plain')}
  1086. end
  1087. def html_part
  1088. last_email.parts.detect {|part| part.content_type.include?('text/html')}
  1089. end
  1090. def destination_user(mail)
  1091. EmailAddress.where(:address => [mail.to, mail.cc].flatten).map(&:user).first
  1092. end
  1093. end