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