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