@@ -1,841 +1,835 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class MailerTest < ActiveSupport::TestCase |
|
21 | 21 | include Redmine::I18n |
|
22 | 22 | include Rails::Dom::Testing::Assertions |
|
23 | 23 | fixtures :projects, :enabled_modules, :issues, :users, :email_addresses, :members, |
|
24 | 24 | :member_roles, :roles, :documents, :attachments, :news, |
|
25 | 25 | :tokens, :journals, :journal_details, :changesets, |
|
26 | 26 | :trackers, :projects_trackers, |
|
27 | 27 | :issue_statuses, :enumerations, :messages, :boards, :repositories, |
|
28 | 28 | :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions, |
|
29 | 29 | :versions, |
|
30 | 30 | :comments |
|
31 | 31 | |
|
32 | 32 | def setup |
|
33 | 33 | ActionMailer::Base.deliveries.clear |
|
34 | 34 | Setting.host_name = 'mydomain.foo' |
|
35 | 35 | Setting.protocol = 'http' |
|
36 | 36 | Setting.plain_text_mail = '0' |
|
37 | 37 | Setting.default_language = 'en' |
|
38 | 38 | User.current = nil |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def test_generated_links_in_emails |
|
42 | 42 | Setting.host_name = 'mydomain.foo' |
|
43 | 43 | Setting.protocol = 'https' |
|
44 | 44 | |
|
45 | 45 | journal = Journal.find(3) |
|
46 | 46 | assert Mailer.deliver_issue_edit(journal) |
|
47 | 47 | |
|
48 | 48 | mail = last_email |
|
49 | 49 | assert_not_nil mail |
|
50 | 50 | |
|
51 | 51 | assert_select_email do |
|
52 | 52 | # link to the main ticket |
|
53 | 53 | assert_select 'a[href=?]', |
|
54 | 54 | 'https://mydomain.foo/issues/2#change-3', |
|
55 | 55 | :text => 'Feature request #2: Add ingredients categories' |
|
56 | 56 | # link to a referenced ticket |
|
57 | 57 | assert_select 'a[href=?][title=?]', |
|
58 | 58 | 'https://mydomain.foo/issues/1', |
|
59 | 59 | "Cannot print recipes (New)", |
|
60 | 60 | :text => '#1' |
|
61 | 61 | # link to a changeset |
|
62 | 62 | assert_select 'a[href=?][title=?]', |
|
63 | 63 | 'https://mydomain.foo/projects/ecookbook/repository/revisions/2', |
|
64 | 64 | 'This commit fixes #1, #2 and references #1 & #3', |
|
65 | 65 | :text => 'r2' |
|
66 | 66 | # link to a description diff |
|
67 | 67 | assert_select 'a[href^=?][title=?]', |
|
68 | 68 | # should be https://mydomain.foo/journals/diff/3?detail_id=4 |
|
69 | 69 | # but the Rails 4.2 DOM assertion doesn't handle the ? in the |
|
70 | 70 | # attribute value |
|
71 | 71 | 'https://mydomain.foo/journals/diff/3', |
|
72 | 72 | 'View differences', |
|
73 | 73 | :text => 'diff' |
|
74 | 74 | # link to an attachment |
|
75 | 75 | assert_select 'a[href=?]', |
|
76 | 76 | 'https://mydomain.foo/attachments/download/4/source.rb', |
|
77 | 77 | :text => 'source.rb' |
|
78 | 78 | end |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def test_generated_links_with_prefix |
|
82 | 82 | relative_url_root = Redmine::Utils.relative_url_root |
|
83 | 83 | Setting.host_name = 'mydomain.foo/rdm' |
|
84 | 84 | Setting.protocol = 'http' |
|
85 | 85 | |
|
86 | 86 | journal = Journal.find(3) |
|
87 | 87 | assert Mailer.deliver_issue_edit(journal) |
|
88 | 88 | |
|
89 | 89 | mail = last_email |
|
90 | 90 | assert_not_nil mail |
|
91 | 91 | |
|
92 | 92 | assert_select_email do |
|
93 | 93 | # link to the main ticket |
|
94 | 94 | assert_select 'a[href=?]', |
|
95 | 95 | 'http://mydomain.foo/rdm/issues/2#change-3', |
|
96 | 96 | :text => 'Feature request #2: Add ingredients categories' |
|
97 | 97 | # link to a referenced ticket |
|
98 | 98 | assert_select 'a[href=?][title=?]', |
|
99 | 99 | 'http://mydomain.foo/rdm/issues/1', |
|
100 | 100 | "Cannot print recipes (New)", |
|
101 | 101 | :text => '#1' |
|
102 | 102 | # link to a changeset |
|
103 | 103 | assert_select 'a[href=?][title=?]', |
|
104 | 104 | 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2', |
|
105 | 105 | 'This commit fixes #1, #2 and references #1 & #3', |
|
106 | 106 | :text => 'r2' |
|
107 | 107 | # link to a description diff |
|
108 | 108 | assert_select 'a[href^=?][title=?]', |
|
109 | 109 | # should be http://mydomain.foo/rdm/journals/diff/3?detail_id=4 |
|
110 | 110 | # but the Rails 4.2 DOM assertion doesn't handle the ? in the |
|
111 | 111 | # attribute value |
|
112 | 112 | 'http://mydomain.foo/rdm/journals/diff/3', |
|
113 | 113 | 'View differences', |
|
114 | 114 | :text => 'diff' |
|
115 | 115 | # link to an attachment |
|
116 | 116 | assert_select 'a[href=?]', |
|
117 | 117 | 'http://mydomain.foo/rdm/attachments/download/4/source.rb', |
|
118 | 118 | :text => 'source.rb' |
|
119 | 119 | end |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def test_issue_edit_should_generate_url_with_hostname_for_relations |
|
123 | 123 | journal = Journal.new(:journalized => Issue.find(1), :user => User.find(1), :created_on => Time.now) |
|
124 | 124 | journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'label_relates_to', :value => 2) |
|
125 | 125 | Mailer.deliver_issue_edit(journal) |
|
126 | 126 | assert_not_nil last_email |
|
127 | 127 | assert_select_email do |
|
128 | 128 | assert_select 'a[href=?]', 'http://mydomain.foo/issues/2', :text => 'Feature request #2' |
|
129 | 129 | end |
|
130 | 130 | end |
|
131 | 131 | |
|
132 | 132 | def test_generated_links_with_prefix_and_no_relative_url_root |
|
133 | 133 | relative_url_root = Redmine::Utils.relative_url_root |
|
134 | 134 | Setting.host_name = 'mydomain.foo/rdm' |
|
135 | 135 | Setting.protocol = 'http' |
|
136 | 136 | Redmine::Utils.relative_url_root = nil |
|
137 | 137 | |
|
138 | 138 | journal = Journal.find(3) |
|
139 | 139 | assert Mailer.deliver_issue_edit(journal) |
|
140 | 140 | |
|
141 | 141 | mail = last_email |
|
142 | 142 | assert_not_nil mail |
|
143 | 143 | |
|
144 | 144 | assert_select_email do |
|
145 | 145 | # link to the main ticket |
|
146 | 146 | assert_select 'a[href=?]', |
|
147 | 147 | 'http://mydomain.foo/rdm/issues/2#change-3', |
|
148 | 148 | :text => 'Feature request #2: Add ingredients categories' |
|
149 | 149 | # link to a referenced ticket |
|
150 | 150 | assert_select 'a[href=?][title=?]', |
|
151 | 151 | 'http://mydomain.foo/rdm/issues/1', |
|
152 | 152 | "Cannot print recipes (New)", |
|
153 | 153 | :text => '#1' |
|
154 | 154 | # link to a changeset |
|
155 | 155 | assert_select 'a[href=?][title=?]', |
|
156 | 156 | 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2', |
|
157 | 157 | 'This commit fixes #1, #2 and references #1 & #3', |
|
158 | 158 | :text => 'r2' |
|
159 | 159 | # link to a description diff |
|
160 | 160 | assert_select 'a[href^=?][title=?]', |
|
161 | 161 | # should be http://mydomain.foo/rdm/journals/diff/3?detail_id=4 |
|
162 | 162 | # but the Rails 4.2 DOM assertion doesn't handle the ? in the |
|
163 | 163 | # attribute value |
|
164 | 164 | 'http://mydomain.foo/rdm/journals/diff/3', |
|
165 | 165 | 'View differences', |
|
166 | 166 | :text => 'diff' |
|
167 | 167 | # link to an attachment |
|
168 | 168 | assert_select 'a[href=?]', |
|
169 | 169 | 'http://mydomain.foo/rdm/attachments/download/4/source.rb', |
|
170 | 170 | :text => 'source.rb' |
|
171 | 171 | end |
|
172 | 172 | ensure |
|
173 | 173 | # restore it |
|
174 | 174 | Redmine::Utils.relative_url_root = relative_url_root |
|
175 | 175 | end |
|
176 | 176 | |
|
177 | 177 | def test_email_headers |
|
178 | 178 | issue = Issue.find(1) |
|
179 | 179 | Mailer.deliver_issue_add(issue) |
|
180 | 180 | mail = last_email |
|
181 | 181 | assert_not_nil mail |
|
182 | 182 | assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s |
|
183 | 183 | assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s |
|
184 | 184 | assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s |
|
185 | 185 | end |
|
186 | 186 | |
|
187 | 187 | def test_email_headers_should_include_sender |
|
188 | 188 | issue = Issue.find(1) |
|
189 | 189 | Mailer.deliver_issue_add(issue) |
|
190 | 190 | mail = last_email |
|
191 | 191 | assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s |
|
192 | 192 | end |
|
193 | 193 | |
|
194 | 194 | def test_plain_text_mail |
|
195 | 195 | Setting.plain_text_mail = 1 |
|
196 | 196 | journal = Journal.find(2) |
|
197 | 197 | Mailer.deliver_issue_edit(journal) |
|
198 | 198 | mail = last_email |
|
199 | 199 | assert_equal "text/plain; charset=UTF-8", mail.content_type |
|
200 | 200 | assert_equal 0, mail.parts.size |
|
201 | 201 | assert !mail.encoded.include?('href') |
|
202 | 202 | end |
|
203 | 203 | |
|
204 | 204 | def test_html_mail |
|
205 | 205 | Setting.plain_text_mail = 0 |
|
206 | 206 | journal = Journal.find(2) |
|
207 | 207 | Mailer.deliver_issue_edit(journal) |
|
208 | 208 | mail = last_email |
|
209 | 209 | assert_equal 2, mail.parts.size |
|
210 | 210 | assert mail.encoded.include?('href') |
|
211 | 211 | end |
|
212 | 212 | |
|
213 | 213 | def test_from_header |
|
214 | 214 | with_settings :mail_from => 'redmine@example.net' do |
|
215 | 215 | Mailer.test_email(User.find(1)).deliver |
|
216 | 216 | end |
|
217 | 217 | mail = last_email |
|
218 | 218 | assert_equal 'redmine@example.net', mail.from_addrs.first |
|
219 | 219 | end |
|
220 | 220 | |
|
221 | 221 | def test_from_header_with_phrase |
|
222 | 222 | with_settings :mail_from => 'Redmine app <redmine@example.net>' do |
|
223 | 223 | Mailer.test_email(User.find(1)).deliver |
|
224 | 224 | end |
|
225 | 225 | mail = last_email |
|
226 | 226 | assert_equal 'redmine@example.net', mail.from_addrs.first |
|
227 | 227 | assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s |
|
228 | 228 | end |
|
229 | 229 | |
|
230 | 230 | def test_should_not_send_email_without_recipient |
|
231 | 231 | news = News.first |
|
232 | 232 | user = news.author |
|
233 | 233 | # Remove members except news author |
|
234 | 234 | news.project.memberships.each {|m| m.destroy unless m.user == user} |
|
235 | 235 | |
|
236 | 236 | user.pref.no_self_notified = false |
|
237 | 237 | user.pref.save |
|
238 | 238 | User.current = user |
|
239 | 239 | Mailer.news_added(news.reload).deliver |
|
240 | 240 | assert_equal 1, last_email.bcc.size |
|
241 | 241 | |
|
242 | 242 | # nobody to notify |
|
243 | 243 | user.pref.no_self_notified = true |
|
244 | 244 | user.pref.save |
|
245 | 245 | User.current = user |
|
246 | 246 | ActionMailer::Base.deliveries.clear |
|
247 | 247 | Mailer.news_added(news.reload).deliver |
|
248 | 248 | assert ActionMailer::Base.deliveries.empty? |
|
249 | 249 | end |
|
250 | 250 | |
|
251 | 251 | def test_issue_add_message_id |
|
252 | 252 | issue = Issue.find(2) |
|
253 | 253 | Mailer.deliver_issue_add(issue) |
|
254 | 254 | mail = last_email |
|
255 | 255 | assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id |
|
256 | 256 | assert_include "redmine.issue-2.20060719190421@example.net", mail.references |
|
257 | 257 | end |
|
258 | 258 | |
|
259 | 259 | def test_issue_edit_message_id |
|
260 | 260 | journal = Journal.find(3) |
|
261 | 261 | journal.issue = Issue.find(2) |
|
262 | 262 | |
|
263 | 263 | Mailer.deliver_issue_edit(journal) |
|
264 | 264 | mail = last_email |
|
265 | 265 | assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id |
|
266 | 266 | assert_include "redmine.issue-2.20060719190421@example.net", mail.references |
|
267 | 267 | assert_select_email do |
|
268 | 268 | # link to the update |
|
269 | 269 | assert_select "a[href=?]", |
|
270 | 270 | "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}" |
|
271 | 271 | end |
|
272 | 272 | end |
|
273 | 273 | |
|
274 | 274 | def test_message_posted_message_id |
|
275 | 275 | message = Message.find(1) |
|
276 | 276 | Mailer.message_posted(message).deliver |
|
277 | 277 | mail = last_email |
|
278 | 278 | assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id |
|
279 | 279 | assert_include "redmine.message-1.20070512151532@example.net", mail.references |
|
280 | 280 | assert_select_email do |
|
281 | 281 | # link to the message |
|
282 | 282 | assert_select "a[href=?]", |
|
283 | 283 | "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}", |
|
284 | 284 | :text => message.subject |
|
285 | 285 | end |
|
286 | 286 | end |
|
287 | 287 | |
|
288 | 288 | def test_reply_posted_message_id |
|
289 | 289 | message = Message.find(3) |
|
290 | 290 | Mailer.message_posted(message).deliver |
|
291 | 291 | mail = last_email |
|
292 | 292 | assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id |
|
293 | 293 | assert_include "redmine.message-1.20070512151532@example.net", mail.references |
|
294 | 294 | assert_select_email do |
|
295 | 295 | # link to the reply |
|
296 | 296 | assert_select "a[href=?]", |
|
297 | 297 | "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}", |
|
298 | 298 | :text => message.subject |
|
299 | 299 | end |
|
300 | 300 | end |
|
301 | 301 | |
|
302 | 302 | test "#issue_add should notify project members" do |
|
303 | 303 | issue = Issue.find(1) |
|
304 | 304 | assert Mailer.deliver_issue_add(issue) |
|
305 | 305 | assert last_email.bcc.include?('dlopper@somenet.foo') |
|
306 | 306 | end |
|
307 | 307 | |
|
308 | 308 | def test_issue_add_should_send_mail_to_all_user_email_address |
|
309 | 309 | EmailAddress.create!(:user_id => 3, :address => 'otheremail@somenet.foo') |
|
310 | 310 | issue = Issue.find(1) |
|
311 | 311 | assert Mailer.deliver_issue_add(issue) |
|
312 | 312 | assert last_email.bcc.include?('dlopper@somenet.foo') |
|
313 | 313 | assert last_email.bcc.include?('otheremail@somenet.foo') |
|
314 | 314 | end |
|
315 | 315 | |
|
316 | 316 | test "#issue_add should not notify project members that are not allow to view the issue" do |
|
317 | 317 | issue = Issue.find(1) |
|
318 | 318 | Role.find(2).remove_permission!(:view_issues) |
|
319 | 319 | assert Mailer.deliver_issue_add(issue) |
|
320 | 320 | assert !last_email.bcc.include?('dlopper@somenet.foo') |
|
321 | 321 | end |
|
322 | 322 | |
|
323 | 323 | test "#issue_add should notify issue watchers" do |
|
324 | 324 | issue = Issue.find(1) |
|
325 | 325 | user = User.find(9) |
|
326 | 326 | # minimal email notification options |
|
327 | 327 | user.pref.no_self_notified = '1' |
|
328 | 328 | user.pref.save |
|
329 | 329 | user.mail_notification = false |
|
330 | 330 | user.save |
|
331 | 331 | |
|
332 | 332 | Watcher.create!(:watchable => issue, :user => user) |
|
333 | 333 | assert Mailer.deliver_issue_add(issue) |
|
334 | 334 | assert last_email.bcc.include?(user.mail) |
|
335 | 335 | end |
|
336 | 336 | |
|
337 | 337 | test "#issue_add should not notify watchers not allowed to view the issue" do |
|
338 | 338 | issue = Issue.find(1) |
|
339 | 339 | user = User.find(9) |
|
340 | 340 | Watcher.create!(:watchable => issue, :user => user) |
|
341 | 341 | Role.non_member.remove_permission!(:view_issues) |
|
342 | 342 | assert Mailer.deliver_issue_add(issue) |
|
343 | 343 | assert !last_email.bcc.include?(user.mail) |
|
344 | 344 | end |
|
345 | 345 | |
|
346 | 346 | def test_issue_add_should_include_enabled_fields |
|
347 | 347 | issue = Issue.find(2) |
|
348 | 348 | assert Mailer.deliver_issue_add(issue) |
|
349 | 349 | assert_mail_body_match '* Target version: 1.0', last_email |
|
350 | 350 | assert_select_email do |
|
351 | 351 | assert_select 'li', :text => 'Target version: 1.0' |
|
352 | 352 | end |
|
353 | 353 | end |
|
354 | 354 | |
|
355 | 355 | def test_issue_add_should_not_include_disabled_fields |
|
356 | 356 | issue = Issue.find(2) |
|
357 | 357 | tracker = issue.tracker |
|
358 | 358 | tracker.core_fields -= ['fixed_version_id'] |
|
359 | 359 | tracker.save! |
|
360 | 360 | assert Mailer.deliver_issue_add(issue) |
|
361 | 361 | assert_mail_body_no_match 'Target version', last_email |
|
362 | 362 | assert_select_email do |
|
363 | 363 | assert_select 'li', :text => /Target version/, :count => 0 |
|
364 | 364 | end |
|
365 | 365 | end |
|
366 | 366 | |
|
367 | 367 | # test mailer methods for each language |
|
368 | 368 | def test_issue_add |
|
369 | 369 | issue = Issue.find(1) |
|
370 | 370 | with_each_language_as_default do |
|
371 | 371 | assert Mailer.deliver_issue_add(issue) |
|
372 | 372 | end |
|
373 | 373 | end |
|
374 | 374 | |
|
375 | 375 | def test_issue_edit |
|
376 | 376 | journal = Journal.find(1) |
|
377 | 377 | with_each_language_as_default do |
|
378 | 378 | assert Mailer.deliver_issue_edit(journal) |
|
379 | 379 | end |
|
380 | 380 | end |
|
381 | 381 | |
|
382 | 382 | def test_issue_edit_should_send_private_notes_to_users_with_permission_only |
|
383 | 383 | journal = Journal.find(1) |
|
384 | 384 | journal.private_notes = true |
|
385 | 385 | journal.save! |
|
386 | 386 | |
|
387 | 387 | Role.find(2).add_permission! :view_private_notes |
|
388 | 388 | Mailer.deliver_issue_edit(journal) |
|
389 | 389 | assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort |
|
390 | 390 | |
|
391 | 391 | Role.find(2).remove_permission! :view_private_notes |
|
392 | 392 | Mailer.deliver_issue_edit(journal) |
|
393 | 393 | assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort |
|
394 | 394 | end |
|
395 | 395 | |
|
396 | 396 | def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only |
|
397 | 397 | Issue.find(1).set_watcher(User.find_by_login('someone')) |
|
398 | 398 | journal = Journal.find(1) |
|
399 | 399 | journal.private_notes = true |
|
400 | 400 | journal.save! |
|
401 | 401 | |
|
402 | 402 | Role.non_member.add_permission! :view_private_notes |
|
403 | 403 | Mailer.deliver_issue_edit(journal) |
|
404 | 404 | assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort |
|
405 | 405 | |
|
406 | 406 | Role.non_member.remove_permission! :view_private_notes |
|
407 | 407 | Mailer.deliver_issue_edit(journal) |
|
408 | 408 | assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort |
|
409 | 409 | end |
|
410 | 410 | |
|
411 | 411 | def test_issue_edit_should_mark_private_notes |
|
412 | 412 | journal = Journal.find(2) |
|
413 | 413 | journal.private_notes = true |
|
414 | 414 | journal.save! |
|
415 | 415 | |
|
416 | 416 | with_settings :default_language => 'en' do |
|
417 | 417 | Mailer.deliver_issue_edit(journal) |
|
418 | 418 | end |
|
419 | 419 | assert_mail_body_match '(Private notes)', last_email |
|
420 | 420 | end |
|
421 | 421 | |
|
422 | 422 | def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue |
|
423 | 423 | issue = Issue.generate! |
|
424 | 424 | issue.init_journal(User.find(1)) |
|
425 | 425 | private_issue = Issue.generate!(:is_private => true) |
|
426 | 426 | IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates') |
|
427 | 427 | issue.reload |
|
428 | 428 | assert_equal 1, issue.journals.size |
|
429 | 429 | journal = issue.journals.first |
|
430 | 430 | ActionMailer::Base.deliveries.clear |
|
431 | 431 | |
|
432 | 432 | Mailer.deliver_issue_edit(journal) |
|
433 | 433 | last_email.bcc.each do |email| |
|
434 | 434 | user = User.find_by_mail(email) |
|
435 | 435 | assert private_issue.visible?(user), "Issue was not visible to #{user}" |
|
436 | 436 | end |
|
437 | 437 | end |
|
438 | 438 | |
|
439 | 439 | def test_document_added |
|
440 | 440 | document = Document.find(1) |
|
441 | 441 | with_each_language_as_default do |
|
442 | 442 | assert Mailer.document_added(document).deliver |
|
443 | 443 | end |
|
444 | 444 | end |
|
445 | 445 | |
|
446 | 446 | def test_attachments_added |
|
447 | 447 | attachements = [ Attachment.find_by_container_type('Document') ] |
|
448 | 448 | with_each_language_as_default do |
|
449 | 449 | assert Mailer.attachments_added(attachements).deliver |
|
450 | 450 | end |
|
451 | 451 | end |
|
452 | 452 | |
|
453 | 453 | def test_version_file_added |
|
454 | 454 | attachements = [ Attachment.find_by_container_type('Version') ] |
|
455 | 455 | assert Mailer.attachments_added(attachements).deliver |
|
456 | 456 | assert_not_nil last_email.bcc |
|
457 | 457 | assert last_email.bcc.any? |
|
458 | 458 | assert_select_email do |
|
459 | 459 | assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files" |
|
460 | 460 | end |
|
461 | 461 | end |
|
462 | 462 | |
|
463 | 463 | def test_project_file_added |
|
464 | 464 | attachements = [ Attachment.find_by_container_type('Project') ] |
|
465 | 465 | assert Mailer.attachments_added(attachements).deliver |
|
466 | 466 | assert_not_nil last_email.bcc |
|
467 | 467 | assert last_email.bcc.any? |
|
468 | 468 | assert_select_email do |
|
469 | 469 | assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files" |
|
470 | 470 | end |
|
471 | 471 | end |
|
472 | 472 | |
|
473 | 473 | def test_news_added |
|
474 | 474 | news = News.first |
|
475 | 475 | with_each_language_as_default do |
|
476 | 476 | assert Mailer.news_added(news).deliver |
|
477 | 477 | end |
|
478 | 478 | end |
|
479 | 479 | |
|
480 | 480 | def test_news_added_should_notify_project_news_watchers |
|
481 | 481 | user1 = User.generate! |
|
482 | 482 | user2 = User.generate! |
|
483 | 483 | news = News.find(1) |
|
484 | 484 | news.project.enabled_module('news').add_watcher(user1) |
|
485 | 485 | |
|
486 | 486 | Mailer.news_added(news).deliver |
|
487 | 487 | assert_include user1.mail, last_email.bcc |
|
488 | 488 | assert_not_include user2.mail, last_email.bcc |
|
489 | 489 | end |
|
490 | 490 | |
|
491 | 491 | def test_news_comment_added |
|
492 | 492 | comment = Comment.find(2) |
|
493 | 493 | with_each_language_as_default do |
|
494 | 494 | assert Mailer.news_comment_added(comment).deliver |
|
495 | 495 | end |
|
496 | 496 | end |
|
497 | 497 | |
|
498 | 498 | def test_message_posted |
|
499 | 499 | message = Message.first |
|
500 | 500 | recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author} |
|
501 | 501 | recipients = recipients.compact.uniq |
|
502 | 502 | with_each_language_as_default do |
|
503 | 503 | assert Mailer.message_posted(message).deliver |
|
504 | 504 | end |
|
505 | 505 | end |
|
506 | 506 | |
|
507 | 507 | def test_wiki_content_added |
|
508 | 508 | content = WikiContent.find(1) |
|
509 | 509 | with_each_language_as_default do |
|
510 | 510 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
511 | 511 | assert Mailer.wiki_content_added(content).deliver |
|
512 | 512 | assert_select_email do |
|
513 | 513 | assert_select 'a[href=?]', |
|
514 | 514 | 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation', |
|
515 | 515 | :text => 'CookBook documentation' |
|
516 | 516 | end |
|
517 | 517 | end |
|
518 | 518 | end |
|
519 | 519 | end |
|
520 | 520 | |
|
521 | 521 | def test_wiki_content_updated |
|
522 | 522 | content = WikiContent.find(1) |
|
523 | 523 | with_each_language_as_default do |
|
524 | 524 | assert_difference 'ActionMailer::Base.deliveries.size' do |
|
525 | 525 | assert Mailer.wiki_content_updated(content).deliver |
|
526 | 526 | assert_select_email do |
|
527 | 527 | assert_select 'a[href=?]', |
|
528 | 528 | 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation', |
|
529 | 529 | :text => 'CookBook documentation' |
|
530 | 530 | end |
|
531 | 531 | end |
|
532 | 532 | end |
|
533 | 533 | end |
|
534 | 534 | |
|
535 | 535 | def test_account_information |
|
536 | 536 | user = User.find(2) |
|
537 | 537 | valid_languages.each do |lang| |
|
538 | 538 | user.update_attribute :language, lang.to_s |
|
539 | 539 | user.reload |
|
540 | 540 | assert Mailer.account_information(user, 'pAsswORd').deliver |
|
541 | 541 | end |
|
542 | 542 | end |
|
543 | 543 | |
|
544 | 544 | def test_lost_password |
|
545 | 545 | token = Token.find(2) |
|
546 | 546 | valid_languages.each do |lang| |
|
547 | 547 | token.user.update_attribute :language, lang.to_s |
|
548 | 548 | token.reload |
|
549 | 549 | assert Mailer.lost_password(token).deliver |
|
550 | 550 | end |
|
551 | 551 | end |
|
552 | 552 | |
|
553 | 553 | def test_register |
|
554 | 554 | token = Token.find(1) |
|
555 | 555 | Setting.host_name = 'redmine.foo' |
|
556 | 556 | Setting.protocol = 'https' |
|
557 | 557 | |
|
558 | 558 | valid_languages.each do |lang| |
|
559 | 559 | token.user.update_attribute :language, lang.to_s |
|
560 | 560 | token.reload |
|
561 | 561 | ActionMailer::Base.deliveries.clear |
|
562 | 562 | assert Mailer.register(token).deliver |
|
563 | 563 | mail = last_email |
|
564 | 564 | assert_select_email do |
|
565 | 565 | assert_select "a[href=?]", |
|
566 | 566 | "https://redmine.foo/account/activate?token=#{token.value}", |
|
567 | 567 | :text => "https://redmine.foo/account/activate?token=#{token.value}" |
|
568 | 568 | end |
|
569 | 569 | end |
|
570 | 570 | end |
|
571 | 571 | |
|
572 | 572 | def test_test |
|
573 | 573 | user = User.find(1) |
|
574 | 574 | valid_languages.each do |lang| |
|
575 | 575 | user.update_attribute :language, lang.to_s |
|
576 | 576 | assert Mailer.test_email(user).deliver |
|
577 | 577 | end |
|
578 | 578 | end |
|
579 | 579 | |
|
580 | 580 | def test_reminders |
|
581 | 581 | Mailer.reminders(:days => 42) |
|
582 | 582 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
583 | 583 | mail = last_email |
|
584 | 584 | assert mail.bcc.include?('dlopper@somenet.foo') |
|
585 | 585 | assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail |
|
586 | 586 | assert_equal '1 issue(s) due in the next 42 days', mail.subject |
|
587 | 587 | end |
|
588 | 588 | |
|
589 | 589 | def test_reminders_should_not_include_closed_issues |
|
590 | 590 | with_settings :default_language => 'en' do |
|
591 | 591 | Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5, |
|
592 | 592 | :subject => 'Closed issue', :assigned_to_id => 3, |
|
593 | 593 | :due_date => 5.days.from_now, |
|
594 | 594 | :author_id => 2) |
|
595 | 595 | ActionMailer::Base.deliveries.clear |
|
596 | 596 | |
|
597 | 597 | Mailer.reminders(:days => 42) |
|
598 | 598 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
599 | 599 | mail = last_email |
|
600 | 600 | assert mail.bcc.include?('dlopper@somenet.foo') |
|
601 | 601 | assert_mail_body_no_match 'Closed issue', mail |
|
602 | 602 | end |
|
603 | 603 | end |
|
604 | 604 | |
|
605 | 605 | def test_reminders_for_users |
|
606 | 606 | Mailer.reminders(:days => 42, :users => ['5']) |
|
607 | 607 | assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper |
|
608 | 608 | Mailer.reminders(:days => 42, :users => ['3']) |
|
609 | 609 | assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper |
|
610 | 610 | mail = last_email |
|
611 | 611 | assert mail.bcc.include?('dlopper@somenet.foo') |
|
612 | 612 | assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail |
|
613 | 613 | end |
|
614 | 614 | |
|
615 | 615 | def test_reminder_should_include_issues_assigned_to_groups |
|
616 | 616 | with_settings :default_language => 'en' do |
|
617 | 617 | group = Group.generate! |
|
618 | 618 | group.users << User.find(2) |
|
619 | 619 | group.users << User.find(3) |
|
620 | 620 | |
|
621 | 621 | Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1, |
|
622 | 622 | :subject => 'Assigned to group', :assigned_to => group, |
|
623 | 623 | :due_date => 5.days.from_now, |
|
624 | 624 | :author_id => 2) |
|
625 | 625 | ActionMailer::Base.deliveries.clear |
|
626 | 626 | |
|
627 | 627 | Mailer.reminders(:days => 7) |
|
628 | 628 | assert_equal 2, ActionMailer::Base.deliveries.size |
|
629 | 629 | assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort |
|
630 | 630 | ActionMailer::Base.deliveries.each do |mail| |
|
631 | 631 | assert_mail_body_match 'Assigned to group', mail |
|
632 | 632 | end |
|
633 | 633 | end |
|
634 | 634 | end |
|
635 | 635 | |
|
636 | 636 | def test_reminders_for_versions |
|
637 | 637 | with_settings :default_language => 'en' do |
|
638 | Mailer.reminders(:days => 42, :users => ['3']) | |
|
639 | assert_equal 1, ActionMailer::Base.deliveries.size | |
|
640 | ||
|
638 | version = Version.generate!(:name => 'Acme', :project_id => 1) | |
|
639 | Issue.generate!(:assigned_to => User.find(2), :due_date => 5.days.from_now) | |
|
640 | Issue.generate!(:assigned_to => User.find(3), :due_date => 5.days.from_now, :fixed_version => version) | |
|
641 | 641 | ActionMailer::Base.deliveries.clear |
|
642 | Version.create!(name: 'Acme', project_id: 1, sharing: 'none') | |
|
643 |
Mailer.reminders(:days => 42, |
|
|
644 | assert_equal 0, ActionMailer::Base.deliveries.size | |
|
645 | Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1, | |
|
646 | :subject => 'Assigned to user', :assigned_to => User.find(3), | |
|
647 | :due_date => 5.days.from_now, | |
|
648 | :author_id => 2) | |
|
649 | Mailer.reminders(:days => 42, :users => ['3'], :version => 'Acme') | |
|
642 | ||
|
643 | Mailer.reminders(:days => 42, :version => 'acme') | |
|
650 | 644 |
assert_equal 1, ActionMailer::Base.deliveries.size |
|
651 | 645 | |
|
652 | 646 | mail = last_email |
|
653 | 647 | assert mail.bcc.include?('dlopper@somenet.foo') |
|
654 | 648 | end |
|
655 | 649 | end |
|
656 | 650 | |
|
657 | 651 | def test_mailer_should_not_change_locale |
|
658 | 652 | # Set current language to italian |
|
659 | 653 | set_language_if_valid 'it' |
|
660 | 654 | # Send an email to a french user |
|
661 | 655 | user = User.find(1) |
|
662 | 656 | user.language = 'fr' |
|
663 | 657 | Mailer.account_activated(user).deliver |
|
664 | 658 | mail = last_email |
|
665 | 659 | assert_mail_body_match 'Votre compte', mail |
|
666 | 660 | |
|
667 | 661 | assert_equal :it, current_language |
|
668 | 662 | end |
|
669 | 663 | |
|
670 | 664 | def test_with_deliveries_off |
|
671 | 665 | Mailer.with_deliveries false do |
|
672 | 666 | Mailer.test_email(User.find(1)).deliver |
|
673 | 667 | end |
|
674 | 668 | assert ActionMailer::Base.deliveries.empty? |
|
675 | 669 | # should restore perform_deliveries |
|
676 | 670 | assert ActionMailer::Base.perform_deliveries |
|
677 | 671 | end |
|
678 | 672 | |
|
679 | 673 | def test_token_for_should_strip_trailing_gt_from_address_with_full_name |
|
680 | 674 | with_settings :mail_from => "Redmine Mailer<no-reply@redmine.org>" do |
|
681 | 675 | assert_match /\Aredmine.issue-\d+\.\d+\.[0-9a-f]+@redmine.org\z/, Mailer.token_for(Issue.generate!) |
|
682 | 676 | end |
|
683 | 677 | end |
|
684 | 678 | |
|
685 | 679 | def test_layout_should_include_the_emails_header |
|
686 | 680 | with_settings :emails_header => "*Header content*" do |
|
687 | 681 | with_settings :plain_text_mail => 0 do |
|
688 | 682 | assert Mailer.test_email(User.find(1)).deliver |
|
689 | 683 | assert_select_email do |
|
690 | 684 | assert_select ".header" do |
|
691 | 685 | assert_select "strong", :text => "Header content" |
|
692 | 686 | end |
|
693 | 687 | end |
|
694 | 688 | end |
|
695 | 689 | with_settings :plain_text_mail => 1 do |
|
696 | 690 | assert Mailer.test_email(User.find(1)).deliver |
|
697 | 691 | mail = last_email |
|
698 | 692 | assert_not_nil mail |
|
699 | 693 | assert_include "*Header content*", mail.body.decoded |
|
700 | 694 | end |
|
701 | 695 | end |
|
702 | 696 | end |
|
703 | 697 | |
|
704 | 698 | def test_layout_should_not_include_empty_emails_header |
|
705 | 699 | with_settings :emails_header => "", :plain_text_mail => 0 do |
|
706 | 700 | assert Mailer.test_email(User.find(1)).deliver |
|
707 | 701 | assert_select_email do |
|
708 | 702 | assert_select ".header", false |
|
709 | 703 | end |
|
710 | 704 | end |
|
711 | 705 | end |
|
712 | 706 | |
|
713 | 707 | def test_layout_should_include_the_emails_footer |
|
714 | 708 | with_settings :emails_footer => "*Footer content*" do |
|
715 | 709 | with_settings :plain_text_mail => 0 do |
|
716 | 710 | assert Mailer.test_email(User.find(1)).deliver |
|
717 | 711 | assert_select_email do |
|
718 | 712 | assert_select ".footer" do |
|
719 | 713 | assert_select "strong", :text => "Footer content" |
|
720 | 714 | end |
|
721 | 715 | end |
|
722 | 716 | end |
|
723 | 717 | with_settings :plain_text_mail => 1 do |
|
724 | 718 | assert Mailer.test_email(User.find(1)).deliver |
|
725 | 719 | mail = last_email |
|
726 | 720 | assert_not_nil mail |
|
727 | 721 | assert_include "\n-- \n", mail.body.decoded |
|
728 | 722 | assert_include "*Footer content*", mail.body.decoded |
|
729 | 723 | end |
|
730 | 724 | end |
|
731 | 725 | end |
|
732 | 726 | |
|
733 | 727 | def test_layout_should_not_include_empty_emails_footer |
|
734 | 728 | with_settings :emails_footer => "" do |
|
735 | 729 | with_settings :plain_text_mail => 0 do |
|
736 | 730 | assert Mailer.test_email(User.find(1)).deliver |
|
737 | 731 | assert_select_email do |
|
738 | 732 | assert_select ".footer", false |
|
739 | 733 | end |
|
740 | 734 | end |
|
741 | 735 | with_settings :plain_text_mail => 1 do |
|
742 | 736 | assert Mailer.test_email(User.find(1)).deliver |
|
743 | 737 | mail = last_email |
|
744 | 738 | assert_not_nil mail |
|
745 | 739 | assert_not_include "\n-- \n", mail.body.decoded |
|
746 | 740 | end |
|
747 | 741 | end |
|
748 | 742 | end |
|
749 | 743 | |
|
750 | 744 | def test_should_escape_html_templates_only |
|
751 | 745 | Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>') |
|
752 | 746 | mail = last_email |
|
753 | 747 | assert_equal 2, mail.parts.size |
|
754 | 748 | assert_include '<tag>', text_part.body.encoded |
|
755 | 749 | assert_include '<tag>', html_part.body.encoded |
|
756 | 750 | end |
|
757 | 751 | |
|
758 | 752 | def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true |
|
759 | 753 | mail = Mailer.test_email(User.find(1)) |
|
760 | 754 | mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error")) |
|
761 | 755 | |
|
762 | 756 | ActionMailer::Base.raise_delivery_errors = true |
|
763 | 757 | assert_raise Exception, "delivery error" do |
|
764 | 758 | mail.deliver |
|
765 | 759 | end |
|
766 | 760 | ensure |
|
767 | 761 | ActionMailer::Base.raise_delivery_errors = false |
|
768 | 762 | end |
|
769 | 763 | |
|
770 | 764 | def test_should_log_delivery_errors_when_raise_delivery_errors_is_false |
|
771 | 765 | mail = Mailer.test_email(User.find(1)) |
|
772 | 766 | mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error")) |
|
773 | 767 | |
|
774 | 768 | Rails.logger.expects(:error).with("Email delivery error: delivery error") |
|
775 | 769 | ActionMailer::Base.raise_delivery_errors = false |
|
776 | 770 | assert_nothing_raised do |
|
777 | 771 | mail.deliver |
|
778 | 772 | end |
|
779 | 773 | end |
|
780 | 774 | |
|
781 | 775 | def test_with_synched_deliveries_should_yield_with_synced_deliveries |
|
782 | 776 | ActionMailer::Base.delivery_method = :async_smtp |
|
783 | 777 | ActionMailer::Base.async_smtp_settings = {:foo => 'bar'} |
|
784 | 778 | |
|
785 | 779 | Mailer.with_synched_deliveries do |
|
786 | 780 | assert_equal :smtp, ActionMailer::Base.delivery_method |
|
787 | 781 | assert_equal({:foo => 'bar'}, ActionMailer::Base.smtp_settings) |
|
788 | 782 | end |
|
789 | 783 | assert_equal :async_smtp, ActionMailer::Base.delivery_method |
|
790 | 784 | ensure |
|
791 | 785 | ActionMailer::Base.delivery_method = :test |
|
792 | 786 | end |
|
793 | 787 | |
|
794 | 788 | def test_email_addresses_should_keep_addresses |
|
795 | 789 | assert_equal ["foo@example.net"], |
|
796 | 790 | Mailer.email_addresses("foo@example.net") |
|
797 | 791 | |
|
798 | 792 | assert_equal ["foo@example.net", "bar@example.net"], |
|
799 | 793 | Mailer.email_addresses(["foo@example.net", "bar@example.net"]) |
|
800 | 794 | end |
|
801 | 795 | |
|
802 | 796 | def test_email_addresses_should_replace_users_with_their_email_addresses |
|
803 | 797 | assert_equal ["admin@somenet.foo"], |
|
804 | 798 | Mailer.email_addresses(User.find(1)) |
|
805 | 799 | |
|
806 | 800 | assert_equal ["admin@somenet.foo", "jsmith@somenet.foo"], |
|
807 | 801 | Mailer.email_addresses(User.where(:id => [1,2])).sort |
|
808 | 802 | end |
|
809 | 803 | |
|
810 | 804 | def test_email_addresses_should_include_notified_emails_addresses_only |
|
811 | 805 | EmailAddress.create!(:user_id => 2, :address => "another@somenet.foo", :notify => false) |
|
812 | 806 | EmailAddress.create!(:user_id => 2, :address => "another2@somenet.foo") |
|
813 | 807 | |
|
814 | 808 | assert_equal ["another2@somenet.foo", "jsmith@somenet.foo"], |
|
815 | 809 | Mailer.email_addresses(User.find(2)).sort |
|
816 | 810 | end |
|
817 | 811 | |
|
818 | 812 | private |
|
819 | 813 | |
|
820 | 814 | def last_email |
|
821 | 815 | mail = ActionMailer::Base.deliveries.last |
|
822 | 816 | assert_not_nil mail |
|
823 | 817 | |
|
824 | 818 | end |
|
825 | 819 | |
|
826 | 820 | def text_part |
|
827 | 821 | last_email.parts.detect {|part| part.content_type.include?('text/plain')} |
|
828 | 822 | end |
|
829 | 823 | |
|
830 | 824 | def html_part |
|
831 | 825 | last_email.parts.detect {|part| part.content_type.include?('text/html')} |
|
832 | 826 | end |
|
833 | 827 | |
|
834 | 828 | def with_each_language_as_default(&block) |
|
835 | 829 | valid_languages.each do |lang| |
|
836 | 830 | with_settings :default_language => lang.to_s do |
|
837 | 831 | yield lang |
|
838 | 832 | end |
|
839 | 833 | end |
|
840 | 834 | end |
|
841 | 835 | end |
General Comments 0
You need to be logged in to leave comments.
Login now