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