##// END OF EJS Templates
move unit mailer test last_email method to the bottom and change to private...
Toshi MARUYAMA -
r9452:6e7eadf9fb7f
parent child
Show More
@@ -1,539 +1,540
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.deliver_issue_edit(journal)
45 assert Mailer.deliver_issue_edit(journal)
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.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 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.deliver_issue_edit(journal)
124 assert Mailer.deliver_issue_edit(journal)
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.deliver_issue_add(issue)
161 Mailer.deliver_issue_add(issue)
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 end
166 end
167
167
168 def test_email_headers_should_include_sender
168 def test_email_headers_should_include_sender
169 issue = Issue.find(1)
169 issue = Issue.find(1)
170 Mailer.deliver_issue_add(issue)
170 Mailer.deliver_issue_add(issue)
171 mail = last_email
171 mail = last_email
172 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
172 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
173 end
173 end
174
174
175 def test_plain_text_mail
175 def test_plain_text_mail
176 Setting.plain_text_mail = 1
176 Setting.plain_text_mail = 1
177 journal = Journal.find(2)
177 journal = Journal.find(2)
178 Mailer.deliver_issue_edit(journal)
178 Mailer.deliver_issue_edit(journal)
179 mail = last_email
179 mail = last_email
180 assert_equal "text/plain; charset=UTF-8", mail.content_type
180 assert_equal "text/plain; charset=UTF-8", mail.content_type
181 assert_equal 0, mail.parts.size
181 assert_equal 0, mail.parts.size
182 assert !mail.encoded.include?('href')
182 assert !mail.encoded.include?('href')
183 end
183 end
184
184
185 def test_html_mail
185 def test_html_mail
186 Setting.plain_text_mail = 0
186 Setting.plain_text_mail = 0
187 journal = Journal.find(2)
187 journal = Journal.find(2)
188 Mailer.deliver_issue_edit(journal)
188 Mailer.deliver_issue_edit(journal)
189 mail = last_email
189 mail = last_email
190 assert_equal 2, mail.parts.size
190 assert_equal 2, mail.parts.size
191 assert mail.encoded.include?('href')
191 assert mail.encoded.include?('href')
192 end
192 end
193
193
194 def test_from_header
194 def test_from_header
195 with_settings :mail_from => 'redmine@example.net' do
195 with_settings :mail_from => 'redmine@example.net' do
196 Mailer.deliver_test_email(User.find(1))
196 Mailer.deliver_test_email(User.find(1))
197 end
197 end
198 mail = last_email
198 mail = last_email
199 assert_equal 'redmine@example.net', mail.from_addrs.first
199 assert_equal 'redmine@example.net', mail.from_addrs.first
200 end
200 end
201
201
202 def test_from_header_with_phrase
202 def test_from_header_with_phrase
203 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
203 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
204 Mailer.deliver_test_email(User.find(1))
204 Mailer.deliver_test_email(User.find(1))
205 end
205 end
206 mail = last_email
206 mail = last_email
207 assert_equal 'redmine@example.net', mail.from_addrs.first
207 assert_equal 'redmine@example.net', mail.from_addrs.first
208 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
208 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
209 end
209 end
210
210
211 def test_should_not_send_email_without_recipient
211 def test_should_not_send_email_without_recipient
212 news = News.find(:first)
212 news = News.find(:first)
213 user = news.author
213 user = news.author
214 # Remove members except news author
214 # Remove members except news author
215 news.project.memberships.each {|m| m.destroy unless m.user == user}
215 news.project.memberships.each {|m| m.destroy unless m.user == user}
216
216
217 user.pref[:no_self_notified] = false
217 user.pref[:no_self_notified] = false
218 user.pref.save
218 user.pref.save
219 User.current = user
219 User.current = user
220 Mailer.deliver_news_added(news.reload)
220 Mailer.deliver_news_added(news.reload)
221 assert_equal 1, last_email.bcc.size
221 assert_equal 1, last_email.bcc.size
222
222
223 # nobody to notify
223 # nobody to notify
224 user.pref[:no_self_notified] = true
224 user.pref[:no_self_notified] = true
225 user.pref.save
225 user.pref.save
226 User.current = user
226 User.current = user
227 ActionMailer::Base.deliveries.clear
227 ActionMailer::Base.deliveries.clear
228 Mailer.deliver_news_added(news.reload)
228 Mailer.deliver_news_added(news.reload)
229 assert ActionMailer::Base.deliveries.empty?
229 assert ActionMailer::Base.deliveries.empty?
230 end
230 end
231
231
232 def test_issue_add_message_id
232 def test_issue_add_message_id
233 issue = Issue.find(1)
233 issue = Issue.find(1)
234 Mailer.deliver_issue_add(issue)
234 Mailer.deliver_issue_add(issue)
235 mail = last_email
235 mail = last_email
236 assert_equal Mailer.message_id_for(issue), mail.message_id
236 assert_equal Mailer.message_id_for(issue), mail.message_id
237 assert_nil mail.references
237 assert_nil mail.references
238 end
238 end
239
239
240 def test_issue_edit_message_id
240 def test_issue_edit_message_id
241 journal = Journal.find(1)
241 journal = Journal.find(1)
242 Mailer.deliver_issue_edit(journal)
242 Mailer.deliver_issue_edit(journal)
243 mail = last_email
243 mail = last_email
244 assert_equal Mailer.message_id_for(journal), mail.message_id
244 assert_equal Mailer.message_id_for(journal), mail.message_id
245 assert_include Mailer.message_id_for(journal.issue), mail.references
245 assert_include Mailer.message_id_for(journal.issue), mail.references
246 assert_select_email do
246 assert_select_email do
247 # link to the update
247 # link to the update
248 assert_select "a[href=?]",
248 assert_select "a[href=?]",
249 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
249 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
250 end
250 end
251 end
251 end
252
252
253 def test_message_posted_message_id
253 def test_message_posted_message_id
254 message = Message.find(1)
254 message = Message.find(1)
255 Mailer.deliver_message_posted(message)
255 Mailer.deliver_message_posted(message)
256 mail = last_email
256 mail = last_email
257 assert_equal Mailer.message_id_for(message), mail.message_id
257 assert_equal Mailer.message_id_for(message), mail.message_id
258 assert_nil mail.references
258 assert_nil mail.references
259 assert_select_email do
259 assert_select_email do
260 # link to the message
260 # link to the message
261 assert_select "a[href=?]",
261 assert_select "a[href=?]",
262 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
262 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
263 :text => message.subject
263 :text => message.subject
264 end
264 end
265 end
265 end
266
266
267 def test_reply_posted_message_id
267 def test_reply_posted_message_id
268 message = Message.find(3)
268 message = Message.find(3)
269 Mailer.deliver_message_posted(message)
269 Mailer.deliver_message_posted(message)
270 mail = last_email
270 mail = last_email
271 assert_equal Mailer.message_id_for(message), mail.message_id
271 assert_equal Mailer.message_id_for(message), mail.message_id
272 assert_include Mailer.message_id_for(message.parent), mail.references
272 assert_include Mailer.message_id_for(message.parent), mail.references
273 assert_select_email do
273 assert_select_email do
274 # link to the reply
274 # link to the reply
275 assert_select "a[href=?]",
275 assert_select "a[href=?]",
276 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
276 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
277 :text => message.subject
277 :text => message.subject
278 end
278 end
279 end
279 end
280
280
281 context("#issue_add") do
281 context("#issue_add") do
282 setup do
282 setup do
283 ActionMailer::Base.deliveries.clear
283 ActionMailer::Base.deliveries.clear
284 Setting.bcc_recipients = '1'
284 Setting.bcc_recipients = '1'
285 @issue = Issue.find(1)
285 @issue = Issue.find(1)
286 end
286 end
287
287
288 should "notify project members" do
288 should "notify project members" do
289 assert Mailer.deliver_issue_add(@issue)
289 assert Mailer.deliver_issue_add(@issue)
290 assert last_email.bcc.include?('dlopper@somenet.foo')
290 assert last_email.bcc.include?('dlopper@somenet.foo')
291 end
291 end
292
292
293 should "not notify project members that are not allow to view the issue" do
293 should "not notify project members that are not allow to view the issue" do
294 Role.find(2).remove_permission!(:view_issues)
294 Role.find(2).remove_permission!(:view_issues)
295 assert Mailer.deliver_issue_add(@issue)
295 assert Mailer.deliver_issue_add(@issue)
296 assert !last_email.bcc.include?('dlopper@somenet.foo')
296 assert !last_email.bcc.include?('dlopper@somenet.foo')
297 end
297 end
298
298
299 should "notify issue watchers" do
299 should "notify issue watchers" do
300 user = User.find(9)
300 user = User.find(9)
301 # minimal email notification options
301 # minimal email notification options
302 user.pref[:no_self_notified] = '1'
302 user.pref[:no_self_notified] = '1'
303 user.pref.save
303 user.pref.save
304 user.mail_notification = false
304 user.mail_notification = false
305 user.save
305 user.save
306
306
307 Watcher.create!(:watchable => @issue, :user => user)
307 Watcher.create!(:watchable => @issue, :user => user)
308 assert Mailer.deliver_issue_add(@issue)
308 assert Mailer.deliver_issue_add(@issue)
309 assert last_email.bcc.include?(user.mail)
309 assert last_email.bcc.include?(user.mail)
310 end
310 end
311
311
312 should "not notify watchers not allowed to view the issue" do
312 should "not notify watchers not allowed to view the issue" do
313 user = User.find(9)
313 user = User.find(9)
314 Watcher.create!(:watchable => @issue, :user => user)
314 Watcher.create!(:watchable => @issue, :user => user)
315 Role.non_member.remove_permission!(:view_issues)
315 Role.non_member.remove_permission!(:view_issues)
316 assert Mailer.deliver_issue_add(@issue)
316 assert Mailer.deliver_issue_add(@issue)
317 assert !last_email.bcc.include?(user.mail)
317 assert !last_email.bcc.include?(user.mail)
318 end
318 end
319 end
319 end
320
320
321 # test mailer methods for each language
321 # test mailer methods for each language
322 def test_issue_add
322 def test_issue_add
323 issue = Issue.find(1)
323 issue = Issue.find(1)
324 valid_languages.each do |lang|
324 valid_languages.each do |lang|
325 Setting.default_language = lang.to_s
325 Setting.default_language = lang.to_s
326 assert Mailer.deliver_issue_add(issue)
326 assert Mailer.deliver_issue_add(issue)
327 end
327 end
328 end
328 end
329
329
330 def test_issue_edit
330 def test_issue_edit
331 journal = Journal.find(1)
331 journal = Journal.find(1)
332 valid_languages.each do |lang|
332 valid_languages.each do |lang|
333 Setting.default_language = lang.to_s
333 Setting.default_language = lang.to_s
334 assert Mailer.deliver_issue_edit(journal)
334 assert Mailer.deliver_issue_edit(journal)
335 end
335 end
336 end
336 end
337
337
338 def test_document_added
338 def test_document_added
339 document = Document.find(1)
339 document = Document.find(1)
340 valid_languages.each do |lang|
340 valid_languages.each do |lang|
341 Setting.default_language = lang.to_s
341 Setting.default_language = lang.to_s
342 assert Mailer.deliver_document_added(document)
342 assert Mailer.deliver_document_added(document)
343 end
343 end
344 end
344 end
345
345
346 def test_attachments_added
346 def test_attachments_added
347 attachements = [ Attachment.find_by_container_type('Document') ]
347 attachements = [ Attachment.find_by_container_type('Document') ]
348 valid_languages.each do |lang|
348 valid_languages.each do |lang|
349 Setting.default_language = lang.to_s
349 Setting.default_language = lang.to_s
350 assert Mailer.deliver_attachments_added(attachements)
350 assert Mailer.deliver_attachments_added(attachements)
351 end
351 end
352 end
352 end
353
353
354 def test_version_file_added
354 def test_version_file_added
355 attachements = [ Attachment.find_by_container_type('Version') ]
355 attachements = [ Attachment.find_by_container_type('Version') ]
356 assert Mailer.deliver_attachments_added(attachements)
356 assert Mailer.deliver_attachments_added(attachements)
357 assert_not_nil last_email.bcc
357 assert_not_nil last_email.bcc
358 assert last_email.bcc.any?
358 assert last_email.bcc.any?
359 assert_select_email do
359 assert_select_email do
360 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
360 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
361 end
361 end
362 end
362 end
363
363
364 def test_project_file_added
364 def test_project_file_added
365 attachements = [ Attachment.find_by_container_type('Project') ]
365 attachements = [ Attachment.find_by_container_type('Project') ]
366 assert Mailer.deliver_attachments_added(attachements)
366 assert Mailer.deliver_attachments_added(attachements)
367 assert_not_nil last_email.bcc
367 assert_not_nil last_email.bcc
368 assert last_email.bcc.any?
368 assert last_email.bcc.any?
369 assert_select_email do
369 assert_select_email do
370 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
370 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
371 end
371 end
372 end
372 end
373
373
374 def test_news_added
374 def test_news_added
375 news = News.find(:first)
375 news = News.find(:first)
376 valid_languages.each do |lang|
376 valid_languages.each do |lang|
377 Setting.default_language = lang.to_s
377 Setting.default_language = lang.to_s
378 assert Mailer.deliver_news_added(news)
378 assert Mailer.deliver_news_added(news)
379 end
379 end
380 end
380 end
381
381
382 def test_news_comment_added
382 def test_news_comment_added
383 comment = Comment.find(2)
383 comment = Comment.find(2)
384 valid_languages.each do |lang|
384 valid_languages.each do |lang|
385 Setting.default_language = lang.to_s
385 Setting.default_language = lang.to_s
386 assert Mailer.deliver_news_comment_added(comment)
386 assert Mailer.deliver_news_comment_added(comment)
387 end
387 end
388 end
388 end
389
389
390 def test_message_posted
390 def test_message_posted
391 message = Message.find(:first)
391 message = Message.find(:first)
392 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
392 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
393 recipients = recipients.compact.uniq
393 recipients = recipients.compact.uniq
394 valid_languages.each do |lang|
394 valid_languages.each do |lang|
395 Setting.default_language = lang.to_s
395 Setting.default_language = lang.to_s
396 assert Mailer.deliver_message_posted(message)
396 assert Mailer.deliver_message_posted(message)
397 end
397 end
398 end
398 end
399
399
400 def test_wiki_content_added
400 def test_wiki_content_added
401 content = WikiContent.find(1)
401 content = WikiContent.find(1)
402 valid_languages.each do |lang|
402 valid_languages.each do |lang|
403 Setting.default_language = lang.to_s
403 Setting.default_language = lang.to_s
404 assert_difference 'ActionMailer::Base.deliveries.size' do
404 assert_difference 'ActionMailer::Base.deliveries.size' do
405 assert Mailer.deliver_wiki_content_added(content)
405 assert Mailer.deliver_wiki_content_added(content)
406 end
406 end
407 end
407 end
408 end
408 end
409
409
410 def test_wiki_content_updated
410 def test_wiki_content_updated
411 content = WikiContent.find(1)
411 content = WikiContent.find(1)
412 valid_languages.each do |lang|
412 valid_languages.each do |lang|
413 Setting.default_language = lang.to_s
413 Setting.default_language = lang.to_s
414 assert_difference 'ActionMailer::Base.deliveries.size' do
414 assert_difference 'ActionMailer::Base.deliveries.size' do
415 assert Mailer.deliver_wiki_content_updated(content)
415 assert Mailer.deliver_wiki_content_updated(content)
416 end
416 end
417 end
417 end
418 end
418 end
419
419
420 def test_account_information
420 def test_account_information
421 user = User.find(2)
421 user = User.find(2)
422 valid_languages.each do |lang|
422 valid_languages.each do |lang|
423 user.update_attribute :language, lang.to_s
423 user.update_attribute :language, lang.to_s
424 user.reload
424 user.reload
425 assert Mailer.deliver_account_information(user, 'pAsswORd')
425 assert Mailer.deliver_account_information(user, 'pAsswORd')
426 end
426 end
427 end
427 end
428
428
429 def test_lost_password
429 def test_lost_password
430 token = Token.find(2)
430 token = Token.find(2)
431 valid_languages.each do |lang|
431 valid_languages.each do |lang|
432 token.user.update_attribute :language, lang.to_s
432 token.user.update_attribute :language, lang.to_s
433 token.reload
433 token.reload
434 assert Mailer.deliver_lost_password(token)
434 assert Mailer.deliver_lost_password(token)
435 end
435 end
436 end
436 end
437
437
438 def test_register
438 def test_register
439 token = Token.find(1)
439 token = Token.find(1)
440 Setting.host_name = 'redmine.foo'
440 Setting.host_name = 'redmine.foo'
441 Setting.protocol = 'https'
441 Setting.protocol = 'https'
442
442
443 valid_languages.each do |lang|
443 valid_languages.each do |lang|
444 token.user.update_attribute :language, lang.to_s
444 token.user.update_attribute :language, lang.to_s
445 token.reload
445 token.reload
446 ActionMailer::Base.deliveries.clear
446 ActionMailer::Base.deliveries.clear
447 assert Mailer.deliver_register(token)
447 assert Mailer.deliver_register(token)
448 mail = last_email
448 mail = last_email
449 assert_select_email do
449 assert_select_email do
450 assert_select "a[href=?]",
450 assert_select "a[href=?]",
451 "https://redmine.foo/account/activate?token=#{token.value}",
451 "https://redmine.foo/account/activate?token=#{token.value}",
452 :text => "https://redmine.foo/account/activate?token=#{token.value}"
452 :text => "https://redmine.foo/account/activate?token=#{token.value}"
453 end
453 end
454 end
454 end
455 end
455 end
456
456
457 def test_test
457 def test_test
458 user = User.find(1)
458 user = User.find(1)
459 valid_languages.each do |lang|
459 valid_languages.each do |lang|
460 user.update_attribute :language, lang.to_s
460 user.update_attribute :language, lang.to_s
461 assert Mailer.deliver_test_email(user)
461 assert Mailer.deliver_test_email(user)
462 end
462 end
463 end
463 end
464
464
465 def test_reminders
465 def test_reminders
466 Mailer.reminders(:days => 42)
466 Mailer.reminders(:days => 42)
467 assert_equal 1, ActionMailer::Base.deliveries.size
467 assert_equal 1, ActionMailer::Base.deliveries.size
468 mail = last_email
468 mail = last_email
469 assert mail.bcc.include?('dlopper@somenet.foo')
469 assert mail.bcc.include?('dlopper@somenet.foo')
470 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
470 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
471 assert_equal '1 issue(s) due in the next 42 days', mail.subject
471 assert_equal '1 issue(s) due in the next 42 days', mail.subject
472 end
472 end
473
473
474 def test_reminders_should_not_include_closed_issues
474 def test_reminders_should_not_include_closed_issues
475 with_settings :default_language => 'en' do
475 with_settings :default_language => 'en' do
476 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
476 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
477 :subject => 'Closed issue', :assigned_to_id => 3,
477 :subject => 'Closed issue', :assigned_to_id => 3,
478 :due_date => 5.days.from_now,
478 :due_date => 5.days.from_now,
479 :author_id => 2)
479 :author_id => 2)
480 ActionMailer::Base.deliveries.clear
480 ActionMailer::Base.deliveries.clear
481
481
482 Mailer.reminders(:days => 42)
482 Mailer.reminders(:days => 42)
483 assert_equal 1, ActionMailer::Base.deliveries.size
483 assert_equal 1, ActionMailer::Base.deliveries.size
484 mail = last_email
484 mail = last_email
485 assert mail.bcc.include?('dlopper@somenet.foo')
485 assert mail.bcc.include?('dlopper@somenet.foo')
486 assert_mail_body_no_match 'Closed issue', mail
486 assert_mail_body_no_match 'Closed issue', mail
487 end
487 end
488 end
488 end
489
489
490 def test_reminders_for_users
490 def test_reminders_for_users
491 Mailer.reminders(:days => 42, :users => ['5'])
491 Mailer.reminders(:days => 42, :users => ['5'])
492 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
492 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
493 Mailer.reminders(:days => 42, :users => ['3'])
493 Mailer.reminders(:days => 42, :users => ['3'])
494 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
494 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
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_match 'Bug #3: Error 281 when updating a recipe', mail
497 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
498 end
498 end
499
499
500 def last_email
501 mail = ActionMailer::Base.deliveries.last
502 assert_not_nil mail
503 mail
504 end
505
506 def test_mailer_should_not_change_locale
500 def test_mailer_should_not_change_locale
507 Setting.default_language = 'en'
501 Setting.default_language = 'en'
508 # Set current language to italian
502 # Set current language to italian
509 set_language_if_valid 'it'
503 set_language_if_valid 'it'
510 # Send an email to a french user
504 # Send an email to a french user
511 user = User.find(1)
505 user = User.find(1)
512 user.language = 'fr'
506 user.language = 'fr'
513 Mailer.deliver_account_activated(user)
507 Mailer.deliver_account_activated(user)
514 mail = last_email
508 mail = last_email
515 assert_mail_body_match 'Votre compte', mail
509 assert_mail_body_match 'Votre compte', mail
516
510
517 assert_equal :it, current_language
511 assert_equal :it, current_language
518 end
512 end
519
513
520 def test_with_deliveries_off
514 def test_with_deliveries_off
521 Mailer.with_deliveries false do
515 Mailer.with_deliveries false do
522 Mailer.deliver_test_email(User.find(1))
516 Mailer.deliver_test_email(User.find(1))
523 end
517 end
524 assert ActionMailer::Base.deliveries.empty?
518 assert ActionMailer::Base.deliveries.empty?
525 # should restore perform_deliveries
519 # should restore perform_deliveries
526 assert ActionMailer::Base.perform_deliveries
520 assert ActionMailer::Base.perform_deliveries
527 end
521 end
528
522
529 def test_layout_should_include_the_emails_header
523 def test_layout_should_include_the_emails_header
530 with_settings :emails_header => "*Header content*" do
524 with_settings :emails_header => "*Header content*" do
531 assert Mailer.deliver_test_email(User.find(1))
525 assert Mailer.deliver_test_email(User.find(1))
532 assert_select_email do
526 assert_select_email do
533 assert_select ".header" do
527 assert_select ".header" do
534 assert_select "strong", :text => "Header content"
528 assert_select "strong", :text => "Header content"
535 end
529 end
536 end
530 end
537 end
531 end
538 end
532 end
533
534 private
535 def last_email
536 mail = ActionMailer::Base.deliveries.last
537 assert_not_nil mail
538 mail
539 end
539 end
540 end
General Comments 0
You need to be logged in to leave comments. Login now