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