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