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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class MailerTest < ActiveSupport::TestCase
  19. include Redmine::I18n
  20. include ActionDispatch::Assertions::SelectorAssertions
  21. fixtures :projects, :enabled_modules, :issues, :users, :members,
  22. :member_roles, :roles, :documents, :attachments, :news,
  23. :tokens, :journals, :journal_details, :changesets,
  24. :trackers, :projects_trackers,
  25. :issue_statuses, :enumerations, :messages, :boards, :repositories,
  26. :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
  27. :versions,
  28. :comments
  29. def setup
  30. ActionMailer::Base.deliveries.clear
  31. Setting.host_name = 'mydomain.foo'
  32. Setting.protocol = 'http'
  33. Setting.plain_text_mail = '0'
  34. end
  35. def test_generated_links_in_emails
  36. Setting.default_language = 'en'
  37. Setting.host_name = 'mydomain.foo'
  38. Setting.protocol = 'https'
  39. journal = Journal.find(3)
  40. assert Mailer.issue_edit(journal).deliver
  41. mail = last_email
  42. assert_not_nil mail
  43. assert_select_email do
  44. # link to the main ticket
  45. assert_select 'a[href=?]',
  46. 'https://mydomain.foo/issues/2#change-3',
  47. :text => 'Feature request #2: Add ingredients categories'
  48. # link to a referenced ticket
  49. assert_select 'a[href=?][title=?]',
  50. 'https://mydomain.foo/issues/1',
  51. 'Can&#x27;t print recipes (New)',
  52. :text => '#1'
  53. # link to a changeset
  54. assert_select 'a[href=?][title=?]',
  55. 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
  56. 'This commit fixes #1, #2 and references #1 &amp; #3',
  57. :text => 'r2'
  58. # link to a description diff
  59. assert_select 'a[href=?][title=?]',
  60. 'https://mydomain.foo/journals/diff/3?detail_id=4',
  61. 'View differences',
  62. :text => 'diff'
  63. # link to an attachment
  64. assert_select 'a[href=?]',
  65. 'https://mydomain.foo/attachments/download/4/source.rb',
  66. :text => 'source.rb'
  67. end
  68. end
  69. def test_generated_links_with_prefix
  70. Setting.default_language = 'en'
  71. relative_url_root = Redmine::Utils.relative_url_root
  72. Setting.host_name = 'mydomain.foo/rdm'
  73. Setting.protocol = 'http'
  74. journal = Journal.find(3)
  75. assert Mailer.issue_edit(journal).deliver
  76. mail = last_email
  77. assert_not_nil mail
  78. assert_select_email do
  79. # link to the main ticket
  80. assert_select 'a[href=?]',
  81. 'http://mydomain.foo/rdm/issues/2#change-3',
  82. :text => 'Feature request #2: Add ingredients categories'
  83. # link to a referenced ticket
  84. assert_select 'a[href=?][title=?]',
  85. 'http://mydomain.foo/rdm/issues/1',
  86. 'Can&#x27;t print recipes (New)',
  87. :text => '#1'
  88. # link to a changeset
  89. assert_select 'a[href=?][title=?]',
  90. 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
  91. 'This commit fixes #1, #2 and references #1 &amp; #3',
  92. :text => 'r2'
  93. # link to a description diff
  94. assert_select 'a[href=?][title=?]',
  95. 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
  96. 'View differences',
  97. :text => 'diff'
  98. # link to an attachment
  99. assert_select 'a[href=?]',
  100. 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
  101. :text => 'source.rb'
  102. end
  103. end
  104. def test_generated_links_with_prefix_and_no_relative_url_root
  105. Setting.default_language = 'en'
  106. relative_url_root = Redmine::Utils.relative_url_root
  107. Setting.host_name = 'mydomain.foo/rdm'
  108. Setting.protocol = 'http'
  109. Redmine::Utils.relative_url_root = nil
  110. journal = Journal.find(3)
  111. assert Mailer.issue_edit(journal).deliver
  112. mail = last_email
  113. assert_not_nil mail
  114. assert_select_email do
  115. # link to the main ticket
  116. assert_select 'a[href=?]',
  117. 'http://mydomain.foo/rdm/issues/2#change-3',
  118. :text => 'Feature request #2: Add ingredients categories'
  119. # link to a referenced ticket
  120. assert_select 'a[href=?][title=?]',
  121. 'http://mydomain.foo/rdm/issues/1',
  122. 'Can&#x27;t print recipes (New)',
  123. :text => '#1'
  124. # link to a changeset
  125. assert_select 'a[href=?][title=?]',
  126. 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
  127. 'This commit fixes #1, #2 and references #1 &amp; #3',
  128. :text => 'r2'
  129. # link to a description diff
  130. assert_select 'a[href=?][title=?]',
  131. 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
  132. 'View differences',
  133. :text => 'diff'
  134. # link to an attachment
  135. assert_select 'a[href=?]',
  136. 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
  137. :text => 'source.rb'
  138. end
  139. ensure
  140. # restore it
  141. Redmine::Utils.relative_url_root = relative_url_root
  142. end
  143. def test_email_headers
  144. issue = Issue.find(1)
  145. Mailer.issue_add(issue).deliver
  146. mail = last_email
  147. assert_not_nil mail
  148. assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
  149. assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
  150. assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
  151. end
  152. def test_email_headers_should_include_sender
  153. issue = Issue.find(1)
  154. Mailer.issue_add(issue).deliver
  155. mail = last_email
  156. assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
  157. end
  158. def test_plain_text_mail
  159. Setting.plain_text_mail = 1
  160. journal = Journal.find(2)
  161. Mailer.issue_edit(journal).deliver
  162. mail = last_email
  163. assert_equal "text/plain; charset=UTF-8", mail.content_type
  164. assert_equal 0, mail.parts.size
  165. assert !mail.encoded.include?('href')
  166. end
  167. def test_html_mail
  168. Setting.plain_text_mail = 0
  169. journal = Journal.find(2)
  170. Mailer.issue_edit(journal).deliver
  171. mail = last_email
  172. assert_equal 2, mail.parts.size
  173. assert mail.encoded.include?('href')
  174. end
  175. def test_from_header
  176. with_settings :mail_from => 'redmine@example.net' do
  177. Mailer.test_email(User.find(1)).deliver
  178. end
  179. mail = last_email
  180. assert_equal 'redmine@example.net', mail.from_addrs.first
  181. end
  182. def test_from_header_with_phrase
  183. with_settings :mail_from => 'Redmine app <redmine@example.net>' do
  184. Mailer.test_email(User.find(1)).deliver
  185. end
  186. mail = last_email
  187. assert_equal 'redmine@example.net', mail.from_addrs.first
  188. assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
  189. end
  190. def test_should_not_send_email_without_recipient
  191. news = News.first
  192. user = news.author
  193. # Remove members except news author
  194. news.project.memberships.each {|m| m.destroy unless m.user == user}
  195. user.pref[:no_self_notified] = false
  196. user.pref.save
  197. User.current = user
  198. Mailer.news_added(news.reload).deliver
  199. assert_equal 1, last_email.bcc.size
  200. # nobody to notify
  201. user.pref[:no_self_notified] = true
  202. user.pref.save
  203. User.current = user
  204. ActionMailer::Base.deliveries.clear
  205. Mailer.news_added(news.reload).deliver
  206. assert ActionMailer::Base.deliveries.empty?
  207. end
  208. def test_issue_add_message_id
  209. issue = Issue.find(1)
  210. Mailer.issue_add(issue).deliver
  211. mail = last_email
  212. assert_equal Mailer.message_id_for(issue), mail.message_id
  213. assert_nil mail.references
  214. end
  215. def test_issue_edit_message_id
  216. journal = Journal.find(1)
  217. Mailer.issue_edit(journal).deliver
  218. mail = last_email
  219. assert_equal Mailer.message_id_for(journal), mail.message_id
  220. assert_include Mailer.message_id_for(journal.issue), mail.references
  221. assert_select_email do
  222. # link to the update
  223. assert_select "a[href=?]",
  224. "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
  225. end
  226. end
  227. def test_message_posted_message_id
  228. message = Message.find(1)
  229. Mailer.message_posted(message).deliver
  230. mail = last_email
  231. assert_equal Mailer.message_id_for(message), mail.message_id
  232. assert_nil mail.references
  233. assert_select_email do
  234. # link to the message
  235. assert_select "a[href=?]",
  236. "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
  237. :text => message.subject
  238. end
  239. end
  240. def test_reply_posted_message_id
  241. message = Message.find(3)
  242. Mailer.message_posted(message).deliver
  243. mail = last_email
  244. assert_equal Mailer.message_id_for(message), mail.message_id
  245. assert_include Mailer.message_id_for(message.parent), mail.references
  246. assert_select_email do
  247. # link to the reply
  248. assert_select "a[href=?]",
  249. "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
  250. :text => message.subject
  251. end
  252. end
  253. context("#issue_add") do
  254. setup do
  255. ActionMailer::Base.deliveries.clear
  256. Setting.bcc_recipients = '1'
  257. @issue = Issue.find(1)
  258. end
  259. should "notify project members" do
  260. assert Mailer.issue_add(@issue).deliver
  261. assert last_email.bcc.include?('dlopper@somenet.foo')
  262. end
  263. should "not notify project members that are not allow to view the issue" do
  264. Role.find(2).remove_permission!(:view_issues)
  265. assert Mailer.issue_add(@issue).deliver
  266. assert !last_email.bcc.include?('dlopper@somenet.foo')
  267. end
  268. should "notify issue watchers" do
  269. user = User.find(9)
  270. # minimal email notification options
  271. user.pref[:no_self_notified] = '1'
  272. user.pref.save
  273. user.mail_notification = false
  274. user.save
  275. Watcher.create!(:watchable => @issue, :user => user)
  276. assert Mailer.issue_add(@issue).deliver
  277. assert last_email.bcc.include?(user.mail)
  278. end
  279. should "not notify watchers not allowed to view the issue" do
  280. user = User.find(9)
  281. Watcher.create!(:watchable => @issue, :user => user)
  282. Role.non_member.remove_permission!(:view_issues)
  283. assert Mailer.issue_add(@issue).deliver
  284. assert !last_email.bcc.include?(user.mail)
  285. end
  286. end
  287. # test mailer methods for each language
  288. def test_issue_add
  289. issue = Issue.find(1)
  290. valid_languages.each do |lang|
  291. Setting.default_language = lang.to_s
  292. assert Mailer.issue_add(issue).deliver
  293. end
  294. end
  295. def test_issue_edit
  296. journal = Journal.find(1)
  297. valid_languages.each do |lang|
  298. Setting.default_language = lang.to_s
  299. assert Mailer.issue_edit(journal).deliver
  300. end
  301. end
  302. def test_issue_edit_should_send_private_notes_to_users_with_permission_only
  303. journal = Journal.find(1)
  304. journal.private_notes = true
  305. journal.save!
  306. Role.find(2).add_permission! :view_private_notes
  307. Mailer.issue_edit(journal).deliver
  308. assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
  309. Role.find(2).remove_permission! :view_private_notes
  310. Mailer.issue_edit(journal).deliver
  311. assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
  312. end
  313. def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
  314. Issue.find(1).set_watcher(User.find_by_login('someone'))
  315. journal = Journal.find(1)
  316. journal.private_notes = true
  317. journal.save!
  318. Role.non_member.add_permission! :view_private_notes
  319. Mailer.issue_edit(journal).deliver
  320. assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
  321. Role.non_member.remove_permission! :view_private_notes
  322. Mailer.issue_edit(journal).deliver
  323. assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
  324. end
  325. def test_document_added
  326. document = Document.find(1)
  327. valid_languages.each do |lang|
  328. Setting.default_language = lang.to_s
  329. assert Mailer.document_added(document).deliver
  330. end
  331. end
  332. def test_attachments_added
  333. attachements = [ Attachment.find_by_container_type('Document') ]
  334. valid_languages.each do |lang|
  335. Setting.default_language = lang.to_s
  336. assert Mailer.attachments_added(attachements).deliver
  337. end
  338. end
  339. def test_version_file_added
  340. attachements = [ Attachment.find_by_container_type('Version') ]
  341. assert Mailer.attachments_added(attachements).deliver
  342. assert_not_nil last_email.bcc
  343. assert last_email.bcc.any?
  344. assert_select_email do
  345. assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
  346. end
  347. end
  348. def test_project_file_added
  349. attachements = [ Attachment.find_by_container_type('Project') ]
  350. assert Mailer.attachments_added(attachements).deliver
  351. assert_not_nil last_email.bcc
  352. assert last_email.bcc.any?
  353. assert_select_email do
  354. assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
  355. end
  356. end
  357. def test_news_added
  358. news = News.first
  359. valid_languages.each do |lang|
  360. Setting.default_language = lang.to_s
  361. assert Mailer.news_added(news).deliver
  362. end
  363. end
  364. def test_news_comment_added
  365. comment = Comment.find(2)
  366. valid_languages.each do |lang|
  367. Setting.default_language = lang.to_s
  368. assert Mailer.news_comment_added(comment).deliver
  369. end
  370. end
  371. def test_message_posted
  372. message = Message.first
  373. recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
  374. recipients = recipients.compact.uniq
  375. valid_languages.each do |lang|
  376. Setting.default_language = lang.to_s
  377. assert Mailer.message_posted(message).deliver
  378. end
  379. end
  380. def test_wiki_content_added
  381. content = WikiContent.find(1)
  382. valid_languages.each do |lang|
  383. Setting.default_language = lang.to_s
  384. assert_difference 'ActionMailer::Base.deliveries.size' do
  385. assert Mailer.wiki_content_added(content).deliver
  386. assert_select_email do
  387. assert_select 'a[href=?]',
  388. 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
  389. :text => 'CookBook documentation'
  390. end
  391. end
  392. end
  393. end
  394. def test_wiki_content_updated
  395. content = WikiContent.find(1)
  396. valid_languages.each do |lang|
  397. Setting.default_language = lang.to_s
  398. assert_difference 'ActionMailer::Base.deliveries.size' do
  399. assert Mailer.wiki_content_updated(content).deliver
  400. assert_select_email do
  401. assert_select 'a[href=?]',
  402. 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
  403. :text => 'CookBook documentation'
  404. end
  405. end
  406. end
  407. end
  408. def test_account_information
  409. user = User.find(2)
  410. valid_languages.each do |lang|
  411. user.update_attribute :language, lang.to_s
  412. user.reload
  413. assert Mailer.account_information(user, 'pAsswORd').deliver
  414. end
  415. end
  416. def test_lost_password
  417. token = Token.find(2)
  418. valid_languages.each do |lang|
  419. token.user.update_attribute :language, lang.to_s
  420. token.reload
  421. assert Mailer.lost_password(token).deliver
  422. end
  423. end
  424. def test_register
  425. token = Token.find(1)
  426. Setting.host_name = 'redmine.foo'
  427. Setting.protocol = 'https'
  428. valid_languages.each do |lang|
  429. token.user.update_attribute :language, lang.to_s
  430. token.reload
  431. ActionMailer::Base.deliveries.clear
  432. assert Mailer.register(token).deliver
  433. mail = last_email
  434. assert_select_email do
  435. assert_select "a[href=?]",
  436. "https://redmine.foo/account/activate?token=#{token.value}",
  437. :text => "https://redmine.foo/account/activate?token=#{token.value}"
  438. end
  439. end
  440. end
  441. def test_test
  442. user = User.find(1)
  443. valid_languages.each do |lang|
  444. user.update_attribute :language, lang.to_s
  445. assert Mailer.test_email(user).deliver
  446. end
  447. end
  448. def test_reminders
  449. Mailer.reminders(:days => 42)
  450. assert_equal 1, ActionMailer::Base.deliveries.size
  451. mail = last_email
  452. assert mail.bcc.include?('dlopper@somenet.foo')
  453. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
  454. assert_equal '1 issue(s) due in the next 42 days', mail.subject
  455. end
  456. def test_reminders_should_not_include_closed_issues
  457. with_settings :default_language => 'en' do
  458. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
  459. :subject => 'Closed issue', :assigned_to_id => 3,
  460. :due_date => 5.days.from_now,
  461. :author_id => 2)
  462. ActionMailer::Base.deliveries.clear
  463. Mailer.reminders(:days => 42)
  464. assert_equal 1, ActionMailer::Base.deliveries.size
  465. mail = last_email
  466. assert mail.bcc.include?('dlopper@somenet.foo')
  467. assert_mail_body_no_match 'Closed issue', mail
  468. end
  469. end
  470. def test_reminders_for_users
  471. Mailer.reminders(:days => 42, :users => ['5'])
  472. assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
  473. Mailer.reminders(:days => 42, :users => ['3'])
  474. assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
  475. mail = last_email
  476. assert mail.bcc.include?('dlopper@somenet.foo')
  477. assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
  478. end
  479. def test_reminder_should_include_issues_assigned_to_groups
  480. with_settings :default_language => 'en' do
  481. group = Group.generate!
  482. group.users << User.find(2)
  483. group.users << User.find(3)
  484. Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
  485. :subject => 'Assigned to group', :assigned_to => group,
  486. :due_date => 5.days.from_now,
  487. :author_id => 2)
  488. ActionMailer::Base.deliveries.clear
  489. Mailer.reminders(:days => 7)
  490. assert_equal 2, ActionMailer::Base.deliveries.size
  491. assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
  492. ActionMailer::Base.deliveries.each do |mail|
  493. assert_mail_body_match 'Assigned to group', mail
  494. end
  495. end
  496. end
  497. def test_mailer_should_not_change_locale
  498. Setting.default_language = 'en'
  499. # Set current language to italian
  500. set_language_if_valid 'it'
  501. # Send an email to a french user
  502. user = User.find(1)
  503. user.language = 'fr'
  504. Mailer.account_activated(user).deliver
  505. mail = last_email
  506. assert_mail_body_match 'Votre compte', mail
  507. assert_equal :it, current_language
  508. end
  509. def test_with_deliveries_off
  510. Mailer.with_deliveries false do
  511. Mailer.test_email(User.find(1)).deliver
  512. end
  513. assert ActionMailer::Base.deliveries.empty?
  514. # should restore perform_deliveries
  515. assert ActionMailer::Base.perform_deliveries
  516. end
  517. def test_layout_should_include_the_emails_header
  518. with_settings :emails_header => "*Header content*" do
  519. assert Mailer.test_email(User.find(1)).deliver
  520. assert_select_email do
  521. assert_select ".header" do
  522. assert_select "strong", :text => "Header content"
  523. end
  524. end
  525. end
  526. end
  527. def test_should_escape_html_templates_only
  528. Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
  529. mail = last_email
  530. assert_equal 2, mail.parts.size
  531. assert_include '<tag>', text_part.body.encoded
  532. assert_include '&lt;tag&gt;', html_part.body.encoded
  533. end
  534. private
  535. def last_email
  536. mail = ActionMailer::Base.deliveries.last
  537. assert_not_nil mail
  538. mail
  539. end
  540. def text_part
  541. last_email.parts.detect {|part| part.content_type.include?('text/plain')}
  542. end
  543. def html_part
  544. last_email.parts.detect {|part| part.content_type.include?('text/html')}
  545. end
  546. end