3 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
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
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")
18 test "should get index" do
19 authorize_with :active
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")
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'
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']
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']
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
71 test "items.count == items_available with filters" do
72 authorize_with :active
75 filters: [['uuid','=',collections(:foo_file).uuid]]
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
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']
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)
100 select: %w(uuid manifest_text),
101 filters: [["owner_uuid", "=", coll1.owner_uuid]],
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"])
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"])
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/
129 assert_equal(16, json_response["limit"])
130 assert_equal(201, json_response["items_available"])
133 test "admin can create collection with unsigned manifest" do
134 authorize_with :admin
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
143 test_collection[:portable_data_hash] =
144 Digest::MD5.hexdigest(test_collection[:manifest_text]) +
146 test_collection[:manifest_text].length.to_s
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))
152 collection: post_collection
155 assert_response :success
156 assert_nil assigns(:objects)
158 response_collection = assigns(:object)
160 stored_collection = Collection.select([:uuid, :portable_data_hash, :manifest_text]).
161 where(portable_data_hash: response_collection['portable_data_hash']).first
163 assert_equal test_collection[:portable_data_hash], stored_collection['portable_data_hash']
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
170 # TBD: create action should add permission signatures to manifest_text in the response,
171 # and we need to check those permission signatures here.
174 [:admin, :active].each do |user|
175 test "#{user} can get collection using portable data hash" do
178 foo_collection = collections(:foo_file)
180 # Get foo_file using its portable data hash
182 id: foo_collection[:portable_data_hash]
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']
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
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"
203 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
204 manifest_text: manifest_text,
205 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
208 assert_response :success
209 resp = JSON.parse(@response.body)
210 assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
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"
219 owner_uuid: 'zzzzz-tpzed-000000000000000',
220 manifest_text: manifest_text,
221 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
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}")
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"
238 manifest_text = Collection.sign_manifest manifest_text, api_token(:active)
242 owner_uuid: users(:active).uuid,
243 manifest_text: manifest_text,
244 name: "owned_by_active"
246 ensure_unique_name: true
248 assert_response :success
249 assert_equal 'owned_by_active (2)', json_response['name']
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"
259 owner_uuid: groups(:active_user_has_can_manage).uuid,
260 manifest_text: manifest_text,
261 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
264 assert_response :success
265 resp = JSON.parse(@response.body)
266 assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
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"
275 owner_uuid: groups(:all_users).uuid,
276 manifest_text: manifest_text,
277 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
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"
289 owner_uuid: groups(:public).uuid,
290 manifest_text: manifest_text,
291 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
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"
303 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
304 manifest_text: manifest_text,
305 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
308 assert_response :success
311 test "should create with collection passed as json" do
312 permit_unsigned_manifests
313 authorize_with :active
317 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
318 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
322 assert_response :success
325 test "should fail to create with checksum mismatch" do
326 permit_unsigned_manifests
327 authorize_with :active
331 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
332 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
339 test "collection UUID is normalized when created" do
340 permit_unsigned_manifests
341 authorize_with :active
344 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
345 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
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']
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
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'
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
386 test "search collections with 'any' operator" do
387 expect_pdh = collections(:docker_image).portable_data_hash
388 authorize_with :active
390 where: { any: ['contains', expect_pdh[5..25]] }
392 assert_response :success
393 found = assigns(:objects)
394 assert_equal 1, found.count
395 assert_equal expect_pdh, found.first.portable_data_hash
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
403 d41d8cd98f00b204e9800998ecf8427e+0
404 acbd18db4cc2f85cedef654fccc4a4d8+3
405 ea10d51bcf88862dbcc36eb292017dfd+45)
407 unsigned_manifest = locators.map { |loc|
408 ". " + loc + " 0:0:foo.txt\n"
410 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
412 unsigned_manifest.length.to_s
414 # Build a manifest with both signed and unsigned locators.
416 key: Rails.configuration.blob_signing_key,
417 api_token: api_token(:active),
419 signed_locators = locators.collect do |x|
420 Blob.sign_locator x, signing_opts
423 # Leave a non-empty blob unsigned.
424 signed_locators[1] = locators[1]
426 # Leave the empty blob unsigned. This should still be allowed.
427 signed_locators[0] = locators[0]
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"
436 manifest_text: signed_manifest,
437 portable_data_hash: manifest_uuid,
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)
448 assert Blob.verify_signature m[0], signing_opts
454 test "create collection with signed manifest and explicit TTL" do
455 authorize_with :active
457 d41d8cd98f00b204e9800998ecf8427e+0
458 acbd18db4cc2f85cedef654fccc4a4d8+3
459 ea10d51bcf88862dbcc36eb292017dfd+45)
461 unsigned_manifest = locators.map { |loc|
462 ". " + loc + " 0:0:foo.txt\n"
464 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
466 unsigned_manifest.length.to_s
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.
472 key: Rails.configuration.blob_signing_key,
473 api_token: api_token(:active),
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"
483 manifest_text: signed_manifest,
484 portable_data_hash: manifest_uuid,
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)
495 assert Blob.verify_signature m[0], signing_opts
500 test "create fails with invalid signature" do
501 authorize_with :active
503 key: Rails.configuration.blob_signing_key,
504 api_token: api_token(:active),
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)
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) +
517 unsigned_manifest.length.to_s
519 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
522 manifest_text: bad_manifest,
523 portable_data_hash: manifest_uuid
530 test "create fails with uuid of signed manifest" do
531 authorize_with :active
533 key: Rails.configuration.blob_signing_key,
534 api_token: api_token(:active),
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) +
542 signed_manifest.length.to_s
546 manifest_text: signed_manifest,
547 portable_data_hash: manifest_uuid
554 test "multiple locators per line" do
555 permit_unsigned_manifests
556 authorize_with :active
558 d41d8cd98f00b204e9800998ecf8427e+0
559 acbd18db4cc2f85cedef654fccc4a4d8+3
560 ea10d51bcf88862dbcc36eb292017dfd+45)
562 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
563 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
565 manifest_text.length.to_s
568 manifest_text: manifest_text,
569 portable_data_hash: manifest_uuid,
571 post_collection = Marshal.load(Marshal.dump(test_collection))
573 collection: post_collection
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']
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
586 test "multiple signed locators per line" do
587 permit_unsigned_manifests
588 authorize_with :active
590 d41d8cd98f00b204e9800998ecf8427e+0
591 acbd18db4cc2f85cedef654fccc4a4d8+3
592 ea10d51bcf88862dbcc36eb292017dfd+45)
595 key: Rails.configuration.blob_signing_key,
596 api_token: api_token(:active),
599 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
600 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
602 unsigned_manifest.length.to_s
604 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
605 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
609 manifest_text: signed_manifest,
610 portable_data_hash: manifest_uuid,
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
627 assert_equal locators.count, returned_locator_count
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)
637 manifest_text: unsigned_manifest,
638 portable_data_hash: manifest_uuid,
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"
647 test 'List expired collection returns empty list' do
648 authorize_with :active
650 where: {name: 'expired_collection'},
652 assert_response :success
653 found = assigns(:objects)
654 assert_equal 0, found.count
657 test 'Show expired collection returns 404' do
658 authorize_with :active
660 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
665 test 'Update expired collection returns 404' do
666 authorize_with :active
668 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
670 name: "still expired"
676 test 'List collection with future expiration time succeeds' do
677 authorize_with :active
679 where: {name: 'collection_expires_in_future'},
681 found = assigns(:objects)
682 assert_equal 1, found.count
686 test 'Show collection with future expiration time succeeds' do
687 authorize_with :active
689 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
691 assert_response :success
694 test 'Update collection with future expiration time succeeds' do
695 authorize_with :active
697 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
699 name: "still not expired"
702 assert_response :success
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']
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.)"
723 authorize_with :active
725 description = 'here is a collection with a very large description'
726 while description.length < description_size
727 description = description + description
730 post :create, collection: {
731 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt\n",
732 description: description,
735 assert_response expected_response
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
744 id: collections(:replication_undesired_unconfirmed).uuid,
746 replication_desired: ask,
749 assert_response :success
750 assert_equal ask, json_response['replication_desired']
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']
762 test "create collection with properties" do
763 authorize_with :active
764 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
767 manifest_text: manifest_text,
768 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
769 properties: {'property_1' => 'value_1'}
772 assert_response :success
773 assert_not_nil json_response['uuid']
774 assert_equal 'value_1', json_response['properties']['property_1']