##// END OF EJS Templates
Adds a test for emission address with phrase (#4855)....
Jean-Philippe Lang -
r3329:b733accfe336
parent child
Show More
@@ -1,319 +1,329
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class MailerTest < ActiveSupport::TestCase
20 class MailerTest < ActiveSupport::TestCase
21 include Redmine::I18n
21 include Redmine::I18n
22 include ActionController::Assertions::SelectorAssertions
22 include ActionController::Assertions::SelectorAssertions
23 fixtures :projects, :enabled_modules, :issues, :users, :members, :member_roles, :roles, :documents, :attachments, :news, :tokens, :journals, :journal_details, :changesets, :trackers, :issue_statuses, :enumerations, :messages, :boards, :repositories
23 fixtures :projects, :enabled_modules, :issues, :users, :members, :member_roles, :roles, :documents, :attachments, :news, :tokens, :journals, :journal_details, :changesets, :trackers, :issue_statuses, :enumerations, :messages, :boards, :repositories
24
24
25 def test_generated_links_in_emails
25 def test_generated_links_in_emails
26 ActionMailer::Base.deliveries.clear
26 ActionMailer::Base.deliveries.clear
27 Setting.host_name = 'mydomain.foo'
27 Setting.host_name = 'mydomain.foo'
28 Setting.protocol = 'https'
28 Setting.protocol = 'https'
29
29
30 journal = Journal.find(2)
30 journal = Journal.find(2)
31 assert Mailer.deliver_issue_edit(journal)
31 assert Mailer.deliver_issue_edit(journal)
32
32
33 mail = ActionMailer::Base.deliveries.last
33 mail = ActionMailer::Base.deliveries.last
34 assert_kind_of TMail::Mail, mail
34 assert_kind_of TMail::Mail, mail
35
35
36 assert_select_email do
36 assert_select_email do
37 # link to the main ticket
37 # link to the main ticket
38 assert_select "a[href=?]", "https://mydomain.foo/issues/1", :text => "Bug #1: Can't print recipes"
38 assert_select "a[href=?]", "https://mydomain.foo/issues/1", :text => "Bug #1: Can't print recipes"
39 # link to a referenced ticket
39 # link to a referenced ticket
40 assert_select "a[href=?][title=?]", "https://mydomain.foo/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
40 assert_select "a[href=?][title=?]", "https://mydomain.foo/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
41 # link to a changeset
41 # link to a changeset
42 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"
42 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"
43 end
43 end
44 end
44 end
45
45
46 def test_generated_links_with_prefix
46 def test_generated_links_with_prefix
47 relative_url_root = Redmine::Utils.relative_url_root
47 relative_url_root = Redmine::Utils.relative_url_root
48 ActionMailer::Base.deliveries.clear
48 ActionMailer::Base.deliveries.clear
49 Setting.host_name = 'mydomain.foo/rdm'
49 Setting.host_name = 'mydomain.foo/rdm'
50 Setting.protocol = 'http'
50 Setting.protocol = 'http'
51 Redmine::Utils.relative_url_root = '/rdm'
51 Redmine::Utils.relative_url_root = '/rdm'
52
52
53 journal = Journal.find(2)
53 journal = Journal.find(2)
54 assert Mailer.deliver_issue_edit(journal)
54 assert Mailer.deliver_issue_edit(journal)
55
55
56 mail = ActionMailer::Base.deliveries.last
56 mail = ActionMailer::Base.deliveries.last
57 assert_kind_of TMail::Mail, mail
57 assert_kind_of TMail::Mail, mail
58
58
59 assert_select_email do
59 assert_select_email do
60 # link to the main ticket
60 # link to the main ticket
61 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
61 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
62 # link to a referenced ticket
62 # link to a referenced ticket
63 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
63 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
64 # link to a changeset
64 # link to a changeset
65 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"
65 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"
66 end
66 end
67 ensure
67 ensure
68 # restore it
68 # restore it
69 Redmine::Utils.relative_url_root = relative_url_root
69 Redmine::Utils.relative_url_root = relative_url_root
70 end
70 end
71
71
72 def test_generated_links_with_prefix_and_no_relative_url_root
72 def test_generated_links_with_prefix_and_no_relative_url_root
73 relative_url_root = Redmine::Utils.relative_url_root
73 relative_url_root = Redmine::Utils.relative_url_root
74 ActionMailer::Base.deliveries.clear
74 ActionMailer::Base.deliveries.clear
75 Setting.host_name = 'mydomain.foo/rdm'
75 Setting.host_name = 'mydomain.foo/rdm'
76 Setting.protocol = 'http'
76 Setting.protocol = 'http'
77 Redmine::Utils.relative_url_root = nil
77 Redmine::Utils.relative_url_root = nil
78
78
79 journal = Journal.find(2)
79 journal = Journal.find(2)
80 assert Mailer.deliver_issue_edit(journal)
80 assert Mailer.deliver_issue_edit(journal)
81
81
82 mail = ActionMailer::Base.deliveries.last
82 mail = ActionMailer::Base.deliveries.last
83 assert_kind_of TMail::Mail, mail
83 assert_kind_of TMail::Mail, mail
84
84
85 assert_select_email do
85 assert_select_email do
86 # link to the main ticket
86 # link to the main ticket
87 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
87 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
88 # link to a referenced ticket
88 # link to a referenced ticket
89 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
89 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
90 # link to a changeset
90 # link to a changeset
91 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"
91 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"
92 end
92 end
93 ensure
93 ensure
94 # restore it
94 # restore it
95 Redmine::Utils.relative_url_root = relative_url_root
95 Redmine::Utils.relative_url_root = relative_url_root
96 end
96 end
97
97
98 def test_email_headers
98 def test_email_headers
99 ActionMailer::Base.deliveries.clear
99 ActionMailer::Base.deliveries.clear
100 issue = Issue.find(1)
100 issue = Issue.find(1)
101 Mailer.deliver_issue_add(issue)
101 Mailer.deliver_issue_add(issue)
102 mail = ActionMailer::Base.deliveries.last
102 mail = ActionMailer::Base.deliveries.last
103 assert_not_nil mail
103 assert_not_nil mail
104 assert_equal 'bulk', mail.header_string('Precedence')
104 assert_equal 'bulk', mail.header_string('Precedence')
105 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
105 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
106 end
106 end
107
107
108 def test_plain_text_mail
108 def test_plain_text_mail
109 Setting.plain_text_mail = 1
109 Setting.plain_text_mail = 1
110 journal = Journal.find(2)
110 journal = Journal.find(2)
111 Mailer.deliver_issue_edit(journal)
111 Mailer.deliver_issue_edit(journal)
112 mail = ActionMailer::Base.deliveries.last
112 mail = ActionMailer::Base.deliveries.last
113 assert_equal "text/plain", mail.content_type
113 assert_equal "text/plain", mail.content_type
114 assert_equal 0, mail.parts.size
114 assert_equal 0, mail.parts.size
115 assert !mail.encoded.include?('href')
115 assert !mail.encoded.include?('href')
116 end
116 end
117
117
118 def test_html_mail
118 def test_html_mail
119 Setting.plain_text_mail = 0
119 Setting.plain_text_mail = 0
120 journal = Journal.find(2)
120 journal = Journal.find(2)
121 Mailer.deliver_issue_edit(journal)
121 Mailer.deliver_issue_edit(journal)
122 mail = ActionMailer::Base.deliveries.last
122 mail = ActionMailer::Base.deliveries.last
123 assert_equal 2, mail.parts.size
123 assert_equal 2, mail.parts.size
124 assert mail.encoded.include?('href')
124 assert mail.encoded.include?('href')
125 end
125 end
126
127 def test_mail_from_with_phrase
128 ActionMailer::Base.deliveries.clear
129 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
130 Mailer.deliver_test(User.find(1))
131 end
132 mail = ActionMailer::Base.deliveries.last
133 assert_not_nil mail
134 assert_equal 'Redmine app', mail.from_addrs.first.name
135 end
126
136
127 def test_issue_add_message_id
137 def test_issue_add_message_id
128 ActionMailer::Base.deliveries.clear
138 ActionMailer::Base.deliveries.clear
129 issue = Issue.find(1)
139 issue = Issue.find(1)
130 Mailer.deliver_issue_add(issue)
140 Mailer.deliver_issue_add(issue)
131 mail = ActionMailer::Base.deliveries.last
141 mail = ActionMailer::Base.deliveries.last
132 assert_not_nil mail
142 assert_not_nil mail
133 assert_equal Mailer.message_id_for(issue), mail.message_id
143 assert_equal Mailer.message_id_for(issue), mail.message_id
134 assert_nil mail.references
144 assert_nil mail.references
135 end
145 end
136
146
137 def test_issue_edit_message_id
147 def test_issue_edit_message_id
138 ActionMailer::Base.deliveries.clear
148 ActionMailer::Base.deliveries.clear
139 journal = Journal.find(1)
149 journal = Journal.find(1)
140 Mailer.deliver_issue_edit(journal)
150 Mailer.deliver_issue_edit(journal)
141 mail = ActionMailer::Base.deliveries.last
151 mail = ActionMailer::Base.deliveries.last
142 assert_not_nil mail
152 assert_not_nil mail
143 assert_equal Mailer.message_id_for(journal), mail.message_id
153 assert_equal Mailer.message_id_for(journal), mail.message_id
144 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
154 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
145 end
155 end
146
156
147 def test_message_posted_message_id
157 def test_message_posted_message_id
148 ActionMailer::Base.deliveries.clear
158 ActionMailer::Base.deliveries.clear
149 message = Message.find(1)
159 message = Message.find(1)
150 Mailer.deliver_message_posted(message)
160 Mailer.deliver_message_posted(message)
151 mail = ActionMailer::Base.deliveries.last
161 mail = ActionMailer::Base.deliveries.last
152 assert_not_nil mail
162 assert_not_nil mail
153 assert_equal Mailer.message_id_for(message), mail.message_id
163 assert_equal Mailer.message_id_for(message), mail.message_id
154 assert_nil mail.references
164 assert_nil mail.references
155 end
165 end
156
166
157 def test_reply_posted_message_id
167 def test_reply_posted_message_id
158 ActionMailer::Base.deliveries.clear
168 ActionMailer::Base.deliveries.clear
159 message = Message.find(3)
169 message = Message.find(3)
160 Mailer.deliver_message_posted(message)
170 Mailer.deliver_message_posted(message)
161 mail = ActionMailer::Base.deliveries.last
171 mail = ActionMailer::Base.deliveries.last
162 assert_not_nil mail
172 assert_not_nil mail
163 assert_equal Mailer.message_id_for(message), mail.message_id
173 assert_equal Mailer.message_id_for(message), mail.message_id
164 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
174 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
165 end
175 end
166
176
167 context("#issue_add") do
177 context("#issue_add") do
168 setup do
178 setup do
169 ActionMailer::Base.deliveries.clear
179 ActionMailer::Base.deliveries.clear
170 Setting.bcc_recipients = '1'
180 Setting.bcc_recipients = '1'
171 @issue = Issue.find(1)
181 @issue = Issue.find(1)
172 end
182 end
173
183
174 should "notify project members" do
184 should "notify project members" do
175 assert Mailer.deliver_issue_add(@issue)
185 assert Mailer.deliver_issue_add(@issue)
176 assert last_email.bcc.include?('dlopper@somenet.foo')
186 assert last_email.bcc.include?('dlopper@somenet.foo')
177 end
187 end
178
188
179 should "not notify project members that are not allow to view the issue" do
189 should "not notify project members that are not allow to view the issue" do
180 Role.find(2).remove_permission!(:view_issues)
190 Role.find(2).remove_permission!(:view_issues)
181 assert Mailer.deliver_issue_add(@issue)
191 assert Mailer.deliver_issue_add(@issue)
182 assert !last_email.bcc.include?('dlopper@somenet.foo')
192 assert !last_email.bcc.include?('dlopper@somenet.foo')
183 end
193 end
184
194
185 should "notify issue watchers" do
195 should "notify issue watchers" do
186 user = User.find(9)
196 user = User.find(9)
187 # minimal email notification options
197 # minimal email notification options
188 user.pref[:no_self_notified] = '1'
198 user.pref[:no_self_notified] = '1'
189 user.pref.save
199 user.pref.save
190 user.mail_notification = false
200 user.mail_notification = false
191 user.save
201 user.save
192
202
193 Watcher.create!(:watchable => @issue, :user => user)
203 Watcher.create!(:watchable => @issue, :user => user)
194 assert Mailer.deliver_issue_add(@issue)
204 assert Mailer.deliver_issue_add(@issue)
195 assert last_email.bcc.include?(user.mail)
205 assert last_email.bcc.include?(user.mail)
196 end
206 end
197
207
198 should "not notify watchers not allowed to view the issue" do
208 should "not notify watchers not allowed to view the issue" do
199 user = User.find(9)
209 user = User.find(9)
200 Watcher.create!(:watchable => @issue, :user => user)
210 Watcher.create!(:watchable => @issue, :user => user)
201 Role.non_member.remove_permission!(:view_issues)
211 Role.non_member.remove_permission!(:view_issues)
202 assert Mailer.deliver_issue_add(@issue)
212 assert Mailer.deliver_issue_add(@issue)
203 assert !last_email.bcc.include?(user.mail)
213 assert !last_email.bcc.include?(user.mail)
204 end
214 end
205 end
215 end
206
216
207 # test mailer methods for each language
217 # test mailer methods for each language
208 def test_issue_add
218 def test_issue_add
209 issue = Issue.find(1)
219 issue = Issue.find(1)
210 valid_languages.each do |lang|
220 valid_languages.each do |lang|
211 Setting.default_language = lang.to_s
221 Setting.default_language = lang.to_s
212 assert Mailer.deliver_issue_add(issue)
222 assert Mailer.deliver_issue_add(issue)
213 end
223 end
214 end
224 end
215
225
216 def test_issue_edit
226 def test_issue_edit
217 journal = Journal.find(1)
227 journal = Journal.find(1)
218 valid_languages.each do |lang|
228 valid_languages.each do |lang|
219 Setting.default_language = lang.to_s
229 Setting.default_language = lang.to_s
220 assert Mailer.deliver_issue_edit(journal)
230 assert Mailer.deliver_issue_edit(journal)
221 end
231 end
222 end
232 end
223
233
224 def test_document_added
234 def test_document_added
225 document = Document.find(1)
235 document = Document.find(1)
226 valid_languages.each do |lang|
236 valid_languages.each do |lang|
227 Setting.default_language = lang.to_s
237 Setting.default_language = lang.to_s
228 assert Mailer.deliver_document_added(document)
238 assert Mailer.deliver_document_added(document)
229 end
239 end
230 end
240 end
231
241
232 def test_attachments_added
242 def test_attachments_added
233 attachements = [ Attachment.find_by_container_type('Document') ]
243 attachements = [ Attachment.find_by_container_type('Document') ]
234 valid_languages.each do |lang|
244 valid_languages.each do |lang|
235 Setting.default_language = lang.to_s
245 Setting.default_language = lang.to_s
236 assert Mailer.deliver_attachments_added(attachements)
246 assert Mailer.deliver_attachments_added(attachements)
237 end
247 end
238 end
248 end
239
249
240 def test_news_added
250 def test_news_added
241 news = News.find(:first)
251 news = News.find(:first)
242 valid_languages.each do |lang|
252 valid_languages.each do |lang|
243 Setting.default_language = lang.to_s
253 Setting.default_language = lang.to_s
244 assert Mailer.deliver_news_added(news)
254 assert Mailer.deliver_news_added(news)
245 end
255 end
246 end
256 end
247
257
248 def test_message_posted
258 def test_message_posted
249 message = Message.find(:first)
259 message = Message.find(:first)
250 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
260 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
251 recipients = recipients.compact.uniq
261 recipients = recipients.compact.uniq
252 valid_languages.each do |lang|
262 valid_languages.each do |lang|
253 Setting.default_language = lang.to_s
263 Setting.default_language = lang.to_s
254 assert Mailer.deliver_message_posted(message)
264 assert Mailer.deliver_message_posted(message)
255 end
265 end
256 end
266 end
257
267
258 def test_account_information
268 def test_account_information
259 user = User.find(:first)
269 user = User.find(:first)
260 valid_languages.each do |lang|
270 valid_languages.each do |lang|
261 user.update_attribute :language, lang.to_s
271 user.update_attribute :language, lang.to_s
262 user.reload
272 user.reload
263 assert Mailer.deliver_account_information(user, 'pAsswORd')
273 assert Mailer.deliver_account_information(user, 'pAsswORd')
264 end
274 end
265 end
275 end
266
276
267 def test_lost_password
277 def test_lost_password
268 token = Token.find(2)
278 token = Token.find(2)
269 valid_languages.each do |lang|
279 valid_languages.each do |lang|
270 token.user.update_attribute :language, lang.to_s
280 token.user.update_attribute :language, lang.to_s
271 token.reload
281 token.reload
272 assert Mailer.deliver_lost_password(token)
282 assert Mailer.deliver_lost_password(token)
273 end
283 end
274 end
284 end
275
285
276 def test_register
286 def test_register
277 token = Token.find(1)
287 token = Token.find(1)
278 Setting.host_name = 'redmine.foo'
288 Setting.host_name = 'redmine.foo'
279 Setting.protocol = 'https'
289 Setting.protocol = 'https'
280
290
281 valid_languages.each do |lang|
291 valid_languages.each do |lang|
282 token.user.update_attribute :language, lang.to_s
292 token.user.update_attribute :language, lang.to_s
283 token.reload
293 token.reload
284 ActionMailer::Base.deliveries.clear
294 ActionMailer::Base.deliveries.clear
285 assert Mailer.deliver_register(token)
295 assert Mailer.deliver_register(token)
286 mail = ActionMailer::Base.deliveries.last
296 mail = ActionMailer::Base.deliveries.last
287 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
297 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
288 end
298 end
289 end
299 end
290
300
291 def test_reminders
301 def test_reminders
292 ActionMailer::Base.deliveries.clear
302 ActionMailer::Base.deliveries.clear
293 Mailer.reminders(:days => 42)
303 Mailer.reminders(:days => 42)
294 assert_equal 1, ActionMailer::Base.deliveries.size
304 assert_equal 1, ActionMailer::Base.deliveries.size
295 mail = ActionMailer::Base.deliveries.last
305 mail = ActionMailer::Base.deliveries.last
296 assert mail.bcc.include?('dlopper@somenet.foo')
306 assert mail.bcc.include?('dlopper@somenet.foo')
297 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
307 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
298 end
308 end
299
309
300 def last_email
310 def last_email
301 mail = ActionMailer::Base.deliveries.last
311 mail = ActionMailer::Base.deliveries.last
302 assert_not_nil mail
312 assert_not_nil mail
303 mail
313 mail
304 end
314 end
305
315
306 def test_mailer_should_not_change_locale
316 def test_mailer_should_not_change_locale
307 Setting.default_language = 'en'
317 Setting.default_language = 'en'
308 # Set current language to italian
318 # Set current language to italian
309 set_language_if_valid 'it'
319 set_language_if_valid 'it'
310 # Send an email to a french user
320 # Send an email to a french user
311 user = User.find(1)
321 user = User.find(1)
312 user.language = 'fr'
322 user.language = 'fr'
313 Mailer.deliver_account_activated(user)
323 Mailer.deliver_account_activated(user)
314 mail = ActionMailer::Base.deliveries.last
324 mail = ActionMailer::Base.deliveries.last
315 assert mail.body.include?('Votre compte')
325 assert mail.body.include?('Votre compte')
316
326
317 assert_equal :it, current_language
327 assert_equal :it, current_language
318 end
328 end
319 end
329 end
General Comments 0
You need to be logged in to leave comments. Login now