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