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