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 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2017 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require File.expand_path('../../test_helper', __FILE__)
  20. class MailerTest < ActiveSupport::TestCase
  21. include Redmine::I18n
  22. include Rails::Dom::Testing::Assertions
  23. fixtures :projects, :enabled_modules, :issues, :users, :email_addresses, :user_preferences, :members,
  24. :member_roles, :roles, :documents, :attachments, :news,
  25. :tokens, :journals, :journal_details, :changesets,
  26. :trackers, :projects_trackers,
  27. :issue_statuses, :enumerations, :messages, :boards, :repositories,
  28. :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
  29. :versions,
  30. :comments
  31. def setup
  32. ActionMailer::Base.deliveries.clear
  33. Setting.plain_text_mail = '0'
  34. Setting.default_language = 'en'
  35. User.current = nil
  36. end
  37. def test_generated_links_in_emails
  38. with_settings :host_name => 'mydomain.foo', :protocol => 'https' do
  39. journal = Journal.find(3)
  40. assert Mailer.deliver_issue_edit(journal)
  41. end
  42. mail = last_email
  43. assert_select_email do
  44. # link to the main ticket on issue id
  45. assert_select 'a[href=?]',
  46. 'https://mydomain.foo/issues/2#change-3',
  47. :text => '#2'
  48. # link to the main ticket
  49. assert_select 'a[href=?]',
  50. 'https://mydomain.foo/issues/2#change-3',
  51. :text => 'Feature request #2: Add ingredients categories'
  52. # link to a referenced ticket
  53. assert_select 'a[href=?][title=?]',
  54. 'https://mydomain.foo/issues/1',
  55. "Bug: Cannot print recipes (New)",
  56. :text => '#1'
  57. # link to a changeset
  58. assert_select 'a[href=?][title=?]',
  59. 'https://mydomain.foo/projects/ecookbook/repository/10/revisions/2',
  60. 'This commit fixes #1, #2 and references #1 & #3',
  61. :text => 'r2'
  62. # link to a description diff
  63. assert_select 'a[href^=?][title=?]',
  64. # should be https://mydomain.foo/journals/diff/3?detail_id=4
  65. # but the Rails 4.2 DOM assertion doesn't handle the ? in the
  66. # attribute value
  67. 'https://mydomain.foo/journals/3/diff',
  68. 'View differences',
  69. :text => 'diff'
  70. # link to an attachment
  71. assert_select 'a[href=?]',
  72. 'https://mydomain.foo/attachments/download/4/source.rb',
  73. :text => 'source.rb'
  74. end
  75. end
  76. def test_generated_links_with_prefix
  77. relative_url_root = Redmine::Utils.relative_url_root
  78. with_settings :host_name => 'mydomain.foo/rdm', :protocol => 'http' do
  79. journal = Journal.find(3)
  80. assert Mailer.deliver_issue_edit(journal)
  81. end
  82. mail = last_email
  83. assert_select_email do
  84. # link to the main ticket
  85. assert_select 'a[href=?]',
  86. 'http://mydomain.foo/rdm/issues/2#change-3',
  87. :text => 'Feature request #2: Add ingredients categories'
  88. # link to a referenced ticket
  89. assert_select 'a[href=?][title=?]',
  90. 'http://mydomain.foo/rdm/issues/1',
  91. "Bug: Cannot print recipes (New)",
  92. :text => '#1'
  93. # link to a changeset
  94. assert_select 'a[href=?][title=?]',
  95. 'http://mydomain.foo/rdm/projects/ecookbook/repository/10/revisions/2',
  96. 'This commit fixes #1, #2 and references #1 & #3',
  97. :text => 'r2'
  98. # link to a description diff
  99. assert_select 'a[href^=?][title=?]',
  100. # should be http://mydomain.foo/rdm/journals/diff/3?detail_id=4
  101. # but the Rails 4.2 DOM assertion doesn't handle the ? in the
  102. # attribute value
  103. 'http://mydomain.foo/rdm/journals/3/diff',
  104. 'View differences',
  105. :text => 'diff'
  106. # link to an attachment
  107. assert_select 'a[href=?]',
  108. 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
  109. :text => 'source.rb'
  110. end
  111. end
  112. def test_generated_links_with_port_and_prefix
  113. with_settings :host_name => '10.0.0.1:81/redmine', :protocol => 'http' do
  114. Mailer.test_email(User.find(1)).deliver_now
  115. mail = last_email
  116. assert_include 'http://10.0.0.1:81/redmine', mail_body(mail)
  117. end
  118. end
  119. def test_generated_links_with_port
  120. with_settings :host_name => '10.0.0.1:81', :protocol => 'http' do
  121. Mailer.test_email(User.find(1)).deliver_now
  122. mail = last_email
  123. assert_include 'http://10.0.0.1:81', mail_body(mail)
  124. end
  125. end
  126. def test_issue_edit_should_generate_url_with_hostname_for_relations
  127. journal = Journal.new(:journalized => Issue.find(1), :user => User.find(1), :created_on => Time.now)
  128. journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'label_relates_to', :value => 2)
  129. journal.save
  130. Mailer.deliver_issue_edit(journal)
  131. assert_not_nil last_email
  132. assert_select_email do
  133. assert_select 'a[href=?]', 'http://localhost:3000/issues/2', :text => 'Feature request #2'
  134. end
  135. end
  136. def test_generated_links_with_prefix_and_no_relative_url_root
  137. relative_url_root = Redmine::Utils.relative_url_root
  138. Redmine::Utils.relative_url_root = nil
  139. with_settings :host_name => 'mydomain.foo/rdm', :protocol => 'http' do
  140. journal = Journal.find(3)
  141. assert Mailer.deliver_issue_edit(journal)
  142. end
  143. mail = last_email
  144. assert_select_email do
  145. # link to the main ticket
  146. assert_select 'a[href=?]',
  147. 'http://mydomain.foo/rdm/issues/2#change-3',
  148. :text => 'Feature request #2: Add ingredients categories'
  149. # link to a referenced ticket
  150. assert_select 'a[href=?][title=?]',
  151. 'http://mydomain.foo/rdm/issues/1',
  152. "Bug: Cannot print recipes (New)",
  153. :text => '#1'
  154. # link to a changeset
  155. assert_select 'a[href=?][title=?]',
  156. 'http://mydomain.foo/rdm/projects/ecookbook/repository/10/revisions/2',
  157. 'This commit fixes #1, #2 and references #1 & #3',
  158. :text => 'r2'
  159. # link to a description diff
  160. assert_select 'a[href^=?][title=?]',
  161. # should be http://mydomain.foo/rdm/journals/diff/3?detail_id=4
  162. # but the Rails 4.2 DOM assertion doesn't handle the ? in the
  163. # attribute value
  164. 'http://mydomain.foo/rdm/journals/3/diff',
  165. 'View differences',
  166. :text => 'diff'
  167. # link to an attachment
  168. assert_select 'a[href=?]',
  169. 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
  170. :text => 'source.rb'
  171. end
  172. ensure
  173. # restore it
  174. Redmine::Utils.relative_url_root = relative_url_root
  175. end
  176. def test_link_to_user_in_email
  177. issue = Issue.generate!(:description => '@jsmith')
  178. assert Mailer.deliver_issue_add(issue)
  179. assert_select_email do
  180. assert_select "a[href=?]", "http://localhost:3000/users/2", :text => 'John Smith'
  181. end
  182. end
  183. def test_email_headers
  184. with_settings :mail_from => 'Redmine <redmine@example.net>' do
  185. issue = Issue.find(1)
  186. Mailer.deliver_issue_add(issue)
  187. end
  188. mail = last_email
  189. assert_equal 'All', mail.header['X-Auto-Response-Suppress'].to_s
  190. assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
  191. # List-Id should not include the display name "Redmine"
  192. assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
  193. end
  194. def test_email_headers_should_include_sender
  195. issue = Issue.find(1)
  196. Mailer.deliver_issue_add(issue)
  197. mail = last_email
  198. assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
  199. end
  200. def test_plain_text_mail
  201. Setting.plain_text_mail = 1
  202. journal = Journal.find(2)
  203. Mailer.deliver_issue_edit(journal)
  204. mail = last_email
  205. assert_equal "text/plain; charset=UTF-8", mail.content_type
  206. assert_equal 0, mail.parts.size
  207. assert !mail.encoded.include?('href')
  208. end
  209. def test_html_mail
  210. Setting.plain_text_mail = 0
  211. journal = Journal.find(2)
  212. Mailer.deliver_issue_edit(journal)
  213. mail = last_email
  214. assert_equal 2, mail.parts.size
  215. assert mail.encoded.include?('href')
  216. end
  217. def test_from_header
  218. with_settings :mail_from => 'redmine@example.net' do
  219. Mailer.deliver_test_email(User.find(1))
  220. end
  221. mail = last_email
  222. assert_equal 'redmine@example.net', mail.from_addrs.first
  223. end
  224. def test_from_header_with_phrase
  225. with_settings :mail_from => 'Redmine app <redmine@example.net>' do
  226. Mailer.deliver_test_email(User.find(1))
  227. end
  228. mail = last_email
  229. assert_equal 'redmine@example.net', mail.from_addrs.first
  230. assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
  231. end
  232. def test_from_header_with_author_name
  233. # Use the author's name or Setting.app_title as a display name
  234. # when Setting.mail_from does not include a display name
  235. with_settings :mail_from => 'redmine@example.net', :app_title => 'Foo' do
  236. # Use @author.name as a display name
  237. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
  238. :subject => 'Issue created by Dave Lopper', :author_id => 3)
  239. mail = last_email
  240. assert_equal 'redmine@example.net', mail.from_addrs.first
  241. assert_equal 'Dave Lopper <redmine@example.net>', mail.header['From'].to_s
  242. # Use app_title if @author is nil or AnonymousUser
  243. Mailer.deliver_test_email(User.find(1))
  244. mail = last_email
  245. assert_equal 'redmine@example.net', mail.from_addrs.first
  246. assert_equal "Foo <redmine@example.net>", mail.header['From'].to_s
  247. end
  248. end
  249. def test_should_not_send_email_without_recipient
  250. news = News.first
  251. user = news.author
  252. # Remove members except news author
  253. news.project.memberships.each {|m| m.destroy unless m.user == user}
  254. user.pref.no_self_notified = false
  255. user.pref.save
  256. User.current = user
  257. Mailer.deliver_news_added(news.reload)
  258. assert_equal 1, last_email.bcc.size
  259. # nobody to notify
  260. user.pref.no_self_notified = true
  261. user.pref.save
  262. User.current = user
  263. ActionMailer::Base.deliveries.clear
  264. Mailer.deliver_news_added(news.reload)
  265. assert ActionMailer::Base.deliveries.empty?
  266. end
  267. def test_issue_add_message_id
  268. issue = Issue.find(2)
  269. Mailer.deliver_issue_add(issue)
  270. mail = last_email
  271. assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
  272. assert_include "redmine.issue-2.20060719190421@example.net", mail.references
  273. end
  274. def test_issue_edit_message_id
  275. journal = Journal.find(3)
  276. journal.issue = Issue.find(2)
  277. Mailer.deliver_issue_edit(journal)
  278. mail = last_email
  279. assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
  280. assert_include "redmine.issue-2.20060719190421@example.net", mail.references
  281. assert_select_email do
  282. # link to the update
  283. assert_select "a[href=?]",
  284. "http://localhost:3000/issues/#{journal.journalized_id}#change-#{journal.id}"
  285. end
  286. end
  287. def test_message_posted_message_id
  288. message = Message.find(1)
  289. Mailer.deliver_message_posted(message)
  290. mail = last_email
  291. assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
  292. assert_include "redmine.message-1.20070512151532@example.net", mail.references
  293. assert_select_email do
  294. # link to the message
  295. assert_select "a[href=?]",
  296. "http://localhost:3000/boards/#{message.board.id}/topics/#{message.id}",
  297. :text => message.subject
  298. end
  299. end
  300. def test_reply_posted_message_id
  301. message = Message.find(3)
  302. Mailer.deliver_message_posted(message)
  303. mail = last_email
  304. assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
  305. assert_include "redmine.message-1.20070512151532@example.net", mail.references
  306. assert_select_email do
  307. # link to the reply
  308. assert_select "a[href=?]",
  309. "http://localhost:3000/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
  310. :text => message.subject
  311. end
  312. end
  313. test "#issue_add should notify project members" do
  314. issue = Issue.find(1)
  315. assert Mailer.deliver_issue_add(issue)
  316. assert_include 'dlopper@somenet.foo', recipients
  317. end
  318. def test_issue_add_should_send_mail_to_all_user_email_address
  319. EmailAddress.create!(:user_id => 3, :address => 'otheremail@somenet.foo')
  320. issue = Issue.find(1)
  321. assert Mailer.deliver_issue_add(issue)
  322. assert mail = ActionMailer::Base.deliveries.find {|m| m.bcc.include?('dlopper@somenet.foo')}
  323. assert mail.bcc.include?('otheremail@somenet.foo')
  324. end
  325. test "#issue_add should not notify project members that are not allow to view the issue" do
  326. issue = Issue.find(1)
  327. Role.find(2).remove_permission!(:view_issues)
  328. assert Mailer.deliver_issue_add(issue)
  329. assert_not_include 'dlopper@somenet.foo', recipients
  330. end
  331. test "#issue_add should notify issue watchers" do
  332. issue = Issue.find(1)
  333. user = User.find(9)
  334. # minimal email notification options
  335. user.pref.no_self_notified = '1'
  336. user.pref.save
  337. user.mail_notification = false
  338. user.save
  339. Watcher.create!(:watchable => issue, :user => user)
  340. assert Mailer.deliver_issue_add(issue)
  341. assert_include user.mail, recipients
  342. end
  343. test "#issue_add should not notify watchers not allowed to view the issue" do
  344. issue = Issue.find(1)
  345. user = User.find(9)
  346. Watcher.create!(:watchable => issue, :user => user)
  347. Role.non_member.remove_permission!(:view_issues)
  348. assert Mailer.deliver_issue_add(issue)
  349. assert_not_include user.mail, recipients
  350. end
  351. def test_issue_add_should_include_enabled_fields
  352. issue = Issue.find(2)
  353. assert Mailer.deliver_issue_add(issue)
  354. assert_mail_body_match '* Target version: 1.0', last_email
  355. assert_select_email do
  356. assert_select 'li', :text => 'Target version: 1.0'
  357. end
  358. end
  359. def test_issue_add_should_not_include_disabled_fields
  360. issue = Issue.find(2)
  361. tracker = issue.tracker
  362. tracker.core_fields -= ['fixed_version_id', 'start_date']
  363. tracker.save!
  364. assert Mailer.deliver_issue_add(issue)
  365. assert_mail_body_no_match 'Target version', last_email
  366. assert_mail_body_no_match 'Start date', last_email
  367. assert_select_email do
  368. assert_select 'li', :text => /Target version/, :count => 0
  369. assert_select 'li', :text => /Start date/, :count => 0
  370. end
  371. end
  372. def test_issue_edit_should_send_private_notes_to_users_with_permission_only
  373. journal = Journal.find(1)
  374. journal.private_notes = true
  375. journal.save!
  376. Role.find(2).add_permission! :view_private_notes
  377. assert_difference 'ActionMailer::Base.deliveries.size', 2 do
  378. Mailer.deliver_issue_edit(journal)
  379. end
  380. assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), recipients
  381. ActionMailer::Base.deliveries.clear
  382. Role.find(2).remove_permission! :view_private_notes
  383. assert_difference 'ActionMailer::Base.deliveries.size', 1 do
  384. Mailer.deliver_issue_edit(journal)
  385. end
  386. assert_equal %w(jsmith@somenet.foo), recipients
  387. end
  388. def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
  389. Issue.find(1).set_watcher(User.find_by_login('someone'))
  390. journal = Journal.find(1)
  391. journal.private_notes = true
  392. journal.save!
  393. Role.non_member.add_permission! :view_private_notes
  394. Mailer.deliver_issue_edit(journal)
  395. assert_include 'someone@foo.bar', recipients
  396. ActionMailer::Base.deliveries.clear
  397. Role.non_member.remove_permission! :view_private_notes
  398. Mailer.deliver_issue_edit(journal)
  399. assert_not_include 'someone@foo.bar', recipients
  400. end
  401. def test_issue_edit_should_mark_private_notes
  402. journal = Journal.find(2)
  403. journal.private_notes = true
  404. journal.save!
  405. with_settings :default_language => 'en' do
  406. Mailer.deliver_issue_edit(journal)
  407. end
  408. assert_mail_body_match '(Private notes)', last_email
  409. end
  410. def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
  411. issue = Issue.generate!
  412. issue.init_journal(User.find(1))
  413. private_issue = Issue.generate!(:is_private => true)
  414. IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
  415. issue.reload
  416. assert_equal 1, issue.journals.size
  417. journal = issue.journals.first
  418. ActionMailer::Base.deliveries.clear
  419. Mailer.deliver_issue_edit(journal)
  420. recipients.each do |email|
  421. user = User.find_by_mail(email)
  422. assert private_issue.visible?(user), "Issue was not visible to #{user}"
  423. end
  424. end
  425. def test_version_file_added
  426. attachements = [ Attachment.find_by_container_type('Version') ]
  427. assert Mailer.deliver_attachments_added(attachements)
  428. assert_not_nil last_email.bcc
  429. assert last_email.bcc.any?
  430. assert_select_email do
  431. assert_select "a[href=?]", "http://localhost:3000/projects/ecookbook/files"
  432. end
  433. end
  434. def test_project_file_added
  435. attachements = [ Attachment.find_by_container_type('Project') ]
  436. assert Mailer.deliver_attachments_added(attachements)
  437. assert_not_nil last_email.bcc
  438. assert last_email.bcc.any?
  439. assert_select_email do
  440. assert_select "a[href=?]", "http://localhost:3000/projects/ecookbook/files"
  441. end
  442. end
  443. def test_news_added_should_notify_project_news_watchers
  444. user1 = User.generate!
  445. user2 = User.generate!
  446. news = News.find(1)
  447. news.project.enabled_module('news').add_watcher(user1)
  448. Mailer.deliver_news_added(news)
  449. assert_include user1.mail, recipients
  450. assert_not_include user2.mail, recipients
  451. end
  452. def test_wiki_content_added
  453. content = WikiContent.find(1)
  454. assert_difference 'ActionMailer::Base.deliveries.size', 2 do
  455. assert Mailer.deliver_wiki_content_added(content)
  456. assert_select_email do
  457. assert_select 'a[href=?]',
  458. 'http://localhost:3000/projects/ecookbook/wiki/CookBook_documentation',
  459. :text => 'CookBook documentation'
  460. end
  461. end
  462. end
  463. def test_wiki_content_updated
  464. content = WikiContent.find(1)
  465. assert Mailer.deliver_wiki_content_updated(content)
  466. assert_select_email do
  467. assert_select 'a[href=?]',
  468. 'http://localhost:3000/projects/ecookbook/wiki/CookBook_documentation',
  469. :text => 'CookBook documentation'
  470. end
  471. end
  472. def test_register
  473. token = Token.find(1)
  474. assert Mailer.deliver_register(token.user, token)
  475. assert_select_email do
  476. assert_select "a[href=?]",
  477. "http://localhost:3000/account/activate?token=#{token.value}",
  478. :text => "http://localhost:3000/account/activate?token=#{token.value}"
  479. end
  480. end
  481. def test_test_email_later
  482. user = User.find(1)
  483. assert Mailer.test_email(user).deliver_later
  484. assert_equal 1, ActionMailer::Base.deliveries.size
  485. end
  486. def test_reminders
  487. Mailer.reminders(:days => 42)
  488. assert_equal 1, ActionMailer::Base.deliveries.size
  489. mail = last_email
  490. assert mail.bcc.include?('dlopper@somenet.foo')
  491. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
  492. assert_equal '1 issue(s) due in the next 42 days', mail.subject
  493. end
  494. def test_reminders_language_auto
  495. with_settings :default_language => 'fr' do
  496. user = User.find(3)
  497. user.update_attribute :language, ''
  498. Mailer.reminders(:days => 42)
  499. assert_equal 1, ActionMailer::Base.deliveries.size
  500. mail = last_email
  501. assert mail.bcc.include?('dlopper@somenet.foo')
  502. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
  503. assert_equal "1 demande(s) arrivent à échéance (42)", mail.subject
  504. end
  505. end
  506. def test_reminders_should_not_include_closed_issues
  507. with_settings :default_language => 'en' do
  508. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
  509. :subject => 'Closed issue', :assigned_to_id => 3,
  510. :due_date => 5.days.from_now,
  511. :author_id => 2)
  512. ActionMailer::Base.deliveries.clear
  513. Mailer.reminders(:days => 42)
  514. assert_equal 1, ActionMailer::Base.deliveries.size
  515. mail = last_email
  516. assert mail.bcc.include?('dlopper@somenet.foo')
  517. assert_mail_body_no_match 'Closed issue', mail
  518. end
  519. end
  520. def test_reminders_for_users
  521. Mailer.reminders(:days => 42, :users => ['5'])
  522. assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
  523. Mailer.reminders(:days => 42, :users => ['3'])
  524. assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
  525. mail = last_email
  526. assert mail.bcc.include?('dlopper@somenet.foo')
  527. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
  528. end
  529. def test_reminder_should_include_issues_assigned_to_groups
  530. with_settings :default_language => 'en', :issue_group_assignment => '1' do
  531. group = Group.generate!
  532. Member.create!(:project_id => 1, :principal => group, :role_ids => [1])
  533. group.users << User.find(2)
  534. group.users << User.find(3)
  535. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
  536. :subject => 'Assigned to group', :assigned_to => group,
  537. :due_date => 5.days.from_now,
  538. :author_id => 2)
  539. ActionMailer::Base.deliveries.clear
  540. Mailer.reminders(:days => 7)
  541. assert_equal 2, ActionMailer::Base.deliveries.size
  542. assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), recipients
  543. ActionMailer::Base.deliveries.each do |mail|
  544. assert_mail_body_match 'Assigned to group', mail
  545. end
  546. end
  547. end
  548. def test_reminders_with_version_option
  549. with_settings :default_language => 'en' do
  550. version = Version.generate!(:name => 'Acme', :project_id => 1)
  551. Issue.generate!(:assigned_to => User.find(2), :due_date => 5.days.from_now)
  552. Issue.generate!(:assigned_to => User.find(3), :due_date => 5.days.from_now, :fixed_version => version)
  553. ActionMailer::Base.deliveries.clear
  554. Mailer.reminders(:days => 42, :version => 'acme')
  555. assert_equal 1, ActionMailer::Base.deliveries.size
  556. assert_include 'dlopper@somenet.foo', recipients
  557. end
  558. end
  559. def test_reminders_should_only_include_issues_the_user_can_see
  560. with_settings :default_language => 'en' do
  561. user = User.find(3)
  562. member = Member.create!(:project_id => 2, :principal => user, :role_ids => [1])
  563. Issue.create!(:project_id => 2, :tracker_id => 1, :status_id => 1,
  564. :subject => 'Issue dlopper should not see', :assigned_to_id => 3,
  565. :due_date => 5.days.from_now,
  566. :author_id => 2)
  567. member.destroy
  568. ActionMailer::Base.deliveries.clear
  569. Mailer.reminders(:days => 42)
  570. assert_equal 1, ActionMailer::Base.deliveries.size
  571. assert_include 'dlopper@somenet.foo', recipients
  572. mail = last_email
  573. assert_mail_body_no_match 'Issue dlopper should not see', mail
  574. end
  575. end
  576. def test_reminders_should_sort_issues_by_due_date
  577. user = User.find(2)
  578. Issue.generate!(:assigned_to => user, :due_date => 2.days.from_now, :subject => 'quux')
  579. Issue.generate!(:assigned_to => user, :due_date => 0.days.from_now, :subject => 'baz')
  580. Issue.generate!(:assigned_to => user, :due_date => 1.days.from_now, :subject => 'qux')
  581. Issue.generate!(:assigned_to => user, :due_date => -1.days.from_now, :subject => 'foo')
  582. Issue.generate!(:assigned_to => user, :due_date => -1.days.from_now, :subject => 'bar')
  583. ActionMailer::Base.deliveries.clear
  584. Mailer.reminders(:days => 7, :users => [user.id])
  585. assert_equal 1, ActionMailer::Base.deliveries.size
  586. assert_select_email do
  587. assert_select 'li', 5
  588. assert_select 'li:nth-child(1)', /foo/
  589. assert_select 'li:nth-child(2)', /bar/
  590. assert_select 'li:nth-child(3)', /baz/
  591. assert_select 'li:nth-child(4)', /qux/
  592. assert_select 'li:nth-child(5)', /quux/
  593. end
  594. end
  595. def test_security_notification
  596. set_language_if_valid User.find(1).language
  597. with_settings :emails_footer => "footer without link" do
  598. sender = User.find(2)
  599. sender.remote_ip = '192.168.1.1'
  600. assert Mailer.deliver_security_notification(User.find(1), sender, message: :notice_account_password_updated)
  601. mail = last_email
  602. assert_mail_body_match sender.login, mail
  603. assert_mail_body_match '192.168.1.1', mail
  604. assert_mail_body_match I18n.t(:notice_account_password_updated), mail
  605. assert_select_email do
  606. assert_select "h1", false
  607. assert_select "a", false
  608. end
  609. end
  610. end
  611. def test_security_notification_with_overridden_remote_ip
  612. set_language_if_valid User.find(1).language
  613. with_settings :emails_footer => "footer without link" do
  614. sender = User.find(2)
  615. sender.remote_ip = '192.168.1.1'
  616. assert Mailer.deliver_security_notification(User.find(1), sender, message: :notice_account_password_updated, remote_ip: '10.0.0.42')
  617. mail = last_email
  618. assert_mail_body_match '10.0.0.42', mail
  619. end
  620. end
  621. def test_security_notification_should_include_title
  622. set_language_if_valid User.find(2).language
  623. with_settings :emails_footer => "footer without link" do
  624. assert Mailer.deliver_security_notification(User.find(2), User.find(2),
  625. message: :notice_account_password_updated,
  626. title: :label_my_account
  627. )
  628. assert_select_email do
  629. assert_select "a", false
  630. assert_select "h1", :text => I18n.t(:label_my_account)
  631. end
  632. end
  633. end
  634. def test_security_notification_should_include_link
  635. set_language_if_valid User.find(3).language
  636. with_settings :emails_footer => "footer without link" do
  637. assert Mailer.deliver_security_notification(User.find(3), User.find(3),
  638. message: :notice_account_password_updated,
  639. title: :label_my_account,
  640. url: {controller: 'my', action: 'account'}
  641. )
  642. assert_select_email do
  643. assert_select "h1", false
  644. assert_select 'a[href=?]', 'http://localhost:3000/my/account', :text => I18n.t(:label_my_account)
  645. end
  646. end
  647. end
  648. def test_mailer_should_not_change_locale
  649. # Set current language to italian
  650. set_language_if_valid 'it'
  651. # Send an email to a french user
  652. user = User.find(1)
  653. user.update_attribute :language, 'fr'
  654. Mailer.deliver_account_activated(user)
  655. mail = last_email
  656. assert_mail_body_match 'Votre compte', mail
  657. assert_equal :it, current_language
  658. end
  659. def test_with_deliveries_off
  660. Mailer.with_deliveries false do
  661. Mailer.test_email(User.find(1)).deliver_now
  662. end
  663. assert ActionMailer::Base.deliveries.empty?
  664. # should restore perform_deliveries
  665. assert ActionMailer::Base.perform_deliveries
  666. end
  667. def test_token_for_should_strip_trailing_gt_from_address_with_full_name
  668. with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do
  669. assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/, Mailer.token_for(Issue.generate!)
  670. end
  671. end
  672. def test_layout_should_include_the_emails_header
  673. with_settings :emails_header => "*Header content*" do
  674. with_settings :plain_text_mail => 0 do
  675. assert Mailer.test_email(User.find(1)).deliver_now
  676. assert_select_email do
  677. assert_select ".header" do
  678. assert_select "strong", :text => "Header content"
  679. end
  680. end
  681. end
  682. with_settings :plain_text_mail => 1 do
  683. assert Mailer.test_email(User.find(1)).deliver_now
  684. mail = last_email
  685. assert_include "*Header content*", mail.body.decoded
  686. end
  687. end
  688. end
  689. def test_layout_should_not_include_empty_emails_header
  690. with_settings :emails_header => "", :plain_text_mail => 0 do
  691. assert Mailer.test_email(User.find(1)).deliver_now
  692. assert_select_email do
  693. assert_select ".header", false
  694. end
  695. end
  696. end
  697. def test_layout_should_include_the_emails_footer
  698. with_settings :emails_footer => "*Footer content*" do
  699. with_settings :plain_text_mail => 0 do
  700. assert Mailer.test_email(User.find(1)).deliver_now
  701. assert_select_email do
  702. assert_select ".footer" do
  703. assert_select "strong", :text => "Footer content"
  704. end
  705. end
  706. end
  707. with_settings :plain_text_mail => 1 do
  708. assert Mailer.test_email(User.find(1)).deliver_now
  709. mail = last_email
  710. assert_include "\n-- \n", mail.body.decoded
  711. assert_include "*Footer content*", mail.body.decoded
  712. end
  713. end
  714. end
  715. def test_layout_should_not_include_empty_emails_footer
  716. with_settings :emails_footer => "" do
  717. with_settings :plain_text_mail => 0 do
  718. assert Mailer.test_email(User.find(1)).deliver_now
  719. assert_select_email do
  720. assert_select ".footer", false
  721. end
  722. end
  723. with_settings :plain_text_mail => 1 do
  724. assert Mailer.test_email(User.find(1)).deliver_now
  725. mail = last_email
  726. assert_not_include "\n-- \n", mail.body.decoded
  727. end
  728. end
  729. end
  730. def test_should_escape_html_templates_only
  731. Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>', :notify => true)
  732. mail = last_email
  733. assert_equal 2, mail.parts.size
  734. assert_include '<tag>', text_part.body.encoded
  735. assert_include '&lt;tag&gt;', html_part.body.encoded
  736. end
  737. def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
  738. mail = Mailer.test_email(User.find(1))
  739. mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
  740. ActionMailer::Base.raise_delivery_errors = true
  741. assert_raise Exception, "delivery error" do
  742. mail.deliver
  743. end
  744. ensure
  745. ActionMailer::Base.raise_delivery_errors = false
  746. end
  747. def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
  748. mail = Mailer.test_email(User.find(1))
  749. mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
  750. Rails.logger.expects(:error).with("Email delivery error: delivery error")
  751. ActionMailer::Base.raise_delivery_errors = false
  752. assert_nothing_raised do
  753. mail.deliver
  754. end
  755. end
  756. def test_with_synched_deliveries_should_yield_with_synced_deliveries
  757. ActionMailer::DeliveryJob.queue_adapter = ActiveJob::QueueAdapters::AsyncAdapter.new
  758. Mailer.with_synched_deliveries do
  759. assert_kind_of ActiveJob::QueueAdapters::InlineAdapter, ActionMailer::DeliveryJob.queue_adapter
  760. end
  761. assert_kind_of ActiveJob::QueueAdapters::AsyncAdapter, ActionMailer::DeliveryJob.queue_adapter
  762. ensure
  763. ActionMailer::DeliveryJob.queue_adapter = ActiveJob::QueueAdapters::InlineAdapter.new
  764. end
  765. def test_email_addresses_should_keep_addresses
  766. assert_equal ["foo@example.net"],
  767. Mailer.email_addresses("foo@example.net")
  768. assert_equal ["foo@example.net", "bar@example.net"],
  769. Mailer.email_addresses(["foo@example.net", "bar@example.net"])
  770. end
  771. def test_email_addresses_should_replace_users_with_their_email_addresses
  772. assert_equal ["admin@somenet.foo"],
  773. Mailer.email_addresses(User.find(1))
  774. assert_equal ["admin@somenet.foo", "jsmith@somenet.foo"],
  775. Mailer.email_addresses(User.where(:id => [1,2])).sort
  776. end
  777. def test_email_addresses_should_include_notified_emails_addresses_only
  778. EmailAddress.create!(:user_id => 2, :address => "another@somenet.foo", :notify => false)
  779. EmailAddress.create!(:user_id => 2, :address => "another2@somenet.foo")
  780. assert_equal ["another2@somenet.foo", "jsmith@somenet.foo"],
  781. Mailer.email_addresses(User.find(2)).sort
  782. end
  783. private
  784. # Returns an array of email addresses to which emails were sent
  785. def recipients
  786. ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
  787. end
  788. def last_email
  789. mail = ActionMailer::Base.deliveries.last
  790. assert_not_nil mail
  791. mail
  792. end
  793. def text_part
  794. last_email.parts.detect {|part| part.content_type.include?('text/plain')}
  795. end
  796. def html_part
  797. last_email.parts.detect {|part| part.content_type.include?('text/html')}
  798. end
  799. end