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