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