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

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