##// END OF EJS Templates
code layout clean up test/unit/mail_handler_test.rb...
Toshi MARUYAMA -
r8621:65a1f54f71d7
parent child
Show More
@@ -1,534 +1,595
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2011 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 test_add_issue
39 39 ActionMailer::Base.deliveries.clear
40 40 # This email contains: 'Project: onlinestore'
41 41 issue = submit_email('ticket_on_given_project.eml')
42 42 assert issue.is_a?(Issue)
43 43 assert !issue.new_record?
44 44 issue.reload
45 45 assert_equal Project.find(2), issue.project
46 46 assert_equal issue.project.trackers.first, issue.tracker
47 47 assert_equal 'New ticket on a given project', issue.subject
48 48 assert_equal User.find_by_login('jsmith'), issue.author
49 49 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
50 50 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
51 51 assert_equal '2010-01-01', issue.start_date.to_s
52 52 assert_equal '2010-12-31', issue.due_date.to_s
53 53 assert_equal User.find_by_login('jsmith'), issue.assigned_to
54 54 assert_equal Version.find_by_name('Alpha'), issue.fixed_version
55 55 assert_equal 2.5, issue.estimated_hours
56 56 assert_equal 30, issue.done_ratio
57 57 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
58 58 # keywords should be removed from the email body
59 59 assert !issue.description.match(/^Project:/i)
60 60 assert !issue.description.match(/^Status:/i)
61 61 assert !issue.description.match(/^Start Date:/i)
62 62 # Email notification should be sent
63 63 mail = ActionMailer::Base.deliveries.last
64 64 assert_not_nil mail
65 65 assert mail.subject.include?('New ticket on a given project')
66 66 end
67 67
68 68 def test_add_issue_with_default_tracker
69 69 # This email contains: 'Project: onlinestore'
70 issue = submit_email('ticket_on_given_project.eml', :issue => {:tracker => 'Support request'})
70 issue = submit_email(
71 'ticket_on_given_project.eml',
72 :issue => {:tracker => 'Support request'}
73 )
71 74 assert issue.is_a?(Issue)
72 75 assert !issue.new_record?
73 76 issue.reload
74 77 assert_equal 'Support request', issue.tracker.name
75 78 end
76 79
77 80 def test_add_issue_with_status
78 81 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
79 82 issue = submit_email('ticket_on_given_project.eml')
80 83 assert issue.is_a?(Issue)
81 84 assert !issue.new_record?
82 85 issue.reload
83 86 assert_equal Project.find(2), issue.project
84 87 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
85 88 end
86 89
87 90 def test_add_issue_with_attributes_override
88 issue = submit_email('ticket_with_attributes.eml', :allow_override => 'tracker,category,priority')
91 issue = submit_email(
92 'ticket_with_attributes.eml',
93 :allow_override => 'tracker,category,priority'
94 )
89 95 assert issue.is_a?(Issue)
90 96 assert !issue.new_record?
91 97 issue.reload
92 98 assert_equal 'New ticket on a given project', issue.subject
93 99 assert_equal User.find_by_login('jsmith'), issue.author
94 100 assert_equal Project.find(2), issue.project
95 101 assert_equal 'Feature request', issue.tracker.to_s
96 102 assert_equal 'Stock management', issue.category.to_s
97 103 assert_equal 'Urgent', issue.priority.to_s
98 104 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
99 105 end
100 106
101 107 def test_add_issue_with_group_assignment
102 108 with_settings :issue_group_assignment => '1' do
103 109 issue = submit_email('ticket_on_given_project.eml') do |email|
104 110 email.gsub!('Assigned to: John Smith', 'Assigned to: B Team')
105 111 end
106 112 assert issue.is_a?(Issue)
107 113 assert !issue.new_record?
108 114 issue.reload
109 115 assert_equal Group.find(11), issue.assigned_to
110 116 end
111 117 end
112 118
113 119 def test_add_issue_with_partial_attributes_override
114 issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker'])
120 issue = submit_email(
121 'ticket_with_attributes.eml',
122 :issue => {:priority => 'High'},
123 :allow_override => ['tracker']
124 )
115 125 assert issue.is_a?(Issue)
116 126 assert !issue.new_record?
117 127 issue.reload
118 128 assert_equal 'New ticket on a given project', issue.subject
119 129 assert_equal User.find_by_login('jsmith'), issue.author
120 130 assert_equal Project.find(2), issue.project
121 131 assert_equal 'Feature request', issue.tracker.to_s
122 132 assert_nil issue.category
123 133 assert_equal 'High', issue.priority.to_s
124 134 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
125 135 end
126 136
127 137 def test_add_issue_with_spaces_between_attribute_and_separator
128 issue = submit_email('ticket_with_spaces_between_attribute_and_separator.eml', :allow_override => 'tracker,category,priority')
138 issue = submit_email(
139 'ticket_with_spaces_between_attribute_and_separator.eml',
140 :allow_override => 'tracker,category,priority'
141 )
129 142 assert issue.is_a?(Issue)
130 143 assert !issue.new_record?
131 144 issue.reload
132 145 assert_equal 'New ticket on a given project', issue.subject
133 146 assert_equal User.find_by_login('jsmith'), issue.author
134 147 assert_equal Project.find(2), issue.project
135 148 assert_equal 'Feature request', issue.tracker.to_s
136 149 assert_equal 'Stock management', issue.category.to_s
137 150 assert_equal 'Urgent', issue.priority.to_s
138 151 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
139 152 end
140 153
141 154 def test_add_issue_with_attachment_to_specific_project
142 155 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
143 156 assert issue.is_a?(Issue)
144 157 assert !issue.new_record?
145 158 issue.reload
146 159 assert_equal 'Ticket created by email with attachment', issue.subject
147 160 assert_equal User.find_by_login('jsmith'), issue.author
148 161 assert_equal Project.find(2), issue.project
149 162 assert_equal 'This is a new ticket with attachments', issue.description
150 163 # Attachment properties
151 164 assert_equal 1, issue.attachments.size
152 165 assert_equal 'Paella.jpg', issue.attachments.first.filename
153 166 assert_equal 'image/jpeg', issue.attachments.first.content_type
154 167 assert_equal 10790, issue.attachments.first.filesize
155 168 end
156 169
157 170 def test_add_issue_with_custom_fields
158 171 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
159 172 assert issue.is_a?(Issue)
160 173 assert !issue.new_record?
161 174 issue.reload
162 175 assert_equal 'New ticket with custom field values', issue.subject
163 assert_equal 'Value for a custom field', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
176 assert_equal 'Value for a custom field',
177 issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
164 178 assert !issue.description.match(/^searchable field:/i)
165 179 end
166 180
167 181 def test_add_issue_with_cc
168 182 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
169 183 assert issue.is_a?(Issue)
170 184 assert !issue.new_record?
171 185 issue.reload
172 186 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
173 187 assert_equal 1, issue.watcher_user_ids.size
174 188 end
175 189
176 190 def test_add_issue_by_unknown_user
177 191 assert_no_difference 'User.count' do
178 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'})
192 assert_equal false,
193 submit_email(
194 'ticket_by_unknown_user.eml',
195 :issue => {:project => 'ecookbook'}
196 )
179 197 end
180 198 end
181 199
182 200 def test_add_issue_by_anonymous_user
183 201 Role.anonymous.add_permission!(:add_issues)
184 202 assert_no_difference 'User.count' do
185 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
203 issue = submit_email(
204 'ticket_by_unknown_user.eml',
205 :issue => {:project => 'ecookbook'},
206 :unknown_user => 'accept'
207 )
186 208 assert issue.is_a?(Issue)
187 209 assert issue.author.anonymous?
188 210 end
189 211 end
190 212
191 213 def test_add_issue_by_anonymous_user_with_no_from_address
192 214 Role.anonymous.add_permission!(:add_issues)
193 215 assert_no_difference 'User.count' do
194 issue = submit_email('ticket_by_empty_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'accept')
216 issue = submit_email(
217 'ticket_by_empty_user.eml',
218 :issue => {:project => 'ecookbook'},
219 :unknown_user => 'accept'
220 )
195 221 assert issue.is_a?(Issue)
196 222 assert issue.author.anonymous?
197 223 end
198 224 end
199 225
200 226 def test_add_issue_by_anonymous_user_on_private_project
201 227 Role.anonymous.add_permission!(:add_issues)
202 228 assert_no_difference 'User.count' do
203 229 assert_no_difference 'Issue.count' do
204 assert_equal false, submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :unknown_user => 'accept')
230 assert_equal false,
231 submit_email(
232 'ticket_by_unknown_user.eml',
233 :issue => {:project => 'onlinestore'},
234 :unknown_user => 'accept'
235 )
205 236 end
206 237 end
207 238 end
208 239
209 240 def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
210 241 assert_no_difference 'User.count' do
211 242 assert_difference 'Issue.count' do
212 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'onlinestore'}, :no_permission_check => '1', :unknown_user => 'accept')
243 issue = submit_email(
244 'ticket_by_unknown_user.eml',
245 :issue => {:project => 'onlinestore'},
246 :no_permission_check => '1',
247 :unknown_user => 'accept'
248 )
213 249 assert issue.is_a?(Issue)
214 250 assert issue.author.anonymous?
215 251 assert !issue.project.is_public?
216 252 assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
217 253 end
218 254 end
219 255 end
220 256
221 257 def test_add_issue_by_created_user
222 258 Setting.default_language = 'en'
223 259 assert_difference 'User.count' do
224 issue = submit_email('ticket_by_unknown_user.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
260 issue = submit_email(
261 'ticket_by_unknown_user.eml',
262 :issue => {:project => 'ecookbook'},
263 :unknown_user => 'create'
264 )
225 265 assert issue.is_a?(Issue)
226 266 assert issue.author.active?
227 267 assert_equal 'john.doe@somenet.foo', issue.author.mail
228 268 assert_equal 'John', issue.author.firstname
229 269 assert_equal 'Doe', issue.author.lastname
230 270
231 271 # account information
232 272 email = ActionMailer::Base.deliveries.first
233 273 assert_not_nil email
234 274 assert email.subject.include?('account activation')
235 275 login = email.body.match(/\* Login: (.*)$/)[1]
236 276 password = email.body.match(/\* Password: (.*)$/)[1]
237 277 assert_equal issue.author, User.try_to_login(login, password)
238 278 end
239 279 end
240 280
241 281 def test_add_issue_without_from_header
242 282 Role.anonymous.add_permission!(:add_issues)
243 283 assert_equal false, submit_email('ticket_without_from_header.eml')
244 284 end
245 285
246 286 def test_add_issue_with_invalid_attributes
247 issue = submit_email('ticket_with_invalid_attributes.eml', :allow_override => 'tracker,category,priority')
287 issue = submit_email(
288 'ticket_with_invalid_attributes.eml',
289 :allow_override => 'tracker,category,priority'
290 )
248 291 assert issue.is_a?(Issue)
249 292 assert !issue.new_record?
250 293 issue.reload
251 294 assert_nil issue.assigned_to
252 295 assert_nil issue.start_date
253 296 assert_nil issue.due_date
254 297 assert_equal 0, issue.done_ratio
255 298 assert_equal 'Normal', issue.priority.to_s
256 299 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
257 300 end
258 301
259 302 def test_add_issue_with_localized_attributes
260 303 User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
261 issue = submit_email('ticket_with_localized_attributes.eml', :allow_override => 'tracker,category,priority')
304 issue = submit_email(
305 'ticket_with_localized_attributes.eml',
306 :allow_override => 'tracker,category,priority'
307 )
262 308 assert issue.is_a?(Issue)
263 309 assert !issue.new_record?
264 310 issue.reload
265 311 assert_equal 'New ticket on a given project', issue.subject
266 312 assert_equal User.find_by_login('jsmith'), issue.author
267 313 assert_equal Project.find(2), issue.project
268 314 assert_equal 'Feature request', issue.tracker.to_s
269 315 assert_equal 'Stock management', issue.category.to_s
270 316 assert_equal 'Urgent', issue.priority.to_s
271 317 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
272 318 end
273 319
274 320 def test_add_issue_with_japanese_keywords
275 321 ja_dev = "\xe9\x96\x8b\xe7\x99\xba"
276 322 ja_dev.force_encoding('UTF-8') if ja_dev.respond_to?(:force_encoding)
277 323 tracker = Tracker.create!(:name => ja_dev)
278 324 Project.find(1).trackers << tracker
279 issue = submit_email('japanese_keywords_iso_2022_jp.eml', :issue => {:project => 'ecookbook'}, :allow_override => 'tracker')
325 issue = submit_email(
326 'japanese_keywords_iso_2022_jp.eml',
327 :issue => {:project => 'ecookbook'},
328 :allow_override => 'tracker'
329 )
280 330 assert_kind_of Issue, issue
281 331 assert_equal tracker, issue.tracker
282 332 end
283 333
284 334 def test_add_issue_from_apple_mail
285 issue = submit_email('apple_mail_with_attachment.eml', :issue => {:project => 'ecookbook'})
335 issue = submit_email(
336 'apple_mail_with_attachment.eml',
337 :issue => {:project => 'ecookbook'}
338 )
286 339 assert_kind_of Issue, issue
287 340 assert_equal 1, issue.attachments.size
288 341
289 342 attachment = issue.attachments.first
290 343 assert_equal 'paella.jpg', attachment.filename
291 344 assert_equal 10790, attachment.filesize
292 345 assert File.exist?(attachment.diskfile)
293 346 assert_equal 10790, File.size(attachment.diskfile)
294 347 assert_equal 'caaf384198bcbc9563ab5c058acd73cd', attachment.digest
295 348 end
296 349
297 350 def test_should_ignore_emails_from_emission_address
298 351 Role.anonymous.add_permission!(:add_issues)
299 352 assert_no_difference 'User.count' do
300 assert_equal false, submit_email('ticket_from_emission_address.eml', :issue => {:project => 'ecookbook'}, :unknown_user => 'create')
353 assert_equal false,
354 submit_email(
355 'ticket_from_emission_address.eml',
356 :issue => {:project => 'ecookbook'},
357 :unknown_user => 'create'
358 )
301 359 end
302 360 end
303 361
304 362 def test_add_issue_should_send_email_notification
305 363 Setting.notified_events = ['issue_added']
306 364 ActionMailer::Base.deliveries.clear
307 365 # This email contains: 'Project: onlinestore'
308 366 issue = submit_email('ticket_on_given_project.eml')
309 367 assert issue.is_a?(Issue)
310 368 assert_equal 1, ActionMailer::Base.deliveries.size
311 369 end
312 370
313 371 def test_update_issue
314 372 journal = submit_email('ticket_reply.eml')
315 373 assert journal.is_a?(Journal)
316 374 assert_equal User.find_by_login('jsmith'), journal.user
317 375 assert_equal Issue.find(2), journal.journalized
318 376 assert_match /This is reply/, journal.notes
319 377 assert_equal 'Feature request', journal.issue.tracker.name
320 378 end
321 379
322 380 def test_update_issue_with_attribute_changes
323 381 # This email contains: 'Status: Resolved'
324 382 journal = submit_email('ticket_reply_with_status.eml')
325 383 assert journal.is_a?(Journal)
326 384 issue = Issue.find(journal.issue.id)
327 385 assert_equal User.find_by_login('jsmith'), journal.user
328 386 assert_equal Issue.find(2), journal.journalized
329 387 assert_match /This is reply/, journal.notes
330 388 assert_equal 'Feature request', journal.issue.tracker.name
331 389 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
332 390 assert_equal '2010-01-01', issue.start_date.to_s
333 391 assert_equal '2010-12-31', issue.due_date.to_s
334 392 assert_equal User.find_by_login('jsmith'), issue.assigned_to
335 393 assert_equal "52.6", issue.custom_value_for(CustomField.find_by_name('Float field')).value
336 394 # keywords should be removed from the email body
337 395 assert !journal.notes.match(/^Status:/i)
338 396 assert !journal.notes.match(/^Start Date:/i)
339 397 end
340 398
341 399 def test_update_issue_with_attachment
342 400 assert_difference 'Journal.count' do
343 401 assert_difference 'JournalDetail.count' do
344 402 assert_difference 'Attachment.count' do
345 403 assert_no_difference 'Issue.count' do
346 404 journal = submit_email('ticket_with_attachment.eml') do |raw|
347 405 raw.gsub! /^Subject: .*$/, 'Subject: Re: [Cookbook - Feature #2] (New) Add ingredients categories'
348 406 end
349 407 end
350 408 end
351 409 end
352 410 end
353 411 journal = Journal.first(:order => 'id DESC')
354 412 assert_equal Issue.find(2), journal.journalized
355 413 assert_equal 1, journal.details.size
356 414
357 415 detail = journal.details.first
358 416 assert_equal 'attachment', detail.property
359 417 assert_equal 'Paella.jpg', detail.value
360 418 end
361 419
362 420 def test_update_issue_should_send_email_notification
363 421 ActionMailer::Base.deliveries.clear
364 422 journal = submit_email('ticket_reply.eml')
365 423 assert journal.is_a?(Journal)
366 424 assert_equal 1, ActionMailer::Base.deliveries.size
367 425 end
368 426
369 427 def test_update_issue_should_not_set_defaults
370 journal = submit_email('ticket_reply.eml', :issue => {:tracker => 'Support request', :priority => 'High'})
428 journal = submit_email(
429 'ticket_reply.eml',
430 :issue => {:tracker => 'Support request', :priority => 'High'}
431 )
371 432 assert journal.is_a?(Journal)
372 433 assert_match /This is reply/, journal.notes
373 434 assert_equal 'Feature request', journal.issue.tracker.name
374 435 assert_equal 'Normal', journal.issue.priority.name
375 436 end
376 437
377 438 def test_reply_to_a_message
378 439 m = submit_email('message_reply.eml')
379 440 assert m.is_a?(Message)
380 441 assert !m.new_record?
381 442 m.reload
382 443 assert_equal 'Reply via email', m.subject
383 444 # The email replies to message #2 which is part of the thread of message #1
384 445 assert_equal Message.find(1), m.parent
385 446 end
386 447
387 448 def test_reply_to_a_message_by_subject
388 449 m = submit_email('message_reply_by_subject.eml')
389 450 assert m.is_a?(Message)
390 451 assert !m.new_record?
391 452 m.reload
392 453 assert_equal 'Reply to the first post', m.subject
393 454 assert_equal Message.find(1), m.parent
394 455 end
395 456
396 457 def test_should_strip_tags_of_html_only_emails
397 458 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
398 459 assert issue.is_a?(Issue)
399 460 assert !issue.new_record?
400 461 issue.reload
401 462 assert_equal 'HTML email', issue.subject
402 463 assert_equal 'This is a html-only email.', issue.description
403 464 end
404 465
405 466 context "truncate emails based on the Setting" do
406 467 context "with no setting" do
407 468 setup do
408 469 Setting.mail_handler_body_delimiters = ''
409 470 end
410 471
411 472 should "add the entire email into the issue" do
412 473 issue = submit_email('ticket_on_given_project.eml')
413 474 assert_issue_created(issue)
414 475 assert issue.description.include?('---')
415 476 assert issue.description.include?('This paragraph is after the delimiter')
416 477 end
417 478 end
418 479
419 480 context "with a single string" do
420 481 setup do
421 482 Setting.mail_handler_body_delimiters = '---'
422 483 end
423 484 should "truncate the email at the delimiter for the issue" do
424 485 issue = submit_email('ticket_on_given_project.eml')
425 486 assert_issue_created(issue)
426 487 assert issue.description.include?('This paragraph is before delimiters')
427 488 assert issue.description.include?('--- This line starts with a delimiter')
428 489 assert !issue.description.match(/^---$/)
429 490 assert !issue.description.include?('This paragraph is after the delimiter')
430 491 end
431 492 end
432 493
433 494 context "with a single quoted reply (e.g. reply to a Redmine email notification)" do
434 495 setup do
435 496 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
436 497 end
437 498 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
438 499 journal = submit_email('issue_update_with_quoted_reply_above.eml')
439 500 assert journal.is_a?(Journal)
440 501 assert journal.notes.include?('An update to the issue by the sender.')
441 502 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
442 503 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
443 504 end
444 505 end
445 506
446 507 context "with multiple quoted replies (e.g. reply to a reply of a Redmine email notification)" do
447 508 setup do
448 509 Setting.mail_handler_body_delimiters = '--- Reply above. Do not remove this line. ---'
449 510 end
450 511 should "truncate the email at the delimiter with the quoted reply symbols (>)" do
451 512 journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
452 513 assert journal.is_a?(Journal)
453 514 assert journal.notes.include?('An update to the issue by the sender.')
454 515 assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
455 516 assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
456 517 end
457 518 end
458 519
459 520 context "with multiple strings" do
460 521 setup do
461 522 Setting.mail_handler_body_delimiters = "---\nBREAK"
462 523 end
463 524 should "truncate the email at the first delimiter found (BREAK)" do
464 525 issue = submit_email('ticket_on_given_project.eml')
465 526 assert_issue_created(issue)
466 527 assert issue.description.include?('This paragraph is before delimiters')
467 528 assert !issue.description.include?('BREAK')
468 529 assert !issue.description.include?('This paragraph is between delimiters')
469 530 assert !issue.description.match(/^---$/)
470 531 assert !issue.description.include?('This paragraph is after the delimiter')
471 532 end
472 533 end
473 534 end
474 535
475 536 def test_email_with_long_subject_line
476 537 issue = submit_email('ticket_with_long_subject.eml')
477 538 assert issue.is_a?(Issue)
478 539 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]
479 540 end
480 541
481 542 def test_new_user_from_attributes_should_return_valid_user
482 543 to_test = {
483 544 # [address, name] => [login, firstname, lastname]
484 545 ['jsmith@example.net', nil] => ['jsmith@example.net', 'jsmith', '-'],
485 546 ['jsmith@example.net', 'John'] => ['jsmith@example.net', 'John', '-'],
486 547 ['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'],
487 548 ['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'],
488 549 ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'],
489 550 ['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh'],
490 551 ['alongemailaddressthatexceedsloginlength@example.net', 'John Smith'] => ['alongemailaddressthatexceedslo', 'John', 'Smith']
491 552 }
492 553
493 554 to_test.each do |attrs, expected|
494 555 user = MailHandler.new_user_from_attributes(attrs.first, attrs.last)
495 556
496 557 assert user.valid?
497 558 assert_equal attrs.first, user.mail
498 559 assert_equal expected[0], user.login
499 560 assert_equal expected[1], user.firstname
500 561 assert_equal expected[2], user.lastname
501 562 end
502 563 end
503 564
504 565 def test_new_user_from_attributes_should_respect_minimum_password_length
505 566 with_settings :password_min_length => 15 do
506 567 user = MailHandler.new_user_from_attributes('jsmith@example.net')
507 568 assert user.valid?
508 569 assert user.password.length >= 15
509 570 end
510 571 end
511 572
512 573 def test_new_user_from_attributes_should_use_default_login_if_invalid
513 574 MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-1@example.net').save!
514 575
515 576 # another long address that would result in duplicate login
516 577 user = MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-2@example.net')
517 578 assert user.valid?
518 579 assert user.login =~ /^user[a-f0-9]+$/
519 580 end
520 581
521 582 private
522 583
523 584 def submit_email(filename, options={})
524 585 raw = IO.read(File.join(FIXTURES_PATH, filename))
525 586 yield raw if block_given?
526 587 MailHandler.receive(raw, options)
527 588 end
528 589
529 590 def assert_issue_created(issue)
530 591 assert issue.is_a?(Issue)
531 592 assert !issue.new_record?
532 593 issue.reload
533 594 end
534 595 end
General Comments 0
You need to be logged in to leave comments. Login now