##// END OF EJS Templates
Adds unit test for #3645....
Jean-Philippe Lang -
r3213:d55e1ec5b092
parent child
Show More
@@ -1,313 +1,319
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
126
127 def test_issue_add_message_id
127 def test_issue_add_message_id
128 ActionMailer::Base.deliveries.clear
128 ActionMailer::Base.deliveries.clear
129 issue = Issue.find(1)
129 issue = Issue.find(1)
130 Mailer.deliver_issue_add(issue)
130 Mailer.deliver_issue_add(issue)
131 mail = ActionMailer::Base.deliveries.last
131 mail = ActionMailer::Base.deliveries.last
132 assert_not_nil mail
132 assert_not_nil mail
133 assert_equal Mailer.message_id_for(issue), mail.message_id
133 assert_equal Mailer.message_id_for(issue), mail.message_id
134 assert_nil mail.references
134 assert_nil mail.references
135 end
135 end
136
136
137 def test_issue_edit_message_id
137 def test_issue_edit_message_id
138 ActionMailer::Base.deliveries.clear
138 ActionMailer::Base.deliveries.clear
139 journal = Journal.find(1)
139 journal = Journal.find(1)
140 Mailer.deliver_issue_edit(journal)
140 Mailer.deliver_issue_edit(journal)
141 mail = ActionMailer::Base.deliveries.last
141 mail = ActionMailer::Base.deliveries.last
142 assert_not_nil mail
142 assert_not_nil mail
143 assert_equal Mailer.message_id_for(journal), mail.message_id
143 assert_equal Mailer.message_id_for(journal), mail.message_id
144 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
144 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
145 end
145 end
146
146
147 def test_message_posted_message_id
147 def test_message_posted_message_id
148 ActionMailer::Base.deliveries.clear
148 ActionMailer::Base.deliveries.clear
149 message = Message.find(1)
149 message = Message.find(1)
150 Mailer.deliver_message_posted(message)
150 Mailer.deliver_message_posted(message)
151 mail = ActionMailer::Base.deliveries.last
151 mail = ActionMailer::Base.deliveries.last
152 assert_not_nil mail
152 assert_not_nil mail
153 assert_equal Mailer.message_id_for(message), mail.message_id
153 assert_equal Mailer.message_id_for(message), mail.message_id
154 assert_nil mail.references
154 assert_nil mail.references
155 end
155 end
156
156
157 def test_reply_posted_message_id
157 def test_reply_posted_message_id
158 ActionMailer::Base.deliveries.clear
158 ActionMailer::Base.deliveries.clear
159 message = Message.find(3)
159 message = Message.find(3)
160 Mailer.deliver_message_posted(message)
160 Mailer.deliver_message_posted(message)
161 mail = ActionMailer::Base.deliveries.last
161 mail = ActionMailer::Base.deliveries.last
162 assert_not_nil mail
162 assert_not_nil mail
163 assert_equal Mailer.message_id_for(message), mail.message_id
163 assert_equal Mailer.message_id_for(message), mail.message_id
164 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
164 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
165 end
165 end
166
166
167 context("#issue_add") do
167 context("#issue_add") do
168 setup do
168 setup do
169 ActionMailer::Base.deliveries.clear
169 ActionMailer::Base.deliveries.clear
170 Setting.bcc_recipients = '1'
170 Setting.bcc_recipients = '1'
171 @issue = Issue.find(1)
171 @issue = Issue.find(1)
172 end
172 end
173
173
174 should "notify project members" do
174 should "notify project members" do
175 assert Mailer.deliver_issue_add(@issue)
175 assert Mailer.deliver_issue_add(@issue)
176 assert last_email.bcc.include?('dlopper@somenet.foo')
176 assert last_email.bcc.include?('dlopper@somenet.foo')
177 end
177 end
178
178
179 should "not notify project members that are not allow to view the issue" do
179 should "not notify project members that are not allow to view the issue" do
180 Role.find(2).remove_permission!(:view_issues)
180 Role.find(2).remove_permission!(:view_issues)
181 assert Mailer.deliver_issue_add(@issue)
181 assert Mailer.deliver_issue_add(@issue)
182 assert !last_email.bcc.include?('dlopper@somenet.foo')
182 assert !last_email.bcc.include?('dlopper@somenet.foo')
183 end
183 end
184
184
185 should "notify issue watchers" do
185 should "notify issue watchers" do
186 user = User.find(9)
186 user = User.find(9)
187 # minimal email notification options
188 user.pref[:no_self_notified] = '1'
189 user.pref.save
190 user.mail_notification = false
191 user.save
192
187 Watcher.create!(:watchable => @issue, :user => user)
193 Watcher.create!(:watchable => @issue, :user => user)
188 assert Mailer.deliver_issue_add(@issue)
194 assert Mailer.deliver_issue_add(@issue)
189 assert last_email.bcc.include?(user.mail)
195 assert last_email.bcc.include?(user.mail)
190 end
196 end
191
197
192 should "not notify watchers not allowed to view the issue" do
198 should "not notify watchers not allowed to view the issue" do
193 user = User.find(9)
199 user = User.find(9)
194 Watcher.create!(:watchable => @issue, :user => user)
200 Watcher.create!(:watchable => @issue, :user => user)
195 Role.non_member.remove_permission!(:view_issues)
201 Role.non_member.remove_permission!(:view_issues)
196 assert Mailer.deliver_issue_add(@issue)
202 assert Mailer.deliver_issue_add(@issue)
197 assert !last_email.bcc.include?(user.mail)
203 assert !last_email.bcc.include?(user.mail)
198 end
204 end
199 end
205 end
200
206
201 # test mailer methods for each language
207 # test mailer methods for each language
202 def test_issue_add
208 def test_issue_add
203 issue = Issue.find(1)
209 issue = Issue.find(1)
204 valid_languages.each do |lang|
210 valid_languages.each do |lang|
205 Setting.default_language = lang.to_s
211 Setting.default_language = lang.to_s
206 assert Mailer.deliver_issue_add(issue)
212 assert Mailer.deliver_issue_add(issue)
207 end
213 end
208 end
214 end
209
215
210 def test_issue_edit
216 def test_issue_edit
211 journal = Journal.find(1)
217 journal = Journal.find(1)
212 valid_languages.each do |lang|
218 valid_languages.each do |lang|
213 Setting.default_language = lang.to_s
219 Setting.default_language = lang.to_s
214 assert Mailer.deliver_issue_edit(journal)
220 assert Mailer.deliver_issue_edit(journal)
215 end
221 end
216 end
222 end
217
223
218 def test_document_added
224 def test_document_added
219 document = Document.find(1)
225 document = Document.find(1)
220 valid_languages.each do |lang|
226 valid_languages.each do |lang|
221 Setting.default_language = lang.to_s
227 Setting.default_language = lang.to_s
222 assert Mailer.deliver_document_added(document)
228 assert Mailer.deliver_document_added(document)
223 end
229 end
224 end
230 end
225
231
226 def test_attachments_added
232 def test_attachments_added
227 attachements = [ Attachment.find_by_container_type('Document') ]
233 attachements = [ Attachment.find_by_container_type('Document') ]
228 valid_languages.each do |lang|
234 valid_languages.each do |lang|
229 Setting.default_language = lang.to_s
235 Setting.default_language = lang.to_s
230 assert Mailer.deliver_attachments_added(attachements)
236 assert Mailer.deliver_attachments_added(attachements)
231 end
237 end
232 end
238 end
233
239
234 def test_news_added
240 def test_news_added
235 news = News.find(:first)
241 news = News.find(:first)
236 valid_languages.each do |lang|
242 valid_languages.each do |lang|
237 Setting.default_language = lang.to_s
243 Setting.default_language = lang.to_s
238 assert Mailer.deliver_news_added(news)
244 assert Mailer.deliver_news_added(news)
239 end
245 end
240 end
246 end
241
247
242 def test_message_posted
248 def test_message_posted
243 message = Message.find(:first)
249 message = Message.find(:first)
244 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
250 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
245 recipients = recipients.compact.uniq
251 recipients = recipients.compact.uniq
246 valid_languages.each do |lang|
252 valid_languages.each do |lang|
247 Setting.default_language = lang.to_s
253 Setting.default_language = lang.to_s
248 assert Mailer.deliver_message_posted(message)
254 assert Mailer.deliver_message_posted(message)
249 end
255 end
250 end
256 end
251
257
252 def test_account_information
258 def test_account_information
253 user = User.find(:first)
259 user = User.find(:first)
254 valid_languages.each do |lang|
260 valid_languages.each do |lang|
255 user.update_attribute :language, lang.to_s
261 user.update_attribute :language, lang.to_s
256 user.reload
262 user.reload
257 assert Mailer.deliver_account_information(user, 'pAsswORd')
263 assert Mailer.deliver_account_information(user, 'pAsswORd')
258 end
264 end
259 end
265 end
260
266
261 def test_lost_password
267 def test_lost_password
262 token = Token.find(2)
268 token = Token.find(2)
263 valid_languages.each do |lang|
269 valid_languages.each do |lang|
264 token.user.update_attribute :language, lang.to_s
270 token.user.update_attribute :language, lang.to_s
265 token.reload
271 token.reload
266 assert Mailer.deliver_lost_password(token)
272 assert Mailer.deliver_lost_password(token)
267 end
273 end
268 end
274 end
269
275
270 def test_register
276 def test_register
271 token = Token.find(1)
277 token = Token.find(1)
272 Setting.host_name = 'redmine.foo'
278 Setting.host_name = 'redmine.foo'
273 Setting.protocol = 'https'
279 Setting.protocol = 'https'
274
280
275 valid_languages.each do |lang|
281 valid_languages.each do |lang|
276 token.user.update_attribute :language, lang.to_s
282 token.user.update_attribute :language, lang.to_s
277 token.reload
283 token.reload
278 ActionMailer::Base.deliveries.clear
284 ActionMailer::Base.deliveries.clear
279 assert Mailer.deliver_register(token)
285 assert Mailer.deliver_register(token)
280 mail = ActionMailer::Base.deliveries.last
286 mail = ActionMailer::Base.deliveries.last
281 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
287 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
282 end
288 end
283 end
289 end
284
290
285 def test_reminders
291 def test_reminders
286 ActionMailer::Base.deliveries.clear
292 ActionMailer::Base.deliveries.clear
287 Mailer.reminders(:days => 42)
293 Mailer.reminders(:days => 42)
288 assert_equal 1, ActionMailer::Base.deliveries.size
294 assert_equal 1, ActionMailer::Base.deliveries.size
289 mail = ActionMailer::Base.deliveries.last
295 mail = ActionMailer::Base.deliveries.last
290 assert mail.bcc.include?('dlopper@somenet.foo')
296 assert mail.bcc.include?('dlopper@somenet.foo')
291 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
297 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
292 end
298 end
293
299
294 def last_email
300 def last_email
295 mail = ActionMailer::Base.deliveries.last
301 mail = ActionMailer::Base.deliveries.last
296 assert_not_nil mail
302 assert_not_nil mail
297 mail
303 mail
298 end
304 end
299
305
300 def test_mailer_should_not_change_locale
306 def test_mailer_should_not_change_locale
301 Setting.default_language = 'en'
307 Setting.default_language = 'en'
302 # Set current language to italian
308 # Set current language to italian
303 set_language_if_valid 'it'
309 set_language_if_valid 'it'
304 # Send an email to a french user
310 # Send an email to a french user
305 user = User.find(1)
311 user = User.find(1)
306 user.language = 'fr'
312 user.language = 'fr'
307 Mailer.deliver_account_activated(user)
313 Mailer.deliver_account_activated(user)
308 mail = ActionMailer::Base.deliveries.last
314 mail = ActionMailer::Base.deliveries.last
309 assert mail.body.include?('Votre compte')
315 assert mail.body.include?('Votre compte')
310
316
311 assert_equal :it, current_language
317 assert_equal :it, current_language
312 end
318 end
313 end
319 end
General Comments 0
You need to be logged in to leave comments. Login now