3410: Merge branch 'master' into 3410-replication-attrs
[arvados.git] / services / api / test / functional / arvados / v1 / collections_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
4
5   def permit_unsigned_manifests isok=true
6     # Set security model for the life of a test.
7     Rails.configuration.permit_create_collection_with_unsigned_manifest = isok
8   end
9
10   def assert_signed_manifest manifest_text, label=''
11     assert_not_nil manifest_text, "#{label} manifest_text was nil"
12     manifest_text.scan(/ [[:xdigit:]]{32}\S*/) do |tok|
13       assert_match(/\+A[[:xdigit:]]+@[[:xdigit:]]{8}\b/, tok,
14                    "Locator in #{label} manifest_text was not signed")
15     end
16   end
17
18   test "should get index" do
19     authorize_with :active
20     get :index
21     assert_response :success
22     assert(assigns(:objects).andand.any?, "no Collections returned in index")
23     refute(json_response["items"].any? { |c| c.has_key?("manifest_text") },
24            "basic Collections index included manifest_text")
25   end
26
27   test "collections.get returns signed locators" do
28     permit_unsigned_manifests
29     authorize_with :active
30     get :show, {id: collections(:foo_file).uuid}
31     assert_response :success
32     assert_signed_manifest json_response['manifest_text'], 'foo_file'
33   end
34
35   test "index with manifest_text selected returns signed locators" do
36     columns = %w(uuid owner_uuid manifest_text)
37     authorize_with :active
38     get :index, select: columns
39     assert_response :success
40     assert(assigns(:objects).andand.any?,
41            "no Collections returned for index with columns selected")
42     json_response["items"].each do |coll|
43       assert_equal(columns, columns & coll.keys,
44                    "Collections index did not respect selected columns")
45       assert_signed_manifest coll['manifest_text'], coll['uuid']
46     end
47   end
48
49   [0,1,2].each do |limit|
50     test "get index with limit=#{limit}" do
51       authorize_with :active
52       get :index, limit: limit
53       assert_response :success
54       assert_equal limit, assigns(:objects).count
55       resp = JSON.parse(@response.body)
56       assert_equal limit, resp['limit']
57     end
58   end
59
60   test "items.count == items_available" do
61     authorize_with :active
62     get :index, limit: 100000
63     assert_response :success
64     resp = JSON.parse(@response.body)
65     assert_equal resp['items_available'], assigns(:objects).length
66     assert_equal resp['items_available'], resp['items'].count
67     unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq
68     assert_equal unique_uuids.count, resp['items'].count
69   end
70
71   test "items.count == items_available with filters" do
72     authorize_with :active
73     get :index, {
74       limit: 100,
75       filters: [['uuid','=',collections(:foo_file).uuid]]
76     }
77     assert_response :success
78     assert_equal 1, assigns(:objects).length
79     assert_equal 1, json_response['items_available']
80     assert_equal 1, json_response['items'].count
81   end
82
83   test "get index with limit=2 offset=99999" do
84     # Assume there are not that many test fixtures.
85     authorize_with :active
86     get :index, limit: 2, offset: 99999
87     assert_response :success
88     assert_equal 0, assigns(:objects).count
89     resp = JSON.parse(@response.body)
90     assert_equal 2, resp['limit']
91     assert_equal 99999, resp['offset']
92   end
93
94   test "admin can create collection with unsigned manifest" do
95     authorize_with :admin
96     test_collection = {
97       manifest_text: <<-EOS
98 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
99 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
100 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
101 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
102 EOS
103     }
104     test_collection[:portable_data_hash] =
105       Digest::MD5.hexdigest(test_collection[:manifest_text]) +
106       '+' +
107       test_collection[:manifest_text].length.to_s
108
109     # post :create will modify test_collection in place, so we save a copy first.
110     # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
111     post_collection = Marshal.load(Marshal.dump(test_collection))
112     post :create, {
113       collection: post_collection
114     }
115
116     assert_response :success
117     assert_nil assigns(:objects)
118
119     response_collection = assigns(:object)
120
121     stored_collection = Collection.select([:uuid, :portable_data_hash, :manifest_text]).
122       where(portable_data_hash: response_collection['portable_data_hash']).first
123
124     assert_equal test_collection[:portable_data_hash], stored_collection['portable_data_hash']
125
126     # The manifest in the response will have had permission hints added.
127     # Remove any permission hints in the response before comparing it to the source.
128     stripped_manifest = stored_collection['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
129     assert_equal test_collection[:manifest_text], stripped_manifest
130
131     # TBD: create action should add permission signatures to manifest_text in the response,
132     # and we need to check those permission signatures here.
133   end
134
135   [:admin, :active].each do |user|
136     test "#{user} can get collection using portable data hash" do
137       authorize_with user
138
139       foo_collection = collections(:foo_file)
140
141       # Get foo_file using its portable data hash
142       get :show, {
143         id: foo_collection[:portable_data_hash]
144       }
145       assert_response :success
146       assert_not_nil assigns(:object)
147       resp = assigns(:object)
148       assert_equal foo_collection[:portable_data_hash], resp['portable_data_hash']
149       assert_signed_manifest resp['manifest_text']
150
151       # The manifest in the response will have had permission hints added.
152       # Remove any permission hints in the response before comparing it to the source.
153       stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
154       assert_equal foo_collection[:manifest_text], stripped_manifest
155     end
156   end
157
158   test "create with owner_uuid set to owned group" do
159     permit_unsigned_manifests
160     authorize_with :active
161     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
162     post :create, {
163       collection: {
164         owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
165         manifest_text: manifest_text,
166         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
167       }
168     }
169     assert_response :success
170     resp = JSON.parse(@response.body)
171     assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
172   end
173
174   test "create fails with duplicate name" do
175     permit_unsigned_manifests
176     authorize_with :admin
177     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
178     post :create, {
179       collection: {
180         owner_uuid: 'zzzzz-tpzed-000000000000000',
181         manifest_text: manifest_text,
182         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
183         name: "foo_file"
184       }
185     }
186     assert_response 422
187     response_errors = json_response['errors']
188     assert_not_nil response_errors, 'Expected error in response'
189     assert(response_errors.first.include?('duplicate key'),
190            "Expected 'duplicate key' error in #{response_errors.first}")
191   end
192
193   [false, true].each do |unsigned|
194     test "create with duplicate name, ensure_unique_name, unsigned=#{unsigned}" do
195       permit_unsigned_manifests unsigned
196       authorize_with :active
197       manifest_text = ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:0:foo.txt\n"
198       if !unsigned
199         manifest_text = Collection.sign_manifest manifest_text, api_token(:active)
200       end
201       post :create, {
202         collection: {
203           owner_uuid: users(:active).uuid,
204           manifest_text: manifest_text,
205           name: "owned_by_active"
206         },
207         ensure_unique_name: true
208       }
209       assert_response :success
210       assert_equal 'owned_by_active (2)', json_response['name']
211     end
212   end
213
214   test "create with owner_uuid set to group i can_manage" do
215     permit_unsigned_manifests
216     authorize_with :active
217     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
218     post :create, {
219       collection: {
220         owner_uuid: groups(:active_user_has_can_manage).uuid,
221         manifest_text: manifest_text,
222         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
223       }
224     }
225     assert_response :success
226     resp = JSON.parse(@response.body)
227     assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
228   end
229
230   test "create with owner_uuid fails on group with only can_read permission" do
231     permit_unsigned_manifests
232     authorize_with :active
233     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
234     post :create, {
235       collection: {
236         owner_uuid: groups(:all_users).uuid,
237         manifest_text: manifest_text,
238         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
239       }
240     }
241     assert_response 403
242   end
243
244   test "create with owner_uuid fails on group with no permission" do
245     permit_unsigned_manifests
246     authorize_with :active
247     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
248     post :create, {
249       collection: {
250         owner_uuid: groups(:public).uuid,
251         manifest_text: manifest_text,
252         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
253       }
254     }
255     assert_response 422
256   end
257
258   test "admin create with owner_uuid set to group with no permission" do
259     permit_unsigned_manifests
260     authorize_with :admin
261     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
262     post :create, {
263       collection: {
264         owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
265         manifest_text: manifest_text,
266         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
267       }
268     }
269     assert_response :success
270   end
271
272   test "should create with collection passed as json" do
273     permit_unsigned_manifests
274     authorize_with :active
275     post :create, {
276       collection: <<-EOS
277       {
278         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
279         "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
280       }
281       EOS
282     }
283     assert_response :success
284   end
285
286   test "should fail to create with checksum mismatch" do
287     permit_unsigned_manifests
288     authorize_with :active
289     post :create, {
290       collection: <<-EOS
291       {
292         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
293         "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
294       }
295       EOS
296     }
297     assert_response 422
298   end
299
300   test "collection UUID is normalized when created" do
301     permit_unsigned_manifests
302     authorize_with :active
303     post :create, {
304       collection: {
305         manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
306         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
307       }
308     }
309     assert_response :success
310     assert_not_nil assigns(:object)
311     resp = JSON.parse(@response.body)
312     assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['portable_data_hash']
313   end
314
315   test "get full provenance for baz file" do
316     authorize_with :active
317     get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
318     assert_response :success
319     resp = JSON.parse(@response.body)
320     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
321     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
322     assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
323     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
324     assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
325   end
326
327   test "get no provenance for foo file" do
328     # spectator user cannot even see baz collection
329     authorize_with :spectator
330     get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
331     assert_response 404
332   end
333
334   test "get partial provenance for baz file" do
335     # spectator user can see bar->baz job, but not foo->bar job
336     authorize_with :spectator
337     get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
338     assert_response :success
339     resp = JSON.parse(@response.body)
340     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
341     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
342     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq']     # bar->baz
343     assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon']         # foo->bar
344     assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
345   end
346
347   test "search collections with 'any' operator" do
348     expect_pdh = collections(:docker_image).portable_data_hash
349     authorize_with :active
350     get :index, {
351       where: { any: ['contains', expect_pdh[5..25]] }
352     }
353     assert_response :success
354     found = assigns(:objects)
355     assert_equal 1, found.count
356     assert_equal expect_pdh, found.first.portable_data_hash
357   end
358
359   [false, true].each do |permit_unsigned|
360     test "create collection with signed manifest, permit_unsigned=#{permit_unsigned}" do
361       permit_unsigned_manifests permit_unsigned
362       authorize_with :active
363       locators = %w(
364       d41d8cd98f00b204e9800998ecf8427e+0
365       acbd18db4cc2f85cedef654fccc4a4d8+3
366       ea10d51bcf88862dbcc36eb292017dfd+45)
367
368       unsigned_manifest = locators.map { |loc|
369         ". " + loc + " 0:0:foo.txt\n"
370       }.join()
371       manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
372         '+' +
373         unsigned_manifest.length.to_s
374
375       # Build a manifest with both signed and unsigned locators.
376       signing_opts = {
377         key: Rails.configuration.blob_signing_key,
378         api_token: api_token(:active),
379       }
380       signed_locators = locators.collect do |x|
381         Blob.sign_locator x, signing_opts
382       end
383       if permit_unsigned
384         # Leave a non-empty blob unsigned.
385         signed_locators[1] = locators[1]
386       else
387         # Leave the empty blob unsigned. This should still be allowed.
388         signed_locators[0] = locators[0]
389       end
390       signed_manifest =
391         ". " + signed_locators[0] + " 0:0:foo.txt\n" +
392         ". " + signed_locators[1] + " 0:0:foo.txt\n" +
393         ". " + signed_locators[2] + " 0:0:foo.txt\n"
394
395       post :create, {
396         collection: {
397           manifest_text: signed_manifest,
398           portable_data_hash: manifest_uuid,
399         }
400       }
401       assert_response :success
402       assert_not_nil assigns(:object)
403       resp = JSON.parse(@response.body)
404       assert_equal manifest_uuid, resp['portable_data_hash']
405       # All of the locators in the output must be signed.
406       resp['manifest_text'].lines.each do |entry|
407         m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
408         if m
409           assert Blob.verify_signature m[0], signing_opts
410         end
411       end
412     end
413   end
414
415   test "create collection with signed manifest and explicit TTL" do
416     authorize_with :active
417     locators = %w(
418       d41d8cd98f00b204e9800998ecf8427e+0
419       acbd18db4cc2f85cedef654fccc4a4d8+3
420       ea10d51bcf88862dbcc36eb292017dfd+45)
421
422     unsigned_manifest = locators.map { |loc|
423       ". " + loc + " 0:0:foo.txt\n"
424     }.join()
425     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
426       '+' +
427       unsigned_manifest.length.to_s
428
429     # build a manifest with both signed and unsigned locators.
430     # TODO(twp): in phase 4, all locators will need to be signed, so
431     # this test should break and will need to be rewritten. Issue #2755.
432     signing_opts = {
433       key: Rails.configuration.blob_signing_key,
434       api_token: api_token(:active),
435       ttl: 3600   # 1 hour
436     }
437     signed_manifest =
438       ". " + locators[0] + " 0:0:foo.txt\n" +
439       ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
440       ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
441
442     post :create, {
443       collection: {
444         manifest_text: signed_manifest,
445         portable_data_hash: manifest_uuid,
446       }
447     }
448     assert_response :success
449     assert_not_nil assigns(:object)
450     resp = JSON.parse(@response.body)
451     assert_equal manifest_uuid, resp['portable_data_hash']
452     # All of the locators in the output must be signed.
453     resp['manifest_text'].lines.each do |entry|
454       m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
455       if m
456         assert Blob.verify_signature m[0], signing_opts
457       end
458     end
459   end
460
461   test "create fails with invalid signature" do
462     authorize_with :active
463     signing_opts = {
464       key: Rails.configuration.blob_signing_key,
465       api_token: api_token(:active),
466     }
467
468     # Generate a locator with a bad signature.
469     unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
470     bad_locator = unsigned_locator + "+Affffffff@ffffffff"
471     assert !Blob.verify_signature(bad_locator, signing_opts)
472
473     # Creating a collection with this locator should
474     # produce 403 Permission denied.
475     unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
476     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
477       '+' +
478       unsigned_manifest.length.to_s
479
480     bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
481     post :create, {
482       collection: {
483         manifest_text: bad_manifest,
484         portable_data_hash: manifest_uuid
485       }
486     }
487
488     assert_response 403
489   end
490
491   test "create fails with uuid of signed manifest" do
492     authorize_with :active
493     signing_opts = {
494       key: Rails.configuration.blob_signing_key,
495       api_token: api_token(:active),
496     }
497
498     unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
499     signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
500     signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
501     manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
502       '+' +
503       signed_manifest.length.to_s
504
505     post :create, {
506       collection: {
507         manifest_text: signed_manifest,
508         portable_data_hash: manifest_uuid
509       }
510     }
511
512     assert_response 422
513   end
514
515   test "multiple locators per line" do
516     permit_unsigned_manifests
517     authorize_with :active
518     locators = %w(
519       d41d8cd98f00b204e9800998ecf8427e+0
520       acbd18db4cc2f85cedef654fccc4a4d8+3
521       ea10d51bcf88862dbcc36eb292017dfd+45)
522
523     manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
524     manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
525       '+' +
526       manifest_text.length.to_s
527
528     test_collection = {
529       manifest_text: manifest_text,
530       portable_data_hash: manifest_uuid,
531     }
532     post_collection = Marshal.load(Marshal.dump(test_collection))
533     post :create, {
534       collection: post_collection
535     }
536     assert_response :success
537     assert_not_nil assigns(:object)
538     resp = JSON.parse(@response.body)
539     assert_equal manifest_uuid, resp['portable_data_hash']
540
541     # The manifest in the response will have had permission hints added.
542     # Remove any permission hints in the response before comparing it to the source.
543     stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
544     assert_equal manifest_text, stripped_manifest
545   end
546
547   test "multiple signed locators per line" do
548     permit_unsigned_manifests
549     authorize_with :active
550     locators = %w(
551       d41d8cd98f00b204e9800998ecf8427e+0
552       acbd18db4cc2f85cedef654fccc4a4d8+3
553       ea10d51bcf88862dbcc36eb292017dfd+45)
554
555     signing_opts = {
556       key: Rails.configuration.blob_signing_key,
557       api_token: api_token(:active),
558     }
559
560     unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
561     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
562       '+' +
563       unsigned_manifest.length.to_s
564
565     signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
566     signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
567
568     post :create, {
569       collection: {
570         manifest_text: signed_manifest,
571         portable_data_hash: manifest_uuid,
572       }
573     }
574     assert_response :success
575     assert_not_nil assigns(:object)
576     resp = JSON.parse(@response.body)
577     assert_equal manifest_uuid, resp['portable_data_hash']
578     # All of the locators in the output must be signed.
579     # Each line is of the form "path locator locator ... 0:0:file.txt"
580     # entry.split[1..-2] will yield just the tokens in the middle of the line
581     returned_locator_count = 0
582     resp['manifest_text'].lines.each do |entry|
583       entry.split[1..-2].each do |tok|
584         returned_locator_count += 1
585         assert Blob.verify_signature tok, signing_opts
586       end
587     end
588     assert_equal locators.count, returned_locator_count
589   end
590
591   test 'Reject manifest with unsigned blob' do
592     permit_unsigned_manifests false
593     authorize_with :active
594     unsigned_manifest = ". 0cc175b9c0f1b6a831c399e269772661+1 0:1:a.txt\n"
595     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest)
596     post :create, {
597       collection: {
598         manifest_text: unsigned_manifest,
599         portable_data_hash: manifest_uuid,
600       }
601     }
602     assert_response 403,
603     "Creating a collection with unsigned blobs should respond 403"
604     assert_empty Collection.where('uuid like ?', manifest_uuid+'%'),
605     "Collection should not exist in database after failed create"
606   end
607
608   test 'List expired collection returns empty list' do
609     authorize_with :active
610     get :index, {
611       where: {name: 'expired_collection'},
612     }
613     assert_response :success
614     found = assigns(:objects)
615     assert_equal 0, found.count
616   end
617
618   test 'Show expired collection returns 404' do
619     authorize_with :active
620     get :show, {
621       id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
622     }
623     assert_response 404
624   end
625
626   test 'Update expired collection returns 404' do
627     authorize_with :active
628     post :update, {
629       id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
630       collection: {
631         name: "still expired"
632       }
633     }
634     assert_response 404
635   end
636
637   test 'List collection with future expiration time succeeds' do
638     authorize_with :active
639     get :index, {
640       where: {name: 'collection_expires_in_future'},
641     }
642     found = assigns(:objects)
643     assert_equal 1, found.count
644   end
645
646
647   test 'Show collection with future expiration time succeeds' do
648     authorize_with :active
649     get :show, {
650       id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
651     }
652     assert_response :success
653   end
654
655   test 'Update collection with future expiration time succeeds' do
656     authorize_with :active
657     post :update, {
658       id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
659       collection: {
660         name: "still not expired"
661       }
662     }
663     assert_response :success
664   end
665
666   test "get collection and verify that file_names is not included" do
667     authorize_with :active
668     get :show, {id: collections(:foo_file).uuid}
669     assert_response :success
670     assert_equal collections(:foo_file).uuid, json_response['uuid']
671     assert_nil json_response['file_names']
672     assert json_response['manifest_text']
673   end
674
675   [
676     [2**8, :success],
677     [2**18, 422],
678   ].each do |description_size, expected_response|
679     test "create collection with description size #{description_size}
680           and expect response #{expected_response}" do
681       skip "(Descriptions are not part of search indexes. Skip until full-text search
682             is implemented, at which point replace with a search in description.)"
683
684       authorize_with :active
685
686       description = 'here is a collection with a very large description'
687       while description.length < description_size
688         description = description + description
689       end
690
691       post :create, collection: {
692         manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt\n",
693         description: description,
694       }
695
696       assert_response expected_response
697     end
698   end
699
700   [1, 5, nil].each do |ask|
701     test "Set replication_desired=#{ask.inspect}" do
702       Rails.configuration.default_collection_replication = 2
703       authorize_with :active
704       put :update, {
705         id: collections(:replication_undesired_unconfirmed).uuid,
706         collection: {
707           replication_desired: ask,
708         },
709       }
710       assert_response :success
711       assert_equal ask, json_response['replication_desired']
712     end
713   end
714
715   test "get collection with properties" do
716     authorize_with :active
717     get :show, {id: collections(:collection_with_one_property).uuid}
718     assert_response :success
719     assert_not_nil json_response['uuid']
720     assert_equal 'value1', json_response['properties']['property1']
721   end
722
723   test "create collection with properties" do
724     authorize_with :active
725     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
726     post :create, {
727       collection: {
728         manifest_text: manifest_text,
729         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
730         properties: {'property_1' => 'value_1'}
731       }
732     }
733     assert_response :success
734     assert_not_nil json_response['uuid']
735     assert_equal 'value_1', json_response['properties']['property_1']
736   end
737 end