##// END OF EJS Templates
Backported r16293 (#24875)....
Jean-Philippe Lang -
r15927:1efe3eb3a1f4
parent child
Show More
@@ -1,796 +1,827
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 20 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
21 21 fixtures :projects,
22 22 :users,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :issues,
27 27 :issue_statuses,
28 28 :issue_relations,
29 29 :versions,
30 30 :trackers,
31 31 :projects_trackers,
32 32 :issue_categories,
33 33 :enabled_modules,
34 34 :enumerations,
35 35 :attachments,
36 36 :workflows,
37 37 :custom_fields,
38 38 :custom_values,
39 39 :custom_fields_projects,
40 40 :custom_fields_trackers,
41 41 :time_entries,
42 42 :journals,
43 43 :journal_details,
44 44 :queries,
45 45 :attachments
46 46
47 47 test "GET /issues.xml should contain metadata" do
48 48 get '/issues.xml'
49 49 assert_select 'issues[type=array][total_count=?][limit="25"][offset="0"]',
50 50 assigns(:issue_count).to_s
51 51 end
52 52
53 53 test "GET /issues.xml with nometa param should not contain metadata" do
54 54 get '/issues.xml?nometa=1'
55 55 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
56 56 end
57 57
58 58 test "GET /issues.xml with nometa header should not contain metadata" do
59 59 get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
60 60 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
61 61 end
62 62
63 63 test "GET /issues.xml with offset and limit" do
64 64 get '/issues.xml?offset=2&limit=3'
65 65
66 66 assert_equal 3, assigns(:limit)
67 67 assert_equal 2, assigns(:offset)
68 68 assert_select 'issues issue', 3
69 69 end
70 70
71 71 test "GET /issues.xml with relations" do
72 72 get '/issues.xml?include=relations'
73 73
74 74 assert_response :success
75 75 assert_equal 'application/xml', @response.content_type
76 76
77 77 assert_select 'issue id', :text => '3' do
78 78 assert_select '~ relations relation', 1
79 79 assert_select '~ relations relation[id="2"][issue_id="2"][issue_to_id="3"][relation_type=relates]'
80 80 end
81 81
82 82 assert_select 'issue id', :text => '1' do
83 83 assert_select '~ relations'
84 84 assert_select '~ relations relation', 0
85 85 end
86 86 end
87 87
88 88 test "GET /issues.xml with invalid query params" do
89 89 get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
90 90
91 91 assert_response :unprocessable_entity
92 92 assert_equal 'application/xml', @response.content_type
93 93 assert_select 'errors error', :text => "Start date cannot be blank"
94 94 end
95 95
96 96 test "GET /issues.xml with custom field filter" do
97 97 get '/issues.xml',
98 98 {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='}, :v => {:cf_1 => ['MySQL']}}
99 99
100 100 expected_ids = Issue.visible.
101 101 joins(:custom_values).
102 102 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
103 103 assert expected_ids.any?
104 104
105 105 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
106 106 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
107 107 end
108 108 end
109 109
110 110 test "GET /issues.xml with custom field filter (shorthand method)" do
111 111 get '/issues.xml', {:cf_1 => 'MySQL'}
112 112
113 113 expected_ids = Issue.visible.
114 114 joins(:custom_values).
115 115 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
116 116 assert expected_ids.any?
117 117
118 118 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
119 119 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
120 120 end
121 121 end
122 122
123 123 def test_index_should_include_issue_attributes
124 124 get '/issues.xml'
125 125 assert_select 'issues>issue>is_private', :text => 'false'
126 126 end
127 127
128 128 def test_index_should_allow_timestamp_filtering
129 129 Issue.delete_all
130 130 Issue.generate!(:subject => '1').update_column(:updated_on, Time.parse("2014-01-02T10:25:00Z"))
131 131 Issue.generate!(:subject => '2').update_column(:updated_on, Time.parse("2014-01-02T12:13:00Z"))
132 132
133 133 get '/issues.xml',
134 134 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '<='},
135 135 :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
136 136 assert_select 'issues>issue', :count => 1
137 137 assert_select 'issues>issue>subject', :text => '1'
138 138
139 139 get '/issues.xml',
140 140 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
141 141 :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
142 142 assert_select 'issues>issue', :count => 1
143 143 assert_select 'issues>issue>subject', :text => '2'
144 144
145 145 get '/issues.xml',
146 146 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
147 147 :v => {:updated_on => ['2014-01-02T08:00:00Z']}}
148 148 assert_select 'issues>issue', :count => 2
149 149 end
150 150
151 151 test "GET /issues.xml with filter" do
152 152 get '/issues.xml?status_id=5'
153 153
154 154 expected_ids = Issue.visible.where(:status_id => 5).map(&:id)
155 155 assert expected_ids.any?
156 156
157 157 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
158 158 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
159 159 end
160 160 end
161 161
162 162 test "GET /issues.json with filter" do
163 163 get '/issues.json?status_id=5'
164 164
165 165 json = ActiveSupport::JSON.decode(response.body)
166 166 status_ids_used = json['issues'].collect {|j| j['status']['id'] }
167 167 assert_equal 3, status_ids_used.length
168 168 assert status_ids_used.all? {|id| id == 5 }
169 169 end
170 170
171 171 test "GET /issues/:id.xml with journals" do
172 172 get '/issues/1.xml?include=journals'
173 173
174 174 assert_select 'issue journals[type=array]' do
175 175 assert_select 'journal[id="1"]' do
176 176 assert_select 'details[type=array]' do
177 177 assert_select 'detail[name=status_id]' do
178 178 assert_select 'old_value', :text => '1'
179 179 assert_select 'new_value', :text => '2'
180 180 end
181 181 end
182 182 end
183 183 end
184 184 end
185 185
186 186 test "GET /issues/:id.xml with journals should format timestamps in ISO 8601" do
187 187 get '/issues/1.xml?include=journals'
188 188
189 189 iso_date = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
190 190 assert_select 'issue>created_on', :text => iso_date
191 191 assert_select 'issue>updated_on', :text => iso_date
192 192 assert_select 'issue journal>created_on', :text => iso_date
193 193 end
194 194
195 195 test "GET /issues/:id.xml with custom fields" do
196 196 get '/issues/3.xml'
197 197
198 198 assert_select 'issue custom_fields[type=array]' do
199 199 assert_select 'custom_field[id="1"]' do
200 200 assert_select 'value', :text => 'MySQL'
201 201 end
202 202 end
203 203 assert_nothing_raised do
204 204 Hash.from_xml(response.body).to_xml
205 205 end
206 206 end
207 207
208 208 test "GET /issues/:id.xml with multi custom fields" do
209 209 field = CustomField.find(1)
210 210 field.update_attribute :multiple, true
211 211 issue = Issue.find(3)
212 212 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
213 213 issue.save!
214 214
215 215 get '/issues/3.xml'
216 216 assert_response :success
217 217
218 218 assert_select 'issue custom_fields[type=array]' do
219 219 assert_select 'custom_field[id="1"]' do
220 220 assert_select 'value[type=array] value', 2
221 221 end
222 222 end
223 223 xml = Hash.from_xml(response.body)
224 224 custom_fields = xml['issue']['custom_fields']
225 225 assert_kind_of Array, custom_fields
226 226 field = custom_fields.detect {|f| f['id'] == '1'}
227 227 assert_kind_of Hash, field
228 228 assert_equal ['MySQL', 'Oracle'], field['value'].sort
229 229 end
230 230
231 231 test "GET /issues/:id.json with multi custom fields" do
232 232 field = CustomField.find(1)
233 233 field.update_attribute :multiple, true
234 234 issue = Issue.find(3)
235 235 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
236 236 issue.save!
237 237
238 238 get '/issues/3.json'
239 239 assert_response :success
240 240
241 241 json = ActiveSupport::JSON.decode(response.body)
242 242 custom_fields = json['issue']['custom_fields']
243 243 assert_kind_of Array, custom_fields
244 244 field = custom_fields.detect {|f| f['id'] == 1}
245 245 assert_kind_of Hash, field
246 246 assert_equal ['MySQL', 'Oracle'], field['value'].sort
247 247 end
248 248
249 249 test "GET /issues/:id.xml with empty value for multi custom field" do
250 250 field = CustomField.find(1)
251 251 field.update_attribute :multiple, true
252 252 issue = Issue.find(3)
253 253 issue.custom_field_values = {1 => ['']}
254 254 issue.save!
255 255
256 256 get '/issues/3.xml'
257 257
258 258 assert_select 'issue custom_fields[type=array]' do
259 259 assert_select 'custom_field[id="1"]' do
260 260 assert_select 'value[type=array]:empty'
261 261 end
262 262 end
263 263 xml = Hash.from_xml(response.body)
264 264 custom_fields = xml['issue']['custom_fields']
265 265 assert_kind_of Array, custom_fields
266 266 field = custom_fields.detect {|f| f['id'] == '1'}
267 267 assert_kind_of Hash, field
268 268 assert_equal [], field['value']
269 269 end
270 270
271 271 test "GET /issues/:id.json with empty value for multi custom field" do
272 272 field = CustomField.find(1)
273 273 field.update_attribute :multiple, true
274 274 issue = Issue.find(3)
275 275 issue.custom_field_values = {1 => ['']}
276 276 issue.save!
277 277
278 278 get '/issues/3.json'
279 279 assert_response :success
280 280 json = ActiveSupport::JSON.decode(response.body)
281 281 custom_fields = json['issue']['custom_fields']
282 282 assert_kind_of Array, custom_fields
283 283 field = custom_fields.detect {|f| f['id'] == 1}
284 284 assert_kind_of Hash, field
285 285 assert_equal [], field['value'].sort
286 286 end
287 287
288 288 test "GET /issues/:id.xml with attachments" do
289 289 get '/issues/3.xml?include=attachments'
290 290
291 291 assert_select 'issue attachments[type=array]' do
292 292 assert_select 'attachment', 4
293 293 assert_select 'attachment id', :text => '1' do
294 294 assert_select '~ filename', :text => 'error281.txt'
295 295 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/1/error281.txt'
296 296 end
297 297 end
298 298 end
299 299
300 300 test "GET /issues/:id.xml with subtasks" do
301 301 issue = Issue.generate_with_descendants!(:project_id => 1)
302 302 get "/issues/#{issue.id}.xml?include=children"
303 303
304 304 assert_select 'issue id', :text => issue.id.to_s do
305 305 assert_select '~ children[type=array] > issue', 2
306 306 assert_select '~ children[type=array] > issue > children', 1
307 307 end
308 308 end
309 309
310 310 test "GET /issues/:id.json with subtasks" do
311 311 issue = Issue.generate_with_descendants!(:project_id => 1)
312 312 get "/issues/#{issue.id}.json?include=children"
313 313
314 314 json = ActiveSupport::JSON.decode(response.body)
315 315 assert_equal 2, json['issue']['children'].size
316 316 assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
317 317 end
318 318
319 319 def test_show_should_include_issue_attributes
320 320 get '/issues/1.xml'
321 321 assert_select 'issue>is_private', :text => 'false'
322 322 end
323 323
324 324 test "GET /issues/:id.xml?include=watchers should include watchers" do
325 325 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
326 326
327 327 get '/issues/1.xml?include=watchers', {}, credentials('jsmith')
328 328
329 329 assert_response :ok
330 330 assert_equal 'application/xml', response.content_type
331 331 assert_select 'issue' do
332 332 assert_select 'watchers', Issue.find(1).watchers.count
333 333 assert_select 'watchers' do
334 334 assert_select 'user[id="3"]'
335 335 end
336 336 end
337 337 end
338 338
339 339 test "GET /issues/:id.xml should not disclose associated changesets from projects the user has no access to" do
340 340 project = Project.generate!(:is_public => false)
341 341 repository = Repository::Subversion.create!(:project => project, :url => "svn://localhost")
342 342 Issue.find(1).changesets << Changeset.generate!(:repository => repository)
343 343 assert Issue.find(1).changesets.any?
344 344
345 345 get '/issues/1.xml?include=changesets', {}, credentials('jsmith')
346 346
347 347 # the user jsmith has no permission to view the associated changeset
348 348 assert_select 'issue changesets[type=array]' do
349 349 assert_select 'changeset', 0
350 350 end
351 351 end
352 352
353 test "GET /issues/:id.xml should contains visible spent_hours only" do
354 user = User.find_by_login('jsmith')
355 Role.find(1).update(:time_entries_visibility => 'own')
356 parent = Issue.find(3)
357 child = Issue.generate!(:parent_issue_id => parent.id)
358 TimeEntry.generate!(:user => user, :hours => 5.5, :issue_id => parent.id)
359 TimeEntry.generate!(:user => user, :hours => 2, :issue_id => child.id)
360 TimeEntry.generate!(:user => User.find(1), :hours => 100, :issue_id => child.id)
361 get '/issues/3.xml', {} , credentials(user.login)
362
363 assert_equal 'application/xml', response.content_type
364 assert_select 'issue' do
365 assert_select 'spent_hours', '5.5'
366 end
367 end
368
369 test "GET /issues/:id.json should contains visible spent_hours only" do
370 user = User.find_by_login('jsmith')
371 Role.find(1).update(:time_entries_visibility => 'own')
372 parent = Issue.find(3)
373 child = Issue.generate!(:parent_issue_id => parent.id)
374 TimeEntry.generate!(:user => user, :hours => 5.5, :issue_id => parent.id)
375 TimeEntry.generate!(:user => user, :hours => 2, :issue_id => child.id)
376 TimeEntry.generate!(:user => User.find(1), :hours => 100, :issue_id => child.id)
377 get '/issues/3.json', {} , credentials(user.login)
378
379 assert_equal 'application/json', response.content_type
380 json = ActiveSupport::JSON.decode(response.body)
381 assert_equal 5.5, json['issue']['spent_hours']
382 end
383
353 384 test "POST /issues.xml should create an issue with the attributes" do
354 385
355 386 payload = <<-XML
356 387 <?xml version="1.0" encoding="UTF-8" ?>
357 388 <issue>
358 389 <project_id>1</project_id>
359 390 <tracker_id>2</tracker_id>
360 391 <status_id>3</status_id>
361 392 <subject>API test</subject>
362 393 </issue>
363 394 XML
364 395
365 396 assert_difference('Issue.count') do
366 397 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
367 398 end
368 399 issue = Issue.order('id DESC').first
369 400 assert_equal 1, issue.project_id
370 401 assert_equal 2, issue.tracker_id
371 402 assert_equal 3, issue.status_id
372 403 assert_equal 'API test', issue.subject
373 404
374 405 assert_response :created
375 406 assert_equal 'application/xml', @response.content_type
376 407 assert_select 'issue > id', :text => issue.id.to_s
377 408 end
378 409
379 410 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
380 411 assert_difference('Issue.count') do
381 412 post '/issues.xml',
382 413 {:issue => {:project_id => 1, :subject => 'Watchers',
383 414 :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
384 415 assert_response :created
385 416 end
386 417 issue = Issue.order('id desc').first
387 418 assert_equal 2, issue.watchers.size
388 419 assert_equal [1, 3], issue.watcher_user_ids.sort
389 420 end
390 421
391 422 test "POST /issues.xml with failure should return errors" do
392 423 assert_no_difference('Issue.count') do
393 424 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
394 425 end
395 426
396 427 assert_select 'errors error', :text => "Subject cannot be blank"
397 428 end
398 429
399 430 test "POST /issues.json should create an issue with the attributes" do
400 431
401 432 payload = <<-JSON
402 433 {
403 434 "issue": {
404 435 "project_id": "1",
405 436 "tracker_id": "2",
406 437 "status_id": "3",
407 438 "subject": "API test"
408 439 }
409 440 }
410 441 JSON
411 442
412 443 assert_difference('Issue.count') do
413 444 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
414 445 end
415 446
416 447 issue = Issue.order('id DESC').first
417 448 assert_equal 1, issue.project_id
418 449 assert_equal 2, issue.tracker_id
419 450 assert_equal 3, issue.status_id
420 451 assert_equal 'API test', issue.subject
421 452 end
422 453
423 454 test "POST /issues.json without tracker_id should accept custom fields" do
424 455 field = IssueCustomField.generate!(
425 456 :field_format => 'list',
426 457 :multiple => true,
427 458 :possible_values => ["V1", "V2", "V3"],
428 459 :default_value => "V2",
429 460 :is_for_all => true,
430 461 :trackers => Tracker.all.to_a
431 462 )
432 463
433 464 payload = <<-JSON
434 465 {
435 466 "issue": {
436 467 "project_id": "1",
437 468 "subject": "Multivalued custom field",
438 469 "custom_field_values":{"#{field.id}":["V1","V3"]}
439 470 }
440 471 }
441 472 JSON
442 473
443 474 assert_difference('Issue.count') do
444 475 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
445 476 end
446 477
447 478 assert_response :created
448 479 issue = Issue.order('id DESC').first
449 480 assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
450 481 end
451 482
452 483 test "POST /issues.json with omitted custom field should set default value" do
453 484 field = IssueCustomField.generate!(:default_value => "Default")
454 485
455 486 issue = new_record(Issue) do
456 487 post '/issues.json',
457 488 {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {}}},
458 489 credentials('jsmith')
459 490 end
460 491 assert_equal "Default", issue.custom_field_value(field)
461 492 end
462 493
463 494 test "POST /issues.json with custom field set to blank should not set default value" do
464 495 field = IssueCustomField.generate!(:default_value => "Default")
465 496
466 497 issue = new_record(Issue) do
467 498 post '/issues.json',
468 499 {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {field.id.to_s => ""}}},
469 500 credentials('jsmith')
470 501 end
471 502 assert_equal "", issue.custom_field_value(field)
472 503 end
473 504
474 505 test "POST /issues.json with failure should return errors" do
475 506 assert_no_difference('Issue.count') do
476 507 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
477 508 end
478 509
479 510 json = ActiveSupport::JSON.decode(response.body)
480 511 assert json['errors'].include?("Subject cannot be blank")
481 512 end
482 513
483 514 test "POST /issues.json with invalid project_id should respond with 422" do
484 515 post '/issues.json', {:issue => {:project_id => 999, :subject => "API"}}, credentials('jsmith')
485 516 assert_response 422
486 517 end
487 518
488 519 test "PUT /issues/:id.xml" do
489 520 assert_difference('Journal.count') do
490 521 put '/issues/6.xml',
491 522 {:issue => {:subject => 'API update', :notes => 'A new note'}},
492 523 credentials('jsmith')
493 524 end
494 525
495 526 issue = Issue.find(6)
496 527 assert_equal "API update", issue.subject
497 528 journal = Journal.last
498 529 assert_equal "A new note", journal.notes
499 530 end
500 531
501 532 test "PUT /issues/:id.xml with custom fields" do
502 533 put '/issues/3.xml',
503 534 {:issue => {:custom_fields => [
504 535 {'id' => '1', 'value' => 'PostgreSQL' },
505 536 {'id' => '2', 'value' => '150'}
506 537 ]}},
507 538 credentials('jsmith')
508 539
509 540 issue = Issue.find(3)
510 541 assert_equal '150', issue.custom_value_for(2).value
511 542 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
512 543 end
513 544
514 545 test "PUT /issues/:id.xml with multi custom fields" do
515 546 field = CustomField.find(1)
516 547 field.update_attribute :multiple, true
517 548
518 549 put '/issues/3.xml',
519 550 {:issue => {:custom_fields => [
520 551 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
521 552 {'id' => '2', 'value' => '150'}
522 553 ]}},
523 554 credentials('jsmith')
524 555
525 556 issue = Issue.find(3)
526 557 assert_equal '150', issue.custom_value_for(2).value
527 558 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
528 559 end
529 560
530 561 test "PUT /issues/:id.xml with project change" do
531 562 put '/issues/3.xml',
532 563 {:issue => {:project_id => 2, :subject => 'Project changed'}},
533 564 credentials('jsmith')
534 565
535 566 issue = Issue.find(3)
536 567 assert_equal 2, issue.project_id
537 568 assert_equal 'Project changed', issue.subject
538 569 end
539 570
540 571 test "PUT /issues/:id.xml with notes only" do
541 572 assert_difference('Journal.count') do
542 573 put '/issues/6.xml',
543 574 {:issue => {:notes => 'Notes only'}},
544 575 credentials('jsmith')
545 576 end
546 577
547 578 journal = Journal.last
548 579 assert_equal "Notes only", journal.notes
549 580 end
550 581
551 582 test "PUT /issues/:id.json with omitted custom field should not change blank value to default value" do
552 583 field = IssueCustomField.generate!(:default_value => "Default")
553 584 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
554 585 assert_equal "", issue.reload.custom_field_value(field)
555 586
556 587 assert_difference('Journal.count') do
557 588 put "/issues/#{issue.id}.json",
558 589 {:issue => {:custom_field_values => {}, :notes => 'API'}},
559 590 credentials('jsmith')
560 591 end
561 592
562 593 assert_equal "", issue.reload.custom_field_value(field)
563 594 end
564 595
565 596 test "PUT /issues/:id.json with custom field set to blank should not change blank value to default value" do
566 597 field = IssueCustomField.generate!(:default_value => "Default")
567 598 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
568 599 assert_equal "", issue.reload.custom_field_value(field)
569 600
570 601 assert_difference('Journal.count') do
571 602 put "/issues/#{issue.id}.json",
572 603 {:issue => {:custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
573 604 credentials('jsmith')
574 605 end
575 606
576 607 assert_equal "", issue.reload.custom_field_value(field)
577 608 end
578 609
579 610 test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker should set default value" do
580 611 field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
581 612 issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
582 613
583 614 assert_difference('Journal.count') do
584 615 put "/issues/#{issue.id}.json",
585 616 {:issue => {:tracker_id => 2, :custom_field_values => {}, :notes => 'API'}},
586 617 credentials('jsmith')
587 618 end
588 619
589 620 assert_equal 2, issue.reload.tracker_id
590 621 assert_equal "Default", issue.reload.custom_field_value(field)
591 622 end
592 623
593 624 test "PUT /issues/:id.json with tracker change and custom field specific to that tracker set to blank should not set default value" do
594 625 field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
595 626 issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
596 627
597 628 assert_difference('Journal.count') do
598 629 put "/issues/#{issue.id}.json",
599 630 {:issue => {:tracker_id => 2, :custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
600 631 credentials('jsmith')
601 632 end
602 633
603 634 assert_equal 2, issue.reload.tracker_id
604 635 assert_equal "", issue.reload.custom_field_value(field)
605 636 end
606 637
607 638 test "PUT /issues/:id.xml with failed update" do
608 639 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
609 640
610 641 assert_response :unprocessable_entity
611 642 assert_select 'errors error', :text => "Subject cannot be blank"
612 643 end
613 644
614 645 test "PUT /issues/:id.json" do
615 646 assert_difference('Journal.count') do
616 647 put '/issues/6.json',
617 648 {:issue => {:subject => 'API update', :notes => 'A new note'}},
618 649 credentials('jsmith')
619 650
620 651 assert_response :ok
621 652 assert_equal '', response.body
622 653 end
623 654
624 655 issue = Issue.find(6)
625 656 assert_equal "API update", issue.subject
626 657 journal = Journal.last
627 658 assert_equal "A new note", journal.notes
628 659 end
629 660
630 661 test "PUT /issues/:id.json with failed update" do
631 662 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
632 663
633 664 assert_response :unprocessable_entity
634 665 json = ActiveSupport::JSON.decode(response.body)
635 666 assert json['errors'].include?("Subject cannot be blank")
636 667 end
637 668
638 669 test "DELETE /issues/:id.xml" do
639 670 assert_difference('Issue.count', -1) do
640 671 delete '/issues/6.xml', {}, credentials('jsmith')
641 672
642 673 assert_response :ok
643 674 assert_equal '', response.body
644 675 end
645 676 assert_nil Issue.find_by_id(6)
646 677 end
647 678
648 679 test "DELETE /issues/:id.json" do
649 680 assert_difference('Issue.count', -1) do
650 681 delete '/issues/6.json', {}, credentials('jsmith')
651 682
652 683 assert_response :ok
653 684 assert_equal '', response.body
654 685 end
655 686 assert_nil Issue.find_by_id(6)
656 687 end
657 688
658 689 test "POST /issues/:id/watchers.xml should add watcher" do
659 690 assert_difference 'Watcher.count' do
660 691 post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
661 692
662 693 assert_response :ok
663 694 assert_equal '', response.body
664 695 end
665 696 watcher = Watcher.order('id desc').first
666 697 assert_equal Issue.find(1), watcher.watchable
667 698 assert_equal User.find(3), watcher.user
668 699 end
669 700
670 701 test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
671 702 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
672 703
673 704 assert_difference 'Watcher.count', -1 do
674 705 delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
675 706
676 707 assert_response :ok
677 708 assert_equal '', response.body
678 709 end
679 710 assert_equal false, Issue.find(1).watched_by?(User.find(3))
680 711 end
681 712
682 713 def test_create_issue_with_uploaded_file
683 714 token = xml_upload('test_create_with_upload', credentials('jsmith'))
684 715 attachment = Attachment.find_by_token(token)
685 716
686 717 # create the issue with the upload's token
687 718 assert_difference 'Issue.count' do
688 719 post '/issues.xml',
689 720 {:issue => {:project_id => 1, :subject => 'Uploaded file',
690 721 :uploads => [{:token => token, :filename => 'test.txt',
691 722 :content_type => 'text/plain'}]}},
692 723 credentials('jsmith')
693 724 assert_response :created
694 725 end
695 726 issue = Issue.order('id DESC').first
696 727 assert_equal 1, issue.attachments.count
697 728 assert_equal attachment, issue.attachments.first
698 729
699 730 attachment.reload
700 731 assert_equal 'test.txt', attachment.filename
701 732 assert_equal 'text/plain', attachment.content_type
702 733 assert_equal 'test_create_with_upload'.size, attachment.filesize
703 734 assert_equal 2, attachment.author_id
704 735
705 736 # get the issue with its attachments
706 737 get "/issues/#{issue.id}.xml", :include => 'attachments'
707 738 assert_response :success
708 739 xml = Hash.from_xml(response.body)
709 740 attachments = xml['issue']['attachments']
710 741 assert_kind_of Array, attachments
711 742 assert_equal 1, attachments.size
712 743 url = attachments.first['content_url']
713 744 assert_not_nil url
714 745
715 746 # download the attachment
716 747 get url
717 748 assert_response :success
718 749 assert_equal 'test_create_with_upload', response.body
719 750 end
720 751
721 752 def test_create_issue_with_multiple_uploaded_files_as_xml
722 753 token1 = xml_upload('File content 1', credentials('jsmith'))
723 754 token2 = xml_upload('File content 2', credentials('jsmith'))
724 755
725 756 payload = <<-XML
726 757 <?xml version="1.0" encoding="UTF-8" ?>
727 758 <issue>
728 759 <project_id>1</project_id>
729 760 <tracker_id>1</tracker_id>
730 761 <subject>Issue with multiple attachments</subject>
731 762 <uploads type="array">
732 763 <upload>
733 764 <token>#{token1}</token>
734 765 <filename>test1.txt</filename>
735 766 </upload>
736 767 <upload>
737 768 <token>#{token2}</token>
738 769 <filename>test1.txt</filename>
739 770 </upload>
740 771 </uploads>
741 772 </issue>
742 773 XML
743 774
744 775 assert_difference 'Issue.count' do
745 776 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
746 777 assert_response :created
747 778 end
748 779 issue = Issue.order('id DESC').first
749 780 assert_equal 2, issue.attachments.count
750 781 end
751 782
752 783 def test_create_issue_with_multiple_uploaded_files_as_json
753 784 token1 = json_upload('File content 1', credentials('jsmith'))
754 785 token2 = json_upload('File content 2', credentials('jsmith'))
755 786
756 787 payload = <<-JSON
757 788 {
758 789 "issue": {
759 790 "project_id": "1",
760 791 "tracker_id": "1",
761 792 "subject": "Issue with multiple attachments",
762 793 "uploads": [
763 794 {"token": "#{token1}", "filename": "test1.txt"},
764 795 {"token": "#{token2}", "filename": "test2.txt"}
765 796 ]
766 797 }
767 798 }
768 799 JSON
769 800
770 801 assert_difference 'Issue.count' do
771 802 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
772 803 assert_response :created
773 804 end
774 805 issue = Issue.order('id DESC').first
775 806 assert_equal 2, issue.attachments.count
776 807 end
777 808
778 809 def test_update_issue_with_uploaded_file
779 810 token = xml_upload('test_upload_with_upload', credentials('jsmith'))
780 811 attachment = Attachment.find_by_token(token)
781 812
782 813 # update the issue with the upload's token
783 814 assert_difference 'Journal.count' do
784 815 put '/issues/1.xml',
785 816 {:issue => {:notes => 'Attachment added',
786 817 :uploads => [{:token => token, :filename => 'test.txt',
787 818 :content_type => 'text/plain'}]}},
788 819 credentials('jsmith')
789 820 assert_response :ok
790 821 assert_equal '', @response.body
791 822 end
792 823
793 824 issue = Issue.find(1)
794 825 assert_include attachment, issue.attachments
795 826 end
796 827 end
General Comments 0
You need to be logged in to leave comments. Login now