##// END OF EJS Templates
code layout cleanup test/integration/api_test/issues_test.rb...
Toshi MARUYAMA -
r10363:92f92803522a
parent child
Show More
@@ -1,765 +1,795
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 ApiTest::IssuesTest < ActionController::IntegrationTest
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 def setup
48 48 Setting.rest_api_enabled = '1'
49 49 end
50 50
51 51 context "/issues" do
52 52 # Use a private project to make sure auth is really working and not just
53 53 # only showing public issues.
54 54 should_allow_api_authentication(:get, "/projects/private-child/issues.xml")
55 55
56 56 should "contain metadata" do
57 57 get '/issues.xml'
58 58
59 59 assert_tag :tag => 'issues',
60 60 :attributes => {
61 61 :type => 'array',
62 62 :total_count => assigns(:issue_count),
63 63 :limit => 25,
64 64 :offset => 0
65 65 }
66 66 end
67 67
68 68 context "with offset and limit" do
69 69 should "use the params" do
70 70 get '/issues.xml?offset=2&limit=3'
71 71
72 72 assert_equal 3, assigns(:limit)
73 73 assert_equal 2, assigns(:offset)
74 74 assert_tag :tag => 'issues', :children => {:count => 3, :only => {:tag => 'issue'}}
75 75 end
76 76 end
77 77
78 78 context "with nometa param" do
79 79 should "not contain metadata" do
80 80 get '/issues.xml?nometa=1'
81 81
82 82 assert_tag :tag => 'issues',
83 83 :attributes => {
84 84 :type => 'array',
85 85 :total_count => nil,
86 86 :limit => nil,
87 87 :offset => nil
88 88 }
89 89 end
90 90 end
91 91
92 92 context "with nometa header" do
93 93 should "not contain metadata" do
94 94 get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
95 95
96 96 assert_tag :tag => 'issues',
97 97 :attributes => {
98 98 :type => 'array',
99 99 :total_count => nil,
100 100 :limit => nil,
101 101 :offset => nil
102 102 }
103 103 end
104 104 end
105 105
106 106 context "with relations" do
107 107 should "display relations" do
108 108 get '/issues.xml?include=relations'
109 109
110 110 assert_response :success
111 111 assert_equal 'application/xml', @response.content_type
112 112 assert_tag 'relations',
113 113 :parent => {:tag => 'issue', :child => {:tag => 'id', :content => '3'}},
114 114 :children => {:count => 1},
115 115 :child => {
116 116 :tag => 'relation',
117 :attributes => {:id => '2', :issue_id => '2', :issue_to_id => '3', :relation_type => 'relates'}
117 :attributes => {:id => '2', :issue_id => '2', :issue_to_id => '3',
118 :relation_type => 'relates'}
118 119 }
119 120 assert_tag 'relations',
120 121 :parent => {:tag => 'issue', :child => {:tag => 'id', :content => '1'}},
121 122 :children => {:count => 0}
122 123 end
123 124 end
124 125
125 126 context "with invalid query params" do
126 127 should "return errors" do
127 128 get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
128 129
129 130 assert_response :unprocessable_entity
130 131 assert_equal 'application/xml', @response.content_type
131 132 assert_tag 'errors', :child => {:tag => 'error', :content => "Start date can't be blank"}
132 133 end
133 134 end
134 135
135 136 context "with custom field filter" do
136 137 should "show only issues with the custom field value" do
137 get '/issues.xml', { :set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='}, :v => {:cf_1 => ['MySQL']}}
138
138 get '/issues.xml',
139 {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='},
140 :v => {:cf_1 => ['MySQL']}}
139 141 expected_ids = Issue.visible.all(
140 142 :include => :custom_values,
141 143 :conditions => {:custom_values => {:custom_field_id => 1, :value => 'MySQL'}}).map(&:id)
142
143 144 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
144 145 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
145 146 end
146 147 end
147 148 end
148 149
149 150 context "with custom field filter (shorthand method)" do
150 151 should "show only issues with the custom field value" do
151 152 get '/issues.xml', { :cf_1 => 'MySQL' }
152 153
153 154 expected_ids = Issue.visible.all(
154 155 :include => :custom_values,
155 156 :conditions => {:custom_values => {:custom_field_id => 1, :value => 'MySQL'}}).map(&:id)
156 157
157 158 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
158 159 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
159 160 end
160 161 end
161 162 end
162 163 end
163 164
164 165 context "/index.json" do
165 166 should_allow_api_authentication(:get, "/projects/private-child/issues.json")
166 167 end
167 168
168 169 context "/index.xml with filter" do
169 170 should "show only issues with the status_id" do
170 171 get '/issues.xml?status_id=5'
171 172
172 173 expected_ids = Issue.visible.all(:conditions => {:status_id => 5}).map(&:id)
173 174
174 175 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
175 176 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
176 177 end
177 178 end
178 179 end
179 180
180 181 context "/index.json with filter" do
181 182 should "show only issues with the status_id" do
182 183 get '/issues.json?status_id=5'
183 184
184 185 json = ActiveSupport::JSON.decode(response.body)
185 186 status_ids_used = json['issues'].collect {|j| j['status']['id'] }
186 187 assert_equal 3, status_ids_used.length
187 188 assert status_ids_used.all? {|id| id == 5 }
188 189 end
189 190
190 191 end
191 192
192 193 # Issue 6 is on a private project
193 194 context "/issues/6.xml" do
194 195 should_allow_api_authentication(:get, "/issues/6.xml")
195 196 end
196 197
197 198 context "/issues/6.json" do
198 199 should_allow_api_authentication(:get, "/issues/6.json")
199 200 end
200 201
201 202 context "GET /issues/:id" do
202 203 context "with journals" do
203 204 context ".xml" do
204 205 should "display journals" do
205 206 get '/issues/1.xml?include=journals'
206 207
207 208 assert_tag :tag => 'issue',
208 209 :child => {
209 210 :tag => 'journals',
210 211 :attributes => { :type => 'array' },
211 212 :child => {
212 213 :tag => 'journal',
213 214 :attributes => { :id => '1'},
214 215 :child => {
215 216 :tag => 'details',
216 217 :attributes => { :type => 'array' },
217 218 :child => {
218 219 :tag => 'detail',
219 220 :attributes => { :name => 'status_id' },
220 221 :child => {
221 222 :tag => 'old_value',
222 223 :content => '1',
223 224 :sibling => {
224 225 :tag => 'new_value',
225 226 :content => '2'
226 227 }
227 228 }
228 229 }
229 230 }
230 231 }
231 232 }
232 233 end
233 234 end
234 235 end
235 236
236 237 context "with custom fields" do
237 238 context ".xml" do
238 239 should "display custom fields" do
239 240 get '/issues/3.xml'
240 241
241 242 assert_tag :tag => 'issue',
242 243 :child => {
243 244 :tag => 'custom_fields',
244 245 :attributes => { :type => 'array' },
245 246 :child => {
246 247 :tag => 'custom_field',
247 248 :attributes => { :id => '1'},
248 249 :child => {
249 250 :tag => 'value',
250 251 :content => 'MySQL'
251 252 }
252 253 }
253 254 }
254 255
255 256 assert_nothing_raised do
256 257 Hash.from_xml(response.body).to_xml
257 258 end
258 259 end
259 260 end
260 261 end
261 262
262 263 context "with multi custom fields" do
263 264 setup do
264 265 field = CustomField.find(1)
265 266 field.update_attribute :multiple, true
266 267 issue = Issue.find(3)
267 268 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
268 269 issue.save!
269 270 end
270 271
271 272 context ".xml" do
272 273 should "display custom fields" do
273 274 get '/issues/3.xml'
274 275 assert_response :success
275 276 assert_tag :tag => 'issue',
276 277 :child => {
277 278 :tag => 'custom_fields',
278 279 :attributes => { :type => 'array' },
279 280 :child => {
280 281 :tag => 'custom_field',
281 282 :attributes => { :id => '1'},
282 283 :child => {
283 284 :tag => 'value',
284 285 :attributes => { :type => 'array' },
285 286 :children => { :count => 2 }
286 287 }
287 288 }
288 289 }
289 290
290 291 xml = Hash.from_xml(response.body)
291 292 custom_fields = xml['issue']['custom_fields']
292 293 assert_kind_of Array, custom_fields
293 294 field = custom_fields.detect {|f| f['id'] == '1'}
294 295 assert_kind_of Hash, field
295 296 assert_equal ['MySQL', 'Oracle'], field['value'].sort
296 297 end
297 298 end
298 299
299 300 context ".json" do
300 301 should "display custom fields" do
301 302 get '/issues/3.json'
302 303 assert_response :success
303 304 json = ActiveSupport::JSON.decode(response.body)
304 305 custom_fields = json['issue']['custom_fields']
305 306 assert_kind_of Array, custom_fields
306 307 field = custom_fields.detect {|f| f['id'] == 1}
307 308 assert_kind_of Hash, field
308 309 assert_equal ['MySQL', 'Oracle'], field['value'].sort
309 310 end
310 311 end
311 312 end
312 313
313 314 context "with empty value for multi custom field" do
314 315 setup do
315 316 field = CustomField.find(1)
316 317 field.update_attribute :multiple, true
317 318 issue = Issue.find(3)
318 319 issue.custom_field_values = {1 => ['']}
319 320 issue.save!
320 321 end
321 322
322 323 context ".xml" do
323 324 should "display custom fields" do
324 325 get '/issues/3.xml'
325 326 assert_response :success
326 327 assert_tag :tag => 'issue',
327 328 :child => {
328 329 :tag => 'custom_fields',
329 330 :attributes => { :type => 'array' },
330 331 :child => {
331 332 :tag => 'custom_field',
332 333 :attributes => { :id => '1'},
333 334 :child => {
334 335 :tag => 'value',
335 336 :attributes => { :type => 'array' },
336 337 :children => { :count => 0 }
337 338 }
338 339 }
339 340 }
340 341
341 342 xml = Hash.from_xml(response.body)
342 343 custom_fields = xml['issue']['custom_fields']
343 344 assert_kind_of Array, custom_fields
344 345 field = custom_fields.detect {|f| f['id'] == '1'}
345 346 assert_kind_of Hash, field
346 347 assert_equal [], field['value']
347 348 end
348 349 end
349 350
350 351 context ".json" do
351 352 should "display custom fields" do
352 353 get '/issues/3.json'
353 354 assert_response :success
354 355 json = ActiveSupport::JSON.decode(response.body)
355 356 custom_fields = json['issue']['custom_fields']
356 357 assert_kind_of Array, custom_fields
357 358 field = custom_fields.detect {|f| f['id'] == 1}
358 359 assert_kind_of Hash, field
359 360 assert_equal [], field['value'].sort
360 361 end
361 362 end
362 363 end
363 364
364 365 context "with attachments" do
365 366 context ".xml" do
366 367 should "display attachments" do
367 368 get '/issues/3.xml?include=attachments'
368 369
369 370 assert_tag :tag => 'issue',
370 371 :child => {
371 372 :tag => 'attachments',
372 373 :children => {:count => 5},
373 374 :child => {
374 375 :tag => 'attachment',
375 376 :child => {
376 377 :tag => 'filename',
377 378 :content => 'source.rb',
378 379 :sibling => {
379 380 :tag => 'content_url',
380 381 :content => 'http://www.example.com/attachments/download/4/source.rb'
381 382 }
382 383 }
383 384 }
384 385 }
385 386 end
386 387 end
387 388 end
388 389
389 390 context "with subtasks" do
390 391 setup do
391 @c1 = Issue.create!(:status_id => 1, :subject => "child c1", :tracker_id => 1, :project_id => 1, :author_id => 1, :parent_issue_id => 1)
392 @c2 = Issue.create!(:status_id => 1, :subject => "child c2", :tracker_id => 1, :project_id => 1, :author_id => 1, :parent_issue_id => 1)
393 @c3 = Issue.create!(:status_id => 1, :subject => "child c3", :tracker_id => 1, :project_id => 1, :author_id => 1, :parent_issue_id => @c1.id)
392 @c1 = Issue.create!(
393 :status_id => 1, :subject => "child c1",
394 :tracker_id => 1, :project_id => 1, :author_id => 1,
395 :parent_issue_id => 1
396 )
397 @c2 = Issue.create!(
398 :status_id => 1, :subject => "child c2",
399 :tracker_id => 1, :project_id => 1, :author_id => 1,
400 :parent_issue_id => 1
401 )
402 @c3 = Issue.create!(
403 :status_id => 1, :subject => "child c3",
404 :tracker_id => 1, :project_id => 1, :author_id => 1,
405 :parent_issue_id => @c1.id
406 )
394 407 end
395 408
396 409 context ".xml" do
397 410 should "display children" do
398 411 get '/issues/1.xml?include=children'
399 412
400 413 assert_tag :tag => 'issue',
401 414 :child => {
402 415 :tag => 'children',
403 416 :children => {:count => 2},
404 417 :child => {
405 418 :tag => 'issue',
406 419 :attributes => {:id => @c1.id.to_s},
407 420 :child => {
408 421 :tag => 'subject',
409 422 :content => 'child c1',
410 423 :sibling => {
411 424 :tag => 'children',
412 425 :children => {:count => 1},
413 426 :child => {
414 427 :tag => 'issue',
415 428 :attributes => {:id => @c3.id.to_s}
416 429 }
417 430 }
418 431 }
419 432 }
420 433 }
421 434 end
422 435
423 436 context ".json" do
424 437 should "display children" do
425 438 get '/issues/1.json?include=children'
426 439
427 440 json = ActiveSupport::JSON.decode(response.body)
428 441 assert_equal([
429 442 {
430 443 'id' => @c1.id, 'subject' => 'child c1', 'tracker' => {'id' => 1, 'name' => 'Bug'},
431 'children' => [{ 'id' => @c3.id, 'subject' => 'child c3', 'tracker' => {'id' => 1, 'name' => 'Bug'} }]
444 'children' => [{'id' => @c3.id, 'subject' => 'child c3',
445 'tracker' => {'id' => 1, 'name' => 'Bug'} }]
432 446 },
433 447 { 'id' => @c2.id, 'subject' => 'child c2', 'tracker' => {'id' => 1, 'name' => 'Bug'} }
434 448 ],
435 449 json['issue']['children'])
436 450 end
437 451 end
438 452 end
439 453 end
440 454 end
441 455
442 456 context "POST /issues.xml" do
443 should_allow_api_authentication(:post,
444 '/issues.xml',
445 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
446 {:success_code => :created})
447
457 should_allow_api_authentication(
458 :post,
459 '/issues.xml',
460 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
461 {:success_code => :created}
462 )
448 463 should "create an issue with the attributes" do
449 464 assert_difference('Issue.count') do
450 post '/issues.xml', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, credentials('jsmith')
465 post '/issues.xml',
466 {:issue => {:project_id => 1, :subject => 'API test',
467 :tracker_id => 2, :status_id => 3}}, credentials('jsmith')
451 468 end
452
453 469 issue = Issue.first(:order => 'id DESC')
454 470 assert_equal 1, issue.project_id
455 471 assert_equal 2, issue.tracker_id
456 472 assert_equal 3, issue.status_id
457 473 assert_equal 'API test', issue.subject
458 474
459 475 assert_response :created
460 476 assert_equal 'application/xml', @response.content_type
461 477 assert_tag 'issue', :child => {:tag => 'id', :content => issue.id.to_s}
462 478 end
463 479 end
464 480
465 481 context "POST /issues.xml with failure" do
466 482 should "have an errors tag" do
467 483 assert_no_difference('Issue.count') do
468 484 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
469 485 end
470 486
471 487 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
472 488 end
473 489 end
474 490
475 491 context "POST /issues.json" do
476 492 should_allow_api_authentication(:post,
477 493 '/issues.json',
478 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
494 {:issue => {:project_id => 1, :subject => 'API test',
495 :tracker_id => 2, :status_id => 3}},
479 496 {:success_code => :created})
480 497
481 498 should "create an issue with the attributes" do
482 499 assert_difference('Issue.count') do
483 post '/issues.json', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, credentials('jsmith')
500 post '/issues.json',
501 {:issue => {:project_id => 1, :subject => 'API test',
502 :tracker_id => 2, :status_id => 3}},
503 credentials('jsmith')
484 504 end
485 505
486 506 issue = Issue.first(:order => 'id DESC')
487 507 assert_equal 1, issue.project_id
488 508 assert_equal 2, issue.tracker_id
489 509 assert_equal 3, issue.status_id
490 510 assert_equal 'API test', issue.subject
491 511 end
492 512
493 513 end
494 514
495 515 context "POST /issues.json with failure" do
496 516 should "have an errors element" do
497 517 assert_no_difference('Issue.count') do
498 518 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
499 519 end
500 520
501 521 json = ActiveSupport::JSON.decode(response.body)
502 522 assert json['errors'].include?("Subject can't be blank")
503 523 end
504 524 end
505 525
506 526 # Issue 6 is on a private project
507 527 context "PUT /issues/6.xml" do
508 528 setup do
509 529 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
510 530 end
511 531
512 532 should_allow_api_authentication(:put,
513 533 '/issues/6.xml',
514 534 {:issue => {:subject => 'API update', :notes => 'A new note'}},
515 535 {:success_code => :ok})
516 536
517 537 should "not create a new issue" do
518 538 assert_no_difference('Issue.count') do
519 539 put '/issues/6.xml', @parameters, credentials('jsmith')
520 540 end
521 541 end
522 542
523 543 should "create a new journal" do
524 544 assert_difference('Journal.count') do
525 545 put '/issues/6.xml', @parameters, credentials('jsmith')
526 546 end
527 547 end
528 548
529 549 should "add the note to the journal" do
530 550 put '/issues/6.xml', @parameters, credentials('jsmith')
531 551
532 552 journal = Journal.last
533 553 assert_equal "A new note", journal.notes
534 554 end
535 555
536 556 should "update the issue" do
537 557 put '/issues/6.xml', @parameters, credentials('jsmith')
538 558
539 559 issue = Issue.find(6)
540 560 assert_equal "API update", issue.subject
541 561 end
542 562
543 563 end
544 564
545 565 context "PUT /issues/3.xml with custom fields" do
546 566 setup do
547 @parameters = {:issue => {:custom_fields => [{'id' => '1', 'value' => 'PostgreSQL' }, {'id' => '2', 'value' => '150'}]}}
567 @parameters = {
568 :issue => {:custom_fields => [{'id' => '1', 'value' => 'PostgreSQL' },
569 {'id' => '2', 'value' => '150'}]}
570 }
548 571 end
549 572
550 573 should "update custom fields" do
551 574 assert_no_difference('Issue.count') do
552 575 put '/issues/3.xml', @parameters, credentials('jsmith')
553 576 end
554 577
555 578 issue = Issue.find(3)
556 579 assert_equal '150', issue.custom_value_for(2).value
557 580 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
558 581 end
559 582 end
560 583
561 584 context "PUT /issues/3.xml with multi custom fields" do
562 585 setup do
563 586 field = CustomField.find(1)
564 587 field.update_attribute :multiple, true
565 @parameters = {:issue => {:custom_fields => [{'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] }, {'id' => '2', 'value' => '150'}]}}
588 @parameters = {
589 :issue => {:custom_fields => [{'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
590 {'id' => '2', 'value' => '150'}]}
591 }
566 592 end
567 593
568 594 should "update custom fields" do
569 595 assert_no_difference('Issue.count') do
570 596 put '/issues/3.xml', @parameters, credentials('jsmith')
571 597 end
572 598
573 599 issue = Issue.find(3)
574 600 assert_equal '150', issue.custom_value_for(2).value
575 601 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
576 602 end
577 603 end
578 604
579 605 context "PUT /issues/3.xml with project change" do
580 606 setup do
581 607 @parameters = {:issue => {:project_id => 2, :subject => 'Project changed'}}
582 608 end
583 609
584 610 should "update project" do
585 611 assert_no_difference('Issue.count') do
586 612 put '/issues/3.xml', @parameters, credentials('jsmith')
587 613 end
588 614
589 615 issue = Issue.find(3)
590 616 assert_equal 2, issue.project_id
591 617 assert_equal 'Project changed', issue.subject
592 618 end
593 619 end
594 620
595 621 context "PUT /issues/6.xml with failed update" do
596 622 setup do
597 623 @parameters = {:issue => {:subject => ''}}
598 624 end
599 625
600 626 should "not create a new issue" do
601 627 assert_no_difference('Issue.count') do
602 628 put '/issues/6.xml', @parameters, credentials('jsmith')
603 629 end
604 630 end
605 631
606 632 should "not create a new journal" do
607 633 assert_no_difference('Journal.count') do
608 634 put '/issues/6.xml', @parameters, credentials('jsmith')
609 635 end
610 636 end
611 637
612 638 should "have an errors tag" do
613 639 put '/issues/6.xml', @parameters, credentials('jsmith')
614 640
615 641 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
616 642 end
617 643 end
618 644
619 645 context "PUT /issues/6.json" do
620 646 setup do
621 647 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
622 648 end
623 649
624 650 should_allow_api_authentication(:put,
625 651 '/issues/6.json',
626 652 {:issue => {:subject => 'API update', :notes => 'A new note'}},
627 653 {:success_code => :ok})
628 654
629 655 should "update the issue" do
630 656 assert_no_difference('Issue.count') do
631 657 assert_difference('Journal.count') do
632 658 put '/issues/6.json', @parameters, credentials('jsmith')
633 659
634 660 assert_response :ok
635 661 assert_equal '', response.body
636 662 end
637 663 end
638 664
639 665 issue = Issue.find(6)
640 666 assert_equal "API update", issue.subject
641 667 journal = Journal.last
642 668 assert_equal "A new note", journal.notes
643 669 end
644 670 end
645 671
646 672 context "PUT /issues/6.json with failed update" do
647 673 should "return errors" do
648 674 assert_no_difference('Issue.count') do
649 675 assert_no_difference('Journal.count') do
650 676 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
651 677
652 678 assert_response :unprocessable_entity
653 679 end
654 680 end
655 681
656 682 json = ActiveSupport::JSON.decode(response.body)
657 683 assert json['errors'].include?("Subject can't be blank")
658 684 end
659 685 end
660 686
661 687 context "DELETE /issues/1.xml" do
662 688 should_allow_api_authentication(:delete,
663 689 '/issues/6.xml',
664 690 {},
665 691 {:success_code => :ok})
666 692
667 693 should "delete the issue" do
668 694 assert_difference('Issue.count', -1) do
669 695 delete '/issues/6.xml', {}, credentials('jsmith')
670 696
671 697 assert_response :ok
672 698 assert_equal '', response.body
673 699 end
674 700
675 701 assert_nil Issue.find_by_id(6)
676 702 end
677 703 end
678 704
679 705 context "DELETE /issues/1.json" do
680 706 should_allow_api_authentication(:delete,
681 707 '/issues/6.json',
682 708 {},
683 709 {:success_code => :ok})
684 710
685 711 should "delete the issue" do
686 712 assert_difference('Issue.count', -1) do
687 713 delete '/issues/6.json', {}, credentials('jsmith')
688 714
689 715 assert_response :ok
690 716 assert_equal '', response.body
691 717 end
692 718
693 719 assert_nil Issue.find_by_id(6)
694 720 end
695 721 end
696 722
697 723 def test_create_issue_with_uploaded_file
698 724 set_tmp_attachments_directory
699
700 725 # upload the file
701 726 assert_difference 'Attachment.count' do
702 post '/uploads.xml', 'test_create_with_upload', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
727 post '/uploads.xml', 'test_create_with_upload',
728 {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
703 729 assert_response :created
704 730 end
705 731 xml = Hash.from_xml(response.body)
706 732 token = xml['upload']['token']
707 733 attachment = Attachment.first(:order => 'id DESC')
708 734
709 735 # create the issue with the upload's token
710 736 assert_difference 'Issue.count' do
711 737 post '/issues.xml',
712 {:issue => {:project_id => 1, :subject => 'Uploaded file', :uploads => [{:token => token, :filename => 'test.txt', :content_type => 'text/plain'}]}},
713 credentials('jsmith')
738 {:issue => {:project_id => 1, :subject => 'Uploaded file',
739 :uploads => [{:token => token, :filename => 'test.txt',
740 :content_type => 'text/plain'}]}},
741 credentials('jsmith')
714 742 assert_response :created
715 743 end
716 744 issue = Issue.first(:order => 'id DESC')
717 745 assert_equal 1, issue.attachments.count
718 746 assert_equal attachment, issue.attachments.first
719 747
720 748 attachment.reload
721 749 assert_equal 'test.txt', attachment.filename
722 750 assert_equal 'text/plain', attachment.content_type
723 751 assert_equal 'test_create_with_upload'.size, attachment.filesize
724 752 assert_equal 2, attachment.author_id
725 753
726 754 # get the issue with its attachments
727 755 get "/issues/#{issue.id}.xml", :include => 'attachments'
728 756 assert_response :success
729 757 xml = Hash.from_xml(response.body)
730 758 attachments = xml['issue']['attachments']
731 759 assert_kind_of Array, attachments
732 760 assert_equal 1, attachments.size
733 761 url = attachments.first['content_url']
734 762 assert_not_nil url
735 763
736 764 # download the attachment
737 765 get url
738 766 assert_response :success
739 767 end
740 768
741 769 def test_update_issue_with_uploaded_file
742 770 set_tmp_attachments_directory
743
744 771 # upload the file
745 772 assert_difference 'Attachment.count' do
746 post '/uploads.xml', 'test_upload_with_upload', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
773 post '/uploads.xml', 'test_upload_with_upload',
774 {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
747 775 assert_response :created
748 776 end
749 777 xml = Hash.from_xml(response.body)
750 778 token = xml['upload']['token']
751 779 attachment = Attachment.first(:order => 'id DESC')
752 780
753 781 # update the issue with the upload's token
754 782 assert_difference 'Journal.count' do
755 783 put '/issues/1.xml',
756 {:issue => {:notes => 'Attachment added', :uploads => [{:token => token, :filename => 'test.txt', :content_type => 'text/plain'}]}},
757 credentials('jsmith')
784 {:issue => {:notes => 'Attachment added',
785 :uploads => [{:token => token, :filename => 'test.txt',
786 :content_type => 'text/plain'}]}},
787 credentials('jsmith')
758 788 assert_response :ok
759 789 assert_equal '', @response.body
760 790 end
761 791
762 792 issue = Issue.find(1)
763 793 assert_include attachment, issue.attachments
764 794 end
765 795 end
General Comments 0
You need to be logged in to leave comments. Login now