##// END OF EJS Templates
replace non ASCII characters to hexadecimals at unit mail handler test...
Toshi MARUYAMA -
r10587:70bcbd1404ae
parent child
Show More
@@ -1,709 +1,711
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2012 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 'Value for a custom field',
181 181 issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
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_add_issue_without_from_header
308 308 Role.anonymous.add_permission!(:add_issues)
309 309 assert_equal false, submit_email('ticket_without_from_header.eml')
310 310 end
311 311
312 312 def test_add_issue_with_invalid_attributes
313 313 issue = submit_email(
314 314 'ticket_with_invalid_attributes.eml',
315 315 :allow_override => 'tracker,category,priority'
316 316 )
317 317 assert issue.is_a?(Issue)
318 318 assert !issue.new_record?
319 319 issue.reload
320 320 assert_nil issue.assigned_to
321 321 assert_nil issue.start_date
322 322 assert_nil issue.due_date
323 323 assert_equal 0, issue.done_ratio
324 324 assert_equal 'Normal', issue.priority.to_s
325 325 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
326 326 end
327 327
328 328 def test_add_issue_with_localized_attributes
329 329 User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
330 330 issue = submit_email(
331 331 'ticket_with_localized_attributes.eml',
332 332 :allow_override => 'tracker,category,priority'
333 333 )
334 334 assert issue.is_a?(Issue)
335 335 assert !issue.new_record?
336 336 issue.reload
337 337 assert_equal 'New ticket on a given project', issue.subject
338 338 assert_equal User.find_by_login('jsmith'), issue.author
339 339 assert_equal Project.find(2), issue.project
340 340 assert_equal 'Feature request', issue.tracker.to_s
341 341 assert_equal 'Stock management', issue.category.to_s
342 342 assert_equal 'Urgent', issue.priority.to_s
343 343 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
344 344 end
345 345
346 346 def test_add_issue_with_japanese_keywords
347 347 ja_dev = "\xe9\x96\x8b\xe7\x99\xba"
348 348 ja_dev.force_encoding('UTF-8') if ja_dev.respond_to?(:force_encoding)
349 349 tracker = Tracker.create!(:name => ja_dev)
350 350 Project.find(1).trackers << tracker
351 351 issue = submit_email(
352 352 'japanese_keywords_iso_2022_jp.eml',
353 353 :issue => {:project => 'ecookbook'},
354 354 :allow_override => 'tracker'
355 355 )
356 356 assert_kind_of Issue, issue
357 357 assert_equal tracker, issue.tracker
358 358 end
359 359
360 360 def test_add_issue_from_apple_mail
361 361 issue = submit_email(
362 362 'apple_mail_with_attachment.eml',
363 363 :issue => {:project => 'ecookbook'}
364 364 )
365 365 assert_kind_of Issue, issue
366 366 assert_equal 1, issue.attachments.size
367 367
368 368 attachment = issue.attachments.first
369 369 assert_equal 'paella.jpg', attachment.filename
370 370 assert_equal 10790, attachment.filesize
371 371 assert File.exist?(attachment.diskfile)
372 372 assert_equal 10790, File.size(attachment.diskfile)
373 373 assert_equal 'caaf384198bcbc9563ab5c058acd73cd', attachment.digest
374 374 end
375 375
376 376 def test_add_issue_with_iso_8859_1_subject
377 377 issue = submit_email(
378 378 'subject_as_iso-8859-1.eml',
379 379 :issue => {:project => 'ecookbook'}
380 380 )
381 str = "Testmail from Webmail: \xc3\xa4 \xc3\xb6 \xc3\xbc..."
382 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
381 383 assert_kind_of Issue, issue
382 assert_equal 'Testmail from Webmail: Γ€ ΓΆ ΓΌ...', issue.subject
384 assert_equal str, issue.subject
383 385 end
384 386
385 387 def test_add_issue_with_japanese_subject
386 388 issue = submit_email(
387 389 'subject_japanese_1.eml',
388 390 :issue => {:project => 'ecookbook'}
389 391 )
390 392 assert_kind_of Issue, issue
391 393 ja = "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88"
392 394 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
393 395 assert_equal ja, issue.subject
394 396 end
395 397
396 398 def test_add_issue_with_mixed_japanese_subject
397 399 issue = submit_email(
398 400 'subject_japanese_2.eml',
399 401 :issue => {:project => 'ecookbook'}
400 402 )
401 403 assert_kind_of Issue, issue
402 404 ja = "Re: \xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88"
403 405 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
404 406 assert_equal ja, issue.subject
405 407 end
406 408
407 409 def test_should_ignore_emails_from_locked_users
408 410 User.find(2).lock!
409 411
410 412 MailHandler.any_instance.expects(:dispatch).never
411 413 assert_no_difference 'Issue.count' do
412 414 assert_equal false, submit_email('ticket_on_given_project.eml')
413 415 end
414 416 end
415 417
416 418 def test_should_ignore_emails_from_emission_address
417 419 Role.anonymous.add_permission!(:add_issues)
418 420 assert_no_difference 'User.count' do
419 421 assert_equal false,
420 422 submit_email(
421 423 'ticket_from_emission_address.eml',
422 424 :issue => {:project => 'ecookbook'},
423 425 :unknown_user => 'create'
424 426 )
425 427 end
426 428 end
427 429
428 430 def test_should_ignore_auto_replied_emails
429 431 MailHandler.any_instance.expects(:dispatch).never
430 432 [
431 433 "X-Auto-Response-Suppress: OOF",
432 434 "Auto-Submitted: auto-replied",
433 435 "Auto-Submitted: Auto-Replied",
434 436 "Auto-Submitted: auto-generated"
435 437 ].each do |header|
436 438 raw = IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
437 439 raw = header + "\n" + raw
438 440
439 441 assert_no_difference 'Issue.count' do
440 442 assert_equal false, MailHandler.receive(raw), "email with #{header} header was not ignored"
441 443 end
442 444 end
443 445 end
444 446
445 447 def test_add_issue_should_send_email_notification
446 448 Setting.notified_events = ['issue_added']
447 449 ActionMailer::Base.deliveries.clear
448 450 # This email contains: 'Project: onlinestore'
449 451 issue = submit_email('ticket_on_given_project.eml')
450 452 assert issue.is_a?(Issue)
451 453 assert_equal 1, ActionMailer::Base.deliveries.size
452 454 end
453 455
454 456 def test_update_issue
455 457 journal = submit_email('ticket_reply.eml')
456 458 assert journal.is_a?(Journal)
457 459 assert_equal User.find_by_login('jsmith'), journal.user
458 460 assert_equal Issue.find(2), journal.journalized
459 461 assert_match /This is reply/, journal.notes
460 462 assert_equal false, journal.private_notes
461 463 assert_equal 'Feature request', journal.issue.tracker.name
462 464 end
463 465
464 466 def test_update_issue_with_attribute_changes
465 467 # This email contains: 'Status: Resolved'
466 468 journal = submit_email('ticket_reply_with_status.eml')
467 469 assert journal.is_a?(Journal)
468 470 issue = Issue.find(journal.issue.id)
469 471 assert_equal User.find_by_login('jsmith'), journal.user
470 472 assert_equal Issue.find(2), journal.journalized
471 473 assert_match /This is reply/, journal.notes
472 474 assert_equal 'Feature request', journal.issue.tracker.name
473 475 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
474 476 assert_equal '2010-01-01', issue.start_date.to_s
475 477 assert_equal '2010-12-31', issue.due_date.to_s
476 478 assert_equal User.find_by_login('jsmith'), issue.assigned_to
477 479 assert_equal "52.6", issue.custom_value_for(CustomField.find_by_name('Float field')).value
478 480 # keywords should be removed from the email body
479 481 assert !journal.notes.match(/^Status:/i)
480 482 assert !journal.notes.match(/^Start Date:/i)
481 483 end
482 484
483 485 def test_update_issue_with_attachment
484 486 assert_difference 'Journal.count' do
485 487 assert_difference 'JournalDetail.count' do
486 488 assert_difference 'Attachment.count' do
487 489 assert_no_difference 'Issue.count' do
488 490 journal = submit_email('ticket_with_attachment.eml') do |raw|
489 491 raw.gsub! /^Subject: .*$/, 'Subject: Re: [Cookbook - Feature #2] (New) Add ingredients categories'
490 492 end
491 493 end
492 494 end
493 495 end
494 496 end
495 497 journal = Journal.first(:order => 'id DESC')
496 498 assert_equal Issue.find(2), journal.journalized
497 499 assert_equal 1, journal.details.size
498 500
499 501 detail = journal.details.first
500 502 assert_equal 'attachment', detail.property
501 503 assert_equal 'Paella.jpg', detail.value
502 504 end
503 505
504 506 def test_update_issue_should_send_email_notification
505 507 ActionMailer::Base.deliveries.clear
506 508 journal = submit_email('ticket_reply.eml')
507 509 assert journal.is_a?(Journal)
508 510 assert_equal 1, ActionMailer::Base.deliveries.size
509 511 end
510 512
511 513 def test_update_issue_should_not_set_defaults
512 514 journal = submit_email(
513 515 'ticket_reply.eml',
514 516 :issue => {:tracker => 'Support request', :priority => 'High'}
515 517 )
516 518 assert journal.is_a?(Journal)
517 519 assert_match /This is reply/, journal.notes
518 520 assert_equal 'Feature request', journal.issue.tracker.name
519 521 assert_equal 'Normal', journal.issue.priority.name
520 522 end
521 523
522 524 def test_replying_to_a_private_note_should_add_reply_as_private
523 525 private_journal = Journal.create!(:notes => 'Private notes', :journalized => Issue.find(1), :private_notes => true, :user_id => 2)
524 526
525 527 assert_difference 'Journal.count' do
526 528 journal = submit_email('ticket_reply.eml') do |email|
527 529 email.sub! %r{^In-Reply-To:.*$}, "In-Reply-To: <redmine.journal-#{private_journal.id}.20060719210421@osiris>"
528 530 end
529 531
530 532 assert_kind_of Journal, journal
531 533 assert_match /This is reply/, journal.notes
532 534 assert_equal true, journal.private_notes
533 535 end
534 536 end
535 537
536 538 def test_reply_to_a_message
537 539 m = submit_email('message_reply.eml')
538 540 assert m.is_a?(Message)
539 541 assert !m.new_record?
540 542 m.reload
541 543 assert_equal 'Reply via email', m.subject
542 544 # The email replies to message #2 which is part of the thread of message #1
543 545 assert_equal Message.find(1), m.parent
544 546 end
545 547
546 548 def test_reply_to_a_message_by_subject
547 549 m = submit_email('message_reply_by_subject.eml')
548 550 assert m.is_a?(Message)
549 551 assert !m.new_record?
550 552 m.reload
551 553 assert_equal 'Reply to the first post', m.subject
552 554 assert_equal Message.find(1), m.parent
553 555 end
554 556
555 557 def test_should_strip_tags_of_html_only_emails
556 558 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
557 559 assert issue.is_a?(Issue)
558 560 assert !issue.new_record?
559 561 issue.reload
560 562 assert_equal 'HTML email', issue.subject
561 563 assert_equal 'This is a html-only email.', issue.description
562 564 end
563 565
564 566 context "truncate emails based on the Setting" do
565 567 context "with no setting" do
566 568 setup do
567 569 Setting.mail_handler_body_delimiters = ''
568 570 end
569 571
570 572 should "add the entire email into the issue" do
571 573 issue = submit_email('ticket_on_given_project.eml')
572 574 assert_issue_created(issue)
573 575 assert issue.description.include?('---')
574 576 assert issue.description.include?('This paragraph is after the delimiter')
575 577 end
576 578 end
577 579
578 580 context "with a single string" do
579 581 setup do
580 582 Setting.mail_handler_body_delimiters = '---'
581 583 end
582 584 should "truncate the email at the delimiter for the issue" do
583 585 issue = submit_email('ticket_on_given_project.eml')
584 586 assert_issue_created(issue)
585 587 assert issue.description.include?('This paragraph is before delimiters')
586 588 assert issue.description.include?('--- This line starts with a delimiter')
587 589 assert !issue.description.match(/^---$/)
588 590 assert !issue.description.include?('This paragraph is after the delimiter')
589 591 end
590 592 end
591 593
592 594 context "with a single quoted reply (e.g. reply to a Redmine email notification)" do
593 595 setup do
594 596 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
595 597 end
596 598 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
597 599 journal = submit_email('issue_update_with_quoted_reply_above.eml')
598 600 assert journal.is_a?(Journal)
599 601 assert journal.notes.include?('An update to the issue by the sender.')
600 602 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
601 603 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
602 604 end
603 605 end
604 606
605 607 context "with multiple quoted replies (e.g. reply to a reply of a Redmine email notification)" do
606 608 setup do
607 609 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
608 610 end
609 611 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
610 612 journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
611 613 assert journal.is_a?(Journal)
612 614 assert journal.notes.include?('An update to the issue by the sender.')
613 615 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
614 616 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
615 617 end
616 618 end
617 619
618 620 context "with multiple strings" do
619 621 setup do
620 622 Setting.mail_handler_body_delimiters = "---\nBREAK"
621 623 end
622 624 should "truncate the email at the first delimiter found (BREAK)" do
623 625 issue = submit_email('ticket_on_given_project.eml')
624 626 assert_issue_created(issue)
625 627 assert issue.description.include?('This paragraph is before delimiters')
626 628 assert !issue.description.include?('BREAK')
627 629 assert !issue.description.include?('This paragraph is between delimiters')
628 630 assert !issue.description.match(/^---$/)
629 631 assert !issue.description.include?('This paragraph is after the delimiter')
630 632 end
631 633 end
632 634 end
633 635
634 636 def test_email_with_long_subject_line
635 637 issue = submit_email('ticket_with_long_subject.eml')
636 638 assert issue.is_a?(Issue)
637 639 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]
638 640 end
639 641
640 642 def test_new_user_from_attributes_should_return_valid_user
641 643 to_test = {
642 644 # [address, name] => [login, firstname, lastname]
643 645 ['jsmith@example.net', nil] => ['jsmith@example.net', 'jsmith', '-'],
644 646 ['jsmith@example.net', 'John'] => ['jsmith@example.net', 'John', '-'],
645 647 ['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'],
646 648 ['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'],
647 649 ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'],
648 650 ['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh']
649 651 }
650 652
651 653 to_test.each do |attrs, expected|
652 654 user = MailHandler.new_user_from_attributes(attrs.first, attrs.last)
653 655
654 656 assert user.valid?, user.errors.full_messages.to_s
655 657 assert_equal attrs.first, user.mail
656 658 assert_equal expected[0], user.login
657 659 assert_equal expected[1], user.firstname
658 660 assert_equal expected[2], user.lastname
659 661 end
660 662 end
661 663
662 664 def test_new_user_from_attributes_should_respect_minimum_password_length
663 665 with_settings :password_min_length => 15 do
664 666 user = MailHandler.new_user_from_attributes('jsmith@example.net')
665 667 assert user.valid?
666 668 assert user.password.length >= 15
667 669 end
668 670 end
669 671
670 672 def test_new_user_from_attributes_should_use_default_login_if_invalid
671 673 user = MailHandler.new_user_from_attributes('foo+bar@example.net')
672 674 assert user.valid?
673 675 assert user.login =~ /^user[a-f0-9]+$/
674 676 assert_equal 'foo+bar@example.net', user.mail
675 677 end
676 678
677 679 def test_new_user_with_utf8_encoded_fullname_should_be_decoded
678 680 assert_difference 'User.count' do
679 681 issue = submit_email(
680 682 'fullname_of_sender_as_utf8_encoded.eml',
681 683 :issue => {:project => 'ecookbook'},
682 684 :unknown_user => 'create'
683 685 )
684 686 end
685 687
686 688 user = User.first(:order => 'id DESC')
687 689 assert_equal "foo@example.org", user.mail
688 690 str1 = "\xc3\x84\xc3\xa4"
689 691 str2 = "\xc3\x96\xc3\xb6"
690 692 str1.force_encoding('UTF-8') if str1.respond_to?(:force_encoding)
691 693 str2.force_encoding('UTF-8') if str2.respond_to?(:force_encoding)
692 694 assert_equal str1, user.firstname
693 695 assert_equal str2, user.lastname
694 696 end
695 697
696 698 private
697 699
698 700 def submit_email(filename, options={})
699 701 raw = IO.read(File.join(FIXTURES_PATH, filename))
700 702 yield raw if block_given?
701 703 MailHandler.receive(raw, options)
702 704 end
703 705
704 706 def assert_issue_created(issue)
705 707 assert issue.is_a?(Issue)
706 708 assert !issue.new_record?
707 709 issue.reload
708 710 end
709 711 end
General Comments 0
You need to be logged in to leave comments. Login now