##// END OF EJS Templates
add test of issue id in sending mail when issue is created by mail (#17078)...
Toshi MARUYAMA -
r12920:95482a4fff16
parent child
Show More
@@ -1,919 +1,920
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2014 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.expand_path('../../test_helper', __FILE__)
21 21
22 22 class MailHandlerTest < ActiveSupport::TestCase
23 23 fixtures :users, :projects, :enabled_modules, :roles,
24 24 :members, :member_roles, :users,
25 25 :issues, :issue_statuses,
26 26 :workflows, :trackers, :projects_trackers,
27 27 :versions, :enumerations, :issue_categories,
28 28 :custom_fields, :custom_fields_trackers, :custom_fields_projects,
29 29 :boards, :messages
30 30
31 31 FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
32 32
33 33 def setup
34 34 ActionMailer::Base.deliveries.clear
35 35 Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
36 36 end
37 37
38 38 def teardown
39 39 Setting.clear_cache
40 40 end
41 41
42 42 def test_add_issue
43 43 ActionMailer::Base.deliveries.clear
44 44 lft1 = new_issue_lft
45 45 # This email contains: 'Project: onlinestore'
46 46 issue = submit_email('ticket_on_given_project.eml')
47 47 assert issue.is_a?(Issue)
48 48 assert !issue.new_record?
49 49 issue.reload
50 50 assert_equal Project.find(2), issue.project
51 51 assert_equal issue.project.trackers.first, issue.tracker
52 52 assert_equal 'New ticket on a given project', issue.subject
53 53 assert_equal User.find_by_login('jsmith'), issue.author
54 54 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
55 55 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
56 56 assert_equal '2010-01-01', issue.start_date.to_s
57 57 assert_equal '2010-12-31', issue.due_date.to_s
58 58 assert_equal User.find_by_login('jsmith'), issue.assigned_to
59 59 assert_equal Version.find_by_name('Alpha'), issue.fixed_version
60 60 assert_equal 2.5, issue.estimated_hours
61 61 assert_equal 30, issue.done_ratio
62 62 assert_equal [issue.id, lft1, lft1 + 1], [issue.root_id, issue.lft, issue.rgt]
63 63 # keywords should be removed from the email body
64 64 assert !issue.description.match(/^Project:/i)
65 65 assert !issue.description.match(/^Status:/i)
66 66 assert !issue.description.match(/^Start Date:/i)
67 67 # Email notification should be sent
68 68 mail = ActionMailer::Base.deliveries.last
69 69 assert_not_nil mail
70 assert mail.subject.include?("##{issue.id}")
70 71 assert mail.subject.include?('New ticket on a given project')
71 72 end
72 73
73 74 def test_add_issue_with_default_tracker
74 75 # This email contains: 'Project: onlinestore'
75 76 issue = submit_email(
76 77 'ticket_on_given_project.eml',
77 78 :issue => {:tracker => 'Support request'}
78 79 )
79 80 assert issue.is_a?(Issue)
80 81 assert !issue.new_record?
81 82 issue.reload
82 83 assert_equal 'Support request', issue.tracker.name
83 84 end
84 85
85 86 def test_add_issue_with_status
86 87 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
87 88 issue = submit_email('ticket_on_given_project.eml')
88 89 assert issue.is_a?(Issue)
89 90 assert !issue.new_record?
90 91 issue.reload
91 92 assert_equal Project.find(2), issue.project
92 93 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
93 94 end
94 95
95 96 def test_add_issue_with_attributes_override
96 97 issue = submit_email(
97 98 'ticket_with_attributes.eml',
98 99 :allow_override => 'tracker,category,priority'
99 100 )
100 101 assert issue.is_a?(Issue)
101 102 assert !issue.new_record?
102 103 issue.reload
103 104 assert_equal 'New ticket on a given project', issue.subject
104 105 assert_equal User.find_by_login('jsmith'), issue.author
105 106 assert_equal Project.find(2), issue.project
106 107 assert_equal 'Feature request', issue.tracker.to_s
107 108 assert_equal 'Stock management', issue.category.to_s
108 109 assert_equal 'Urgent', issue.priority.to_s
109 110 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
110 111 end
111 112
112 113 def test_add_issue_with_group_assignment
113 114 with_settings :issue_group_assignment => '1' do
114 115 issue = submit_email('ticket_on_given_project.eml') do |email|
115 116 email.gsub!('Assigned to: John Smith', 'Assigned to: B Team')
116 117 end
117 118 assert issue.is_a?(Issue)
118 119 assert !issue.new_record?
119 120 issue.reload
120 121 assert_equal Group.find(11), issue.assigned_to
121 122 end
122 123 end
123 124
124 125 def test_add_issue_with_partial_attributes_override
125 126 issue = submit_email(
126 127 'ticket_with_attributes.eml',
127 128 :issue => {:priority => 'High'},
128 129 :allow_override => ['tracker']
129 130 )
130 131 assert issue.is_a?(Issue)
131 132 assert !issue.new_record?
132 133 issue.reload
133 134 assert_equal 'New ticket on a given project', issue.subject
134 135 assert_equal User.find_by_login('jsmith'), issue.author
135 136 assert_equal Project.find(2), issue.project
136 137 assert_equal 'Feature request', issue.tracker.to_s
137 138 assert_nil issue.category
138 139 assert_equal 'High', issue.priority.to_s
139 140 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
140 141 end
141 142
142 143 def test_add_issue_with_spaces_between_attribute_and_separator
143 144 issue = submit_email(
144 145 'ticket_with_spaces_between_attribute_and_separator.eml',
145 146 :allow_override => 'tracker,category,priority'
146 147 )
147 148 assert issue.is_a?(Issue)
148 149 assert !issue.new_record?
149 150 issue.reload
150 151 assert_equal 'New ticket on a given project', issue.subject
151 152 assert_equal User.find_by_login('jsmith'), issue.author
152 153 assert_equal Project.find(2), issue.project
153 154 assert_equal 'Feature request', issue.tracker.to_s
154 155 assert_equal 'Stock management', issue.category.to_s
155 156 assert_equal 'Urgent', issue.priority.to_s
156 157 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
157 158 end
158 159
159 160 def test_add_issue_with_attachment_to_specific_project
160 161 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
161 162 assert issue.is_a?(Issue)
162 163 assert !issue.new_record?
163 164 issue.reload
164 165 assert_equal 'Ticket created by email with attachment', issue.subject
165 166 assert_equal User.find_by_login('jsmith'), issue.author
166 167 assert_equal Project.find(2), issue.project
167 168 assert_equal 'This is a new ticket with attachments', issue.description
168 169 # Attachment properties
169 170 assert_equal 1, issue.attachments.size
170 171 assert_equal 'Paella.jpg', issue.attachments.first.filename
171 172 assert_equal 'image/jpeg', issue.attachments.first.content_type
172 173 assert_equal 10790, issue.attachments.first.filesize
173 174 end
174 175
175 176 def test_add_issue_with_custom_fields
176 177 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
177 178 assert issue.is_a?(Issue)
178 179 assert !issue.new_record?
179 180 issue.reload
180 181 assert_equal 'New ticket with custom field values', issue.subject
181 182 assert_equal 'PostgreSQL', issue.custom_field_value(1)
182 183 assert_equal 'Value for a custom field', issue.custom_field_value(2)
183 184 assert !issue.description.match(/^searchable field:/i)
184 185 end
185 186
186 187 def test_add_issue_with_version_custom_fields
187 188 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1,2,3])
188 189
189 190 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'ecookbook'}) do |email|
190 191 email << "Affected version: 1.0\n"
191 192 end
192 193 assert issue.is_a?(Issue)
193 194 assert !issue.new_record?
194 195 issue.reload
195 196 assert_equal '2', issue.custom_field_value(field)
196 197 end
197 198
198 199 def test_add_issue_should_match_assignee_on_display_name
199 200 user = User.generate!(:firstname => 'Foo Bar', :lastname => 'Foo Baz')
200 201 User.add_to_project(user, Project.find(2))
201 202 issue = submit_email('ticket_on_given_project.eml') do |email|
202 203 email.sub!(/^Assigned to.*$/, 'Assigned to: Foo Bar Foo baz')
203 204 end
204 205 assert issue.is_a?(Issue)
205 206 assert_equal user, issue.assigned_to
206 207 end
207 208
208 209 def test_add_issue_should_set_default_start_date
209 210 with_settings :default_issue_start_date_to_creation_date => '1' do
210 211 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
211 212 assert issue.is_a?(Issue)
212 213 assert_equal Date.today, issue.start_date
213 214 end
214 215 end
215 216
216 217 def test_add_issue_with_cc
217 218 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
218 219 assert issue.is_a?(Issue)
219 220 assert !issue.new_record?
220 221 issue.reload
221 222 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
222 223 assert_equal 1, issue.watcher_user_ids.size
223 224 end
224 225
225 226 def test_add_issue_by_unknown_user
226 227 assert_no_difference 'User.count' do
227 228 assert_equal false,
228 229 submit_email(
229 230 'ticket_by_unknown_user.eml',
230 231 :issue => {:project => 'ecookbook'}
231 232 )
232 233 end
233 234 end
234 235
235 236 def test_add_issue_by_anonymous_user
236 237 Role.anonymous.add_permission!(:add_issues)
237 238 assert_no_difference 'User.count' do
238 239 issue = submit_email(
239 240 'ticket_by_unknown_user.eml',
240 241 :issue => {:project => 'ecookbook'},
241 242 :unknown_user => 'accept'
242 243 )
243 244 assert issue.is_a?(Issue)
244 245 assert issue.author.anonymous?
245 246 end
246 247 end
247 248
248 249 def test_add_issue_by_anonymous_user_with_no_from_address
249 250 Role.anonymous.add_permission!(:add_issues)
250 251 assert_no_difference 'User.count' do
251 252 issue = submit_email(
252 253 'ticket_by_empty_user.eml',
253 254 :issue => {:project => 'ecookbook'},
254 255 :unknown_user => 'accept'
255 256 )
256 257 assert issue.is_a?(Issue)
257 258 assert issue.author.anonymous?
258 259 end
259 260 end
260 261
261 262 def test_add_issue_by_anonymous_user_on_private_project
262 263 Role.anonymous.add_permission!(:add_issues)
263 264 assert_no_difference 'User.count' do
264 265 assert_no_difference 'Issue.count' do
265 266 assert_equal false,
266 267 submit_email(
267 268 'ticket_by_unknown_user.eml',
268 269 :issue => {:project => 'onlinestore'},
269 270 :unknown_user => 'accept'
270 271 )
271 272 end
272 273 end
273 274 end
274 275
275 276 def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
276 277 lft1 = new_issue_lft
277 278 assert_no_difference 'User.count' do
278 279 assert_difference 'Issue.count' do
279 280 issue = submit_email(
280 281 'ticket_by_unknown_user.eml',
281 282 :issue => {:project => 'onlinestore'},
282 283 :no_permission_check => '1',
283 284 :unknown_user => 'accept'
284 285 )
285 286 assert issue.is_a?(Issue)
286 287 assert issue.author.anonymous?
287 288 assert !issue.project.is_public?
288 289 assert_equal [issue.id, lft1, lft1 + 1], [issue.root_id, issue.lft, issue.rgt]
289 290 end
290 291 end
291 292 end
292 293
293 294 def test_add_issue_by_created_user
294 295 Setting.default_language = 'en'
295 296 assert_difference 'User.count' do
296 297 issue = submit_email(
297 298 'ticket_by_unknown_user.eml',
298 299 :issue => {:project => 'ecookbook'},
299 300 :unknown_user => 'create'
300 301 )
301 302 assert issue.is_a?(Issue)
302 303 assert issue.author.active?
303 304 assert_equal 'john.doe@somenet.foo', issue.author.mail
304 305 assert_equal 'John', issue.author.firstname
305 306 assert_equal 'Doe', issue.author.lastname
306 307
307 308 # account information
308 309 email = ActionMailer::Base.deliveries.first
309 310 assert_not_nil email
310 311 assert email.subject.include?('account activation')
311 312 login = mail_body(email).match(/\* Login: (.*)$/)[1].strip
312 313 password = mail_body(email).match(/\* Password: (.*)$/)[1].strip
313 314 assert_equal issue.author, User.try_to_login(login, password)
314 315 end
315 316 end
316 317
317 318 def test_created_user_should_be_added_to_groups
318 319 group1 = Group.generate!
319 320 group2 = Group.generate!
320 321
321 322 assert_difference 'User.count' do
322 323 submit_email(
323 324 'ticket_by_unknown_user.eml',
324 325 :issue => {:project => 'ecookbook'},
325 326 :unknown_user => 'create',
326 327 :default_group => "#{group1.name},#{group2.name}"
327 328 )
328 329 end
329 330 user = User.order('id DESC').first
330 331 assert_same_elements [group1, group2], user.groups
331 332 end
332 333
333 334 def test_created_user_should_not_receive_account_information_with_no_account_info_option
334 335 assert_difference 'User.count' do
335 336 submit_email(
336 337 'ticket_by_unknown_user.eml',
337 338 :issue => {:project => 'ecookbook'},
338 339 :unknown_user => 'create',
339 340 :no_account_notice => '1'
340 341 )
341 342 end
342 343
343 344 # only 1 email for the new issue notification
344 345 assert_equal 1, ActionMailer::Base.deliveries.size
345 346 email = ActionMailer::Base.deliveries.first
346 347 assert_include 'Ticket by unknown user', email.subject
347 348 end
348 349
349 350 def test_created_user_should_have_mail_notification_to_none_with_no_notification_option
350 351 assert_difference 'User.count' do
351 352 submit_email(
352 353 'ticket_by_unknown_user.eml',
353 354 :issue => {:project => 'ecookbook'},
354 355 :unknown_user => 'create',
355 356 :no_notification => '1'
356 357 )
357 358 end
358 359 user = User.order('id DESC').first
359 360 assert_equal 'none', user.mail_notification
360 361 end
361 362
362 363 def test_add_issue_without_from_header
363 364 Role.anonymous.add_permission!(:add_issues)
364 365 assert_equal false, submit_email('ticket_without_from_header.eml')
365 366 end
366 367
367 368 def test_add_issue_with_invalid_attributes
368 369 with_settings :default_issue_start_date_to_creation_date => '0' do
369 370 issue = submit_email(
370 371 'ticket_with_invalid_attributes.eml',
371 372 :allow_override => 'tracker,category,priority'
372 373 )
373 374 assert issue.is_a?(Issue)
374 375 assert !issue.new_record?
375 376 issue.reload
376 377 assert_nil issue.assigned_to
377 378 assert_nil issue.start_date
378 379 assert_nil issue.due_date
379 380 assert_equal 0, issue.done_ratio
380 381 assert_equal 'Normal', issue.priority.to_s
381 382 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
382 383 end
383 384 end
384 385
385 386 def test_add_issue_with_invalid_project_should_be_assigned_to_default_project
386 387 issue = submit_email('ticket_on_given_project.eml', :issue => {:project => 'ecookbook'}, :allow_override => 'project') do |email|
387 388 email.gsub!(/^Project:.+$/, 'Project: invalid')
388 389 end
389 390 assert issue.is_a?(Issue)
390 391 assert !issue.new_record?
391 392 assert_equal 'ecookbook', issue.project.identifier
392 393 end
393 394
394 395 def test_add_issue_with_localized_attributes
395 396 User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
396 397 issue = submit_email(
397 398 'ticket_with_localized_attributes.eml',
398 399 :allow_override => 'tracker,category,priority'
399 400 )
400 401 assert issue.is_a?(Issue)
401 402 assert !issue.new_record?
402 403 issue.reload
403 404 assert_equal 'New ticket on a given project', issue.subject
404 405 assert_equal User.find_by_login('jsmith'), issue.author
405 406 assert_equal Project.find(2), issue.project
406 407 assert_equal 'Feature request', issue.tracker.to_s
407 408 assert_equal 'Stock management', issue.category.to_s
408 409 assert_equal 'Urgent', issue.priority.to_s
409 410 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
410 411 end
411 412
412 413 def test_add_issue_with_japanese_keywords
413 414 ja_dev = "\xe9\x96\x8b\xe7\x99\xba"
414 415 ja_dev.force_encoding('UTF-8') if ja_dev.respond_to?(:force_encoding)
415 416 tracker = Tracker.create!(:name => ja_dev)
416 417 Project.find(1).trackers << tracker
417 418 issue = submit_email(
418 419 'japanese_keywords_iso_2022_jp.eml',
419 420 :issue => {:project => 'ecookbook'},
420 421 :allow_override => 'tracker'
421 422 )
422 423 assert_kind_of Issue, issue
423 424 assert_equal tracker, issue.tracker
424 425 end
425 426
426 427 def test_add_issue_from_apple_mail
427 428 issue = submit_email(
428 429 'apple_mail_with_attachment.eml',
429 430 :issue => {:project => 'ecookbook'}
430 431 )
431 432 assert_kind_of Issue, issue
432 433 assert_equal 1, issue.attachments.size
433 434
434 435 attachment = issue.attachments.first
435 436 assert_equal 'paella.jpg', attachment.filename
436 437 assert_equal 10790, attachment.filesize
437 438 assert File.exist?(attachment.diskfile)
438 439 assert_equal 10790, File.size(attachment.diskfile)
439 440 assert_equal 'caaf384198bcbc9563ab5c058acd73cd', attachment.digest
440 441 end
441 442
442 443 def test_thunderbird_with_attachment_ja
443 444 issue = submit_email(
444 445 'thunderbird_with_attachment_ja.eml',
445 446 :issue => {:project => 'ecookbook'}
446 447 )
447 448 assert_kind_of Issue, issue
448 449 assert_equal 1, issue.attachments.size
449 450 ja = "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.txt"
450 451 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
451 452 attachment = issue.attachments.first
452 453 assert_equal ja, attachment.filename
453 454 assert_equal 5, attachment.filesize
454 455 assert File.exist?(attachment.diskfile)
455 456 assert_equal 5, File.size(attachment.diskfile)
456 457 assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
457 458 end
458 459
459 460 def test_gmail_with_attachment_ja
460 461 issue = submit_email(
461 462 'gmail_with_attachment_ja.eml',
462 463 :issue => {:project => 'ecookbook'}
463 464 )
464 465 assert_kind_of Issue, issue
465 466 assert_equal 1, issue.attachments.size
466 467 ja = "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.txt"
467 468 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
468 469 attachment = issue.attachments.first
469 470 assert_equal ja, attachment.filename
470 471 assert_equal 5, attachment.filesize
471 472 assert File.exist?(attachment.diskfile)
472 473 assert_equal 5, File.size(attachment.diskfile)
473 474 assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
474 475 end
475 476
476 477 def test_thunderbird_with_attachment_latin1
477 478 issue = submit_email(
478 479 'thunderbird_with_attachment_iso-8859-1.eml',
479 480 :issue => {:project => 'ecookbook'}
480 481 )
481 482 assert_kind_of Issue, issue
482 483 assert_equal 1, issue.attachments.size
483 484 u = ""
484 485 u.force_encoding('UTF-8') if u.respond_to?(:force_encoding)
485 486 u1 = "\xc3\x84\xc3\xa4\xc3\x96\xc3\xb6\xc3\x9c\xc3\xbc"
486 487 u1.force_encoding('UTF-8') if u1.respond_to?(:force_encoding)
487 488 11.times { u << u1 }
488 489 attachment = issue.attachments.first
489 490 assert_equal "#{u}.png", attachment.filename
490 491 assert_equal 130, attachment.filesize
491 492 assert File.exist?(attachment.diskfile)
492 493 assert_equal 130, File.size(attachment.diskfile)
493 494 assert_equal '4d80e667ac37dddfe05502530f152abb', attachment.digest
494 495 end
495 496
496 497 def test_gmail_with_attachment_latin1
497 498 issue = submit_email(
498 499 'gmail_with_attachment_iso-8859-1.eml',
499 500 :issue => {:project => 'ecookbook'}
500 501 )
501 502 assert_kind_of Issue, issue
502 503 assert_equal 1, issue.attachments.size
503 504 u = ""
504 505 u.force_encoding('UTF-8') if u.respond_to?(:force_encoding)
505 506 u1 = "\xc3\x84\xc3\xa4\xc3\x96\xc3\xb6\xc3\x9c\xc3\xbc"
506 507 u1.force_encoding('UTF-8') if u1.respond_to?(:force_encoding)
507 508 11.times { u << u1 }
508 509 attachment = issue.attachments.first
509 510 assert_equal "#{u}.txt", attachment.filename
510 511 assert_equal 5, attachment.filesize
511 512 assert File.exist?(attachment.diskfile)
512 513 assert_equal 5, File.size(attachment.diskfile)
513 514 assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
514 515 end
515 516
516 517 def test_multiple_inline_text_parts_should_be_appended_to_issue_description
517 518 issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
518 519 assert_include 'first', issue.description
519 520 assert_include 'second', issue.description
520 521 assert_include 'third', issue.description
521 522 end
522 523
523 524 def test_attachment_text_part_should_be_added_as_issue_attachment
524 525 issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
525 526 assert_not_include 'Plain text attachment', issue.description
526 527 attachment = issue.attachments.detect {|a| a.filename == 'textfile.txt'}
527 528 assert_not_nil attachment
528 529 assert_include 'Plain text attachment', File.read(attachment.diskfile)
529 530 end
530 531
531 532 def test_add_issue_with_iso_8859_1_subject
532 533 issue = submit_email(
533 534 'subject_as_iso-8859-1.eml',
534 535 :issue => {:project => 'ecookbook'}
535 536 )
536 537 str = "Testmail from Webmail: \xc3\xa4 \xc3\xb6 \xc3\xbc..."
537 538 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
538 539 assert_kind_of Issue, issue
539 540 assert_equal str, issue.subject
540 541 end
541 542
542 543 def test_quoted_printable_utf8
543 544 issue = submit_email(
544 545 'quoted_printable_utf8.eml',
545 546 :issue => {:project => 'ecookbook'}
546 547 )
547 548 assert_kind_of Issue, issue
548 549 str = "Freundliche Gr\xc3\xbcsse"
549 550 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
550 551 assert_equal str, issue.description
551 552 end
552 553
553 554 def test_gmail_iso8859_2
554 555 issue = submit_email(
555 556 'gmail-iso8859-2.eml',
556 557 :issue => {:project => 'ecookbook'}
557 558 )
558 559 assert_kind_of Issue, issue
559 560 str = "Na \xc5\xa1triku se su\xc5\xa1i \xc5\xa1osi\xc4\x87."
560 561 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
561 562 assert issue.description.include?(str)
562 563 end
563 564
564 565 def test_add_issue_with_japanese_subject
565 566 issue = submit_email(
566 567 'subject_japanese_1.eml',
567 568 :issue => {:project => 'ecookbook'}
568 569 )
569 570 assert_kind_of Issue, issue
570 571 ja = "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88"
571 572 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
572 573 assert_equal ja, issue.subject
573 574 end
574 575
575 576 def test_add_issue_with_korean_body
576 577 # Make sure mail bodies with a charset unknown to Ruby
577 578 # but known to the Mail gem 2.5.4 are handled correctly
578 579 kr = "\xEA\xB3\xA0\xEB\xA7\x99\xEC\x8A\xB5\xEB\x8B\x88\xEB\x8B\xA4."
579 580 if !kr.respond_to?(:force_encoding)
580 581 puts "\nOn Ruby 1.8, skip Korean encoding mail body test"
581 582 else
582 583 kr.force_encoding('UTF-8')
583 584 issue = submit_email(
584 585 'body_ks_c_5601-1987.eml',
585 586 :issue => {:project => 'ecookbook'}
586 587 )
587 588 assert_kind_of Issue, issue
588 589 assert_equal kr, issue.description
589 590 end
590 591 end
591 592
592 593 def test_add_issue_with_no_subject_header
593 594 issue = submit_email(
594 595 'no_subject_header.eml',
595 596 :issue => {:project => 'ecookbook'}
596 597 )
597 598 assert_kind_of Issue, issue
598 599 assert_equal '(no subject)', issue.subject
599 600 end
600 601
601 602 def test_add_issue_with_mixed_japanese_subject
602 603 issue = submit_email(
603 604 'subject_japanese_2.eml',
604 605 :issue => {:project => 'ecookbook'}
605 606 )
606 607 assert_kind_of Issue, issue
607 608 ja = "Re: \xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88"
608 609 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
609 610 assert_equal ja, issue.subject
610 611 end
611 612
612 613 def test_should_ignore_emails_from_locked_users
613 614 User.find(2).lock!
614 615
615 616 MailHandler.any_instance.expects(:dispatch).never
616 617 assert_no_difference 'Issue.count' do
617 618 assert_equal false, submit_email('ticket_on_given_project.eml')
618 619 end
619 620 end
620 621
621 622 def test_should_ignore_emails_from_emission_address
622 623 Role.anonymous.add_permission!(:add_issues)
623 624 assert_no_difference 'User.count' do
624 625 assert_equal false,
625 626 submit_email(
626 627 'ticket_from_emission_address.eml',
627 628 :issue => {:project => 'ecookbook'},
628 629 :unknown_user => 'create'
629 630 )
630 631 end
631 632 end
632 633
633 634 def test_should_ignore_auto_replied_emails
634 635 MailHandler.any_instance.expects(:dispatch).never
635 636 [
636 637 "X-Auto-Response-Suppress: OOF",
637 638 "Auto-Submitted: auto-replied",
638 639 "Auto-Submitted: Auto-Replied",
639 640 "Auto-Submitted: auto-generated"
640 641 ].each do |header|
641 642 raw = IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
642 643 raw = header + "\n" + raw
643 644
644 645 assert_no_difference 'Issue.count' do
645 646 assert_equal false, MailHandler.receive(raw), "email with #{header} header was not ignored"
646 647 end
647 648 end
648 649 end
649 650
650 651 def test_add_issue_should_send_email_notification
651 652 Setting.notified_events = ['issue_added']
652 653 ActionMailer::Base.deliveries.clear
653 654 # This email contains: 'Project: onlinestore'
654 655 issue = submit_email('ticket_on_given_project.eml')
655 656 assert issue.is_a?(Issue)
656 657 assert_equal 1, ActionMailer::Base.deliveries.size
657 658 end
658 659
659 660 def test_update_issue
660 661 journal = submit_email('ticket_reply.eml')
661 662 assert journal.is_a?(Journal)
662 663 assert_equal User.find_by_login('jsmith'), journal.user
663 664 assert_equal Issue.find(2), journal.journalized
664 665 assert_match /This is reply/, journal.notes
665 666 assert_equal false, journal.private_notes
666 667 assert_equal 'Feature request', journal.issue.tracker.name
667 668 end
668 669
669 670 def test_update_issue_with_attribute_changes
670 671 # This email contains: 'Status: Resolved'
671 672 journal = submit_email('ticket_reply_with_status.eml')
672 673 assert journal.is_a?(Journal)
673 674 issue = Issue.find(journal.issue.id)
674 675 assert_equal User.find_by_login('jsmith'), journal.user
675 676 assert_equal Issue.find(2), journal.journalized
676 677 assert_match /This is reply/, journal.notes
677 678 assert_equal 'Feature request', journal.issue.tracker.name
678 679 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
679 680 assert_equal '2010-01-01', issue.start_date.to_s
680 681 assert_equal '2010-12-31', issue.due_date.to_s
681 682 assert_equal User.find_by_login('jsmith'), issue.assigned_to
682 683 assert_equal "52.6", issue.custom_value_for(CustomField.find_by_name('Float field')).value
683 684 # keywords should be removed from the email body
684 685 assert !journal.notes.match(/^Status:/i)
685 686 assert !journal.notes.match(/^Start Date:/i)
686 687 end
687 688
688 689 def test_update_issue_with_attachment
689 690 assert_difference 'Journal.count' do
690 691 assert_difference 'JournalDetail.count' do
691 692 assert_difference 'Attachment.count' do
692 693 assert_no_difference 'Issue.count' do
693 694 journal = submit_email('ticket_with_attachment.eml') do |raw|
694 695 raw.gsub! /^Subject: .*$/, 'Subject: Re: [Cookbook - Feature #2] (New) Add ingredients categories'
695 696 end
696 697 end
697 698 end
698 699 end
699 700 end
700 701 journal = Journal.order('id DESC').first
701 702 assert_equal Issue.find(2), journal.journalized
702 703 assert_equal 1, journal.details.size
703 704
704 705 detail = journal.details.first
705 706 assert_equal 'attachment', detail.property
706 707 assert_equal 'Paella.jpg', detail.value
707 708 end
708 709
709 710 def test_update_issue_should_send_email_notification
710 711 ActionMailer::Base.deliveries.clear
711 712 journal = submit_email('ticket_reply.eml')
712 713 assert journal.is_a?(Journal)
713 714 assert_equal 1, ActionMailer::Base.deliveries.size
714 715 end
715 716
716 717 def test_update_issue_should_not_set_defaults
717 718 journal = submit_email(
718 719 'ticket_reply.eml',
719 720 :issue => {:tracker => 'Support request', :priority => 'High'}
720 721 )
721 722 assert journal.is_a?(Journal)
722 723 assert_match /This is reply/, journal.notes
723 724 assert_equal 'Feature request', journal.issue.tracker.name
724 725 assert_equal 'Normal', journal.issue.priority.name
725 726 end
726 727
727 728 def test_replying_to_a_private_note_should_add_reply_as_private
728 729 private_journal = Journal.create!(:notes => 'Private notes', :journalized => Issue.find(1), :private_notes => true, :user_id => 2)
729 730
730 731 assert_difference 'Journal.count' do
731 732 journal = submit_email('ticket_reply.eml') do |email|
732 733 email.sub! %r{^In-Reply-To:.*$}, "In-Reply-To: <redmine.journal-#{private_journal.id}.20060719210421@osiris>"
733 734 end
734 735
735 736 assert_kind_of Journal, journal
736 737 assert_match /This is reply/, journal.notes
737 738 assert_equal true, journal.private_notes
738 739 end
739 740 end
740 741
741 742 def test_reply_to_a_message
742 743 m = submit_email('message_reply.eml')
743 744 assert m.is_a?(Message)
744 745 assert !m.new_record?
745 746 m.reload
746 747 assert_equal 'Reply via email', m.subject
747 748 # The email replies to message #2 which is part of the thread of message #1
748 749 assert_equal Message.find(1), m.parent
749 750 end
750 751
751 752 def test_reply_to_a_message_by_subject
752 753 m = submit_email('message_reply_by_subject.eml')
753 754 assert m.is_a?(Message)
754 755 assert !m.new_record?
755 756 m.reload
756 757 assert_equal 'Reply to the first post', m.subject
757 758 assert_equal Message.find(1), m.parent
758 759 end
759 760
760 761 def test_should_strip_tags_of_html_only_emails
761 762 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
762 763 assert issue.is_a?(Issue)
763 764 assert !issue.new_record?
764 765 issue.reload
765 766 assert_equal 'HTML email', issue.subject
766 767 assert_equal 'This is a html-only email.', issue.description
767 768 end
768 769
769 770 test "truncate emails with no setting should add the entire email into the issue" do
770 771 with_settings :mail_handler_body_delimiters => '' do
771 772 issue = submit_email('ticket_on_given_project.eml')
772 773 assert_issue_created(issue)
773 774 assert issue.description.include?('---')
774 775 assert issue.description.include?('This paragraph is after the delimiter')
775 776 end
776 777 end
777 778
778 779 test "truncate emails with a single string should truncate the email at the delimiter for the issue" do
779 780 with_settings :mail_handler_body_delimiters => '---' do
780 781 issue = submit_email('ticket_on_given_project.eml')
781 782 assert_issue_created(issue)
782 783 assert issue.description.include?('This paragraph is before delimiters')
783 784 assert issue.description.include?('--- This line starts with a delimiter')
784 785 assert !issue.description.match(/^---$/)
785 786 assert !issue.description.include?('This paragraph is after the delimiter')
786 787 end
787 788 end
788 789
789 790 test "truncate emails with a single quoted reply should truncate the email at the delimiter with the quoted reply symbols (>)" do
790 791 with_settings :mail_handler_body_delimiters => '--- Reply above. Do not remove this line. ---' do
791 792 journal = submit_email('issue_update_with_quoted_reply_above.eml')
792 793 assert journal.is_a?(Journal)
793 794 assert journal.notes.include?('An update to the issue by the sender.')
794 795 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
795 796 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
796 797 end
797 798 end
798 799
799 800 test "truncate emails with multiple quoted replies should truncate the email at the delimiter with the quoted reply symbols (>)" do
800 801 with_settings :mail_handler_body_delimiters => '--- Reply above. Do not remove this line. ---' do
801 802 journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
802 803 assert journal.is_a?(Journal)
803 804 assert journal.notes.include?('An update to the issue by the sender.')
804 805 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
805 806 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
806 807 end
807 808 end
808 809
809 810 test "truncate emails with multiple strings should truncate the email at the first delimiter found (BREAK)" do
810 811 with_settings :mail_handler_body_delimiters => "---\nBREAK" do
811 812 issue = submit_email('ticket_on_given_project.eml')
812 813 assert_issue_created(issue)
813 814 assert issue.description.include?('This paragraph is before delimiters')
814 815 assert !issue.description.include?('BREAK')
815 816 assert !issue.description.include?('This paragraph is between delimiters')
816 817 assert !issue.description.match(/^---$/)
817 818 assert !issue.description.include?('This paragraph is after the delimiter')
818 819 end
819 820 end
820 821
821 822 def test_attachments_that_match_mail_handler_excluded_filenames_should_be_ignored
822 823 with_settings :mail_handler_excluded_filenames => '*.vcf, *.jpg' do
823 824 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
824 825 assert issue.is_a?(Issue)
825 826 assert !issue.new_record?
826 827 assert_equal 0, issue.reload.attachments.size
827 828 end
828 829 end
829 830
830 831 def test_attachments_that_do_not_match_mail_handler_excluded_filenames_should_be_attached
831 832 with_settings :mail_handler_excluded_filenames => '*.vcf, *.gif' do
832 833 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
833 834 assert issue.is_a?(Issue)
834 835 assert !issue.new_record?
835 836 assert_equal 1, issue.reload.attachments.size
836 837 end
837 838 end
838 839
839 840 def test_email_with_long_subject_line
840 841 issue = submit_email('ticket_with_long_subject.eml')
841 842 assert issue.is_a?(Issue)
842 843 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]
843 844 end
844 845
845 846 def test_new_user_from_attributes_should_return_valid_user
846 847 to_test = {
847 848 # [address, name] => [login, firstname, lastname]
848 849 ['jsmith@example.net', nil] => ['jsmith@example.net', 'jsmith', '-'],
849 850 ['jsmith@example.net', 'John'] => ['jsmith@example.net', 'John', '-'],
850 851 ['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'],
851 852 ['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'],
852 853 ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'],
853 854 ['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh']
854 855 }
855 856
856 857 to_test.each do |attrs, expected|
857 858 user = MailHandler.new_user_from_attributes(attrs.first, attrs.last)
858 859
859 860 assert user.valid?, user.errors.full_messages.to_s
860 861 assert_equal attrs.first, user.mail
861 862 assert_equal expected[0], user.login
862 863 assert_equal expected[1], user.firstname
863 864 assert_equal expected[2], user.lastname
864 865 assert_equal 'only_my_events', user.mail_notification
865 866 end
866 867 end
867 868
868 869 def test_new_user_from_attributes_should_use_default_login_if_invalid
869 870 user = MailHandler.new_user_from_attributes('foo+bar@example.net')
870 871 assert user.valid?
871 872 assert user.login =~ /^user[a-f0-9]+$/
872 873 assert_equal 'foo+bar@example.net', user.mail
873 874 end
874 875
875 876 def test_new_user_with_utf8_encoded_fullname_should_be_decoded
876 877 assert_difference 'User.count' do
877 878 issue = submit_email(
878 879 'fullname_of_sender_as_utf8_encoded.eml',
879 880 :issue => {:project => 'ecookbook'},
880 881 :unknown_user => 'create'
881 882 )
882 883 end
883 884 user = User.order('id DESC').first
884 885 assert_equal "foo@example.org", user.mail
885 886 str1 = "\xc3\x84\xc3\xa4"
886 887 str2 = "\xc3\x96\xc3\xb6"
887 888 str1.force_encoding('UTF-8') if str1.respond_to?(:force_encoding)
888 889 str2.force_encoding('UTF-8') if str2.respond_to?(:force_encoding)
889 890 assert_equal str1, user.firstname
890 891 assert_equal str2, user.lastname
891 892 end
892 893
893 894 def test_extract_options_from_env_should_return_options
894 895 options = MailHandler.extract_options_from_env({
895 896 'tracker' => 'defect',
896 897 'project' => 'foo',
897 898 'unknown_user' => 'create'
898 899 })
899 900
900 901 assert_equal({
901 902 :issue => {:tracker => 'defect', :project => 'foo'},
902 903 :unknown_user => 'create'
903 904 }, options)
904 905 end
905 906
906 907 private
907 908
908 909 def submit_email(filename, options={})
909 910 raw = IO.read(File.join(FIXTURES_PATH, filename))
910 911 yield raw if block_given?
911 912 MailHandler.receive(raw, options)
912 913 end
913 914
914 915 def assert_issue_created(issue)
915 916 assert issue.is_a?(Issue)
916 917 assert !issue.new_record?
917 918 issue.reload
918 919 end
919 920 end
General Comments 0
You need to be logged in to leave comments. Login now