##// END OF EJS Templates
Fixed potential test failure....
Jean-Philippe Lang -
r8908:c0048f79544c
parent child
Show More
@@ -1,549 +1,549
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 ActionController::Assertions::SelectorAssertions
22 include ActionController::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, :trackers,
25 :tokens, :journals, :journal_details, :changesets, :trackers,
26 :issue_statuses, :enumerations, :messages, :boards, :repositories,
26 :issue_statuses, :enumerations, :messages, :boards, :repositories,
27 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
27 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
28 :versions,
28 :versions,
29 :comments
29 :comments
30
30
31 def setup
31 def setup
32 ActionMailer::Base.deliveries.clear
32 ActionMailer::Base.deliveries.clear
33 Setting.host_name = 'mydomain.foo'
33 Setting.host_name = 'mydomain.foo'
34 Setting.protocol = 'http'
34 Setting.protocol = 'http'
35 Setting.plain_text_mail = '0'
35 Setting.plain_text_mail = '0'
36 end
36 end
37
37
38 def test_generated_links_in_emails
38 def test_generated_links_in_emails
39 Setting.default_language = 'en'
39 Setting.default_language = 'en'
40 Setting.host_name = 'mydomain.foo'
40 Setting.host_name = 'mydomain.foo'
41 Setting.protocol = 'https'
41 Setting.protocol = 'https'
42
42
43 journal = Journal.find(3)
43 journal = Journal.find(3)
44 assert Mailer.deliver_issue_edit(journal)
44 assert Mailer.deliver_issue_edit(journal)
45
45
46 mail = last_email
46 mail = last_email
47 assert_not_nil mail
47 assert_not_nil mail
48
48
49 assert_select_email do
49 assert_select_email do
50 # link to the main ticket
50 # link to the main ticket
51 assert_select 'a[href=?]',
51 assert_select 'a[href=?]',
52 'https://mydomain.foo/issues/2#change-3',
52 'https://mydomain.foo/issues/2#change-3',
53 :text => 'Feature request #2: Add ingredients categories'
53 :text => 'Feature request #2: Add ingredients categories'
54 # link to a referenced ticket
54 # link to a referenced ticket
55 assert_select 'a[href=?][title=?]',
55 assert_select 'a[href=?][title=?]',
56 'https://mydomain.foo/issues/1',
56 'https://mydomain.foo/issues/1',
57 'Can\'t print recipes (New)',
57 'Can\'t print recipes (New)',
58 :text => '#1'
58 :text => '#1'
59 # link to a changeset
59 # link to a changeset
60 assert_select 'a[href=?][title=?]',
60 assert_select 'a[href=?][title=?]',
61 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
61 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
62 'This commit fixes #1, #2 and references #1 &amp; #3',
62 'This commit fixes #1, #2 and references #1 &amp; #3',
63 :text => 'r2'
63 :text => 'r2'
64 # link to a description diff
64 # link to a description diff
65 assert_select 'a[href=?][title=?]',
65 assert_select 'a[href=?][title=?]',
66 'https://mydomain.foo/journals/diff/3?detail_id=4',
66 'https://mydomain.foo/journals/diff/3?detail_id=4',
67 'View differences',
67 'View differences',
68 :text => 'diff'
68 :text => 'diff'
69 # link to an attachment
69 # link to an attachment
70 assert_select 'a[href=?]',
70 assert_select 'a[href=?]',
71 'https://mydomain.foo/attachments/download/4/source.rb',
71 'https://mydomain.foo/attachments/download/4/source.rb',
72 :text => 'source.rb'
72 :text => 'source.rb'
73 end
73 end
74 end
74 end
75
75
76 def test_generated_links_with_prefix
76 def test_generated_links_with_prefix
77 Setting.default_language = 'en'
77 Setting.default_language = 'en'
78 relative_url_root = Redmine::Utils.relative_url_root
78 relative_url_root = Redmine::Utils.relative_url_root
79 Setting.host_name = 'mydomain.foo/rdm'
79 Setting.host_name = 'mydomain.foo/rdm'
80 Setting.protocol = 'http'
80 Setting.protocol = 'http'
81 Redmine::Utils.relative_url_root = '/rdm'
81 Redmine::Utils.relative_url_root = '/rdm'
82
82
83 journal = Journal.find(3)
83 journal = Journal.find(3)
84 assert Mailer.deliver_issue_edit(journal)
84 assert Mailer.deliver_issue_edit(journal)
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 ensure
114 ensure
115 # restore it
115 # restore it
116 Redmine::Utils.relative_url_root = relative_url_root
116 Redmine::Utils.relative_url_root = relative_url_root
117 end
117 end
118
118
119 def test_generated_links_with_prefix_and_no_relative_url_root
119 def test_generated_links_with_prefix_and_no_relative_url_root
120 Setting.default_language = 'en'
120 Setting.default_language = 'en'
121 relative_url_root = Redmine::Utils.relative_url_root
121 relative_url_root = Redmine::Utils.relative_url_root
122 Setting.host_name = 'mydomain.foo/rdm'
122 Setting.host_name = 'mydomain.foo/rdm'
123 Setting.protocol = 'http'
123 Setting.protocol = 'http'
124 Redmine::Utils.relative_url_root = nil
124 Redmine::Utils.relative_url_root = nil
125
125
126 journal = Journal.find(3)
126 journal = Journal.find(3)
127 assert Mailer.deliver_issue_edit(journal)
127 assert Mailer.deliver_issue_edit(journal)
128
128
129 mail = last_email
129 mail = last_email
130 assert_not_nil mail
130 assert_not_nil mail
131
131
132 assert_select_email do
132 assert_select_email do
133 # link to the main ticket
133 # link to the main ticket
134 assert_select 'a[href=?]',
134 assert_select 'a[href=?]',
135 'http://mydomain.foo/rdm/issues/2#change-3',
135 'http://mydomain.foo/rdm/issues/2#change-3',
136 :text => 'Feature request #2: Add ingredients categories'
136 :text => 'Feature request #2: Add ingredients categories'
137 # link to a referenced ticket
137 # link to a referenced ticket
138 assert_select 'a[href=?][title=?]',
138 assert_select 'a[href=?][title=?]',
139 'http://mydomain.foo/rdm/issues/1',
139 'http://mydomain.foo/rdm/issues/1',
140 'Can\'t print recipes (New)',
140 'Can\'t print recipes (New)',
141 :text => '#1'
141 :text => '#1'
142 # link to a changeset
142 # link to a changeset
143 assert_select 'a[href=?][title=?]',
143 assert_select 'a[href=?][title=?]',
144 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
144 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
145 'This commit fixes #1, #2 and references #1 &amp; #3',
145 'This commit fixes #1, #2 and references #1 &amp; #3',
146 :text => 'r2'
146 :text => 'r2'
147 # link to a description diff
147 # link to a description diff
148 assert_select 'a[href=?][title=?]',
148 assert_select 'a[href=?][title=?]',
149 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
149 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
150 'View differences',
150 'View differences',
151 :text => 'diff'
151 :text => 'diff'
152 # link to an attachment
152 # link to an attachment
153 assert_select 'a[href=?]',
153 assert_select 'a[href=?]',
154 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
154 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
155 :text => 'source.rb'
155 :text => 'source.rb'
156 end
156 end
157 ensure
157 ensure
158 # restore it
158 # restore it
159 Redmine::Utils.relative_url_root = relative_url_root
159 Redmine::Utils.relative_url_root = relative_url_root
160 end
160 end
161
161
162 def test_email_headers
162 def test_email_headers
163 issue = Issue.find(1)
163 issue = Issue.find(1)
164 Mailer.deliver_issue_add(issue)
164 Mailer.deliver_issue_add(issue)
165 mail = last_email
165 mail = last_email
166 assert_not_nil mail
166 assert_not_nil mail
167 assert_equal 'OOF', mail.header_string('X-Auto-Response-Suppress')
167 assert_equal 'OOF', mail.header_string('X-Auto-Response-Suppress')
168 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
168 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
169 end
169 end
170
170
171 def test_email_headers_should_include_sender
171 def test_email_headers_should_include_sender
172 issue = Issue.find(1)
172 issue = Issue.find(1)
173 Mailer.deliver_issue_add(issue)
173 Mailer.deliver_issue_add(issue)
174 mail = last_email
174 mail = last_email
175 assert_equal issue.author.login, mail.header_string('X-Redmine-Sender')
175 assert_equal issue.author.login, mail.header_string('X-Redmine-Sender')
176 end
176 end
177
177
178 def test_plain_text_mail
178 def test_plain_text_mail
179 Setting.plain_text_mail = 1
179 Setting.plain_text_mail = 1
180 journal = Journal.find(2)
180 journal = Journal.find(2)
181 Mailer.deliver_issue_edit(journal)
181 Mailer.deliver_issue_edit(journal)
182 mail = last_email
182 mail = last_email
183 assert_equal "text/plain", mail.content_type
183 assert_equal "text/plain", mail.content_type
184 assert_equal 0, mail.parts.size
184 assert_equal 0, mail.parts.size
185 assert !mail.encoded.include?('href')
185 assert !mail.encoded.include?('href')
186 end
186 end
187
187
188 def test_html_mail
188 def test_html_mail
189 Setting.plain_text_mail = 0
189 Setting.plain_text_mail = 0
190 journal = Journal.find(2)
190 journal = Journal.find(2)
191 Mailer.deliver_issue_edit(journal)
191 Mailer.deliver_issue_edit(journal)
192 mail = last_email
192 mail = last_email
193 assert_equal 2, mail.parts.size
193 assert_equal 2, mail.parts.size
194 assert mail.encoded.include?('href')
194 assert mail.encoded.include?('href')
195 end
195 end
196
196
197 def test_from_header
197 def test_from_header
198 with_settings :mail_from => 'redmine@example.net' do
198 with_settings :mail_from => 'redmine@example.net' do
199 Mailer.deliver_test(User.find(1))
199 Mailer.deliver_test(User.find(1))
200 end
200 end
201 mail = last_email
201 mail = last_email
202 assert_equal 'redmine@example.net', mail.from_addrs.first.address
202 assert_equal 'redmine@example.net', mail.from_addrs.first.address
203 end
203 end
204
204
205 def test_from_header_with_phrase
205 def test_from_header_with_phrase
206 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
206 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
207 Mailer.deliver_test(User.find(1))
207 Mailer.deliver_test(User.find(1))
208 end
208 end
209 mail = last_email
209 mail = last_email
210 assert_equal 'redmine@example.net', mail.from_addrs.first.address
210 assert_equal 'redmine@example.net', mail.from_addrs.first.address
211 assert_equal 'Redmine app', mail.from_addrs.first.name
211 assert_equal 'Redmine app', mail.from_addrs.first.name
212 end
212 end
213
213
214 def test_should_not_send_email_without_recipient
214 def test_should_not_send_email_without_recipient
215 news = News.find(:first)
215 news = News.find(:first)
216 user = news.author
216 user = news.author
217 # Remove members except news author
217 # Remove members except news author
218 news.project.memberships.each {|m| m.destroy unless m.user == user}
218 news.project.memberships.each {|m| m.destroy unless m.user == user}
219
219
220 user.pref[:no_self_notified] = false
220 user.pref[:no_self_notified] = false
221 user.pref.save
221 user.pref.save
222 User.current = user
222 User.current = user
223 Mailer.deliver_news_added(news.reload)
223 Mailer.deliver_news_added(news.reload)
224 assert_equal 1, last_email.bcc.size
224 assert_equal 1, last_email.bcc.size
225
225
226 # nobody to notify
226 # nobody to notify
227 user.pref[:no_self_notified] = true
227 user.pref[:no_self_notified] = true
228 user.pref.save
228 user.pref.save
229 User.current = user
229 User.current = user
230 ActionMailer::Base.deliveries.clear
230 ActionMailer::Base.deliveries.clear
231 Mailer.deliver_news_added(news.reload)
231 Mailer.deliver_news_added(news.reload)
232 assert ActionMailer::Base.deliveries.empty?
232 assert ActionMailer::Base.deliveries.empty?
233 end
233 end
234
234
235 def test_issue_add_message_id
235 def test_issue_add_message_id
236 issue = Issue.find(1)
236 issue = Issue.find(1)
237 Mailer.deliver_issue_add(issue)
237 Mailer.deliver_issue_add(issue)
238 mail = last_email
238 mail = last_email
239 assert_equal Mailer.message_id_for(issue), mail.message_id
239 assert_equal Mailer.message_id_for(issue), mail.message_id
240 assert_nil mail.references
240 assert_nil mail.references
241 end
241 end
242
242
243 def test_issue_edit_message_id
243 def test_issue_edit_message_id
244 journal = Journal.find(1)
244 journal = Journal.find(1)
245 Mailer.deliver_issue_edit(journal)
245 Mailer.deliver_issue_edit(journal)
246 mail = last_email
246 mail = last_email
247 assert_equal Mailer.message_id_for(journal), mail.message_id
247 assert_equal Mailer.message_id_for(journal), mail.message_id
248 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
248 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
249 assert_select_email do
249 assert_select_email do
250 # link to the update
250 # link to the update
251 assert_select "a[href=?]",
251 assert_select "a[href=?]",
252 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
252 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
253 end
253 end
254 end
254 end
255
255
256 def test_message_posted_message_id
256 def test_message_posted_message_id
257 message = Message.find(1)
257 message = Message.find(1)
258 Mailer.deliver_message_posted(message)
258 Mailer.deliver_message_posted(message)
259 mail = last_email
259 mail = last_email
260 assert_equal Mailer.message_id_for(message), mail.message_id
260 assert_equal Mailer.message_id_for(message), mail.message_id
261 assert_nil mail.references
261 assert_nil mail.references
262 assert_select_email do
262 assert_select_email do
263 # link to the message
263 # link to the message
264 assert_select "a[href=?]",
264 assert_select "a[href=?]",
265 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
265 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
266 :text => message.subject
266 :text => message.subject
267 end
267 end
268 end
268 end
269
269
270 def test_reply_posted_message_id
270 def test_reply_posted_message_id
271 message = Message.find(3)
271 message = Message.find(3)
272 Mailer.deliver_message_posted(message)
272 Mailer.deliver_message_posted(message)
273 mail = last_email
273 mail = last_email
274 assert_equal Mailer.message_id_for(message), mail.message_id
274 assert_equal Mailer.message_id_for(message), mail.message_id
275 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
275 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
276 assert_select_email do
276 assert_select_email do
277 # link to the reply
277 # link to the reply
278 assert_select "a[href=?]",
278 assert_select "a[href=?]",
279 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
279 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
280 :text => message.subject
280 :text => message.subject
281 end
281 end
282 end
282 end
283
283
284 context("#issue_add") do
284 context("#issue_add") do
285 setup do
285 setup do
286 ActionMailer::Base.deliveries.clear
286 ActionMailer::Base.deliveries.clear
287 Setting.bcc_recipients = '1'
287 Setting.bcc_recipients = '1'
288 @issue = Issue.find(1)
288 @issue = Issue.find(1)
289 end
289 end
290
290
291 should "notify project members" do
291 should "notify project members" do
292 assert Mailer.deliver_issue_add(@issue)
292 assert Mailer.deliver_issue_add(@issue)
293 assert last_email.bcc.include?('dlopper@somenet.foo')
293 assert last_email.bcc.include?('dlopper@somenet.foo')
294 end
294 end
295
295
296 should "not notify project members that are not allow to view the issue" do
296 should "not notify project members that are not allow to view the issue" do
297 Role.find(2).remove_permission!(:view_issues)
297 Role.find(2).remove_permission!(:view_issues)
298 assert Mailer.deliver_issue_add(@issue)
298 assert Mailer.deliver_issue_add(@issue)
299 assert !last_email.bcc.include?('dlopper@somenet.foo')
299 assert !last_email.bcc.include?('dlopper@somenet.foo')
300 end
300 end
301
301
302 should "notify issue watchers" do
302 should "notify issue watchers" do
303 user = User.find(9)
303 user = User.find(9)
304 # minimal email notification options
304 # minimal email notification options
305 user.pref[:no_self_notified] = '1'
305 user.pref[:no_self_notified] = '1'
306 user.pref.save
306 user.pref.save
307 user.mail_notification = false
307 user.mail_notification = false
308 user.save
308 user.save
309
309
310 Watcher.create!(:watchable => @issue, :user => user)
310 Watcher.create!(:watchable => @issue, :user => user)
311 assert Mailer.deliver_issue_add(@issue)
311 assert Mailer.deliver_issue_add(@issue)
312 assert last_email.bcc.include?(user.mail)
312 assert last_email.bcc.include?(user.mail)
313 end
313 end
314
314
315 should "not notify watchers not allowed to view the issue" do
315 should "not notify watchers not allowed to view the issue" do
316 user = User.find(9)
316 user = User.find(9)
317 Watcher.create!(:watchable => @issue, :user => user)
317 Watcher.create!(:watchable => @issue, :user => user)
318 Role.non_member.remove_permission!(:view_issues)
318 Role.non_member.remove_permission!(:view_issues)
319 assert Mailer.deliver_issue_add(@issue)
319 assert Mailer.deliver_issue_add(@issue)
320 assert !last_email.bcc.include?(user.mail)
320 assert !last_email.bcc.include?(user.mail)
321 end
321 end
322 end
322 end
323
323
324 # test mailer methods for each language
324 # test mailer methods for each language
325 def test_issue_add
325 def test_issue_add
326 issue = Issue.find(1)
326 issue = Issue.find(1)
327 valid_languages.each do |lang|
327 valid_languages.each do |lang|
328 Setting.default_language = lang.to_s
328 Setting.default_language = lang.to_s
329 assert Mailer.deliver_issue_add(issue)
329 assert Mailer.deliver_issue_add(issue)
330 end
330 end
331 end
331 end
332
332
333 def test_issue_edit
333 def test_issue_edit
334 journal = Journal.find(1)
334 journal = Journal.find(1)
335 valid_languages.each do |lang|
335 valid_languages.each do |lang|
336 Setting.default_language = lang.to_s
336 Setting.default_language = lang.to_s
337 assert Mailer.deliver_issue_edit(journal)
337 assert Mailer.deliver_issue_edit(journal)
338 end
338 end
339 end
339 end
340
340
341 def test_document_added
341 def test_document_added
342 document = Document.find(1)
342 document = Document.find(1)
343 valid_languages.each do |lang|
343 valid_languages.each do |lang|
344 Setting.default_language = lang.to_s
344 Setting.default_language = lang.to_s
345 assert Mailer.deliver_document_added(document)
345 assert Mailer.deliver_document_added(document)
346 end
346 end
347 end
347 end
348
348
349 def test_attachments_added
349 def test_attachments_added
350 attachements = [ Attachment.find_by_container_type('Document') ]
350 attachements = [ Attachment.find_by_container_type('Document') ]
351 valid_languages.each do |lang|
351 valid_languages.each do |lang|
352 Setting.default_language = lang.to_s
352 Setting.default_language = lang.to_s
353 assert Mailer.deliver_attachments_added(attachements)
353 assert Mailer.deliver_attachments_added(attachements)
354 end
354 end
355 end
355 end
356
356
357 def test_version_file_added
357 def test_version_file_added
358 attachements = [ Attachment.find_by_container_type('Version') ]
358 attachements = [ Attachment.find_by_container_type('Version') ]
359 assert Mailer.deliver_attachments_added(attachements)
359 assert Mailer.deliver_attachments_added(attachements)
360 assert_not_nil last_email.bcc
360 assert_not_nil last_email.bcc
361 assert last_email.bcc.any?
361 assert last_email.bcc.any?
362 assert_select_email do
362 assert_select_email do
363 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
363 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
364 end
364 end
365 end
365 end
366
366
367 def test_project_file_added
367 def test_project_file_added
368 attachements = [ Attachment.find_by_container_type('Project') ]
368 attachements = [ Attachment.find_by_container_type('Project') ]
369 assert Mailer.deliver_attachments_added(attachements)
369 assert Mailer.deliver_attachments_added(attachements)
370 assert_not_nil last_email.bcc
370 assert_not_nil last_email.bcc
371 assert last_email.bcc.any?
371 assert last_email.bcc.any?
372 assert_select_email do
372 assert_select_email do
373 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
373 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
374 end
374 end
375 end
375 end
376
376
377 def test_news_added
377 def test_news_added
378 news = News.find(:first)
378 news = News.find(:first)
379 valid_languages.each do |lang|
379 valid_languages.each do |lang|
380 Setting.default_language = lang.to_s
380 Setting.default_language = lang.to_s
381 assert Mailer.deliver_news_added(news)
381 assert Mailer.deliver_news_added(news)
382 end
382 end
383 end
383 end
384
384
385 def test_news_comment_added
385 def test_news_comment_added
386 comment = Comment.find(2)
386 comment = Comment.find(2)
387 valid_languages.each do |lang|
387 valid_languages.each do |lang|
388 Setting.default_language = lang.to_s
388 Setting.default_language = lang.to_s
389 assert Mailer.deliver_news_comment_added(comment)
389 assert Mailer.deliver_news_comment_added(comment)
390 end
390 end
391 end
391 end
392
392
393 def test_message_posted
393 def test_message_posted
394 message = Message.find(:first)
394 message = Message.find(:first)
395 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
395 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
396 recipients = recipients.compact.uniq
396 recipients = recipients.compact.uniq
397 valid_languages.each do |lang|
397 valid_languages.each do |lang|
398 Setting.default_language = lang.to_s
398 Setting.default_language = lang.to_s
399 assert Mailer.deliver_message_posted(message)
399 assert Mailer.deliver_message_posted(message)
400 end
400 end
401 end
401 end
402
402
403 def test_wiki_content_added
403 def test_wiki_content_added
404 content = WikiContent.find(:first)
404 content = WikiContent.find(1)
405 valid_languages.each do |lang|
405 valid_languages.each do |lang|
406 Setting.default_language = lang.to_s
406 Setting.default_language = lang.to_s
407 assert_difference 'ActionMailer::Base.deliveries.size' do
407 assert_difference 'ActionMailer::Base.deliveries.size' do
408 assert Mailer.deliver_wiki_content_added(content)
408 assert Mailer.deliver_wiki_content_added(content)
409 end
409 end
410 end
410 end
411 end
411 end
412
412
413 def test_wiki_content_updated
413 def test_wiki_content_updated
414 content = WikiContent.find(:first)
414 content = WikiContent.find(1)
415 valid_languages.each do |lang|
415 valid_languages.each do |lang|
416 Setting.default_language = lang.to_s
416 Setting.default_language = lang.to_s
417 assert_difference 'ActionMailer::Base.deliveries.size' do
417 assert_difference 'ActionMailer::Base.deliveries.size' do
418 assert Mailer.deliver_wiki_content_updated(content)
418 assert Mailer.deliver_wiki_content_updated(content)
419 end
419 end
420 end
420 end
421 end
421 end
422
422
423 def test_account_information
423 def test_account_information
424 user = User.find(2)
424 user = User.find(2)
425 valid_languages.each do |lang|
425 valid_languages.each do |lang|
426 user.update_attribute :language, lang.to_s
426 user.update_attribute :language, lang.to_s
427 user.reload
427 user.reload
428 assert Mailer.deliver_account_information(user, 'pAsswORd')
428 assert Mailer.deliver_account_information(user, 'pAsswORd')
429 end
429 end
430 end
430 end
431
431
432 def test_lost_password
432 def test_lost_password
433 token = Token.find(2)
433 token = Token.find(2)
434 valid_languages.each do |lang|
434 valid_languages.each do |lang|
435 token.user.update_attribute :language, lang.to_s
435 token.user.update_attribute :language, lang.to_s
436 token.reload
436 token.reload
437 assert Mailer.deliver_lost_password(token)
437 assert Mailer.deliver_lost_password(token)
438 end
438 end
439 end
439 end
440
440
441 def test_register
441 def test_register
442 token = Token.find(1)
442 token = Token.find(1)
443 Setting.host_name = 'redmine.foo'
443 Setting.host_name = 'redmine.foo'
444 Setting.protocol = 'https'
444 Setting.protocol = 'https'
445
445
446 valid_languages.each do |lang|
446 valid_languages.each do |lang|
447 token.user.update_attribute :language, lang.to_s
447 token.user.update_attribute :language, lang.to_s
448 token.reload
448 token.reload
449 ActionMailer::Base.deliveries.clear
449 ActionMailer::Base.deliveries.clear
450 assert Mailer.deliver_register(token)
450 assert Mailer.deliver_register(token)
451 mail = last_email
451 mail = last_email
452 assert_select_email do
452 assert_select_email do
453 assert_select "a[href=?]",
453 assert_select "a[href=?]",
454 "https://redmine.foo/account/activate?token=#{token.value}",
454 "https://redmine.foo/account/activate?token=#{token.value}",
455 :text => "https://redmine.foo/account/activate?token=#{token.value}"
455 :text => "https://redmine.foo/account/activate?token=#{token.value}"
456 end
456 end
457 end
457 end
458 end
458 end
459
459
460 def test_test
460 def test_test
461 user = User.find(1)
461 user = User.find(1)
462 valid_languages.each do |lang|
462 valid_languages.each do |lang|
463 user.update_attribute :language, lang.to_s
463 user.update_attribute :language, lang.to_s
464 assert Mailer.deliver_test(user)
464 assert Mailer.deliver_test(user)
465 end
465 end
466 end
466 end
467
467
468 def test_reminders
468 def test_reminders
469 Mailer.reminders(:days => 42)
469 Mailer.reminders(:days => 42)
470 assert_equal 1, ActionMailer::Base.deliveries.size
470 assert_equal 1, ActionMailer::Base.deliveries.size
471 mail = last_email
471 mail = last_email
472 assert mail.bcc.include?('dlopper@somenet.foo')
472 assert mail.bcc.include?('dlopper@somenet.foo')
473 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
473 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
474 assert_equal '1 issue(s) due in the next 42 days', mail.subject
474 assert_equal '1 issue(s) due in the next 42 days', mail.subject
475 end
475 end
476
476
477 def test_reminders_should_not_include_closed_issues
477 def test_reminders_should_not_include_closed_issues
478 with_settings :default_language => 'en' do
478 with_settings :default_language => 'en' do
479 Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 5,
479 Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 5,
480 :subject => 'Closed issue', :assigned_to_id => 3,
480 :subject => 'Closed issue', :assigned_to_id => 3,
481 :due_date => 5.days.from_now)
481 :due_date => 5.days.from_now)
482 ActionMailer::Base.deliveries.clear
482 ActionMailer::Base.deliveries.clear
483
483
484 Mailer.reminders(:days => 42)
484 Mailer.reminders(:days => 42)
485 assert_equal 1, ActionMailer::Base.deliveries.size
485 assert_equal 1, ActionMailer::Base.deliveries.size
486 mail = last_email
486 mail = last_email
487 assert mail.bcc.include?('dlopper@somenet.foo')
487 assert mail.bcc.include?('dlopper@somenet.foo')
488 assert !mail.body.include?('Closed issue')
488 assert !mail.body.include?('Closed issue')
489 end
489 end
490 end
490 end
491
491
492 def test_reminders_for_users
492 def test_reminders_for_users
493 Mailer.reminders(:days => 42, :users => ['5'])
493 Mailer.reminders(:days => 42, :users => ['5'])
494 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
494 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
495 Mailer.reminders(:days => 42, :users => ['3'])
495 Mailer.reminders(:days => 42, :users => ['3'])
496 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
496 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
497 mail = last_email
497 mail = last_email
498 assert mail.bcc.include?('dlopper@somenet.foo')
498 assert mail.bcc.include?('dlopper@somenet.foo')
499 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
499 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
500 end
500 end
501
501
502 def last_email
502 def last_email
503 mail = ActionMailer::Base.deliveries.last
503 mail = ActionMailer::Base.deliveries.last
504 assert_not_nil mail
504 assert_not_nil mail
505 mail
505 mail
506 end
506 end
507
507
508 def test_mailer_should_not_change_locale
508 def test_mailer_should_not_change_locale
509 Setting.default_language = 'en'
509 Setting.default_language = 'en'
510 # Set current language to italian
510 # Set current language to italian
511 set_language_if_valid 'it'
511 set_language_if_valid 'it'
512 # Send an email to a french user
512 # Send an email to a french user
513 user = User.find(1)
513 user = User.find(1)
514 user.language = 'fr'
514 user.language = 'fr'
515 Mailer.deliver_account_activated(user)
515 Mailer.deliver_account_activated(user)
516 mail = last_email
516 mail = last_email
517 assert mail.body.include?('Votre compte')
517 assert mail.body.include?('Votre compte')
518
518
519 assert_equal :it, current_language
519 assert_equal :it, current_language
520 end
520 end
521
521
522 def test_with_deliveries_off
522 def test_with_deliveries_off
523 Mailer.with_deliveries false do
523 Mailer.with_deliveries false do
524 Mailer.deliver_test(User.find(1))
524 Mailer.deliver_test(User.find(1))
525 end
525 end
526 assert ActionMailer::Base.deliveries.empty?
526 assert ActionMailer::Base.deliveries.empty?
527 # should restore perform_deliveries
527 # should restore perform_deliveries
528 assert ActionMailer::Base.perform_deliveries
528 assert ActionMailer::Base.perform_deliveries
529 end
529 end
530
530
531 def test_tmail_to_header_field_should_not_include_blank_lines
531 def test_tmail_to_header_field_should_not_include_blank_lines
532 mail = TMail::Mail.new
532 mail = TMail::Mail.new
533 mail.to = ["a.user@example.com", "v.user2@example.com", "e.smith@example.com", "info@example.com", "v.pupkin@example.com",
533 mail.to = ["a.user@example.com", "v.user2@example.com", "e.smith@example.com", "info@example.com", "v.pupkin@example.com",
534 "b.user@example.com", "w.user2@example.com", "f.smith@example.com", "info2@example.com", "w.pupkin@example.com"]
534 "b.user@example.com", "w.user2@example.com", "f.smith@example.com", "info2@example.com", "w.pupkin@example.com"]
535
535
536 assert !mail.encoded.strip.split("\r\n").detect(&:blank?), "#{mail.encoded} malformed"
536 assert !mail.encoded.strip.split("\r\n").detect(&:blank?), "#{mail.encoded} malformed"
537 end
537 end
538
538
539 def test_layout_should_include_the_emails_header
539 def test_layout_should_include_the_emails_header
540 with_settings :emails_header => "*Header content*" do
540 with_settings :emails_header => "*Header content*" do
541 assert Mailer.deliver_test(User.find(1))
541 assert Mailer.deliver_test(User.find(1))
542 assert_select_email do
542 assert_select_email do
543 assert_select ".header" do
543 assert_select ".header" do
544 assert_select "strong", :text => "Header content"
544 assert_select "strong", :text => "Header content"
545 end
545 end
546 end
546 end
547 end
547 end
548 end
548 end
549 end
549 end
General Comments 0
You need to be logged in to leave comments. Login now