1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
10 PERM_TOKEN_RE = /\+A[[:xdigit:]]+@[[:xdigit:]]{8}\b/
12 def permit_unsigned_manifests isok=true
13 # Set security model for the life of a test.
14 Rails.configuration.permit_create_collection_with_unsigned_manifest = isok
17 def assert_signed_manifest manifest_text, label=''
18 assert_not_nil manifest_text, "#{label} manifest_text was nil"
19 manifest_text.scan(/ [[:xdigit:]]{32}\S*/) do |tok|
20 assert_match(PERM_TOKEN_RE, tok,
21 "Locator in #{label} manifest_text was not signed")
25 def assert_unsigned_manifest resp, label=''
26 txt = resp['unsigned_manifest_text']
27 assert_not_nil(txt, "#{label} unsigned_manifest_text was nil")
29 txt.scan(/ [[:xdigit:]]{32}\S*/) do |tok|
31 refute_match(PERM_TOKEN_RE, tok,
32 "Locator in #{label} unsigned_manifest_text was signed: #{tok}")
37 test "should get index" do
38 authorize_with :active
40 assert_response :success
41 assert(assigns(:objects).andand.any?, "no Collections returned in index")
42 refute(json_response["items"].any? { |c| c.has_key?("manifest_text") },
43 "basic Collections index included manifest_text")
46 test "collections.get returns signed locators, and no unsigned_manifest_text" do
47 permit_unsigned_manifests
48 authorize_with :active
49 get :show, {id: collections(:foo_file).uuid}
50 assert_response :success
51 assert_signed_manifest json_response['manifest_text'], 'foo_file'
52 refute_includes json_response, 'unsigned_manifest_text'
55 test "index with manifest_text selected returns signed locators" do
56 columns = %w(uuid owner_uuid manifest_text)
57 authorize_with :active
58 get :index, select: columns
59 assert_response :success
60 assert(assigns(:objects).andand.any?,
61 "no Collections returned for index with columns selected")
62 json_response["items"].each do |coll|
63 assert_equal(coll.keys - ['kind'], columns,
64 "Collections index did not respect selected columns")
65 assert_signed_manifest coll['manifest_text'], coll['uuid']
69 test "index with unsigned_manifest_text selected returns only unsigned locators" do
70 authorize_with :active
71 get :index, select: ['unsigned_manifest_text']
72 assert_response :success
73 assert_operator json_response["items"].count, :>, 0
75 json_response["items"].each do |coll|
76 assert_equal(coll.keys - ['kind'], ['unsigned_manifest_text'],
77 "Collections index did not respect selected columns")
78 locs += assert_unsigned_manifest coll, coll['uuid']
80 assert_operator locs, :>, 0, "no locators found in any manifests"
83 test 'index without select returns everything except manifest' do
84 authorize_with :active
86 assert_response :success
87 assert json_response['items'].any?
88 json_response['items'].each do |coll|
89 assert_includes(coll.keys, 'uuid')
90 assert_includes(coll.keys, 'name')
91 assert_includes(coll.keys, 'created_at')
92 refute_includes(coll.keys, 'manifest_text')
96 ['', nil, false, 'null'].each do |select|
97 test "index with select=#{select.inspect} returns everything except manifest" do
98 authorize_with :active
99 get :index, select: select
100 assert_response :success
101 assert json_response['items'].any?
102 json_response['items'].each do |coll|
103 assert_includes(coll.keys, 'uuid')
104 assert_includes(coll.keys, 'name')
105 assert_includes(coll.keys, 'created_at')
106 refute_includes(coll.keys, 'manifest_text')
112 ["uuid", "manifest_text"],
114 '["uuid", "manifest_text"]'].each do |select|
115 test "index with select=#{select.inspect} returns no name" do
116 authorize_with :active
117 get :index, select: select
118 assert_response :success
119 assert json_response['items'].any?
120 json_response['items'].each do |coll|
121 refute_includes(coll.keys, 'name')
126 [0,1,2].each do |limit|
127 test "get index with limit=#{limit}" do
128 authorize_with :active
129 get :index, limit: limit
130 assert_response :success
131 assert_equal limit, assigns(:objects).count
132 resp = JSON.parse(@response.body)
133 assert_equal limit, resp['limit']
137 test "items.count == items_available" do
138 authorize_with :active
139 get :index, limit: 100000
140 assert_response :success
141 resp = JSON.parse(@response.body)
142 assert_equal resp['items_available'], assigns(:objects).length
143 assert_equal resp['items_available'], resp['items'].count
144 unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq
145 assert_equal unique_uuids.count, resp['items'].count
148 test "items.count == items_available with filters" do
149 authorize_with :active
152 filters: [['uuid','=',collections(:foo_file).uuid]]
154 assert_response :success
155 assert_equal 1, assigns(:objects).length
156 assert_equal 1, json_response['items_available']
157 assert_equal 1, json_response['items'].count
160 test "get index with limit=2 offset=99999" do
161 # Assume there are not that many test fixtures.
162 authorize_with :active
163 get :index, limit: 2, offset: 99999
164 assert_response :success
165 assert_equal 0, assigns(:objects).count
166 resp = JSON.parse(@response.body)
167 assert_equal 2, resp['limit']
168 assert_equal 99999, resp['offset']
171 def request_capped_index(params={})
172 authorize_with :user1_with_load
173 coll1 = collections(:collection_1_of_201)
174 Rails.configuration.max_index_database_read =
175 yield(coll1.manifest_text.size)
177 select: %w(uuid manifest_text),
178 filters: [["owner_uuid", "=", coll1.owner_uuid]],
183 test "index with manifest_text limited by max_index_database_read returns non-empty" do
184 request_capped_index() { |_| 1 }
185 assert_response :success
186 assert_equal(1, json_response["items"].size)
187 assert_equal(1, json_response["limit"])
188 assert_equal(201, json_response["items_available"])
191 test "max_index_database_read size check follows same order as real query" do
192 authorize_with :user1_with_load
193 txt = '.' + ' d41d8cd98f00b204e9800998ecf8427e+0'*1000 + " 0:0:empty.txt\n"
194 c = Collection.create! manifest_text: txt, name: '0000000000000000000'
195 request_capped_index(select: %w(uuid manifest_text name),
197 filters: [['name','>=',c.name]]) do |_|
200 assert_response :success
201 assert_equal(1, json_response["items"].size)
202 assert_equal(1, json_response["limit"])
203 assert_equal(c.uuid, json_response["items"][0]["uuid"])
204 # The effectiveness of the test depends on >1 item matching the filters.
205 assert_operator(1, :<, json_response["items_available"])
208 test "index with manifest_text limited by max_index_database_read" do
209 request_capped_index() { |size| (size * 3) + 1 }
210 assert_response :success
211 assert_equal(3, json_response["items"].size)
212 assert_equal(3, json_response["limit"])
213 assert_equal(201, json_response["items_available"])
216 test "max_index_database_read does not interfere with limit" do
217 request_capped_index(limit: 5) { |size| size * 20 }
218 assert_response :success
219 assert_equal(5, json_response["items"].size)
220 assert_equal(5, json_response["limit"])
221 assert_equal(201, json_response["items_available"])
224 test "max_index_database_read does not interfere with order" do
225 request_capped_index(select: %w(uuid manifest_text name),
226 order: "name DESC") { |size| (size * 11) + 1 }
227 assert_response :success
228 assert_equal(11, json_response["items"].size)
229 assert_empty(json_response["items"].reject do |coll|
230 coll["name"] =~ /^Collection_9/
232 assert_equal(11, json_response["limit"])
233 assert_equal(201, json_response["items_available"])
236 test "admin can create collection with unsigned manifest" do
237 authorize_with :admin
239 manifest_text: <<-EOS
240 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
241 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
242 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
243 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
246 test_collection[:portable_data_hash] =
247 Digest::MD5.hexdigest(test_collection[:manifest_text]) +
249 test_collection[:manifest_text].length.to_s
251 # post :create will modify test_collection in place, so we save a copy first.
252 # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
253 post_collection = Marshal.load(Marshal.dump(test_collection))
255 collection: post_collection
258 assert_response :success
259 assert_nil assigns(:objects)
261 response_collection = assigns(:object)
263 stored_collection = Collection.select([:uuid, :portable_data_hash, :manifest_text]).
264 where(portable_data_hash: response_collection['portable_data_hash']).first
266 assert_equal test_collection[:portable_data_hash], stored_collection['portable_data_hash']
268 # The manifest in the response will have had permission hints added.
269 # Remove any permission hints in the response before comparing it to the source.
270 stripped_manifest = stored_collection['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
271 assert_equal test_collection[:manifest_text], stripped_manifest
273 # TBD: create action should add permission signatures to manifest_text in the response,
274 # and we need to check those permission signatures here.
277 [:admin, :active].each do |user|
278 test "#{user} can get collection using portable data hash" do
281 foo_collection = collections(:foo_file)
283 # Get foo_file using its portable data hash
285 id: foo_collection[:portable_data_hash]
287 assert_response :success
288 assert_not_nil assigns(:object)
289 resp = assigns(:object)
290 assert_equal foo_collection[:portable_data_hash], resp[:portable_data_hash]
291 assert_signed_manifest resp[:manifest_text]
293 # The manifest in the response will have had permission hints added.
294 # Remove any permission hints in the response before comparing it to the source.
295 stripped_manifest = resp[:manifest_text].gsub(/\+A[A-Za-z0-9@_-]+/, '')
296 assert_equal foo_collection[:manifest_text], stripped_manifest
300 test "create with owner_uuid set to owned group" do
301 permit_unsigned_manifests
302 authorize_with :active
303 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
306 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
307 manifest_text: manifest_text,
308 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
311 assert_response :success
312 resp = JSON.parse(@response.body)
313 assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
316 test "create fails with duplicate name" do
317 permit_unsigned_manifests
318 authorize_with :admin
319 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
322 owner_uuid: 'zzzzz-tpzed-000000000000000',
323 manifest_text: manifest_text,
324 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
329 response_errors = json_response['errors']
330 assert_not_nil response_errors, 'Expected error in response'
331 assert(response_errors.first.include?('duplicate key'),
332 "Expected 'duplicate key' error in #{response_errors.first}")
335 [false, true].each do |unsigned|
336 test "create with duplicate name, ensure_unique_name, unsigned=#{unsigned}" do
337 permit_unsigned_manifests unsigned
338 authorize_with :active
339 manifest_text = ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:0:foo.txt\n"
341 manifest_text = Collection.sign_manifest manifest_text, api_token(:active)
345 owner_uuid: users(:active).uuid,
346 manifest_text: manifest_text,
347 name: "owned_by_active"
349 ensure_unique_name: true
351 assert_response :success
352 assert_match /^owned_by_active \(\d{4}-\d\d-\d\d.*?Z\)$/, json_response['name']
356 test "create with owner_uuid set to group i can_manage" do
357 permit_unsigned_manifests
358 authorize_with :active
359 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
362 owner_uuid: groups(:active_user_has_can_manage).uuid,
363 manifest_text: manifest_text,
364 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
367 assert_response :success
368 resp = JSON.parse(@response.body)
369 assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
372 test "create with owner_uuid fails on group with only can_read permission" do
373 permit_unsigned_manifests
374 authorize_with :active
375 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
378 owner_uuid: groups(:all_users).uuid,
379 manifest_text: manifest_text,
380 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
386 test "create with owner_uuid fails on group with no permission" do
387 permit_unsigned_manifests
388 authorize_with :active
389 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
392 owner_uuid: groups(:public).uuid,
393 manifest_text: manifest_text,
394 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
400 test "admin create with owner_uuid set to group with no permission" do
401 permit_unsigned_manifests
402 authorize_with :admin
403 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
406 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
407 manifest_text: manifest_text,
408 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
411 assert_response :success
414 test "should create with collection passed as json" do
415 permit_unsigned_manifests
416 authorize_with :active
420 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
421 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
425 assert_response :success
428 test "should fail to create with checksum mismatch" do
429 permit_unsigned_manifests
430 authorize_with :active
434 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
435 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
442 test "collection UUID is normalized when created" do
443 permit_unsigned_manifests
444 authorize_with :active
447 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
448 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
451 assert_response :success
452 assert_not_nil assigns(:object)
453 resp = JSON.parse(@response.body)
454 assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['portable_data_hash']
457 test "get full provenance for baz file" do
458 authorize_with :active
459 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
460 assert_response :success
461 resp = JSON.parse(@response.body)
462 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
463 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
464 assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
465 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
466 assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
469 test "get no provenance for foo file" do
470 # spectator user cannot even see baz collection
471 authorize_with :spectator
472 get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
476 test "get partial provenance for baz file" do
477 # spectator user can see bar->baz job, but not foo->bar job
478 authorize_with :spectator
479 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
480 assert_response :success
481 resp = JSON.parse(@response.body)
482 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
483 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
484 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
485 assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
486 assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
489 test "search collections with 'any' operator" do
490 expect_pdh = collections(:docker_image).portable_data_hash
491 authorize_with :active
493 where: { any: ['contains', expect_pdh[5..25]] }
495 assert_response :success
496 found = assigns(:objects)
497 assert_equal 1, found.count
498 assert_equal expect_pdh, found.first.portable_data_hash
501 [false, true].each do |permit_unsigned|
502 test "create collection with signed manifest, permit_unsigned=#{permit_unsigned}" do
503 permit_unsigned_manifests permit_unsigned
504 authorize_with :active
506 d41d8cd98f00b204e9800998ecf8427e+0
507 acbd18db4cc2f85cedef654fccc4a4d8+3
508 ea10d51bcf88862dbcc36eb292017dfd+45)
510 unsigned_manifest = locators.map { |loc|
511 ". " + loc + " 0:0:foo.txt\n"
513 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
515 unsigned_manifest.length.to_s
517 # Build a manifest with both signed and unsigned locators.
519 key: Rails.configuration.blob_signing_key,
520 api_token: api_token(:active),
522 signed_locators = locators.collect do |x|
523 Blob.sign_locator x, signing_opts
526 # Leave a non-empty blob unsigned.
527 signed_locators[1] = locators[1]
529 # Leave the empty blob unsigned. This should still be allowed.
530 signed_locators[0] = locators[0]
533 ". " + signed_locators[0] + " 0:0:foo.txt\n" +
534 ". " + signed_locators[1] + " 0:0:foo.txt\n" +
535 ". " + signed_locators[2] + " 0:0:foo.txt\n"
539 manifest_text: signed_manifest,
540 portable_data_hash: manifest_uuid,
543 assert_response :success
544 assert_not_nil assigns(:object)
545 resp = JSON.parse(@response.body)
546 assert_equal manifest_uuid, resp['portable_data_hash']
547 # All of the locators in the output must be signed.
548 resp['manifest_text'].lines.each do |entry|
549 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
551 assert Blob.verify_signature m[0], signing_opts
557 test "create collection with signed manifest and explicit TTL" do
558 authorize_with :active
560 d41d8cd98f00b204e9800998ecf8427e+0
561 acbd18db4cc2f85cedef654fccc4a4d8+3
562 ea10d51bcf88862dbcc36eb292017dfd+45)
564 unsigned_manifest = locators.map { |loc|
565 ". " + loc + " 0:0:foo.txt\n"
567 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
569 unsigned_manifest.length.to_s
571 # build a manifest with both signed and unsigned locators.
572 # TODO(twp): in phase 4, all locators will need to be signed, so
573 # this test should break and will need to be rewritten. Issue #2755.
575 key: Rails.configuration.blob_signing_key,
576 api_token: api_token(:active),
580 ". " + locators[0] + " 0:0:foo.txt\n" +
581 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
582 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
586 manifest_text: signed_manifest,
587 portable_data_hash: manifest_uuid,
590 assert_response :success
591 assert_not_nil assigns(:object)
592 resp = JSON.parse(@response.body)
593 assert_equal manifest_uuid, resp['portable_data_hash']
594 # All of the locators in the output must be signed.
595 resp['manifest_text'].lines.each do |entry|
596 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
598 assert Blob.verify_signature m[0], signing_opts
603 test "create fails with invalid signature" do
604 authorize_with :active
606 key: Rails.configuration.blob_signing_key,
607 api_token: api_token(:active),
610 # Generate a locator with a bad signature.
611 unsigned_locator = "acbd18db4cc2f85cedef654fccc4a4d8+3"
612 bad_locator = unsigned_locator + "+Affffffffffffffffffffffffffffffffffffffff@ffffffff"
613 assert !Blob.verify_signature(bad_locator, signing_opts)
615 # Creating a collection with this locator should
616 # produce 403 Permission denied.
617 unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
618 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
620 unsigned_manifest.length.to_s
622 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
625 manifest_text: bad_manifest,
626 portable_data_hash: manifest_uuid
633 test "create fails with uuid of signed manifest" do
634 authorize_with :active
636 key: Rails.configuration.blob_signing_key,
637 api_token: api_token(:active),
640 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
641 signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
642 signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
643 manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
645 signed_manifest.length.to_s
649 manifest_text: signed_manifest,
650 portable_data_hash: manifest_uuid
657 test "reject manifest with unsigned block as stream name" do
658 authorize_with :active
661 manifest_text: "00000000000000000000000000000000+1234 d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt\n"
664 assert_includes [422, 403], response.code.to_i
667 test "multiple locators per line" do
668 permit_unsigned_manifests
669 authorize_with :active
671 d41d8cd98f00b204e9800998ecf8427e+0
672 acbd18db4cc2f85cedef654fccc4a4d8+3
673 ea10d51bcf88862dbcc36eb292017dfd+45)
675 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
676 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
678 manifest_text.length.to_s
681 manifest_text: manifest_text,
682 portable_data_hash: manifest_uuid,
684 post_collection = Marshal.load(Marshal.dump(test_collection))
686 collection: post_collection
688 assert_response :success
689 assert_not_nil assigns(:object)
690 resp = JSON.parse(@response.body)
691 assert_equal manifest_uuid, resp['portable_data_hash']
693 # The manifest in the response will have had permission hints added.
694 # Remove any permission hints in the response before comparing it to the source.
695 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
696 assert_equal manifest_text, stripped_manifest
699 test "multiple signed locators per line" do
700 permit_unsigned_manifests
701 authorize_with :active
703 d41d8cd98f00b204e9800998ecf8427e+0
704 acbd18db4cc2f85cedef654fccc4a4d8+3
705 ea10d51bcf88862dbcc36eb292017dfd+45)
708 key: Rails.configuration.blob_signing_key,
709 api_token: api_token(:active),
712 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
713 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
715 unsigned_manifest.length.to_s
717 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
718 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
722 manifest_text: signed_manifest,
723 portable_data_hash: manifest_uuid,
726 assert_response :success
727 assert_not_nil assigns(:object)
728 resp = JSON.parse(@response.body)
729 assert_equal manifest_uuid, resp['portable_data_hash']
730 # All of the locators in the output must be signed.
731 # Each line is of the form "path locator locator ... 0:0:file.txt"
732 # entry.split[1..-2] will yield just the tokens in the middle of the line
733 returned_locator_count = 0
734 resp['manifest_text'].lines.each do |entry|
735 entry.split[1..-2].each do |tok|
736 returned_locator_count += 1
737 assert Blob.verify_signature tok, signing_opts
740 assert_equal locators.count, returned_locator_count
743 test 'Reject manifest with unsigned blob' do
744 permit_unsigned_manifests false
745 authorize_with :active
746 unsigned_manifest = ". 0cc175b9c0f1b6a831c399e269772661+1 0:1:a.txt\n"
747 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest)
750 manifest_text: unsigned_manifest,
751 portable_data_hash: manifest_uuid,
755 "Creating a collection with unsigned blobs should respond 403"
756 assert_empty Collection.where('uuid like ?', manifest_uuid+'%'),
757 "Collection should not exist in database after failed create"
760 test 'List expired collection returns empty list' do
761 authorize_with :active
763 where: {name: 'expired_collection'},
765 assert_response :success
766 found = assigns(:objects)
767 assert_equal 0, found.count
770 test 'Show expired collection returns 404' do
771 authorize_with :active
773 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
778 test 'Update expired collection returns 404' do
779 authorize_with :active
781 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
783 name: "still expired"
789 test 'List collection with future expiration time succeeds' do
790 authorize_with :active
792 where: {name: 'collection_expires_in_future'},
794 found = assigns(:objects)
795 assert_equal 1, found.count
799 test 'Show collection with future expiration time succeeds' do
800 authorize_with :active
802 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
804 assert_response :success
807 test 'Update collection with future expiration time succeeds' do
808 authorize_with :active
810 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
812 name: "still not expired"
815 assert_response :success
818 test "get collection and verify that file_names is not included" do
819 authorize_with :active
820 get :show, {id: collections(:foo_file).uuid}
821 assert_response :success
822 assert_equal collections(:foo_file).uuid, json_response['uuid']
823 assert_nil json_response['file_names']
824 assert json_response['manifest_text']
830 ].each do |description_size, expected_response|
831 # Descriptions are not part of search indexes. Skip until
832 # full-text search is implemented, at which point replace with a
833 # search in description.
834 skip "create collection with description size #{description_size}
835 and expect response #{expected_response}" do
836 authorize_with :active
838 description = 'here is a collection with a very large description'
839 while description.length < description_size
840 description = description + description
843 post :create, collection: {
844 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt\n",
845 description: description,
848 assert_response expected_response
852 [1, 5, nil].each do |ask|
853 test "Set replication_desired=#{ask.inspect}" do
854 Rails.configuration.default_collection_replication = 2
855 authorize_with :active
857 id: collections(:replication_undesired_unconfirmed).uuid,
859 replication_desired: ask,
862 assert_response :success
863 assert_equal ask, json_response['replication_desired']
867 test "get collection with properties" do
868 authorize_with :active
869 get :show, {id: collections(:collection_with_one_property).uuid}
870 assert_response :success
871 assert_not_nil json_response['uuid']
872 assert_equal 'value1', json_response['properties']['property1']
875 test "create collection with properties" do
876 authorize_with :active
877 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
880 manifest_text: manifest_text,
881 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
882 properties: {'property_1' => 'value_1'}
885 assert_response :success
886 assert_not_nil json_response['uuid']
887 assert_equal 'value_1', json_response['properties']['property_1']
892 ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
893 "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
894 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
895 ].each do |manifest_text|
896 test "create collection with invalid manifest #{manifest_text} and expect error" do
897 authorize_with :active
900 manifest_text: manifest_text,
901 portable_data_hash: "d41d8cd98f00b204e9800998ecf8427e+0"
905 response_errors = json_response['errors']
906 assert_not_nil response_errors, 'Expected error in response'
907 assert(response_errors.first.include?('Invalid manifest'),
908 "Expected 'Invalid manifest' error in #{response_errors.first}")
913 [nil, "d41d8cd98f00b204e9800998ecf8427e+0"],
914 ["", "d41d8cd98f00b204e9800998ecf8427e+0"],
915 [". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n", "d30fe8ae534397864cb96c544f4cf102+47"],
916 ].each do |manifest_text, pdh|
917 test "create collection with valid manifest #{manifest_text.inspect} and expect success" do
918 authorize_with :active
921 manifest_text: manifest_text,
922 portable_data_hash: pdh
931 ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
932 "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
933 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
934 ].each do |manifest_text|
935 test "update collection with invalid manifest #{manifest_text} and expect error" do
936 authorize_with :active
938 id: 'zzzzz-4zz18-bv31uwvy3neko21',
940 manifest_text: manifest_text,
944 response_errors = json_response['errors']
945 assert_not_nil response_errors, 'Expected error in response'
946 assert(response_errors.first.include?('Invalid manifest'),
947 "Expected 'Invalid manifest' error in #{response_errors.first}")
954 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
955 ].each do |manifest_text|
956 test "update collection with valid manifest #{manifest_text.inspect} and expect success" do
957 authorize_with :active
959 id: 'zzzzz-4zz18-bv31uwvy3neko21',
961 manifest_text: manifest_text,
968 test 'get trashed collection with include_trash' do
969 uuid = 'zzzzz-4zz18-mto52zx1s7sn3ih' # expired_collection
970 authorize_with :active
978 test 'get trashed collection without include_trash' do
979 uuid = 'zzzzz-4zz18-mto52zx1s7sn3ih' # expired_collection
980 authorize_with :active
987 test 'trash collection using http DELETE verb' do
988 uuid = collections(:collection_owned_by_active).uuid
989 authorize_with :active
994 c = Collection.find_by_uuid(uuid)
995 assert_operator c.trash_at, :<, db_current_time
996 assert_equal c.delete_at, c.trash_at + Rails.configuration.blob_signature_ttl
999 test 'delete long-trashed collection immediately using http DELETE verb' do
1000 uuid = 'zzzzz-4zz18-mto52zx1s7sn3ih' # expired_collection
1001 authorize_with :active
1006 c = Collection.find_by_uuid(uuid)
1007 assert_operator c.trash_at, :<, db_current_time
1008 assert_operator c.delete_at, :<, db_current_time
1011 ['zzzzz-4zz18-mto52zx1s7sn3ih', # expired_collection
1012 :empty_collection_name_in_active_user_home_project,
1014 test "trash collection #{fixture} via trash action with grace period" do
1015 if fixture.is_a? String
1018 uuid = collections(fixture).uuid
1020 authorize_with :active
1021 time_before_trashing = db_current_time
1026 c = Collection.find_by_uuid(uuid)
1027 assert_operator c.trash_at, :<, db_current_time
1028 assert_operator c.delete_at, :>=, time_before_trashing + Rails.configuration.default_trash_lifetime
1032 test 'untrash a trashed collection' do
1033 authorize_with :active
1035 id: collections(:expired_collection).uuid,
1038 assert_equal false, json_response['is_trashed']
1039 assert_nil json_response['trash_at']
1042 test 'untrash error on not trashed collection' do
1043 authorize_with :active
1045 id: collections(:collection_owned_by_active).uuid,
1050 [:active, :admin].each do |user|
1051 test "get trashed collections as #{user}" do
1054 filters: [["is_trashed", "=", true]],
1055 include_trash: true,
1057 assert_response :success
1060 json_response["items"].each do |coll|
1061 items << coll['uuid']
1064 assert_includes(items, collections('unique_expired_collection')['uuid'])
1066 assert_includes(items, collections('unique_expired_collection2')['uuid'])
1068 assert_not_includes(items, collections('unique_expired_collection2')['uuid'])
1073 test 'untrash collection with same name as another with no ensure unique name' do
1074 authorize_with :active
1076 id: collections(:trashed_collection_to_test_name_conflict_on_untrash).uuid,
1081 test 'untrash collection with same name as another with ensure unique name' do
1082 authorize_with :active
1084 id: collections(:trashed_collection_to_test_name_conflict_on_untrash).uuid,
1085 ensure_unique_name: true
1088 assert_equal false, json_response['is_trashed']
1089 assert_nil json_response['trash_at']
1090 assert_nil json_response['delete_at']
1091 assert_match /^same name for trashed and persisted collections \(\d{4}-\d\d-\d\d.*?Z\)$/, json_response['name']
1094 test 'cannot show collection in trashed subproject' do
1095 authorize_with :active
1097 id: collections(:collection_in_trashed_subproject).uuid,
1103 test 'can show collection in untrashed subproject' do
1104 authorize_with :active
1105 Group.find_by_uuid(groups(:trashed_project).uuid).update! is_trashed: false
1107 id: collections(:collection_in_trashed_subproject).uuid,
1110 assert_response :success
1113 test 'cannot index collection in trashed subproject' do
1114 authorize_with :active
1115 get :index, { limit: 1000 }
1116 assert_response :success
1117 item_uuids = json_response['items'].map do |item|
1120 assert_not_includes(item_uuids, collections(:collection_in_trashed_subproject).uuid)
1123 test 'can index collection in untrashed subproject' do
1124 authorize_with :active
1125 Group.find_by_uuid(groups(:trashed_project).uuid).update! is_trashed: false
1126 get :index, { limit: 1000 }
1127 assert_response :success
1128 item_uuids = json_response['items'].map do |item|
1131 assert_includes(item_uuids, collections(:collection_in_trashed_subproject).uuid)
1134 test 'can index trashed subproject collection with include_trash' do
1135 authorize_with :active
1137 include_trash: true,
1140 assert_response :success
1141 item_uuids = json_response['items'].map do |item|
1144 assert_includes(item_uuids, collections(:collection_in_trashed_subproject).uuid)