##// END OF EJS Templates
code layout clean up of test_generated_links_in_emails at test/unit/mailer_test.rb...
Toshi MARUYAMA -
r7378:b3c5f812c94b
parent child
Show More
@@ -1,451 +1,459
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.host_name = 'mydomain.foo'
39 Setting.host_name = 'mydomain.foo'
40 Setting.protocol = 'https'
40 Setting.protocol = 'https'
41
41
42 journal = Journal.find(2)
42 journal = Journal.find(2)
43 assert Mailer.deliver_issue_edit(journal)
43 assert Mailer.deliver_issue_edit(journal)
44
44
45 mail = ActionMailer::Base.deliveries.last
45 mail = ActionMailer::Base.deliveries.last
46 assert_kind_of TMail::Mail, mail
46 assert_kind_of TMail::Mail, mail
47
47
48 assert_select_email do
48 assert_select_email do
49 # link to the main ticket
49 # link to the main ticket
50 assert_select "a[href=?]", "https://mydomain.foo/issues/1", :text => "Bug #1: Can't print recipes"
50 assert_select "a[href=?]",
51 "https://mydomain.foo/issues/1",
52 :text => "Bug #1: Can't print recipes"
51 # link to a referenced ticket
53 # link to a referenced ticket
52 assert_select "a[href=?][title=?]", "https://mydomain.foo/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
54 assert_select "a[href=?][title=?]",
55 "https://mydomain.foo/issues/2",
56 "Add ingredients categories (Assigned)",
57 :text => "#2"
53 # link to a changeset
58 # link to a changeset
54 assert_select "a[href=?][title=?]", "https://mydomain.foo/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
59 assert_select "a[href=?][title=?]",
60 "https://mydomain.foo/projects/ecookbook/repository/revisions/2",
61 "This commit fixes #1, #2 and references #1 &amp; #3",
62 :text => "r2"
55 end
63 end
56 end
64 end
57
65
58 def test_generated_links_with_prefix
66 def test_generated_links_with_prefix
59 relative_url_root = Redmine::Utils.relative_url_root
67 relative_url_root = Redmine::Utils.relative_url_root
60 Setting.host_name = 'mydomain.foo/rdm'
68 Setting.host_name = 'mydomain.foo/rdm'
61 Setting.protocol = 'http'
69 Setting.protocol = 'http'
62 Redmine::Utils.relative_url_root = '/rdm'
70 Redmine::Utils.relative_url_root = '/rdm'
63
71
64 journal = Journal.find(2)
72 journal = Journal.find(2)
65 assert Mailer.deliver_issue_edit(journal)
73 assert Mailer.deliver_issue_edit(journal)
66
74
67 mail = ActionMailer::Base.deliveries.last
75 mail = ActionMailer::Base.deliveries.last
68 assert_kind_of TMail::Mail, mail
76 assert_kind_of TMail::Mail, mail
69
77
70 assert_select_email do
78 assert_select_email do
71 # link to the main ticket
79 # link to the main ticket
72 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
80 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
73 # link to a referenced ticket
81 # link to a referenced ticket
74 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
82 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
75 # link to a changeset
83 # link to a changeset
76 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
84 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
77 end
85 end
78 ensure
86 ensure
79 # restore it
87 # restore it
80 Redmine::Utils.relative_url_root = relative_url_root
88 Redmine::Utils.relative_url_root = relative_url_root
81 end
89 end
82
90
83 def test_generated_links_with_prefix_and_no_relative_url_root
91 def test_generated_links_with_prefix_and_no_relative_url_root
84 relative_url_root = Redmine::Utils.relative_url_root
92 relative_url_root = Redmine::Utils.relative_url_root
85 Setting.host_name = 'mydomain.foo/rdm'
93 Setting.host_name = 'mydomain.foo/rdm'
86 Setting.protocol = 'http'
94 Setting.protocol = 'http'
87 Redmine::Utils.relative_url_root = nil
95 Redmine::Utils.relative_url_root = nil
88
96
89 journal = Journal.find(2)
97 journal = Journal.find(2)
90 assert Mailer.deliver_issue_edit(journal)
98 assert Mailer.deliver_issue_edit(journal)
91
99
92 mail = ActionMailer::Base.deliveries.last
100 mail = ActionMailer::Base.deliveries.last
93 assert_kind_of TMail::Mail, mail
101 assert_kind_of TMail::Mail, mail
94
102
95 assert_select_email do
103 assert_select_email do
96 # link to the main ticket
104 # link to the main ticket
97 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
105 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
98 # link to a referenced ticket
106 # link to a referenced ticket
99 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
107 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
100 # link to a changeset
108 # link to a changeset
101 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
109 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
102 end
110 end
103 ensure
111 ensure
104 # restore it
112 # restore it
105 Redmine::Utils.relative_url_root = relative_url_root
113 Redmine::Utils.relative_url_root = relative_url_root
106 end
114 end
107
115
108 def test_email_headers
116 def test_email_headers
109 issue = Issue.find(1)
117 issue = Issue.find(1)
110 Mailer.deliver_issue_add(issue)
118 Mailer.deliver_issue_add(issue)
111 mail = ActionMailer::Base.deliveries.last
119 mail = ActionMailer::Base.deliveries.last
112 assert_not_nil mail
120 assert_not_nil mail
113 assert_equal 'bulk', mail.header_string('Precedence')
121 assert_equal 'bulk', mail.header_string('Precedence')
114 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
122 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
115 end
123 end
116
124
117 def test_plain_text_mail
125 def test_plain_text_mail
118 Setting.plain_text_mail = 1
126 Setting.plain_text_mail = 1
119 journal = Journal.find(2)
127 journal = Journal.find(2)
120 Mailer.deliver_issue_edit(journal)
128 Mailer.deliver_issue_edit(journal)
121 mail = ActionMailer::Base.deliveries.last
129 mail = ActionMailer::Base.deliveries.last
122 assert_equal "text/plain", mail.content_type
130 assert_equal "text/plain", mail.content_type
123 assert_equal 0, mail.parts.size
131 assert_equal 0, mail.parts.size
124 assert !mail.encoded.include?('href')
132 assert !mail.encoded.include?('href')
125 end
133 end
126
134
127 def test_html_mail
135 def test_html_mail
128 Setting.plain_text_mail = 0
136 Setting.plain_text_mail = 0
129 journal = Journal.find(2)
137 journal = Journal.find(2)
130 Mailer.deliver_issue_edit(journal)
138 Mailer.deliver_issue_edit(journal)
131 mail = ActionMailer::Base.deliveries.last
139 mail = ActionMailer::Base.deliveries.last
132 assert_equal 2, mail.parts.size
140 assert_equal 2, mail.parts.size
133 assert mail.encoded.include?('href')
141 assert mail.encoded.include?('href')
134 end
142 end
135
143
136 def test_mail_from_with_phrase
144 def test_mail_from_with_phrase
137 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
145 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
138 Mailer.deliver_test(User.find(1))
146 Mailer.deliver_test(User.find(1))
139 end
147 end
140 mail = ActionMailer::Base.deliveries.last
148 mail = ActionMailer::Base.deliveries.last
141 assert_not_nil mail
149 assert_not_nil mail
142 assert_equal 'Redmine app', mail.from_addrs.first.name
150 assert_equal 'Redmine app', mail.from_addrs.first.name
143 end
151 end
144
152
145 def test_should_not_send_email_without_recipient
153 def test_should_not_send_email_without_recipient
146 news = News.find(:first)
154 news = News.find(:first)
147 user = news.author
155 user = news.author
148 # Remove members except news author
156 # Remove members except news author
149 news.project.memberships.each {|m| m.destroy unless m.user == user}
157 news.project.memberships.each {|m| m.destroy unless m.user == user}
150
158
151 user.pref[:no_self_notified] = false
159 user.pref[:no_self_notified] = false
152 user.pref.save
160 user.pref.save
153 User.current = user
161 User.current = user
154 Mailer.deliver_news_added(news.reload)
162 Mailer.deliver_news_added(news.reload)
155 assert_equal 1, last_email.bcc.size
163 assert_equal 1, last_email.bcc.size
156
164
157 # nobody to notify
165 # nobody to notify
158 user.pref[:no_self_notified] = true
166 user.pref[:no_self_notified] = true
159 user.pref.save
167 user.pref.save
160 User.current = user
168 User.current = user
161 ActionMailer::Base.deliveries.clear
169 ActionMailer::Base.deliveries.clear
162 Mailer.deliver_news_added(news.reload)
170 Mailer.deliver_news_added(news.reload)
163 assert ActionMailer::Base.deliveries.empty?
171 assert ActionMailer::Base.deliveries.empty?
164 end
172 end
165
173
166 def test_issue_add_message_id
174 def test_issue_add_message_id
167 issue = Issue.find(1)
175 issue = Issue.find(1)
168 Mailer.deliver_issue_add(issue)
176 Mailer.deliver_issue_add(issue)
169 mail = ActionMailer::Base.deliveries.last
177 mail = ActionMailer::Base.deliveries.last
170 assert_not_nil mail
178 assert_not_nil mail
171 assert_equal Mailer.message_id_for(issue), mail.message_id
179 assert_equal Mailer.message_id_for(issue), mail.message_id
172 assert_nil mail.references
180 assert_nil mail.references
173 end
181 end
174
182
175 def test_issue_edit_message_id
183 def test_issue_edit_message_id
176 journal = Journal.find(1)
184 journal = Journal.find(1)
177 Mailer.deliver_issue_edit(journal)
185 Mailer.deliver_issue_edit(journal)
178 mail = ActionMailer::Base.deliveries.last
186 mail = ActionMailer::Base.deliveries.last
179 assert_not_nil mail
187 assert_not_nil mail
180 assert_equal Mailer.message_id_for(journal), mail.message_id
188 assert_equal Mailer.message_id_for(journal), mail.message_id
181 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
189 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
182 end
190 end
183
191
184 def test_message_posted_message_id
192 def test_message_posted_message_id
185 message = Message.find(1)
193 message = Message.find(1)
186 Mailer.deliver_message_posted(message)
194 Mailer.deliver_message_posted(message)
187 mail = ActionMailer::Base.deliveries.last
195 mail = ActionMailer::Base.deliveries.last
188 assert_not_nil mail
196 assert_not_nil mail
189 assert_equal Mailer.message_id_for(message), mail.message_id
197 assert_equal Mailer.message_id_for(message), mail.message_id
190 assert_nil mail.references
198 assert_nil mail.references
191 assert_select_email do
199 assert_select_email do
192 # link to the message
200 # link to the message
193 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}", :text => message.subject
201 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}", :text => message.subject
194 end
202 end
195 end
203 end
196
204
197 def test_reply_posted_message_id
205 def test_reply_posted_message_id
198 message = Message.find(3)
206 message = Message.find(3)
199 Mailer.deliver_message_posted(message)
207 Mailer.deliver_message_posted(message)
200 mail = ActionMailer::Base.deliveries.last
208 mail = ActionMailer::Base.deliveries.last
201 assert_not_nil mail
209 assert_not_nil mail
202 assert_equal Mailer.message_id_for(message), mail.message_id
210 assert_equal Mailer.message_id_for(message), mail.message_id
203 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
211 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
204 assert_select_email do
212 assert_select_email do
205 # link to the reply
213 # link to the reply
206 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}", :text => message.subject
214 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}", :text => message.subject
207 end
215 end
208 end
216 end
209
217
210 context("#issue_add") do
218 context("#issue_add") do
211 setup do
219 setup do
212 ActionMailer::Base.deliveries.clear
220 ActionMailer::Base.deliveries.clear
213 Setting.bcc_recipients = '1'
221 Setting.bcc_recipients = '1'
214 @issue = Issue.find(1)
222 @issue = Issue.find(1)
215 end
223 end
216
224
217 should "notify project members" do
225 should "notify project members" do
218 assert Mailer.deliver_issue_add(@issue)
226 assert Mailer.deliver_issue_add(@issue)
219 assert last_email.bcc.include?('dlopper@somenet.foo')
227 assert last_email.bcc.include?('dlopper@somenet.foo')
220 end
228 end
221
229
222 should "not notify project members that are not allow to view the issue" do
230 should "not notify project members that are not allow to view the issue" do
223 Role.find(2).remove_permission!(:view_issues)
231 Role.find(2).remove_permission!(:view_issues)
224 assert Mailer.deliver_issue_add(@issue)
232 assert Mailer.deliver_issue_add(@issue)
225 assert !last_email.bcc.include?('dlopper@somenet.foo')
233 assert !last_email.bcc.include?('dlopper@somenet.foo')
226 end
234 end
227
235
228 should "notify issue watchers" do
236 should "notify issue watchers" do
229 user = User.find(9)
237 user = User.find(9)
230 # minimal email notification options
238 # minimal email notification options
231 user.pref[:no_self_notified] = '1'
239 user.pref[:no_self_notified] = '1'
232 user.pref.save
240 user.pref.save
233 user.mail_notification = false
241 user.mail_notification = false
234 user.save
242 user.save
235
243
236 Watcher.create!(:watchable => @issue, :user => user)
244 Watcher.create!(:watchable => @issue, :user => user)
237 assert Mailer.deliver_issue_add(@issue)
245 assert Mailer.deliver_issue_add(@issue)
238 assert last_email.bcc.include?(user.mail)
246 assert last_email.bcc.include?(user.mail)
239 end
247 end
240
248
241 should "not notify watchers not allowed to view the issue" do
249 should "not notify watchers not allowed to view the issue" do
242 user = User.find(9)
250 user = User.find(9)
243 Watcher.create!(:watchable => @issue, :user => user)
251 Watcher.create!(:watchable => @issue, :user => user)
244 Role.non_member.remove_permission!(:view_issues)
252 Role.non_member.remove_permission!(:view_issues)
245 assert Mailer.deliver_issue_add(@issue)
253 assert Mailer.deliver_issue_add(@issue)
246 assert !last_email.bcc.include?(user.mail)
254 assert !last_email.bcc.include?(user.mail)
247 end
255 end
248 end
256 end
249
257
250 # test mailer methods for each language
258 # test mailer methods for each language
251 def test_issue_add
259 def test_issue_add
252 issue = Issue.find(1)
260 issue = Issue.find(1)
253 valid_languages.each do |lang|
261 valid_languages.each do |lang|
254 Setting.default_language = lang.to_s
262 Setting.default_language = lang.to_s
255 assert Mailer.deliver_issue_add(issue)
263 assert Mailer.deliver_issue_add(issue)
256 end
264 end
257 end
265 end
258
266
259 def test_issue_edit
267 def test_issue_edit
260 journal = Journal.find(1)
268 journal = Journal.find(1)
261 valid_languages.each do |lang|
269 valid_languages.each do |lang|
262 Setting.default_language = lang.to_s
270 Setting.default_language = lang.to_s
263 assert Mailer.deliver_issue_edit(journal)
271 assert Mailer.deliver_issue_edit(journal)
264 end
272 end
265 end
273 end
266
274
267 def test_document_added
275 def test_document_added
268 document = Document.find(1)
276 document = Document.find(1)
269 valid_languages.each do |lang|
277 valid_languages.each do |lang|
270 Setting.default_language = lang.to_s
278 Setting.default_language = lang.to_s
271 assert Mailer.deliver_document_added(document)
279 assert Mailer.deliver_document_added(document)
272 end
280 end
273 end
281 end
274
282
275 def test_attachments_added
283 def test_attachments_added
276 attachements = [ Attachment.find_by_container_type('Document') ]
284 attachements = [ Attachment.find_by_container_type('Document') ]
277 valid_languages.each do |lang|
285 valid_languages.each do |lang|
278 Setting.default_language = lang.to_s
286 Setting.default_language = lang.to_s
279 assert Mailer.deliver_attachments_added(attachements)
287 assert Mailer.deliver_attachments_added(attachements)
280 end
288 end
281 end
289 end
282
290
283 def test_version_file_added
291 def test_version_file_added
284 attachements = [ Attachment.find_by_container_type('Version') ]
292 attachements = [ Attachment.find_by_container_type('Version') ]
285 assert Mailer.deliver_attachments_added(attachements)
293 assert Mailer.deliver_attachments_added(attachements)
286 assert_not_nil last_email.bcc
294 assert_not_nil last_email.bcc
287 assert last_email.bcc.any?
295 assert last_email.bcc.any?
288 assert_select_email do
296 assert_select_email do
289 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
297 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
290 end
298 end
291 end
299 end
292
300
293 def test_project_file_added
301 def test_project_file_added
294 attachements = [ Attachment.find_by_container_type('Project') ]
302 attachements = [ Attachment.find_by_container_type('Project') ]
295 assert Mailer.deliver_attachments_added(attachements)
303 assert Mailer.deliver_attachments_added(attachements)
296 assert_not_nil last_email.bcc
304 assert_not_nil last_email.bcc
297 assert last_email.bcc.any?
305 assert last_email.bcc.any?
298 assert_select_email do
306 assert_select_email do
299 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
307 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
300 end
308 end
301 end
309 end
302
310
303 def test_news_added
311 def test_news_added
304 news = News.find(:first)
312 news = News.find(:first)
305 valid_languages.each do |lang|
313 valid_languages.each do |lang|
306 Setting.default_language = lang.to_s
314 Setting.default_language = lang.to_s
307 assert Mailer.deliver_news_added(news)
315 assert Mailer.deliver_news_added(news)
308 end
316 end
309 end
317 end
310
318
311 def test_news_comment_added
319 def test_news_comment_added
312 comment = Comment.find(2)
320 comment = Comment.find(2)
313 valid_languages.each do |lang|
321 valid_languages.each do |lang|
314 Setting.default_language = lang.to_s
322 Setting.default_language = lang.to_s
315 assert Mailer.deliver_news_comment_added(comment)
323 assert Mailer.deliver_news_comment_added(comment)
316 end
324 end
317 end
325 end
318
326
319 def test_message_posted
327 def test_message_posted
320 message = Message.find(:first)
328 message = Message.find(:first)
321 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
329 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
322 recipients = recipients.compact.uniq
330 recipients = recipients.compact.uniq
323 valid_languages.each do |lang|
331 valid_languages.each do |lang|
324 Setting.default_language = lang.to_s
332 Setting.default_language = lang.to_s
325 assert Mailer.deliver_message_posted(message)
333 assert Mailer.deliver_message_posted(message)
326 end
334 end
327 end
335 end
328
336
329 def test_wiki_content_added
337 def test_wiki_content_added
330 content = WikiContent.find(:first)
338 content = WikiContent.find(:first)
331 valid_languages.each do |lang|
339 valid_languages.each do |lang|
332 Setting.default_language = lang.to_s
340 Setting.default_language = lang.to_s
333 assert_difference 'ActionMailer::Base.deliveries.size' do
341 assert_difference 'ActionMailer::Base.deliveries.size' do
334 assert Mailer.deliver_wiki_content_added(content)
342 assert Mailer.deliver_wiki_content_added(content)
335 end
343 end
336 end
344 end
337 end
345 end
338
346
339 def test_wiki_content_updated
347 def test_wiki_content_updated
340 content = WikiContent.find(:first)
348 content = WikiContent.find(:first)
341 valid_languages.each do |lang|
349 valid_languages.each do |lang|
342 Setting.default_language = lang.to_s
350 Setting.default_language = lang.to_s
343 assert_difference 'ActionMailer::Base.deliveries.size' do
351 assert_difference 'ActionMailer::Base.deliveries.size' do
344 assert Mailer.deliver_wiki_content_updated(content)
352 assert Mailer.deliver_wiki_content_updated(content)
345 end
353 end
346 end
354 end
347 end
355 end
348
356
349 def test_account_information
357 def test_account_information
350 user = User.find(2)
358 user = User.find(2)
351 valid_languages.each do |lang|
359 valid_languages.each do |lang|
352 user.update_attribute :language, lang.to_s
360 user.update_attribute :language, lang.to_s
353 user.reload
361 user.reload
354 assert Mailer.deliver_account_information(user, 'pAsswORd')
362 assert Mailer.deliver_account_information(user, 'pAsswORd')
355 end
363 end
356 end
364 end
357
365
358 def test_lost_password
366 def test_lost_password
359 token = Token.find(2)
367 token = Token.find(2)
360 valid_languages.each do |lang|
368 valid_languages.each do |lang|
361 token.user.update_attribute :language, lang.to_s
369 token.user.update_attribute :language, lang.to_s
362 token.reload
370 token.reload
363 assert Mailer.deliver_lost_password(token)
371 assert Mailer.deliver_lost_password(token)
364 end
372 end
365 end
373 end
366
374
367 def test_register
375 def test_register
368 token = Token.find(1)
376 token = Token.find(1)
369 Setting.host_name = 'redmine.foo'
377 Setting.host_name = 'redmine.foo'
370 Setting.protocol = 'https'
378 Setting.protocol = 'https'
371
379
372 valid_languages.each do |lang|
380 valid_languages.each do |lang|
373 token.user.update_attribute :language, lang.to_s
381 token.user.update_attribute :language, lang.to_s
374 token.reload
382 token.reload
375 ActionMailer::Base.deliveries.clear
383 ActionMailer::Base.deliveries.clear
376 assert Mailer.deliver_register(token)
384 assert Mailer.deliver_register(token)
377 mail = ActionMailer::Base.deliveries.last
385 mail = ActionMailer::Base.deliveries.last
378 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
386 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
379 end
387 end
380 end
388 end
381
389
382 def test_test
390 def test_test
383 user = User.find(1)
391 user = User.find(1)
384 valid_languages.each do |lang|
392 valid_languages.each do |lang|
385 user.update_attribute :language, lang.to_s
393 user.update_attribute :language, lang.to_s
386 assert Mailer.deliver_test(user)
394 assert Mailer.deliver_test(user)
387 end
395 end
388 end
396 end
389
397
390 def test_reminders
398 def test_reminders
391 Mailer.reminders(:days => 42)
399 Mailer.reminders(:days => 42)
392 assert_equal 1, ActionMailer::Base.deliveries.size
400 assert_equal 1, ActionMailer::Base.deliveries.size
393 mail = ActionMailer::Base.deliveries.last
401 mail = ActionMailer::Base.deliveries.last
394 assert mail.bcc.include?('dlopper@somenet.foo')
402 assert mail.bcc.include?('dlopper@somenet.foo')
395 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
403 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
396 assert_equal '1 issue(s) due in the next 42 days', mail.subject
404 assert_equal '1 issue(s) due in the next 42 days', mail.subject
397 end
405 end
398
406
399 def test_reminders_for_users
407 def test_reminders_for_users
400 Mailer.reminders(:days => 42, :users => ['5'])
408 Mailer.reminders(:days => 42, :users => ['5'])
401 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
409 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
402 Mailer.reminders(:days => 42, :users => ['3'])
410 Mailer.reminders(:days => 42, :users => ['3'])
403 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
411 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
404 mail = ActionMailer::Base.deliveries.last
412 mail = ActionMailer::Base.deliveries.last
405 assert mail.bcc.include?('dlopper@somenet.foo')
413 assert mail.bcc.include?('dlopper@somenet.foo')
406 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
414 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
407 end
415 end
408
416
409 def last_email
417 def last_email
410 mail = ActionMailer::Base.deliveries.last
418 mail = ActionMailer::Base.deliveries.last
411 assert_not_nil mail
419 assert_not_nil mail
412 mail
420 mail
413 end
421 end
414
422
415 def test_mailer_should_not_change_locale
423 def test_mailer_should_not_change_locale
416 Setting.default_language = 'en'
424 Setting.default_language = 'en'
417 # Set current language to italian
425 # Set current language to italian
418 set_language_if_valid 'it'
426 set_language_if_valid 'it'
419 # Send an email to a french user
427 # Send an email to a french user
420 user = User.find(1)
428 user = User.find(1)
421 user.language = 'fr'
429 user.language = 'fr'
422 Mailer.deliver_account_activated(user)
430 Mailer.deliver_account_activated(user)
423 mail = ActionMailer::Base.deliveries.last
431 mail = ActionMailer::Base.deliveries.last
424 assert mail.body.include?('Votre compte')
432 assert mail.body.include?('Votre compte')
425
433
426 assert_equal :it, current_language
434 assert_equal :it, current_language
427 end
435 end
428
436
429 def test_with_deliveries_off
437 def test_with_deliveries_off
430 Mailer.with_deliveries false do
438 Mailer.with_deliveries false do
431 Mailer.deliver_test(User.find(1))
439 Mailer.deliver_test(User.find(1))
432 end
440 end
433 assert ActionMailer::Base.deliveries.empty?
441 assert ActionMailer::Base.deliveries.empty?
434 # should restore perform_deliveries
442 # should restore perform_deliveries
435 assert ActionMailer::Base.perform_deliveries
443 assert ActionMailer::Base.perform_deliveries
436 end
444 end
437
445
438 context "layout" do
446 context "layout" do
439 should "include the emails_header" do
447 should "include the emails_header" do
440 with_settings(:emails_header => "*Header content*") do
448 with_settings(:emails_header => "*Header content*") do
441 assert Mailer.deliver_test(User.find(1))
449 assert Mailer.deliver_test(User.find(1))
442
450
443 assert_select_email do
451 assert_select_email do
444 assert_select ".header" do
452 assert_select ".header" do
445 assert_select "strong", :text => "Header content"
453 assert_select "strong", :text => "Header content"
446 end
454 end
447 end
455 end
448 end
456 end
449 end
457 end
450 end
458 end
451 end
459 end
General Comments 0
You need to be logged in to leave comments. Login now