comparison .svn/pristine/11/11e346e9edcfd6783f0cb16b00aae87132a43f72.svn-base @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents
children
comparison
equal deleted inserted replaced
1516:b450a9d58aed 1517:dffacf8a6908
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.expand_path('../../test_helper', __FILE__)
19
20 class MailerTest < ActiveSupport::TestCase
21 include Redmine::I18n
22 include ActionDispatch::Assertions::SelectorAssertions
23 fixtures :projects, :enabled_modules, :issues, :users, :members,
24 :member_roles, :roles, :documents, :attachments, :news,
25 :tokens, :journals, :journal_details, :changesets,
26 :trackers, :projects_trackers,
27 :issue_statuses, :enumerations, :messages, :boards, :repositories,
28 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
29 :versions,
30 :comments
31
32 def setup
33 ActionMailer::Base.deliveries.clear
34 Setting.host_name = 'mydomain.foo'
35 Setting.protocol = 'http'
36 Setting.plain_text_mail = '0'
37 end
38
39 def test_generated_links_in_emails
40 Setting.default_language = 'en'
41 Setting.host_name = 'mydomain.foo'
42 Setting.protocol = 'https'
43
44 journal = Journal.find(3)
45 assert Mailer.deliver_issue_edit(journal)
46
47 mail = last_email
48 assert_not_nil mail
49
50 assert_select_email do
51 # link to the main ticket
52 assert_select 'a[href=?]',
53 'https://mydomain.foo/issues/2#change-3',
54 :text => 'Feature request #2: Add ingredients categories'
55 # link to a referenced ticket
56 assert_select 'a[href=?][title=?]',
57 'https://mydomain.foo/issues/1',
58 "#{ESCAPED_UCANT} print recipes (New)",
59 :text => '#1'
60 # link to a changeset
61 assert_select 'a[href=?][title=?]',
62 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
63 'This commit fixes #1, #2 and references #1 &amp; #3',
64 :text => 'r2'
65 # link to a description diff
66 assert_select 'a[href=?][title=?]',
67 'https://mydomain.foo/journals/diff/3?detail_id=4',
68 'View differences',
69 :text => 'diff'
70 # link to an attachment
71 assert_select 'a[href=?]',
72 'https://mydomain.foo/attachments/download/4/source.rb',
73 :text => 'source.rb'
74 end
75 end
76
77 def test_generated_links_with_prefix
78 Setting.default_language = 'en'
79 relative_url_root = Redmine::Utils.relative_url_root
80 Setting.host_name = 'mydomain.foo/rdm'
81 Setting.protocol = 'http'
82
83 journal = Journal.find(3)
84 assert Mailer.deliver_issue_edit(journal)
85
86 mail = last_email
87 assert_not_nil mail
88
89 assert_select_email do
90 # link to the main ticket
91 assert_select 'a[href=?]',
92 'http://mydomain.foo/rdm/issues/2#change-3',
93 :text => 'Feature request #2: Add ingredients categories'
94 # link to a referenced ticket
95 assert_select 'a[href=?][title=?]',
96 'http://mydomain.foo/rdm/issues/1',
97 "#{ESCAPED_UCANT} print recipes (New)",
98 :text => '#1'
99 # link to a changeset
100 assert_select 'a[href=?][title=?]',
101 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
102 'This commit fixes #1, #2 and references #1 &amp; #3',
103 :text => 'r2'
104 # link to a description diff
105 assert_select 'a[href=?][title=?]',
106 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
107 'View differences',
108 :text => 'diff'
109 # link to an attachment
110 assert_select 'a[href=?]',
111 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
112 :text => 'source.rb'
113 end
114 end
115
116 def test_issue_edit_should_generate_url_with_hostname_for_relations
117 journal = Journal.new(:journalized => Issue.find(1), :user => User.find(1), :created_on => Time.now)
118 journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'label_relates_to', :value => 2)
119 Mailer.deliver_issue_edit(journal)
120 assert_not_nil last_email
121 assert_select_email do
122 assert_select 'a[href=?]', 'http://mydomain.foo/issues/2', :text => 'Feature request #2'
123 end
124 end
125
126 def test_generated_links_with_prefix_and_no_relative_url_root
127 Setting.default_language = 'en'
128 relative_url_root = Redmine::Utils.relative_url_root
129 Setting.host_name = 'mydomain.foo/rdm'
130 Setting.protocol = 'http'
131 Redmine::Utils.relative_url_root = nil
132
133 journal = Journal.find(3)
134 assert Mailer.deliver_issue_edit(journal)
135
136 mail = last_email
137 assert_not_nil mail
138
139 assert_select_email do
140 # link to the main ticket
141 assert_select 'a[href=?]',
142 'http://mydomain.foo/rdm/issues/2#change-3',
143 :text => 'Feature request #2: Add ingredients categories'
144 # link to a referenced ticket
145 assert_select 'a[href=?][title=?]',
146 'http://mydomain.foo/rdm/issues/1',
147 "#{ESCAPED_UCANT} print recipes (New)",
148 :text => '#1'
149 # link to a changeset
150 assert_select 'a[href=?][title=?]',
151 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
152 'This commit fixes #1, #2 and references #1 &amp; #3',
153 :text => 'r2'
154 # link to a description diff
155 assert_select 'a[href=?][title=?]',
156 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
157 'View differences',
158 :text => 'diff'
159 # link to an attachment
160 assert_select 'a[href=?]',
161 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
162 :text => 'source.rb'
163 end
164 ensure
165 # restore it
166 Redmine::Utils.relative_url_root = relative_url_root
167 end
168
169 def test_email_headers
170 issue = Issue.find(1)
171 Mailer.deliver_issue_add(issue)
172 mail = last_email
173 assert_not_nil mail
174 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
175 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
176 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
177 end
178
179 def test_email_headers_should_include_sender
180 issue = Issue.find(1)
181 Mailer.deliver_issue_add(issue)
182 mail = last_email
183 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
184 end
185
186 def test_plain_text_mail
187 Setting.plain_text_mail = 1
188 journal = Journal.find(2)
189 Mailer.deliver_issue_edit(journal)
190 mail = last_email
191 assert_equal "text/plain; charset=UTF-8", mail.content_type
192 assert_equal 0, mail.parts.size
193 assert !mail.encoded.include?('href')
194 end
195
196 def test_html_mail
197 Setting.plain_text_mail = 0
198 journal = Journal.find(2)
199 Mailer.deliver_issue_edit(journal)
200 mail = last_email
201 assert_equal 2, mail.parts.size
202 assert mail.encoded.include?('href')
203 end
204
205 def test_from_header
206 with_settings :mail_from => 'redmine@example.net' do
207 Mailer.test_email(User.find(1)).deliver
208 end
209 mail = last_email
210 assert_equal 'redmine@example.net', mail.from_addrs.first
211 end
212
213 def test_from_header_with_phrase
214 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
215 Mailer.test_email(User.find(1)).deliver
216 end
217 mail = last_email
218 assert_equal 'redmine@example.net', mail.from_addrs.first
219 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
220 end
221
222 def test_should_not_send_email_without_recipient
223 news = News.first
224 user = news.author
225 # Remove members except news author
226 news.project.memberships.each {|m| m.destroy unless m.user == user}
227
228 user.pref.no_self_notified = false
229 user.pref.save
230 User.current = user
231 Mailer.news_added(news.reload).deliver
232 assert_equal 1, last_email.bcc.size
233
234 # nobody to notify
235 user.pref.no_self_notified = true
236 user.pref.save
237 User.current = user
238 ActionMailer::Base.deliveries.clear
239 Mailer.news_added(news.reload).deliver
240 assert ActionMailer::Base.deliveries.empty?
241 end
242
243 def test_issue_add_message_id
244 issue = Issue.find(2)
245 Mailer.deliver_issue_add(issue)
246 mail = last_email
247 assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
248 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
249 end
250
251 def test_issue_edit_message_id
252 journal = Journal.find(3)
253 journal.issue = Issue.find(2)
254
255 Mailer.deliver_issue_edit(journal)
256 mail = last_email
257 assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
258 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
259 assert_select_email do
260 # link to the update
261 assert_select "a[href=?]",
262 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
263 end
264 end
265
266 def test_message_posted_message_id
267 message = Message.find(1)
268 Mailer.message_posted(message).deliver
269 mail = last_email
270 assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
271 assert_include "redmine.message-1.20070512151532@example.net", mail.references
272 assert_select_email do
273 # link to the message
274 assert_select "a[href=?]",
275 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
276 :text => message.subject
277 end
278 end
279
280 def test_reply_posted_message_id
281 message = Message.find(3)
282 Mailer.message_posted(message).deliver
283 mail = last_email
284 assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
285 assert_include "redmine.message-1.20070512151532@example.net", mail.references
286 assert_select_email do
287 # link to the reply
288 assert_select "a[href=?]",
289 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
290 :text => message.subject
291 end
292 end
293
294 test "#issue_add should notify project members" do
295 issue = Issue.find(1)
296 assert Mailer.deliver_issue_add(issue)
297 assert last_email.bcc.include?('dlopper@somenet.foo')
298 end
299
300 test "#issue_add should not notify project members that are not allow to view the issue" do
301 issue = Issue.find(1)
302 Role.find(2).remove_permission!(:view_issues)
303 assert Mailer.deliver_issue_add(issue)
304 assert !last_email.bcc.include?('dlopper@somenet.foo')
305 end
306
307 test "#issue_add should notify issue watchers" do
308 issue = Issue.find(1)
309 user = User.find(9)
310 # minimal email notification options
311 user.pref.no_self_notified = '1'
312 user.pref.save
313 user.mail_notification = false
314 user.save
315
316 Watcher.create!(:watchable => issue, :user => user)
317 assert Mailer.deliver_issue_add(issue)
318 assert last_email.bcc.include?(user.mail)
319 end
320
321 test "#issue_add should not notify watchers not allowed to view the issue" do
322 issue = Issue.find(1)
323 user = User.find(9)
324 Watcher.create!(:watchable => issue, :user => user)
325 Role.non_member.remove_permission!(:view_issues)
326 assert Mailer.deliver_issue_add(issue)
327 assert !last_email.bcc.include?(user.mail)
328 end
329
330 def test_issue_add_should_include_enabled_fields
331 Setting.default_language = 'en'
332 issue = Issue.find(2)
333 assert Mailer.deliver_issue_add(issue)
334 assert_mail_body_match '* Target version: 1.0', last_email
335 assert_select_email do
336 assert_select 'li', :text => 'Target version: 1.0'
337 end
338 end
339
340 def test_issue_add_should_not_include_disabled_fields
341 Setting.default_language = 'en'
342 issue = Issue.find(2)
343 tracker = issue.tracker
344 tracker.core_fields -= ['fixed_version_id']
345 tracker.save!
346 assert Mailer.deliver_issue_add(issue)
347 assert_mail_body_no_match 'Target version', last_email
348 assert_select_email do
349 assert_select 'li', :text => /Target version/, :count => 0
350 end
351 end
352
353 # test mailer methods for each language
354 def test_issue_add
355 issue = Issue.find(1)
356 valid_languages.each do |lang|
357 Setting.default_language = lang.to_s
358 assert Mailer.deliver_issue_add(issue)
359 end
360 end
361
362 def test_issue_edit
363 journal = Journal.find(1)
364 valid_languages.each do |lang|
365 Setting.default_language = lang.to_s
366 assert Mailer.deliver_issue_edit(journal)
367 end
368 end
369
370 def test_issue_edit_should_send_private_notes_to_users_with_permission_only
371 journal = Journal.find(1)
372 journal.private_notes = true
373 journal.save!
374
375 Role.find(2).add_permission! :view_private_notes
376 Mailer.deliver_issue_edit(journal)
377 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
378
379 Role.find(2).remove_permission! :view_private_notes
380 Mailer.deliver_issue_edit(journal)
381 assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
382 end
383
384 def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
385 Issue.find(1).set_watcher(User.find_by_login('someone'))
386 journal = Journal.find(1)
387 journal.private_notes = true
388 journal.save!
389
390 Role.non_member.add_permission! :view_private_notes
391 Mailer.deliver_issue_edit(journal)
392 assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
393
394 Role.non_member.remove_permission! :view_private_notes
395 Mailer.deliver_issue_edit(journal)
396 assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
397 end
398
399 def test_issue_edit_should_mark_private_notes
400 journal = Journal.find(2)
401 journal.private_notes = true
402 journal.save!
403
404 with_settings :default_language => 'en' do
405 Mailer.deliver_issue_edit(journal)
406 end
407 assert_mail_body_match '(Private notes)', last_email
408 end
409
410 def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
411 issue = Issue.generate!
412 private_issue = Issue.generate!(:is_private => true)
413 IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
414 issue.reload
415 assert_equal 1, issue.journals.size
416 journal = issue.journals.first
417 ActionMailer::Base.deliveries.clear
418
419 Mailer.deliver_issue_edit(journal)
420 last_email.bcc.each do |email|
421 user = User.find_by_mail(email)
422 assert private_issue.visible?(user), "Issue was not visible to #{user}"
423 end
424 end
425
426 def test_document_added
427 document = Document.find(1)
428 valid_languages.each do |lang|
429 Setting.default_language = lang.to_s
430 assert Mailer.document_added(document).deliver
431 end
432 end
433
434 def test_attachments_added
435 attachements = [ Attachment.find_by_container_type('Document') ]
436 valid_languages.each do |lang|
437 Setting.default_language = lang.to_s
438 assert Mailer.attachments_added(attachements).deliver
439 end
440 end
441
442 def test_version_file_added
443 attachements = [ Attachment.find_by_container_type('Version') ]
444 assert Mailer.attachments_added(attachements).deliver
445 assert_not_nil last_email.bcc
446 assert last_email.bcc.any?
447 assert_select_email do
448 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
449 end
450 end
451
452 def test_project_file_added
453 attachements = [ Attachment.find_by_container_type('Project') ]
454 assert Mailer.attachments_added(attachements).deliver
455 assert_not_nil last_email.bcc
456 assert last_email.bcc.any?
457 assert_select_email do
458 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
459 end
460 end
461
462 def test_news_added
463 news = News.first
464 valid_languages.each do |lang|
465 Setting.default_language = lang.to_s
466 assert Mailer.news_added(news).deliver
467 end
468 end
469
470 def test_news_added_should_notify_project_news_watchers
471 user1 = User.generate!
472 user2 = User.generate!
473 news = News.find(1)
474 news.project.enabled_module('news').add_watcher(user1)
475
476 Mailer.news_added(news).deliver
477 assert_include user1.mail, last_email.bcc
478 assert_not_include user2.mail, last_email.bcc
479 end
480
481 def test_news_comment_added
482 comment = Comment.find(2)
483 valid_languages.each do |lang|
484 Setting.default_language = lang.to_s
485 assert Mailer.news_comment_added(comment).deliver
486 end
487 end
488
489 def test_message_posted
490 message = Message.first
491 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
492 recipients = recipients.compact.uniq
493 valid_languages.each do |lang|
494 Setting.default_language = lang.to_s
495 assert Mailer.message_posted(message).deliver
496 end
497 end
498
499 def test_wiki_content_added
500 content = WikiContent.find(1)
501 valid_languages.each do |lang|
502 Setting.default_language = lang.to_s
503 assert_difference 'ActionMailer::Base.deliveries.size' do
504 assert Mailer.wiki_content_added(content).deliver
505 assert_select_email do
506 assert_select 'a[href=?]',
507 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
508 :text => 'CookBook documentation'
509 end
510 end
511 end
512 end
513
514 def test_wiki_content_updated
515 content = WikiContent.find(1)
516 valid_languages.each do |lang|
517 Setting.default_language = lang.to_s
518 assert_difference 'ActionMailer::Base.deliveries.size' do
519 assert Mailer.wiki_content_updated(content).deliver
520 assert_select_email do
521 assert_select 'a[href=?]',
522 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
523 :text => 'CookBook documentation'
524 end
525 end
526 end
527 end
528
529 def test_account_information
530 user = User.find(2)
531 valid_languages.each do |lang|
532 user.update_attribute :language, lang.to_s
533 user.reload
534 assert Mailer.account_information(user, 'pAsswORd').deliver
535 end
536 end
537
538 def test_lost_password
539 token = Token.find(2)
540 valid_languages.each do |lang|
541 token.user.update_attribute :language, lang.to_s
542 token.reload
543 assert Mailer.lost_password(token).deliver
544 end
545 end
546
547 def test_register
548 token = Token.find(1)
549 Setting.host_name = 'redmine.foo'
550 Setting.protocol = 'https'
551
552 valid_languages.each do |lang|
553 token.user.update_attribute :language, lang.to_s
554 token.reload
555 ActionMailer::Base.deliveries.clear
556 assert Mailer.register(token).deliver
557 mail = last_email
558 assert_select_email do
559 assert_select "a[href=?]",
560 "https://redmine.foo/account/activate?token=#{token.value}",
561 :text => "https://redmine.foo/account/activate?token=#{token.value}"
562 end
563 end
564 end
565
566 def test_test
567 user = User.find(1)
568 valid_languages.each do |lang|
569 user.update_attribute :language, lang.to_s
570 assert Mailer.test_email(user).deliver
571 end
572 end
573
574 def test_reminders
575 Mailer.reminders(:days => 42)
576 assert_equal 1, ActionMailer::Base.deliveries.size
577 mail = last_email
578 assert mail.bcc.include?('dlopper@somenet.foo')
579 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
580 assert_equal '1 issue(s) due in the next 42 days', mail.subject
581 end
582
583 def test_reminders_should_not_include_closed_issues
584 with_settings :default_language => 'en' do
585 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
586 :subject => 'Closed issue', :assigned_to_id => 3,
587 :due_date => 5.days.from_now,
588 :author_id => 2)
589 ActionMailer::Base.deliveries.clear
590
591 Mailer.reminders(:days => 42)
592 assert_equal 1, ActionMailer::Base.deliveries.size
593 mail = last_email
594 assert mail.bcc.include?('dlopper@somenet.foo')
595 assert_mail_body_no_match 'Closed issue', mail
596 end
597 end
598
599 def test_reminders_for_users
600 Mailer.reminders(:days => 42, :users => ['5'])
601 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
602 Mailer.reminders(:days => 42, :users => ['3'])
603 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
604 mail = last_email
605 assert mail.bcc.include?('dlopper@somenet.foo')
606 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
607 end
608
609 def test_reminder_should_include_issues_assigned_to_groups
610 with_settings :default_language => 'en' do
611 group = Group.generate!
612 group.users << User.find(2)
613 group.users << User.find(3)
614
615 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
616 :subject => 'Assigned to group', :assigned_to => group,
617 :due_date => 5.days.from_now,
618 :author_id => 2)
619 ActionMailer::Base.deliveries.clear
620
621 Mailer.reminders(:days => 7)
622 assert_equal 2, ActionMailer::Base.deliveries.size
623 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
624 ActionMailer::Base.deliveries.each do |mail|
625 assert_mail_body_match 'Assigned to group', mail
626 end
627 end
628 end
629
630 def test_mailer_should_not_change_locale
631 Setting.default_language = 'en'
632 # Set current language to italian
633 set_language_if_valid 'it'
634 # Send an email to a french user
635 user = User.find(1)
636 user.language = 'fr'
637 Mailer.account_activated(user).deliver
638 mail = last_email
639 assert_mail_body_match 'Votre compte', mail
640
641 assert_equal :it, current_language
642 end
643
644 def test_with_deliveries_off
645 Mailer.with_deliveries false do
646 Mailer.test_email(User.find(1)).deliver
647 end
648 assert ActionMailer::Base.deliveries.empty?
649 # should restore perform_deliveries
650 assert ActionMailer::Base.perform_deliveries
651 end
652
653 def test_token_for_should_strip_trailing_gt_from_address_with_full_name
654 with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do
655 assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/, Mailer.token_for(Issue.generate!)
656 end
657 end
658
659 def test_layout_should_include_the_emails_header
660 with_settings :emails_header => "*Header content*" do
661 with_settings :plain_text_mail => 0 do
662 assert Mailer.test_email(User.find(1)).deliver
663 assert_select_email do
664 assert_select ".header" do
665 assert_select "strong", :text => "Header content"
666 end
667 end
668 end
669 with_settings :plain_text_mail => 1 do
670 assert Mailer.test_email(User.find(1)).deliver
671 mail = last_email
672 assert_not_nil mail
673 assert_include "*Header content*", mail.body.decoded
674 end
675 end
676 end
677
678 def test_layout_should_not_include_empty_emails_header
679 with_settings :emails_header => "", :plain_text_mail => 0 do
680 assert Mailer.test_email(User.find(1)).deliver
681 assert_select_email do
682 assert_select ".header", false
683 end
684 end
685 end
686
687 def test_layout_should_include_the_emails_footer
688 with_settings :emails_footer => "*Footer content*" do
689 with_settings :plain_text_mail => 0 do
690 assert Mailer.test_email(User.find(1)).deliver
691 assert_select_email do
692 assert_select ".footer" do
693 assert_select "strong", :text => "Footer content"
694 end
695 end
696 end
697 with_settings :plain_text_mail => 1 do
698 assert Mailer.test_email(User.find(1)).deliver
699 mail = last_email
700 assert_not_nil mail
701 assert_include "\n-- \n", mail.body.decoded
702 assert_include "*Footer content*", mail.body.decoded
703 end
704 end
705 end
706
707 def test_layout_should_not_include_empty_emails_footer
708 with_settings :emails_footer => "" do
709 with_settings :plain_text_mail => 0 do
710 assert Mailer.test_email(User.find(1)).deliver
711 assert_select_email do
712 assert_select ".footer", false
713 end
714 end
715 with_settings :plain_text_mail => 1 do
716 assert Mailer.test_email(User.find(1)).deliver
717 mail = last_email
718 assert_not_nil mail
719 assert_not_include "\n-- \n", mail.body.decoded
720 end
721 end
722 end
723
724 def test_should_escape_html_templates_only
725 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
726 mail = last_email
727 assert_equal 2, mail.parts.size
728 assert_include '<tag>', text_part.body.encoded
729 assert_include '&lt;tag&gt;', html_part.body.encoded
730 end
731
732 def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
733 mail = Mailer.test_email(User.find(1))
734 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
735
736 ActionMailer::Base.raise_delivery_errors = true
737 assert_raise Exception, "delivery error" do
738 mail.deliver
739 end
740 ensure
741 ActionMailer::Base.raise_delivery_errors = false
742 end
743
744 def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
745 mail = Mailer.test_email(User.find(1))
746 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
747
748 Rails.logger.expects(:error).with("Email delivery error: delivery error")
749 ActionMailer::Base.raise_delivery_errors = false
750 assert_nothing_raised do
751 mail.deliver
752 end
753 end
754
755 def test_mail_should_return_a_mail_message
756 assert_kind_of ::Mail::Message, Mailer.test_email(User.find(1))
757 end
758
759 private
760
761 def last_email
762 mail = ActionMailer::Base.deliveries.last
763 assert_not_nil mail
764 mail
765 end
766
767 def text_part
768 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
769 end
770
771 def html_part
772 last_email.parts.detect {|part| part.content_type.include?('text/html')}
773 end
774 end