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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. issue = Issue.find(1)
  185. Mailer.deliver_issue_add(issue)
  186. mail = last_email
  187. assert_equal 'All', mail.header['X-Auto-Response-Suppress'].to_s
  188. assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
  189. assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
  190. end
  191. def test_email_headers_should_include_sender
  192. issue = Issue.find(1)
  193. Mailer.deliver_issue_add(issue)
  194. mail = last_email
  195. assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
  196. end
  197. def test_plain_text_mail
  198. Setting.plain_text_mail = 1
  199. journal = Journal.find(2)
  200. Mailer.deliver_issue_edit(journal)
  201. mail = last_email
  202. assert_equal "text/plain; charset=UTF-8", mail.content_type
  203. assert_equal 0, mail.parts.size
  204. assert !mail.encoded.include?('href')
  205. end
  206. def test_html_mail
  207. Setting.plain_text_mail = 0
  208. journal = Journal.find(2)
  209. Mailer.deliver_issue_edit(journal)
  210. mail = last_email
  211. assert_equal 2, mail.parts.size
  212. assert mail.encoded.include?('href')
  213. end
  214. def test_from_header
  215. with_settings :mail_from => 'redmine@example.net' do
  216. Mailer.deliver_test_email(User.find(1))
  217. end
  218. mail = last_email
  219. assert_equal 'redmine@example.net', mail.from_addrs.first
  220. end
  221. def test_from_header_with_phrase
  222. with_settings :mail_from => 'Redmine app <redmine@example.net>' do
  223. Mailer.deliver_test_email(User.find(1))
  224. end
  225. mail = last_email
  226. assert_equal 'redmine@example.net', mail.from_addrs.first
  227. assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
  228. end
  229. def test_from_header_with_author_name
  230. # Use the author's name or Setting.app_title as a display name
  231. # when Setting.mail_from does not include a display name
  232. with_settings :mail_from => 'redmine@example.net', :app_title => 'Foo' do
  233. # Use @author.name as a display name
  234. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
  235. :subject => 'Issue created by Dave Lopper', :author_id => 3)
  236. mail = last_email
  237. assert_equal 'redmine@example.net', mail.from_addrs.first
  238. assert_equal 'Dave Lopper <redmine@example.net>', mail.header['From'].to_s
  239. # Use app_title if @author is nil or AnonymousUser
  240. Mailer.deliver_test_email(User.find(1))
  241. mail = last_email
  242. assert_equal 'redmine@example.net', mail.from_addrs.first
  243. assert_equal "Foo <redmine@example.net>", mail.header['From'].to_s
  244. end
  245. end
  246. def test_should_not_send_email_without_recipient
  247. news = News.first
  248. user = news.author
  249. # Remove members except news author
  250. news.project.memberships.each {|m| m.destroy unless m.user == user}
  251. user.pref.no_self_notified = false
  252. user.pref.save
  253. User.current = user
  254. Mailer.deliver_news_added(news.reload)
  255. assert_equal 1, last_email.bcc.size
  256. # nobody to notify
  257. user.pref.no_self_notified = true
  258. user.pref.save
  259. User.current = user
  260. ActionMailer::Base.deliveries.clear
  261. Mailer.deliver_news_added(news.reload)
  262. assert ActionMailer::Base.deliveries.empty?
  263. end
  264. def test_issue_add_message_id
  265. issue = Issue.find(2)
  266. Mailer.deliver_issue_add(issue)
  267. mail = last_email
  268. assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
  269. assert_include "redmine.issue-2.20060719190421@example.net", mail.references
  270. end
  271. def test_issue_edit_message_id
  272. journal = Journal.find(3)
  273. journal.issue = Issue.find(2)
  274. Mailer.deliver_issue_edit(journal)
  275. mail = last_email
  276. assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
  277. assert_include "redmine.issue-2.20060719190421@example.net", mail.references
  278. assert_select_email do
  279. # link to the update
  280. assert_select "a[href=?]",
  281. "http://localhost:3000/issues/#{journal.journalized_id}#change-#{journal.id}"
  282. end
  283. end
  284. def test_message_posted_message_id
  285. message = Message.find(1)
  286. Mailer.deliver_message_posted(message)
  287. mail = last_email
  288. assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
  289. assert_include "redmine.message-1.20070512151532@example.net", mail.references
  290. assert_select_email do
  291. # link to the message
  292. assert_select "a[href=?]",
  293. "http://localhost:3000/boards/#{message.board.id}/topics/#{message.id}",
  294. :text => message.subject
  295. end
  296. end
  297. def test_reply_posted_message_id
  298. message = Message.find(3)
  299. Mailer.deliver_message_posted(message)
  300. mail = last_email
  301. assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
  302. assert_include "redmine.message-1.20070512151532@example.net", mail.references
  303. assert_select_email do
  304. # link to the reply
  305. assert_select "a[href=?]",
  306. "http://localhost:3000/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
  307. :text => message.subject
  308. end
  309. end
  310. test "#issue_add should notify project members" do
  311. issue = Issue.find(1)
  312. assert Mailer.deliver_issue_add(issue)
  313. assert_include 'dlopper@somenet.foo', recipients
  314. end
  315. def test_issue_add_should_send_mail_to_all_user_email_address
  316. EmailAddress.create!(:user_id => 3, :address => 'otheremail@somenet.foo')
  317. issue = Issue.find(1)
  318. assert Mailer.deliver_issue_add(issue)
  319. assert mail = ActionMailer::Base.deliveries.find {|m| m.bcc.include?('dlopper@somenet.foo')}
  320. assert mail.bcc.include?('otheremail@somenet.foo')
  321. end
  322. test "#issue_add should not notify project members that are not allow to view the issue" do
  323. issue = Issue.find(1)
  324. Role.find(2).remove_permission!(:view_issues)
  325. assert Mailer.deliver_issue_add(issue)
  326. assert_not_include 'dlopper@somenet.foo', recipients
  327. end
  328. test "#issue_add should notify issue watchers" do
  329. issue = Issue.find(1)
  330. user = User.find(9)
  331. # minimal email notification options
  332. user.pref.no_self_notified = '1'
  333. user.pref.save
  334. user.mail_notification = false
  335. user.save
  336. Watcher.create!(:watchable => issue, :user => user)
  337. assert Mailer.deliver_issue_add(issue)
  338. assert_include user.mail, recipients
  339. end
  340. test "#issue_add should not notify watchers not allowed to view the issue" do
  341. issue = Issue.find(1)
  342. user = User.find(9)
  343. Watcher.create!(:watchable => issue, :user => user)
  344. Role.non_member.remove_permission!(:view_issues)
  345. assert Mailer.deliver_issue_add(issue)
  346. assert_not_include user.mail, recipients
  347. end
  348. def test_issue_add_should_include_enabled_fields
  349. issue = Issue.find(2)
  350. assert Mailer.deliver_issue_add(issue)
  351. assert_mail_body_match '* Target version: 1.0', last_email
  352. assert_select_email do
  353. assert_select 'li', :text => 'Target version: 1.0'
  354. end
  355. end
  356. def test_issue_add_should_not_include_disabled_fields
  357. issue = Issue.find(2)
  358. tracker = issue.tracker
  359. tracker.core_fields -= ['fixed_version_id', 'start_date']
  360. tracker.save!
  361. assert Mailer.deliver_issue_add(issue)
  362. assert_mail_body_no_match 'Target version', last_email
  363. assert_mail_body_no_match 'Start date', last_email
  364. assert_select_email do
  365. assert_select 'li', :text => /Target version/, :count => 0
  366. assert_select 'li', :text => /Start date/, :count => 0
  367. end
  368. end
  369. def test_issue_edit_should_send_private_notes_to_users_with_permission_only
  370. journal = Journal.find(1)
  371. journal.private_notes = true
  372. journal.save!
  373. Role.find(2).add_permission! :view_private_notes
  374. assert_difference 'ActionMailer::Base.deliveries.size', 2 do
  375. Mailer.deliver_issue_edit(journal)
  376. end
  377. assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), recipients
  378. ActionMailer::Base.deliveries.clear
  379. Role.find(2).remove_permission! :view_private_notes
  380. assert_difference 'ActionMailer::Base.deliveries.size', 1 do
  381. Mailer.deliver_issue_edit(journal)
  382. end
  383. assert_equal %w(jsmith@somenet.foo), recipients
  384. end
  385. def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
  386. Issue.find(1).set_watcher(User.find_by_login('someone'))
  387. journal = Journal.find(1)
  388. journal.private_notes = true
  389. journal.save!
  390. Role.non_member.add_permission! :view_private_notes
  391. Mailer.deliver_issue_edit(journal)
  392. assert_include 'someone@foo.bar', recipients
  393. ActionMailer::Base.deliveries.clear
  394. Role.non_member.remove_permission! :view_private_notes
  395. Mailer.deliver_issue_edit(journal)
  396. assert_not_include 'someone@foo.bar', recipients
  397. end
  398. def test_issue_edit_should_mark_private_notes
  399. journal = Journal.find(2)
  400. journal.private_notes = true
  401. journal.save!
  402. with_settings :default_language => 'en' do
  403. Mailer.deliver_issue_edit(journal)
  404. end
  405. assert_mail_body_match '(Private notes)', last_email
  406. end
  407. def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
  408. issue = Issue.generate!
  409. issue.init_journal(User.find(1))
  410. private_issue = Issue.generate!(:is_private => true)
  411. IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
  412. issue.reload
  413. assert_equal 1, issue.journals.size
  414. journal = issue.journals.first
  415. ActionMailer::Base.deliveries.clear
  416. Mailer.deliver_issue_edit(journal)
  417. recipients.each do |email|
  418. user = User.find_by_mail(email)
  419. assert private_issue.visible?(user), "Issue was not visible to #{user}"
  420. end
  421. end
  422. def test_version_file_added
  423. attachements = [ Attachment.find_by_container_type('Version') ]
  424. assert Mailer.deliver_attachments_added(attachements)
  425. assert_not_nil last_email.bcc
  426. assert last_email.bcc.any?
  427. assert_select_email do
  428. assert_select "a[href=?]", "http://localhost:3000/projects/ecookbook/files"
  429. end
  430. end
  431. def test_project_file_added
  432. attachements = [ Attachment.find_by_container_type('Project') ]
  433. assert Mailer.deliver_attachments_added(attachements)
  434. assert_not_nil last_email.bcc
  435. assert last_email.bcc.any?
  436. assert_select_email do
  437. assert_select "a[href=?]", "http://localhost:3000/projects/ecookbook/files"
  438. end
  439. end
  440. def test_news_added_should_notify_project_news_watchers
  441. user1 = User.generate!
  442. user2 = User.generate!
  443. news = News.find(1)
  444. news.project.enabled_module('news').add_watcher(user1)
  445. Mailer.deliver_news_added(news)
  446. assert_include user1.mail, recipients
  447. assert_not_include user2.mail, recipients
  448. end
  449. def test_wiki_content_added
  450. content = WikiContent.find(1)
  451. assert_difference 'ActionMailer::Base.deliveries.size', 2 do
  452. assert Mailer.deliver_wiki_content_added(content)
  453. assert_select_email do
  454. assert_select 'a[href=?]',
  455. 'http://localhost:3000/projects/ecookbook/wiki/CookBook_documentation',
  456. :text => 'CookBook documentation'
  457. end
  458. end
  459. end
  460. def test_wiki_content_updated
  461. content = WikiContent.find(1)
  462. assert Mailer.deliver_wiki_content_updated(content)
  463. assert_select_email do
  464. assert_select 'a[href=?]',
  465. 'http://localhost:3000/projects/ecookbook/wiki/CookBook_documentation',
  466. :text => 'CookBook documentation'
  467. end
  468. end
  469. def test_register
  470. token = Token.find(1)
  471. assert Mailer.deliver_register(token.user, token)
  472. assert_select_email do
  473. assert_select "a[href=?]",
  474. "http://localhost:3000/account/activate?token=#{token.value}",
  475. :text => "http://localhost:3000/account/activate?token=#{token.value}"
  476. end
  477. end
  478. def test_test_email_later
  479. user = User.find(1)
  480. assert Mailer.test_email(user).deliver_later
  481. assert_equal 1, ActionMailer::Base.deliveries.size
  482. end
  483. def test_reminders
  484. Mailer.reminders(:days => 42)
  485. assert_equal 1, ActionMailer::Base.deliveries.size
  486. mail = last_email
  487. assert mail.bcc.include?('dlopper@somenet.foo')
  488. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
  489. assert_equal '1 issue(s) due in the next 42 days', mail.subject
  490. end
  491. def test_reminders_language_auto
  492. with_settings :default_language => 'fr' do
  493. user = User.find(3)
  494. user.update_attribute :language, ''
  495. Mailer.reminders(:days => 42)
  496. assert_equal 1, ActionMailer::Base.deliveries.size
  497. mail = last_email
  498. assert mail.bcc.include?('dlopper@somenet.foo')
  499. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
  500. assert_equal "1 demande(s) arrivent à échéance (42)", mail.subject
  501. end
  502. end
  503. def test_reminders_should_not_include_closed_issues
  504. with_settings :default_language => 'en' do
  505. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
  506. :subject => 'Closed issue', :assigned_to_id => 3,
  507. :due_date => 5.days.from_now,
  508. :author_id => 2)
  509. ActionMailer::Base.deliveries.clear
  510. Mailer.reminders(:days => 42)
  511. assert_equal 1, ActionMailer::Base.deliveries.size
  512. mail = last_email
  513. assert mail.bcc.include?('dlopper@somenet.foo')
  514. assert_mail_body_no_match 'Closed issue', mail
  515. end
  516. end
  517. def test_reminders_for_users
  518. Mailer.reminders(:days => 42, :users => ['5'])
  519. assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
  520. Mailer.reminders(:days => 42, :users => ['3'])
  521. assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
  522. mail = last_email
  523. assert mail.bcc.include?('dlopper@somenet.foo')
  524. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
  525. end
  526. def test_reminder_should_include_issues_assigned_to_groups
  527. with_settings :default_language => 'en', :issue_group_assignment => '1' do
  528. group = Group.generate!
  529. Member.create!(:project_id => 1, :principal => group, :role_ids => [1])
  530. group.users << User.find(2)
  531. group.users << User.find(3)
  532. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
  533. :subject => 'Assigned to group', :assigned_to => group,
  534. :due_date => 5.days.from_now,
  535. :author_id => 2)
  536. ActionMailer::Base.deliveries.clear
  537. Mailer.reminders(:days => 7)
  538. assert_equal 2, ActionMailer::Base.deliveries.size
  539. assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), recipients
  540. ActionMailer::Base.deliveries.each do |mail|
  541. assert_mail_body_match 'Assigned to group', mail
  542. end
  543. end
  544. end
  545. def test_reminders_with_version_option
  546. with_settings :default_language => 'en' do
  547. version = Version.generate!(:name => 'Acme', :project_id => 1)
  548. Issue.generate!(:assigned_to => User.find(2), :due_date => 5.days.from_now)
  549. Issue.generate!(:assigned_to => User.find(3), :due_date => 5.days.from_now, :fixed_version => version)
  550. ActionMailer::Base.deliveries.clear
  551. Mailer.reminders(:days => 42, :version => 'acme')
  552. assert_equal 1, ActionMailer::Base.deliveries.size
  553. assert_include 'dlopper@somenet.foo', recipients
  554. end
  555. end
  556. def test_reminders_should_only_include_issues_the_user_can_see
  557. with_settings :default_language => 'en' do
  558. user = User.find(3)
  559. member = Member.create!(:project_id => 2, :principal => user, :role_ids => [1])
  560. Issue.create!(:project_id => 2, :tracker_id => 1, :status_id => 1,
  561. :subject => 'Issue dlopper should not see', :assigned_to_id => 3,
  562. :due_date => 5.days.from_now,
  563. :author_id => 2)
  564. member.destroy
  565. ActionMailer::Base.deliveries.clear
  566. Mailer.reminders(:days => 42)
  567. assert_equal 1, ActionMailer::Base.deliveries.size
  568. assert_include 'dlopper@somenet.foo', recipients
  569. mail = last_email
  570. assert_mail_body_no_match 'Issue dlopper should not see', mail
  571. end
  572. end
  573. def test_reminders_should_sort_issues_by_due_date
  574. user = User.find(2)
  575. Issue.generate!(:assigned_to => user, :due_date => 2.days.from_now, :subject => 'quux')
  576. Issue.generate!(:assigned_to => user, :due_date => 0.days.from_now, :subject => 'baz')
  577. Issue.generate!(:assigned_to => user, :due_date => 1.days.from_now, :subject => 'qux')
  578. Issue.generate!(:assigned_to => user, :due_date => -1.days.from_now, :subject => 'foo')
  579. Issue.generate!(:assigned_to => user, :due_date => -1.days.from_now, :subject => 'bar')
  580. ActionMailer::Base.deliveries.clear
  581. Mailer.reminders(:days => 7, :users => [user.id])
  582. assert_equal 1, ActionMailer::Base.deliveries.size
  583. assert_select_email do
  584. assert_select 'li', 5
  585. assert_select 'li:nth-child(1)', /foo/
  586. assert_select 'li:nth-child(2)', /bar/
  587. assert_select 'li:nth-child(3)', /baz/
  588. assert_select 'li:nth-child(4)', /qux/
  589. assert_select 'li:nth-child(5)', /quux/
  590. end
  591. end
  592. def test_security_notification
  593. set_language_if_valid User.find(1).language
  594. with_settings :emails_footer => "footer without link" do
  595. sender = User.find(2)
  596. sender.remote_ip = '192.168.1.1'
  597. assert Mailer.deliver_security_notification(User.find(1), sender, message: :notice_account_password_updated)
  598. mail = last_email
  599. assert_mail_body_match sender.login, mail
  600. assert_mail_body_match '192.168.1.1', mail
  601. assert_mail_body_match I18n.t(:notice_account_password_updated), mail
  602. assert_select_email do
  603. assert_select "h1", false
  604. assert_select "a", false
  605. end
  606. end
  607. end
  608. def test_security_notification_with_overridden_remote_ip
  609. set_language_if_valid User.find(1).language
  610. with_settings :emails_footer => "footer without link" do
  611. sender = User.find(2)
  612. sender.remote_ip = '192.168.1.1'
  613. assert Mailer.deliver_security_notification(User.find(1), sender, message: :notice_account_password_updated, remote_ip: '10.0.0.42')
  614. mail = last_email
  615. assert_mail_body_match '10.0.0.42', mail
  616. end
  617. end
  618. def test_security_notification_should_include_title
  619. set_language_if_valid User.find(2).language
  620. with_settings :emails_footer => "footer without link" do
  621. assert Mailer.deliver_security_notification(User.find(2), User.find(2),
  622. message: :notice_account_password_updated,
  623. title: :label_my_account
  624. )
  625. assert_select_email do
  626. assert_select "a", false
  627. assert_select "h1", :text => I18n.t(:label_my_account)
  628. end
  629. end
  630. end
  631. def test_security_notification_should_include_link
  632. set_language_if_valid User.find(3).language
  633. with_settings :emails_footer => "footer without link" do
  634. assert Mailer.deliver_security_notification(User.find(3), User.find(3),
  635. message: :notice_account_password_updated,
  636. title: :label_my_account,
  637. url: {controller: 'my', action: 'account'}
  638. )
  639. assert_select_email do
  640. assert_select "h1", false
  641. assert_select 'a[href=?]', 'http://localhost:3000/my/account', :text => I18n.t(:label_my_account)
  642. end
  643. end
  644. end
  645. def test_mailer_should_not_change_locale
  646. # Set current language to italian
  647. set_language_if_valid 'it'
  648. # Send an email to a french user
  649. user = User.find(1)
  650. user.update_attribute :language, 'fr'
  651. Mailer.deliver_account_activated(user)
  652. mail = last_email
  653. assert_mail_body_match 'Votre compte', mail
  654. assert_equal :it, current_language
  655. end
  656. def test_with_deliveries_off
  657. Mailer.with_deliveries false do
  658. Mailer.test_email(User.find(1)).deliver_now
  659. end
  660. assert ActionMailer::Base.deliveries.empty?
  661. # should restore perform_deliveries
  662. assert ActionMailer::Base.perform_deliveries
  663. end
  664. def test_token_for_should_strip_trailing_gt_from_address_with_full_name
  665. with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do
  666. assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/, Mailer.token_for(Issue.generate!)
  667. end
  668. end
  669. def test_layout_should_include_the_emails_header
  670. with_settings :emails_header => "*Header content*" do
  671. with_settings :plain_text_mail => 0 do
  672. assert Mailer.test_email(User.find(1)).deliver_now
  673. assert_select_email do
  674. assert_select ".header" do
  675. assert_select "strong", :text => "Header content"
  676. end
  677. end
  678. end
  679. with_settings :plain_text_mail => 1 do
  680. assert Mailer.test_email(User.find(1)).deliver_now
  681. mail = last_email
  682. assert_include "*Header content*", mail.body.decoded
  683. end
  684. end
  685. end
  686. def test_layout_should_not_include_empty_emails_header
  687. with_settings :emails_header => "", :plain_text_mail => 0 do
  688. assert Mailer.test_email(User.find(1)).deliver_now
  689. assert_select_email do
  690. assert_select ".header", false
  691. end
  692. end
  693. end
  694. def test_layout_should_include_the_emails_footer
  695. with_settings :emails_footer => "*Footer content*" do
  696. with_settings :plain_text_mail => 0 do
  697. assert Mailer.test_email(User.find(1)).deliver_now
  698. assert_select_email do
  699. assert_select ".footer" do
  700. assert_select "strong", :text => "Footer content"
  701. end
  702. end
  703. end
  704. with_settings :plain_text_mail => 1 do
  705. assert Mailer.test_email(User.find(1)).deliver_now
  706. mail = last_email
  707. assert_include "\n-- \n", mail.body.decoded
  708. assert_include "*Footer content*", mail.body.decoded
  709. end
  710. end
  711. end
  712. def test_layout_should_not_include_empty_emails_footer
  713. with_settings :emails_footer => "" do
  714. with_settings :plain_text_mail => 0 do
  715. assert Mailer.test_email(User.find(1)).deliver_now
  716. assert_select_email do
  717. assert_select ".footer", false
  718. end
  719. end
  720. with_settings :plain_text_mail => 1 do
  721. assert Mailer.test_email(User.find(1)).deliver_now
  722. mail = last_email
  723. assert_not_include "\n-- \n", mail.body.decoded
  724. end
  725. end
  726. end
  727. def test_should_escape_html_templates_only
  728. Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>', :notify => true)
  729. mail = last_email
  730. assert_equal 2, mail.parts.size
  731. assert_include '<tag>', text_part.body.encoded
  732. assert_include '&lt;tag&gt;', html_part.body.encoded
  733. end
  734. def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
  735. mail = Mailer.test_email(User.find(1))
  736. mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
  737. ActionMailer::Base.raise_delivery_errors = true
  738. assert_raise Exception, "delivery error" do
  739. mail.deliver
  740. end
  741. ensure
  742. ActionMailer::Base.raise_delivery_errors = false
  743. end
  744. def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
  745. mail = Mailer.test_email(User.find(1))
  746. mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
  747. Rails.logger.expects(:error).with("Email delivery error: delivery error")
  748. ActionMailer::Base.raise_delivery_errors = false
  749. assert_nothing_raised do
  750. mail.deliver
  751. end
  752. end
  753. def test_with_synched_deliveries_should_yield_with_synced_deliveries
  754. ActionMailer::DeliveryJob.queue_adapter = ActiveJob::QueueAdapters::AsyncAdapter.new
  755. Mailer.with_synched_deliveries do
  756. assert_kind_of ActiveJob::QueueAdapters::InlineAdapter, ActionMailer::DeliveryJob.queue_adapter
  757. end
  758. assert_kind_of ActiveJob::QueueAdapters::AsyncAdapter, ActionMailer::DeliveryJob.queue_adapter
  759. ensure
  760. ActionMailer::DeliveryJob.queue_adapter = ActiveJob::QueueAdapters::InlineAdapter.new
  761. end
  762. def test_email_addresses_should_keep_addresses
  763. assert_equal ["foo@example.net"],
  764. Mailer.email_addresses("foo@example.net")
  765. assert_equal ["foo@example.net", "bar@example.net"],
  766. Mailer.email_addresses(["foo@example.net", "bar@example.net"])
  767. end
  768. def test_email_addresses_should_replace_users_with_their_email_addresses
  769. assert_equal ["admin@somenet.foo"],
  770. Mailer.email_addresses(User.find(1))
  771. assert_equal ["admin@somenet.foo", "jsmith@somenet.foo"],
  772. Mailer.email_addresses(User.where(:id => [1,2])).sort
  773. end
  774. def test_email_addresses_should_include_notified_emails_addresses_only
  775. EmailAddress.create!(:user_id => 2, :address => "another@somenet.foo", :notify => false)
  776. EmailAddress.create!(:user_id => 2, :address => "another2@somenet.foo")
  777. assert_equal ["another2@somenet.foo", "jsmith@somenet.foo"],
  778. Mailer.email_addresses(User.find(2)).sort
  779. end
  780. private
  781. # Returns an array of email addresses to which emails were sent
  782. def recipients
  783. ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
  784. end
  785. def last_email
  786. mail = ActionMailer::Base.deliveries.last
  787. assert_not_nil mail
  788. mail
  789. end
  790. def text_part
  791. last_email.parts.detect {|part| part.content_type.include?('text/plain')}
  792. end
  793. def html_part
  794. last_email.parts.detect {|part| part.content_type.include?('text/html')}
  795. end
  796. end