##// END OF EJS Templates
Fixed that text email templates are escaped (#11355)....
Jean-Philippe Lang -
r10022:d79bcc43690f
parent child
Show More
@@ -1,110 +1,145
1 require 'active_record'
1 require 'active_record'
2
2
3 module ActiveRecord
3 module ActiveRecord
4 class Base
4 class Base
5 include Redmine::I18n
5 include Redmine::I18n
6 # Translate attribute names for validation errors display
6 # Translate attribute names for validation errors display
7 def self.human_attribute_name(attr, *args)
7 def self.human_attribute_name(attr, *args)
8 attr = attr.to_s.sub(/_id$/, '')
8 attr = attr.to_s.sub(/_id$/, '')
9
9
10 l("field_#{name.underscore.gsub('/', '_')}_#{attr}", :default => ["field_#{attr}".to_sym, attr])
10 l("field_#{name.underscore.gsub('/', '_')}_#{attr}", :default => ["field_#{attr}".to_sym, attr])
11 end
11 end
12 end
12 end
13
13
14 # Undefines private Kernel#open method to allow using `open` scopes in models.
14 # Undefines private Kernel#open method to allow using `open` scopes in models.
15 # See Defect #11545 (http://www.redmine.org/issues/11545) for details.
15 # See Defect #11545 (http://www.redmine.org/issues/11545) for details.
16 class Base ; undef open ; end
16 class Base ; undef open ; end
17 class Relation ; undef open ; end
17 class Relation ; undef open ; end
18 end
18 end
19
19
20 module ActionView
20 module ActionView
21 module Helpers
21 module Helpers
22 module DateHelper
22 module DateHelper
23 # distance_of_time_in_words breaks when difference is greater than 30 years
23 # distance_of_time_in_words breaks when difference is greater than 30 years
24 def distance_of_date_in_words(from_date, to_date = 0, options = {})
24 def distance_of_date_in_words(from_date, to_date = 0, options = {})
25 from_date = from_date.to_date if from_date.respond_to?(:to_date)
25 from_date = from_date.to_date if from_date.respond_to?(:to_date)
26 to_date = to_date.to_date if to_date.respond_to?(:to_date)
26 to_date = to_date.to_date if to_date.respond_to?(:to_date)
27 distance_in_days = (to_date - from_date).abs
27 distance_in_days = (to_date - from_date).abs
28
28
29 I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
29 I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
30 case distance_in_days
30 case distance_in_days
31 when 0..60 then locale.t :x_days, :count => distance_in_days.round
31 when 0..60 then locale.t :x_days, :count => distance_in_days.round
32 when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
32 when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
33 else locale.t :over_x_years, :count => (distance_in_days / 365).floor
33 else locale.t :over_x_years, :count => (distance_in_days / 365).floor
34 end
34 end
35 end
35 end
36 end
36 end
37 end
37 end
38 end
38 end
39
39
40 class Resolver
40 class Resolver
41 def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
41 def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
42 cached(key, [name, prefix, partial], details, locals) do
42 cached(key, [name, prefix, partial], details, locals) do
43 if details[:formats] & [:xml, :json]
43 if details[:formats] & [:xml, :json]
44 details = details.dup
44 details = details.dup
45 details[:formats] = details[:formats].dup + [:api]
45 details[:formats] = details[:formats].dup + [:api]
46 end
46 end
47 find_templates(name, prefix, partial, details)
47 find_templates(name, prefix, partial, details)
48 end
48 end
49 end
49 end
50 end
50 end
51 end
51 end
52
52
53 # Do not HTML escape text templates
54 module ActionView
55 class Template
56 module Handlers
57 class ERB
58 def call(template)
59 if template.source.encoding_aware?
60 # First, convert to BINARY, so in case the encoding is
61 # wrong, we can still find an encoding tag
62 # (<%# encoding %>) inside the String using a regular
63 # expression
64 template_source = template.source.dup.force_encoding("BINARY")
65
66 erb = template_source.gsub(ENCODING_TAG, '')
67 encoding = $2
68
69 erb.force_encoding valid_encoding(template.source.dup, encoding)
70
71 # Always make sure we return a String in the default_internal
72 erb.encode!
73 else
74 erb = template.source.dup
75 end
76
77 self.class.erb_implementation.new(
78 erb,
79 :trim => (self.class.erb_trim_mode == "-"),
80 :escape => template.identifier =~ /\.text/ # only escape HTML templates
81 ).src
82 end
83 end
84 end
85 end
86 end
87
53 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe }
88 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe }
54
89
55 require 'mail'
90 require 'mail'
56
91
57 module DeliveryMethods
92 module DeliveryMethods
58 class AsyncSMTP < ::Mail::SMTP
93 class AsyncSMTP < ::Mail::SMTP
59 def deliver!(*args)
94 def deliver!(*args)
60 Thread.start do
95 Thread.start do
61 super *args
96 super *args
62 end
97 end
63 end
98 end
64 end
99 end
65
100
66 class AsyncSendmail < ::Mail::Sendmail
101 class AsyncSendmail < ::Mail::Sendmail
67 def deliver!(*args)
102 def deliver!(*args)
68 Thread.start do
103 Thread.start do
69 super *args
104 super *args
70 end
105 end
71 end
106 end
72 end
107 end
73
108
74 class TmpFile
109 class TmpFile
75 def initialize(*args); end
110 def initialize(*args); end
76
111
77 def deliver!(mail)
112 def deliver!(mail)
78 dest_dir = File.join(Rails.root, 'tmp', 'emails')
113 dest_dir = File.join(Rails.root, 'tmp', 'emails')
79 Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
114 Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
80 File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
115 File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
81 end
116 end
82 end
117 end
83 end
118 end
84
119
85 ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP
120 ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP
86 ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail
121 ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail
87 ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile
122 ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile
88
123
89 module ActionController
124 module ActionController
90 module MimeResponds
125 module MimeResponds
91 class Collector
126 class Collector
92 def api(&block)
127 def api(&block)
93 any(:xml, :json, &block)
128 any(:xml, :json, &block)
94 end
129 end
95 end
130 end
96 end
131 end
97 end
132 end
98
133
99 module ActionController
134 module ActionController
100 class Base
135 class Base
101 # Displays an explicit message instead of a NoMethodError exception
136 # Displays an explicit message instead of a NoMethodError exception
102 # when trying to start Redmine with an old session_store.rb
137 # when trying to start Redmine with an old session_store.rb
103 # TODO: remove it in a later version
138 # TODO: remove it in a later version
104 def self.session=(*args)
139 def self.session=(*args)
105 $stderr.puts "Please remove config/initializers/session_store.rb and run `rake generate_secret_token`.\n" +
140 $stderr.puts "Please remove config/initializers/session_store.rb and run `rake generate_secret_token`.\n" +
106 "Setting the session secret with ActionController.session= is no longer supported in Rails 3."
141 "Setting the session secret with ActionController.session= is no longer supported in Rails 3."
107 exit 1
142 exit 1
108 end
143 end
109 end
144 end
110 end
145 end
@@ -1,551 +1,568
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class MailerTest < ActiveSupport::TestCase
20 class MailerTest < ActiveSupport::TestCase
21 include Redmine::I18n
21 include Redmine::I18n
22 include ActionDispatch::Assertions::SelectorAssertions
22 include ActionDispatch::Assertions::SelectorAssertions
23 fixtures :projects, :enabled_modules, :issues, :users, :members,
23 fixtures :projects, :enabled_modules, :issues, :users, :members,
24 :member_roles, :roles, :documents, :attachments, :news,
24 :member_roles, :roles, :documents, :attachments, :news,
25 :tokens, :journals, :journal_details, :changesets,
25 :tokens, :journals, :journal_details, :changesets,
26 :trackers, :projects_trackers,
26 :trackers, :projects_trackers,
27 :issue_statuses, :enumerations, :messages, :boards, :repositories,
27 :issue_statuses, :enumerations, :messages, :boards, :repositories,
28 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
28 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
29 :versions,
29 :versions,
30 :comments
30 :comments
31
31
32 def setup
32 def setup
33 ActionMailer::Base.deliveries.clear
33 ActionMailer::Base.deliveries.clear
34 Setting.host_name = 'mydomain.foo'
34 Setting.host_name = 'mydomain.foo'
35 Setting.protocol = 'http'
35 Setting.protocol = 'http'
36 Setting.plain_text_mail = '0'
36 Setting.plain_text_mail = '0'
37 end
37 end
38
38
39 def test_generated_links_in_emails
39 def test_generated_links_in_emails
40 Setting.default_language = 'en'
40 Setting.default_language = 'en'
41 Setting.host_name = 'mydomain.foo'
41 Setting.host_name = 'mydomain.foo'
42 Setting.protocol = 'https'
42 Setting.protocol = 'https'
43
43
44 journal = Journal.find(3)
44 journal = Journal.find(3)
45 assert Mailer.issue_edit(journal).deliver
45 assert Mailer.issue_edit(journal).deliver
46
46
47 mail = last_email
47 mail = last_email
48 assert_not_nil mail
48 assert_not_nil mail
49
49
50 assert_select_email do
50 assert_select_email do
51 # link to the main ticket
51 # link to the main ticket
52 assert_select 'a[href=?]',
52 assert_select 'a[href=?]',
53 'https://mydomain.foo/issues/2#change-3',
53 'https://mydomain.foo/issues/2#change-3',
54 :text => 'Feature request #2: Add ingredients categories'
54 :text => 'Feature request #2: Add ingredients categories'
55 # link to a referenced ticket
55 # link to a referenced ticket
56 assert_select 'a[href=?][title=?]',
56 assert_select 'a[href=?][title=?]',
57 'https://mydomain.foo/issues/1',
57 'https://mydomain.foo/issues/1',
58 'Can\'t print recipes (New)',
58 'Can\'t print recipes (New)',
59 :text => '#1'
59 :text => '#1'
60 # link to a changeset
60 # link to a changeset
61 assert_select 'a[href=?][title=?]',
61 assert_select 'a[href=?][title=?]',
62 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
62 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
63 'This commit fixes #1, #2 and references #1 &amp; #3',
63 'This commit fixes #1, #2 and references #1 &amp; #3',
64 :text => 'r2'
64 :text => 'r2'
65 # link to a description diff
65 # link to a description diff
66 assert_select 'a[href=?][title=?]',
66 assert_select 'a[href=?][title=?]',
67 'https://mydomain.foo/journals/diff/3?detail_id=4',
67 'https://mydomain.foo/journals/diff/3?detail_id=4',
68 'View differences',
68 'View differences',
69 :text => 'diff'
69 :text => 'diff'
70 # link to an attachment
70 # link to an attachment
71 assert_select 'a[href=?]',
71 assert_select 'a[href=?]',
72 'https://mydomain.foo/attachments/download/4/source.rb',
72 'https://mydomain.foo/attachments/download/4/source.rb',
73 :text => 'source.rb'
73 :text => 'source.rb'
74 end
74 end
75 end
75 end
76
76
77 def test_generated_links_with_prefix
77 def test_generated_links_with_prefix
78 Setting.default_language = 'en'
78 Setting.default_language = 'en'
79 relative_url_root = Redmine::Utils.relative_url_root
79 relative_url_root = Redmine::Utils.relative_url_root
80 Setting.host_name = 'mydomain.foo/rdm'
80 Setting.host_name = 'mydomain.foo/rdm'
81 Setting.protocol = 'http'
81 Setting.protocol = 'http'
82
82
83 journal = Journal.find(3)
83 journal = Journal.find(3)
84 assert Mailer.issue_edit(journal).deliver
84 assert Mailer.issue_edit(journal).deliver
85
85
86 mail = last_email
86 mail = last_email
87 assert_not_nil mail
87 assert_not_nil mail
88
88
89 assert_select_email do
89 assert_select_email do
90 # link to the main ticket
90 # link to the main ticket
91 assert_select 'a[href=?]',
91 assert_select 'a[href=?]',
92 'http://mydomain.foo/rdm/issues/2#change-3',
92 'http://mydomain.foo/rdm/issues/2#change-3',
93 :text => 'Feature request #2: Add ingredients categories'
93 :text => 'Feature request #2: Add ingredients categories'
94 # link to a referenced ticket
94 # link to a referenced ticket
95 assert_select 'a[href=?][title=?]',
95 assert_select 'a[href=?][title=?]',
96 'http://mydomain.foo/rdm/issues/1',
96 'http://mydomain.foo/rdm/issues/1',
97 'Can\'t print recipes (New)',
97 'Can\'t print recipes (New)',
98 :text => '#1'
98 :text => '#1'
99 # link to a changeset
99 # link to a changeset
100 assert_select 'a[href=?][title=?]',
100 assert_select 'a[href=?][title=?]',
101 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
101 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
102 'This commit fixes #1, #2 and references #1 &amp; #3',
102 'This commit fixes #1, #2 and references #1 &amp; #3',
103 :text => 'r2'
103 :text => 'r2'
104 # link to a description diff
104 # link to a description diff
105 assert_select 'a[href=?][title=?]',
105 assert_select 'a[href=?][title=?]',
106 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
106 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
107 'View differences',
107 'View differences',
108 :text => 'diff'
108 :text => 'diff'
109 # link to an attachment
109 # link to an attachment
110 assert_select 'a[href=?]',
110 assert_select 'a[href=?]',
111 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
111 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
112 :text => 'source.rb'
112 :text => 'source.rb'
113 end
113 end
114 end
114 end
115
115
116 def test_generated_links_with_prefix_and_no_relative_url_root
116 def test_generated_links_with_prefix_and_no_relative_url_root
117 Setting.default_language = 'en'
117 Setting.default_language = 'en'
118 relative_url_root = Redmine::Utils.relative_url_root
118 relative_url_root = Redmine::Utils.relative_url_root
119 Setting.host_name = 'mydomain.foo/rdm'
119 Setting.host_name = 'mydomain.foo/rdm'
120 Setting.protocol = 'http'
120 Setting.protocol = 'http'
121 Redmine::Utils.relative_url_root = nil
121 Redmine::Utils.relative_url_root = nil
122
122
123 journal = Journal.find(3)
123 journal = Journal.find(3)
124 assert Mailer.issue_edit(journal).deliver
124 assert Mailer.issue_edit(journal).deliver
125
125
126 mail = last_email
126 mail = last_email
127 assert_not_nil mail
127 assert_not_nil mail
128
128
129 assert_select_email do
129 assert_select_email do
130 # link to the main ticket
130 # link to the main ticket
131 assert_select 'a[href=?]',
131 assert_select 'a[href=?]',
132 'http://mydomain.foo/rdm/issues/2#change-3',
132 'http://mydomain.foo/rdm/issues/2#change-3',
133 :text => 'Feature request #2: Add ingredients categories'
133 :text => 'Feature request #2: Add ingredients categories'
134 # link to a referenced ticket
134 # link to a referenced ticket
135 assert_select 'a[href=?][title=?]',
135 assert_select 'a[href=?][title=?]',
136 'http://mydomain.foo/rdm/issues/1',
136 'http://mydomain.foo/rdm/issues/1',
137 'Can\'t print recipes (New)',
137 'Can\'t print recipes (New)',
138 :text => '#1'
138 :text => '#1'
139 # link to a changeset
139 # link to a changeset
140 assert_select 'a[href=?][title=?]',
140 assert_select 'a[href=?][title=?]',
141 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
141 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
142 'This commit fixes #1, #2 and references #1 &amp; #3',
142 'This commit fixes #1, #2 and references #1 &amp; #3',
143 :text => 'r2'
143 :text => 'r2'
144 # link to a description diff
144 # link to a description diff
145 assert_select 'a[href=?][title=?]',
145 assert_select 'a[href=?][title=?]',
146 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
146 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
147 'View differences',
147 'View differences',
148 :text => 'diff'
148 :text => 'diff'
149 # link to an attachment
149 # link to an attachment
150 assert_select 'a[href=?]',
150 assert_select 'a[href=?]',
151 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
151 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
152 :text => 'source.rb'
152 :text => 'source.rb'
153 end
153 end
154 ensure
154 ensure
155 # restore it
155 # restore it
156 Redmine::Utils.relative_url_root = relative_url_root
156 Redmine::Utils.relative_url_root = relative_url_root
157 end
157 end
158
158
159 def test_email_headers
159 def test_email_headers
160 issue = Issue.find(1)
160 issue = Issue.find(1)
161 Mailer.issue_add(issue).deliver
161 Mailer.issue_add(issue).deliver
162 mail = last_email
162 mail = last_email
163 assert_not_nil mail
163 assert_not_nil mail
164 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
164 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
165 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
165 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
166 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
166 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
167 end
167 end
168
168
169 def test_email_headers_should_include_sender
169 def test_email_headers_should_include_sender
170 issue = Issue.find(1)
170 issue = Issue.find(1)
171 Mailer.issue_add(issue).deliver
171 Mailer.issue_add(issue).deliver
172 mail = last_email
172 mail = last_email
173 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
173 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
174 end
174 end
175
175
176 def test_plain_text_mail
176 def test_plain_text_mail
177 Setting.plain_text_mail = 1
177 Setting.plain_text_mail = 1
178 journal = Journal.find(2)
178 journal = Journal.find(2)
179 Mailer.issue_edit(journal).deliver
179 Mailer.issue_edit(journal).deliver
180 mail = last_email
180 mail = last_email
181 assert_equal "text/plain; charset=UTF-8", mail.content_type
181 assert_equal "text/plain; charset=UTF-8", mail.content_type
182 assert_equal 0, mail.parts.size
182 assert_equal 0, mail.parts.size
183 assert !mail.encoded.include?('href')
183 assert !mail.encoded.include?('href')
184 end
184 end
185
185
186 def test_html_mail
186 def test_html_mail
187 Setting.plain_text_mail = 0
187 Setting.plain_text_mail = 0
188 journal = Journal.find(2)
188 journal = Journal.find(2)
189 Mailer.issue_edit(journal).deliver
189 Mailer.issue_edit(journal).deliver
190 mail = last_email
190 mail = last_email
191 assert_equal 2, mail.parts.size
191 assert_equal 2, mail.parts.size
192 assert mail.encoded.include?('href')
192 assert mail.encoded.include?('href')
193 end
193 end
194
194
195 def test_from_header
195 def test_from_header
196 with_settings :mail_from => 'redmine@example.net' do
196 with_settings :mail_from => 'redmine@example.net' do
197 Mailer.test_email(User.find(1)).deliver
197 Mailer.test_email(User.find(1)).deliver
198 end
198 end
199 mail = last_email
199 mail = last_email
200 assert_equal 'redmine@example.net', mail.from_addrs.first
200 assert_equal 'redmine@example.net', mail.from_addrs.first
201 end
201 end
202
202
203 def test_from_header_with_phrase
203 def test_from_header_with_phrase
204 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
204 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
205 Mailer.test_email(User.find(1)).deliver
205 Mailer.test_email(User.find(1)).deliver
206 end
206 end
207 mail = last_email
207 mail = last_email
208 assert_equal 'redmine@example.net', mail.from_addrs.first
208 assert_equal 'redmine@example.net', mail.from_addrs.first
209 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
209 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
210 end
210 end
211
211
212 def test_should_not_send_email_without_recipient
212 def test_should_not_send_email_without_recipient
213 news = News.find(:first)
213 news = News.find(:first)
214 user = news.author
214 user = news.author
215 # Remove members except news author
215 # Remove members except news author
216 news.project.memberships.each {|m| m.destroy unless m.user == user}
216 news.project.memberships.each {|m| m.destroy unless m.user == user}
217
217
218 user.pref[:no_self_notified] = false
218 user.pref[:no_self_notified] = false
219 user.pref.save
219 user.pref.save
220 User.current = user
220 User.current = user
221 Mailer.news_added(news.reload).deliver
221 Mailer.news_added(news.reload).deliver
222 assert_equal 1, last_email.bcc.size
222 assert_equal 1, last_email.bcc.size
223
223
224 # nobody to notify
224 # nobody to notify
225 user.pref[:no_self_notified] = true
225 user.pref[:no_self_notified] = true
226 user.pref.save
226 user.pref.save
227 User.current = user
227 User.current = user
228 ActionMailer::Base.deliveries.clear
228 ActionMailer::Base.deliveries.clear
229 Mailer.news_added(news.reload).deliver
229 Mailer.news_added(news.reload).deliver
230 assert ActionMailer::Base.deliveries.empty?
230 assert ActionMailer::Base.deliveries.empty?
231 end
231 end
232
232
233 def test_issue_add_message_id
233 def test_issue_add_message_id
234 issue = Issue.find(1)
234 issue = Issue.find(1)
235 Mailer.issue_add(issue).deliver
235 Mailer.issue_add(issue).deliver
236 mail = last_email
236 mail = last_email
237 assert_equal Mailer.message_id_for(issue), mail.message_id
237 assert_equal Mailer.message_id_for(issue), mail.message_id
238 assert_nil mail.references
238 assert_nil mail.references
239 end
239 end
240
240
241 def test_issue_edit_message_id
241 def test_issue_edit_message_id
242 journal = Journal.find(1)
242 journal = Journal.find(1)
243 Mailer.issue_edit(journal).deliver
243 Mailer.issue_edit(journal).deliver
244 mail = last_email
244 mail = last_email
245 assert_equal Mailer.message_id_for(journal), mail.message_id
245 assert_equal Mailer.message_id_for(journal), mail.message_id
246 assert_include Mailer.message_id_for(journal.issue), mail.references
246 assert_include Mailer.message_id_for(journal.issue), mail.references
247 assert_select_email do
247 assert_select_email do
248 # link to the update
248 # link to the update
249 assert_select "a[href=?]",
249 assert_select "a[href=?]",
250 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
250 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
251 end
251 end
252 end
252 end
253
253
254 def test_message_posted_message_id
254 def test_message_posted_message_id
255 message = Message.find(1)
255 message = Message.find(1)
256 Mailer.message_posted(message).deliver
256 Mailer.message_posted(message).deliver
257 mail = last_email
257 mail = last_email
258 assert_equal Mailer.message_id_for(message), mail.message_id
258 assert_equal Mailer.message_id_for(message), mail.message_id
259 assert_nil mail.references
259 assert_nil mail.references
260 assert_select_email do
260 assert_select_email do
261 # link to the message
261 # link to the message
262 assert_select "a[href=?]",
262 assert_select "a[href=?]",
263 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
263 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
264 :text => message.subject
264 :text => message.subject
265 end
265 end
266 end
266 end
267
267
268 def test_reply_posted_message_id
268 def test_reply_posted_message_id
269 message = Message.find(3)
269 message = Message.find(3)
270 Mailer.message_posted(message).deliver
270 Mailer.message_posted(message).deliver
271 mail = last_email
271 mail = last_email
272 assert_equal Mailer.message_id_for(message), mail.message_id
272 assert_equal Mailer.message_id_for(message), mail.message_id
273 assert_include Mailer.message_id_for(message.parent), mail.references
273 assert_include Mailer.message_id_for(message.parent), mail.references
274 assert_select_email do
274 assert_select_email do
275 # link to the reply
275 # link to the reply
276 assert_select "a[href=?]",
276 assert_select "a[href=?]",
277 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
277 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
278 :text => message.subject
278 :text => message.subject
279 end
279 end
280 end
280 end
281
281
282 context("#issue_add") do
282 context("#issue_add") do
283 setup do
283 setup do
284 ActionMailer::Base.deliveries.clear
284 ActionMailer::Base.deliveries.clear
285 Setting.bcc_recipients = '1'
285 Setting.bcc_recipients = '1'
286 @issue = Issue.find(1)
286 @issue = Issue.find(1)
287 end
287 end
288
288
289 should "notify project members" do
289 should "notify project members" do
290 assert Mailer.issue_add(@issue).deliver
290 assert Mailer.issue_add(@issue).deliver
291 assert last_email.bcc.include?('dlopper@somenet.foo')
291 assert last_email.bcc.include?('dlopper@somenet.foo')
292 end
292 end
293
293
294 should "not notify project members that are not allow to view the issue" do
294 should "not notify project members that are not allow to view the issue" do
295 Role.find(2).remove_permission!(:view_issues)
295 Role.find(2).remove_permission!(:view_issues)
296 assert Mailer.issue_add(@issue).deliver
296 assert Mailer.issue_add(@issue).deliver
297 assert !last_email.bcc.include?('dlopper@somenet.foo')
297 assert !last_email.bcc.include?('dlopper@somenet.foo')
298 end
298 end
299
299
300 should "notify issue watchers" do
300 should "notify issue watchers" do
301 user = User.find(9)
301 user = User.find(9)
302 # minimal email notification options
302 # minimal email notification options
303 user.pref[:no_self_notified] = '1'
303 user.pref[:no_self_notified] = '1'
304 user.pref.save
304 user.pref.save
305 user.mail_notification = false
305 user.mail_notification = false
306 user.save
306 user.save
307
307
308 Watcher.create!(:watchable => @issue, :user => user)
308 Watcher.create!(:watchable => @issue, :user => user)
309 assert Mailer.issue_add(@issue).deliver
309 assert Mailer.issue_add(@issue).deliver
310 assert last_email.bcc.include?(user.mail)
310 assert last_email.bcc.include?(user.mail)
311 end
311 end
312
312
313 should "not notify watchers not allowed to view the issue" do
313 should "not notify watchers not allowed to view the issue" do
314 user = User.find(9)
314 user = User.find(9)
315 Watcher.create!(:watchable => @issue, :user => user)
315 Watcher.create!(:watchable => @issue, :user => user)
316 Role.non_member.remove_permission!(:view_issues)
316 Role.non_member.remove_permission!(:view_issues)
317 assert Mailer.issue_add(@issue).deliver
317 assert Mailer.issue_add(@issue).deliver
318 assert !last_email.bcc.include?(user.mail)
318 assert !last_email.bcc.include?(user.mail)
319 end
319 end
320 end
320 end
321
321
322 # test mailer methods for each language
322 # test mailer methods for each language
323 def test_issue_add
323 def test_issue_add
324 issue = Issue.find(1)
324 issue = Issue.find(1)
325 valid_languages.each do |lang|
325 valid_languages.each do |lang|
326 Setting.default_language = lang.to_s
326 Setting.default_language = lang.to_s
327 assert Mailer.issue_add(issue).deliver
327 assert Mailer.issue_add(issue).deliver
328 end
328 end
329 end
329 end
330
330
331 def test_issue_edit
331 def test_issue_edit
332 journal = Journal.find(1)
332 journal = Journal.find(1)
333 valid_languages.each do |lang|
333 valid_languages.each do |lang|
334 Setting.default_language = lang.to_s
334 Setting.default_language = lang.to_s
335 assert Mailer.issue_edit(journal).deliver
335 assert Mailer.issue_edit(journal).deliver
336 end
336 end
337 end
337 end
338
338
339 def test_document_added
339 def test_document_added
340 document = Document.find(1)
340 document = Document.find(1)
341 valid_languages.each do |lang|
341 valid_languages.each do |lang|
342 Setting.default_language = lang.to_s
342 Setting.default_language = lang.to_s
343 assert Mailer.document_added(document).deliver
343 assert Mailer.document_added(document).deliver
344 end
344 end
345 end
345 end
346
346
347 def test_attachments_added
347 def test_attachments_added
348 attachements = [ Attachment.find_by_container_type('Document') ]
348 attachements = [ Attachment.find_by_container_type('Document') ]
349 valid_languages.each do |lang|
349 valid_languages.each do |lang|
350 Setting.default_language = lang.to_s
350 Setting.default_language = lang.to_s
351 assert Mailer.attachments_added(attachements).deliver
351 assert Mailer.attachments_added(attachements).deliver
352 end
352 end
353 end
353 end
354
354
355 def test_version_file_added
355 def test_version_file_added
356 attachements = [ Attachment.find_by_container_type('Version') ]
356 attachements = [ Attachment.find_by_container_type('Version') ]
357 assert Mailer.attachments_added(attachements).deliver
357 assert Mailer.attachments_added(attachements).deliver
358 assert_not_nil last_email.bcc
358 assert_not_nil last_email.bcc
359 assert last_email.bcc.any?
359 assert last_email.bcc.any?
360 assert_select_email do
360 assert_select_email do
361 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
361 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
362 end
362 end
363 end
363 end
364
364
365 def test_project_file_added
365 def test_project_file_added
366 attachements = [ Attachment.find_by_container_type('Project') ]
366 attachements = [ Attachment.find_by_container_type('Project') ]
367 assert Mailer.attachments_added(attachements).deliver
367 assert Mailer.attachments_added(attachements).deliver
368 assert_not_nil last_email.bcc
368 assert_not_nil last_email.bcc
369 assert last_email.bcc.any?
369 assert last_email.bcc.any?
370 assert_select_email do
370 assert_select_email do
371 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
371 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
372 end
372 end
373 end
373 end
374
374
375 def test_news_added
375 def test_news_added
376 news = News.find(:first)
376 news = News.find(:first)
377 valid_languages.each do |lang|
377 valid_languages.each do |lang|
378 Setting.default_language = lang.to_s
378 Setting.default_language = lang.to_s
379 assert Mailer.news_added(news).deliver
379 assert Mailer.news_added(news).deliver
380 end
380 end
381 end
381 end
382
382
383 def test_news_comment_added
383 def test_news_comment_added
384 comment = Comment.find(2)
384 comment = Comment.find(2)
385 valid_languages.each do |lang|
385 valid_languages.each do |lang|
386 Setting.default_language = lang.to_s
386 Setting.default_language = lang.to_s
387 assert Mailer.news_comment_added(comment).deliver
387 assert Mailer.news_comment_added(comment).deliver
388 end
388 end
389 end
389 end
390
390
391 def test_message_posted
391 def test_message_posted
392 message = Message.find(:first)
392 message = Message.find(:first)
393 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
393 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
394 recipients = recipients.compact.uniq
394 recipients = recipients.compact.uniq
395 valid_languages.each do |lang|
395 valid_languages.each do |lang|
396 Setting.default_language = lang.to_s
396 Setting.default_language = lang.to_s
397 assert Mailer.message_posted(message).deliver
397 assert Mailer.message_posted(message).deliver
398 end
398 end
399 end
399 end
400
400
401 def test_wiki_content_added
401 def test_wiki_content_added
402 content = WikiContent.find(1)
402 content = WikiContent.find(1)
403 valid_languages.each do |lang|
403 valid_languages.each do |lang|
404 Setting.default_language = lang.to_s
404 Setting.default_language = lang.to_s
405 assert_difference 'ActionMailer::Base.deliveries.size' do
405 assert_difference 'ActionMailer::Base.deliveries.size' do
406 assert Mailer.wiki_content_added(content).deliver
406 assert Mailer.wiki_content_added(content).deliver
407 assert_select_email do
407 assert_select_email do
408 assert_select 'a[href=?]',
408 assert_select 'a[href=?]',
409 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
409 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
410 :text => 'CookBook documentation'
410 :text => 'CookBook documentation'
411 end
411 end
412 end
412 end
413 end
413 end
414 end
414 end
415
415
416 def test_wiki_content_updated
416 def test_wiki_content_updated
417 content = WikiContent.find(1)
417 content = WikiContent.find(1)
418 valid_languages.each do |lang|
418 valid_languages.each do |lang|
419 Setting.default_language = lang.to_s
419 Setting.default_language = lang.to_s
420 assert_difference 'ActionMailer::Base.deliveries.size' do
420 assert_difference 'ActionMailer::Base.deliveries.size' do
421 assert Mailer.wiki_content_updated(content).deliver
421 assert Mailer.wiki_content_updated(content).deliver
422 assert_select_email do
422 assert_select_email do
423 assert_select 'a[href=?]',
423 assert_select 'a[href=?]',
424 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
424 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
425 :text => 'CookBook documentation'
425 :text => 'CookBook documentation'
426 end
426 end
427 end
427 end
428 end
428 end
429 end
429 end
430
430
431 def test_account_information
431 def test_account_information
432 user = User.find(2)
432 user = User.find(2)
433 valid_languages.each do |lang|
433 valid_languages.each do |lang|
434 user.update_attribute :language, lang.to_s
434 user.update_attribute :language, lang.to_s
435 user.reload
435 user.reload
436 assert Mailer.account_information(user, 'pAsswORd').deliver
436 assert Mailer.account_information(user, 'pAsswORd').deliver
437 end
437 end
438 end
438 end
439
439
440 def test_lost_password
440 def test_lost_password
441 token = Token.find(2)
441 token = Token.find(2)
442 valid_languages.each do |lang|
442 valid_languages.each do |lang|
443 token.user.update_attribute :language, lang.to_s
443 token.user.update_attribute :language, lang.to_s
444 token.reload
444 token.reload
445 assert Mailer.lost_password(token).deliver
445 assert Mailer.lost_password(token).deliver
446 end
446 end
447 end
447 end
448
448
449 def test_register
449 def test_register
450 token = Token.find(1)
450 token = Token.find(1)
451 Setting.host_name = 'redmine.foo'
451 Setting.host_name = 'redmine.foo'
452 Setting.protocol = 'https'
452 Setting.protocol = 'https'
453
453
454 valid_languages.each do |lang|
454 valid_languages.each do |lang|
455 token.user.update_attribute :language, lang.to_s
455 token.user.update_attribute :language, lang.to_s
456 token.reload
456 token.reload
457 ActionMailer::Base.deliveries.clear
457 ActionMailer::Base.deliveries.clear
458 assert Mailer.register(token).deliver
458 assert Mailer.register(token).deliver
459 mail = last_email
459 mail = last_email
460 assert_select_email do
460 assert_select_email do
461 assert_select "a[href=?]",
461 assert_select "a[href=?]",
462 "https://redmine.foo/account/activate?token=#{token.value}",
462 "https://redmine.foo/account/activate?token=#{token.value}",
463 :text => "https://redmine.foo/account/activate?token=#{token.value}"
463 :text => "https://redmine.foo/account/activate?token=#{token.value}"
464 end
464 end
465 end
465 end
466 end
466 end
467
467
468 def test_test
468 def test_test
469 user = User.find(1)
469 user = User.find(1)
470 valid_languages.each do |lang|
470 valid_languages.each do |lang|
471 user.update_attribute :language, lang.to_s
471 user.update_attribute :language, lang.to_s
472 assert Mailer.test_email(user).deliver
472 assert Mailer.test_email(user).deliver
473 end
473 end
474 end
474 end
475
475
476 def test_reminders
476 def test_reminders
477 Mailer.reminders(:days => 42)
477 Mailer.reminders(:days => 42)
478 assert_equal 1, ActionMailer::Base.deliveries.size
478 assert_equal 1, ActionMailer::Base.deliveries.size
479 mail = last_email
479 mail = last_email
480 assert mail.bcc.include?('dlopper@somenet.foo')
480 assert mail.bcc.include?('dlopper@somenet.foo')
481 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
481 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
482 assert_equal '1 issue(s) due in the next 42 days', mail.subject
482 assert_equal '1 issue(s) due in the next 42 days', mail.subject
483 end
483 end
484
484
485 def test_reminders_should_not_include_closed_issues
485 def test_reminders_should_not_include_closed_issues
486 with_settings :default_language => 'en' do
486 with_settings :default_language => 'en' do
487 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
487 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
488 :subject => 'Closed issue', :assigned_to_id => 3,
488 :subject => 'Closed issue', :assigned_to_id => 3,
489 :due_date => 5.days.from_now,
489 :due_date => 5.days.from_now,
490 :author_id => 2)
490 :author_id => 2)
491 ActionMailer::Base.deliveries.clear
491 ActionMailer::Base.deliveries.clear
492
492
493 Mailer.reminders(:days => 42)
493 Mailer.reminders(:days => 42)
494 assert_equal 1, ActionMailer::Base.deliveries.size
494 assert_equal 1, ActionMailer::Base.deliveries.size
495 mail = last_email
495 mail = last_email
496 assert mail.bcc.include?('dlopper@somenet.foo')
496 assert mail.bcc.include?('dlopper@somenet.foo')
497 assert_mail_body_no_match 'Closed issue', mail
497 assert_mail_body_no_match 'Closed issue', mail
498 end
498 end
499 end
499 end
500
500
501 def test_reminders_for_users
501 def test_reminders_for_users
502 Mailer.reminders(:days => 42, :users => ['5'])
502 Mailer.reminders(:days => 42, :users => ['5'])
503 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
503 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
504 Mailer.reminders(:days => 42, :users => ['3'])
504 Mailer.reminders(:days => 42, :users => ['3'])
505 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
505 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
506 mail = last_email
506 mail = last_email
507 assert mail.bcc.include?('dlopper@somenet.foo')
507 assert mail.bcc.include?('dlopper@somenet.foo')
508 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
508 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
509 end
509 end
510
510
511 def test_mailer_should_not_change_locale
511 def test_mailer_should_not_change_locale
512 Setting.default_language = 'en'
512 Setting.default_language = 'en'
513 # Set current language to italian
513 # Set current language to italian
514 set_language_if_valid 'it'
514 set_language_if_valid 'it'
515 # Send an email to a french user
515 # Send an email to a french user
516 user = User.find(1)
516 user = User.find(1)
517 user.language = 'fr'
517 user.language = 'fr'
518 Mailer.account_activated(user).deliver
518 Mailer.account_activated(user).deliver
519 mail = last_email
519 mail = last_email
520 assert_mail_body_match 'Votre compte', mail
520 assert_mail_body_match 'Votre compte', mail
521
521
522 assert_equal :it, current_language
522 assert_equal :it, current_language
523 end
523 end
524
524
525 def test_with_deliveries_off
525 def test_with_deliveries_off
526 Mailer.with_deliveries false do
526 Mailer.with_deliveries false do
527 Mailer.test_email(User.find(1)).deliver
527 Mailer.test_email(User.find(1)).deliver
528 end
528 end
529 assert ActionMailer::Base.deliveries.empty?
529 assert ActionMailer::Base.deliveries.empty?
530 # should restore perform_deliveries
530 # should restore perform_deliveries
531 assert ActionMailer::Base.perform_deliveries
531 assert ActionMailer::Base.perform_deliveries
532 end
532 end
533
533
534 def test_layout_should_include_the_emails_header
534 def test_layout_should_include_the_emails_header
535 with_settings :emails_header => "*Header content*" do
535 with_settings :emails_header => "*Header content*" do
536 assert Mailer.test_email(User.find(1)).deliver
536 assert Mailer.test_email(User.find(1)).deliver
537 assert_select_email do
537 assert_select_email do
538 assert_select ".header" do
538 assert_select ".header" do
539 assert_select "strong", :text => "Header content"
539 assert_select "strong", :text => "Header content"
540 end
540 end
541 end
541 end
542 end
542 end
543 end
543 end
544
544
545 private
545 def test_should_escape_html_templates_only
546 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
547 mail = last_email
548 assert_equal 2, mail.parts.size
549 assert_include '<tag>', text_part.body.encoded
550 assert_include '&lt;tag&gt;', html_part.body.encoded
551 end
552
553 private
554
546 def last_email
555 def last_email
547 mail = ActionMailer::Base.deliveries.last
556 mail = ActionMailer::Base.deliveries.last
548 assert_not_nil mail
557 assert_not_nil mail
549 mail
558 mail
550 end
559 end
560
561 def text_part
562 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
563 end
564
565 def html_part
566 last_email.parts.detect {|part| part.content_type.include?('text/html')}
567 end
551 end
568 end
General Comments 0
You need to be logged in to leave comments. Login now