##// END OF EJS Templates
Add assertions for #6929 in MailHandler tests....
Jean-Philippe Lang -
r4302:1f237388bddd
parent child
Show More
@@ -1,439 +1,441
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2009 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require File.dirname(__FILE__) + '/../test_helper'
21 21
22 22 class MailHandlerTest < ActiveSupport::TestCase
23 23 fixtures :users, :projects,
24 24 :enabled_modules,
25 25 :roles,
26 26 :members,
27 27 :member_roles,
28 28 :users,
29 29 :issues,
30 30 :issue_statuses,
31 31 :workflows,
32 32 :trackers,
33 33 :projects_trackers,
34 34 :versions,
35 35 :enumerations,
36 36 :issue_categories,
37 37 :custom_fields,
38 38 :custom_fields_trackers,
39 39 :boards,
40 40 :messages
41 41
42 42 FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
43 43
44 44 def setup
45 45 ActionMailer::Base.deliveries.clear
46 46 Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
47 47 end
48 48
49 49 def test_add_issue
50 50 ActionMailer::Base.deliveries.clear
51 51 # This email contains: 'Project: onlinestore'
52 52 issue = submit_email('ticket_on_given_project.eml')
53 53 assert issue.is_a?(Issue)
54 54 assert !issue.new_record?
55 55 issue.reload
56 56 assert_equal 'New ticket on a given project', issue.subject
57 57 assert_equal User.find_by_login('jsmith'), issue.author
58 58 assert_equal Project.find(2), issue.project
59 59 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
60 60 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
61 61 assert_equal '2010-01-01', issue.start_date.to_s
62 62 assert_equal '2010-12-31', issue.due_date.to_s
63 63 assert_equal User.find_by_login('jsmith'), issue.assigned_to
64 64 assert_equal Version.find_by_name('alpha'), issue.fixed_version
65 65 assert_equal 2.5, issue.estimated_hours
66 66 assert_equal 30, issue.done_ratio
67 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
67 68 # keywords should be removed from the email body
68 69 assert !issue.description.match(/^Project:/i)
69 70 assert !issue.description.match(/^Status:/i)
70 71 # Email notification should be sent
71 72 mail = ActionMailer::Base.deliveries.last
72 73 assert_not_nil mail
73 74 assert mail.subject.include?('New ticket on a given project')
74 75 end
75 76
76 77 def test_add_issue_with_status
77 78 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
78 79 issue = submit_email('ticket_on_given_project.eml')
79 80 assert issue.is_a?(Issue)
80 81 assert !issue.new_record?
81 82 issue.reload
82 83 assert_equal Project.find(2), issue.project
83 84 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
84 85 end
85 86
86 87 def test_add_issue_with_attributes_override
87 88 issue = submit_email('ticket_with_attributes.eml', :allow_override => 'tracker,category,priority')
88 89 assert issue.is_a?(Issue)
89 90 assert !issue.new_record?
90 91 issue.reload
91 92 assert_equal 'New ticket on a given project', issue.subject
92 93 assert_equal User.find_by_login('jsmith'), issue.author
93 94 assert_equal Project.find(2), issue.project
94 95 assert_equal 'Feature request', issue.tracker.to_s
95 96 assert_equal 'Stock management', issue.category.to_s
96 97 assert_equal 'Urgent', issue.priority.to_s
97 98 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
98 99 end
99 100
100 101 def test_add_issue_with_partial_attributes_override
101 102 issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker'])
102 103 assert issue.is_a?(Issue)
103 104 assert !issue.new_record?
104 105 issue.reload
105 106 assert_equal 'New ticket on a given project', issue.subject
106 107 assert_equal User.find_by_login('jsmith'), issue.author
107 108 assert_equal Project.find(2), issue.project
108 109 assert_equal 'Feature request', issue.tracker.to_s
109 110 assert_nil issue.category
110 111 assert_equal 'High', issue.priority.to_s
111 112 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
112 113 end
113 114
114 115 def test_add_issue_with_spaces_between_attribute_and_separator
115 116 issue = submit_email('ticket_with_spaces_between_attribute_and_separator.eml', :allow_override => 'tracker,category,priority')
116 117 assert issue.is_a?(Issue)
117 118 assert !issue.new_record?
118 119 issue.reload
119 120 assert_equal 'New ticket on a given project', issue.subject
120 121 assert_equal User.find_by_login('jsmith'), issue.author
121 122 assert_equal Project.find(2), issue.project
122 123 assert_equal 'Feature request', issue.tracker.to_s
123 124 assert_equal 'Stock management', issue.category.to_s
124 125 assert_equal 'Urgent', issue.priority.to_s
125 126 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
126 127 end
127 128
128 129
129 130 def test_add_issue_with_attachment_to_specific_project
130 131 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
131 132 assert issue.is_a?(Issue)
132 133 assert !issue.new_record?
133 134 issue.reload
134 135 assert_equal 'Ticket created by email with attachment', issue.subject
135 136 assert_equal User.find_by_login('jsmith'), issue.author
136 137 assert_equal Project.find(2), issue.project
137 138 assert_equal 'This is a new ticket with attachments', issue.description
138 139 # Attachment properties
139 140 assert_equal 1, issue.attachments.size
140 141 assert_equal 'Paella.jpg', issue.attachments.first.filename
141 142 assert_equal 'image/jpeg', issue.attachments.first.content_type
142 143 assert_equal 10790, issue.attachments.first.filesize
143 144 end
144 145
145 146 def test_add_issue_with_custom_fields
146 147 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
147 148 assert issue.is_a?(Issue)
148 149 assert !issue.new_record?
149 150 issue.reload
150 151 assert_equal 'New ticket with custom field values', issue.subject
151 152 assert_equal 'Value for a custom field', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
152 153 assert !issue.description.match(/^searchable field:/i)
153 154 end
154 155
155 156 def test_add_issue_with_cc
156 157 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
157 158 assert issue.is_a?(Issue)
158 159 assert !issue.new_record?
159 160 issue.reload
160 161 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
161 162 assert_equal 1, issue.watcher_user_ids.size
162 163 end
163 164
164 165 def test_add_issue_by_unknown_user
165 166 assert_no_difference 'User.count' do
166 167 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'})
167 168 end
168 169 end
169 170
170 171 def test_add_issue_by_anonymous_user
171 172 Role.anonymous.add_permission!(:add_issues)
172 173 assert_no_difference 'User.count' do
173 174 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
174 175 assert issue.is_a?(Issue)
175 176 assert issue.author.anonymous?
176 177 end
177 178 end
178 179
179 180 def test_add_issue_by_anonymous_user_with_no_from_address
180 181 Role.anonymous.add_permission!(:add_issues)
181 182 assert_no_difference 'User.count' do
182 183 issue = submit_email('ticket_by_empty_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
183 184 assert issue.is_a?(Issue)
184 185 assert issue.author.anonymous?
185 186 end
186 187 end
187 188
188 189 def test_add_issue_by_anonymous_user_on_private_project
189 190 Role.anonymous.add_permission!(:add_issues)
190 191 assert_no_difference 'User.count' do
191 192 assert_no_difference 'Issue.count' do
192 193 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :unknown_user => 'accept')
193 194 end
194 195 end
195 196 end
196 197
197 198 def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
198 199 assert_no_difference 'User.count' do
199 200 assert_difference 'Issue.count' do
200 201 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :no_permission_check => '1', :unknown_user => 'accept')
201 202 assert issue.is_a?(Issue)
202 203 assert issue.author.anonymous?
203 204 assert !issue.project.is_public?
205 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
204 206 end
205 207 end
206 208 end
207 209
208 210 def test_add_issue_by_created_user
209 211 Setting.default_language = 'en'
210 212 assert_difference 'User.count' do
211 213 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
212 214 assert issue.is_a?(Issue)
213 215 assert issue.author.active?
214 216 assert_equal 'john.doe@somenet.foo', issue.author.mail
215 217 assert_equal 'John', issue.author.firstname
216 218 assert_equal 'Doe', issue.author.lastname
217 219
218 220 # account information
219 221 email = ActionMailer::Base.deliveries.first
220 222 assert_not_nil email
221 223 assert email.subject.include?('account activation')
222 224 login = email.body.match(/\* Login: (.*)$/)[1]
223 225 password = email.body.match(/\* Password: (.*)$/)[1]
224 226 assert_equal issue.author, User.try_to_login(login, password)
225 227 end
226 228 end
227 229
228 230 def test_add_issue_without_from_header
229 231 Role.anonymous.add_permission!(:add_issues)
230 232 assert_equal false, submit_email('ticket_without_from_header.eml')
231 233 end
232 234
233 235 def test_add_issue_with_invalid_attributes
234 236 issue = submit_email('ticket_with_invalid_attributes.eml', :allow_override => 'tracker,category,priority')
235 237 assert issue.is_a?(Issue)
236 238 assert !issue.new_record?
237 239 issue.reload
238 240 assert_nil issue.assigned_to
239 241 assert_nil issue.start_date
240 242 assert_nil issue.due_date
241 243 assert_equal 0, issue.done_ratio
242 244 assert_equal 'Normal', issue.priority.to_s
243 245 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
244 246 end
245 247
246 248 def test_add_issue_with_localized_attributes
247 249 User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
248 250 issue = submit_email('ticket_with_localized_attributes.eml', :allow_override => 'tracker,category,priority')
249 251 assert issue.is_a?(Issue)
250 252 assert !issue.new_record?
251 253 issue.reload
252 254 assert_equal 'New ticket on a given project', issue.subject
253 255 assert_equal User.find_by_login('jsmith'), issue.author
254 256 assert_equal Project.find(2), issue.project
255 257 assert_equal 'Feature request', issue.tracker.to_s
256 258 assert_equal 'Stock management', issue.category.to_s
257 259 assert_equal 'Urgent', issue.priority.to_s
258 260 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
259 261 end
260 262
261 263 def test_add_issue_with_japanese_keywords
262 264 tracker = Tracker.create!(:name => 'ι–‹η™Ί')
263 265 Project.find(1).trackers << tracker
264 266 issue = submit_email('japanese_keywords_iso_2022_jp.eml', :issue => {:project => 'ecookbook'}, :allow_override => 'tracker')
265 267 assert_kind_of Issue, issue
266 268 assert_equal tracker, issue.tracker
267 269 end
268 270
269 271 def test_should_ignore_emails_from_emission_address
270 272 Role.anonymous.add_permission!(:add_issues)
271 273 assert_no_difference 'User.count' do
272 274 assert_equal false, submit_email('ticket_from_emission_address.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
273 275 end
274 276 end
275 277
276 278 def test_add_issue_should_send_email_notification
277 279 Setting.notified_events = ['issue_added']
278 280 ActionMailer::Base.deliveries.clear
279 281 # This email contains: 'Project: onlinestore'
280 282 issue = submit_email('ticket_on_given_project.eml')
281 283 assert issue.is_a?(Issue)
282 284 assert_equal 1, ActionMailer::Base.deliveries.size
283 285 end
284 286
285 287 def test_add_issue_note
286 288 journal = submit_email('ticket_reply.eml')
287 289 assert journal.is_a?(Journal)
288 290 assert_equal User.find_by_login('jsmith'), journal.user
289 291 assert_equal Issue.find(2), journal.journalized
290 292 assert_match /This is reply/, journal.notes
291 293 end
292 294
293 295 def test_add_issue_note_with_attribute_changes
294 296 # This email contains: 'Status: Resolved'
295 297 journal = submit_email('ticket_reply_with_status.eml')
296 298 assert journal.is_a?(Journal)
297 299 issue = Issue.find(journal.issue.id)
298 300 assert_equal User.find_by_login('jsmith'), journal.user
299 301 assert_equal Issue.find(2), journal.journalized
300 302 assert_match /This is reply/, journal.notes
301 303 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
302 304 assert_equal '2010-01-01', issue.start_date.to_s
303 305 assert_equal '2010-12-31', issue.due_date.to_s
304 306 assert_equal User.find_by_login('jsmith'), issue.assigned_to
305 307 assert_equal 'Updated custom value', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
306 308 end
307 309
308 310 def test_add_issue_note_should_send_email_notification
309 311 ActionMailer::Base.deliveries.clear
310 312 journal = submit_email('ticket_reply.eml')
311 313 assert journal.is_a?(Journal)
312 314 assert_equal 1, ActionMailer::Base.deliveries.size
313 315 end
314 316
315 317 def test_reply_to_a_message
316 318 m = submit_email('message_reply.eml')
317 319 assert m.is_a?(Message)
318 320 assert !m.new_record?
319 321 m.reload
320 322 assert_equal 'Reply via email', m.subject
321 323 # The email replies to message #2 which is part of the thread of message #1
322 324 assert_equal Message.find(1), m.parent
323 325 end
324 326
325 327 def test_reply_to_a_message_by_subject
326 328 m = submit_email('message_reply_by_subject.eml')
327 329 assert m.is_a?(Message)
328 330 assert !m.new_record?
329 331 m.reload
330 332 assert_equal 'Reply to the first post', m.subject
331 333 assert_equal Message.find(1), m.parent
332 334 end
333 335
334 336 def test_should_strip_tags_of_html_only_emails
335 337 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
336 338 assert issue.is_a?(Issue)
337 339 assert !issue.new_record?
338 340 issue.reload
339 341 assert_equal 'HTML email', issue.subject
340 342 assert_equal 'This is a html-only email.', issue.description
341 343 end
342 344
343 345 context "truncate emails based on the Setting" do
344 346 context "with no setting" do
345 347 setup do
346 348 Setting.mail_handler_body_delimiters = ''
347 349 end
348 350
349 351 should "add the entire email into the issue" do
350 352 issue = submit_email('ticket_on_given_project.eml')
351 353 assert_issue_created(issue)
352 354 assert issue.description.include?('---')
353 355 assert issue.description.include?('This paragraph is after the delimiter')
354 356 end
355 357 end
356 358
357 359 context "with a single string" do
358 360 setup do
359 361 Setting.mail_handler_body_delimiters = '---'
360 362 end
361 363
362 364 should "truncate the email at the delimiter for the issue" do
363 365 issue = submit_email('ticket_on_given_project.eml')
364 366 assert_issue_created(issue)
365 367 assert issue.description.include?('This paragraph is before delimiters')
366 368 assert issue.description.include?('--- This line starts with a delimiter')
367 369 assert !issue.description.match(/^---$/)
368 370 assert !issue.description.include?('This paragraph is after the delimiter')
369 371 end
370 372 end
371 373
372 374 context "with a single quoted reply (e.g. reply to a Redmine email notification)" do
373 375 setup do
374 376 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
375 377 end
376 378
377 379 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
378 380 journal = submit_email('issue_update_with_quoted_reply_above.eml')
379 381 assert journal.is_a?(Journal)
380 382 assert journal.notes.include?('An update to the issue by the sender.')
381 383 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
382 384 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
383 385
384 386 end
385 387
386 388 end
387 389
388 390 context "with multiple quoted replies (e.g. reply to a reply of a Redmine email notification)" do
389 391 setup do
390 392 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
391 393 end
392 394
393 395 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
394 396 journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
395 397 assert journal.is_a?(Journal)
396 398 assert journal.notes.include?('An update to the issue by the sender.')
397 399 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
398 400 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
399 401
400 402 end
401 403
402 404 end
403 405
404 406 context "with multiple strings" do
405 407 setup do
406 408 Setting.mail_handler_body_delimiters = "---\nBREAK"
407 409 end
408 410
409 411 should "truncate the email at the first delimiter found (BREAK)" do
410 412 issue = submit_email('ticket_on_given_project.eml')
411 413 assert_issue_created(issue)
412 414 assert issue.description.include?('This paragraph is before delimiters')
413 415 assert !issue.description.include?('BREAK')
414 416 assert !issue.description.include?('This paragraph is between delimiters')
415 417 assert !issue.description.match(/^---$/)
416 418 assert !issue.description.include?('This paragraph is after the delimiter')
417 419 end
418 420 end
419 421 end
420 422
421 423 def test_email_with_long_subject_line
422 424 issue = submit_email('ticket_with_long_subject.eml')
423 425 assert issue.is_a?(Issue)
424 426 assert_equal issue.subject, 'New ticket on a given project with a very long subject line which exceeds 255 chars and should not be ignored but chopped off. And if the subject line is still not long enough, we just add more text. And more text. Wow, this is really annoying. Especially, if you have nothing to say...'[0,255]
425 427 end
426 428
427 429 private
428 430
429 431 def submit_email(filename, options={})
430 432 raw = IO.read(File.join(FIXTURES_PATH, filename))
431 433 MailHandler.receive(raw, options)
432 434 end
433 435
434 436 def assert_issue_created(issue)
435 437 assert issue.is_a?(Issue)
436 438 assert !issue.new_record?
437 439 issue.reload
438 440 end
439 441 end
General Comments 0
You need to be logged in to leave comments. Login now