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