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