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