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 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"])
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"])
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"])
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/
137 assert_equal(15, json_response["limit"])
138 assert_equal(201, json_response["items_available"])
141 test "admin can create collection with unsigned manifest" do
142 authorize_with :admin
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
151 test_collection[:portable_data_hash] =
152 Digest::MD5.hexdigest(test_collection[:manifest_text]) +
154 test_collection[:manifest_text].length.to_s
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))
160 collection: post_collection
163 assert_response :success
164 assert_nil assigns(:objects)
166 response_collection = assigns(:object)
168 stored_collection = Collection.select([:uuid, :portable_data_hash, :manifest_text]).
169 where(portable_data_hash: response_collection['portable_data_hash']).first
171 assert_equal test_collection[:portable_data_hash], stored_collection['portable_data_hash']
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
178 # TBD: create action should add permission signatures to manifest_text in the response,
179 # and we need to check those permission signatures here.
182 [:admin, :active].each do |user|
183 test "#{user} can get collection using portable data hash" do
186 foo_collection = collections(:foo_file)
188 # Get foo_file using its portable data hash
190 id: foo_collection[:portable_data_hash]
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']
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
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"
211 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
212 manifest_text: manifest_text,
213 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
216 assert_response :success
217 resp = JSON.parse(@response.body)
218 assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
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"
227 owner_uuid: 'zzzzz-tpzed-000000000000000',
228 manifest_text: manifest_text,
229 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
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}")
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"
246 manifest_text = Collection.sign_manifest manifest_text, api_token(:active)
250 owner_uuid: users(:active).uuid,
251 manifest_text: manifest_text,
252 name: "owned_by_active"
254 ensure_unique_name: true
256 assert_response :success
257 assert_equal 'owned_by_active (2)', json_response['name']
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"
267 owner_uuid: groups(:active_user_has_can_manage).uuid,
268 manifest_text: manifest_text,
269 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
272 assert_response :success
273 resp = JSON.parse(@response.body)
274 assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
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"
283 owner_uuid: groups(:all_users).uuid,
284 manifest_text: manifest_text,
285 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
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"
297 owner_uuid: groups(:public).uuid,
298 manifest_text: manifest_text,
299 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
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"
311 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
312 manifest_text: manifest_text,
313 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
316 assert_response :success
319 test "should create with collection passed as json" do
320 permit_unsigned_manifests
321 authorize_with :active
325 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
326 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
330 assert_response :success
333 test "should fail to create with checksum mismatch" do
334 permit_unsigned_manifests
335 authorize_with :active
339 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
340 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
347 test "collection UUID is normalized when created" do
348 permit_unsigned_manifests
349 authorize_with :active
352 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
353 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
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']
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
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'
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
394 test "search collections with 'any' operator" do
395 expect_pdh = collections(:docker_image).portable_data_hash
396 authorize_with :active
398 where: { any: ['contains', expect_pdh[5..25]] }
400 assert_response :success
401 found = assigns(:objects)
402 assert_equal 1, found.count
403 assert_equal expect_pdh, found.first.portable_data_hash
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
411 d41d8cd98f00b204e9800998ecf8427e+0
412 acbd18db4cc2f85cedef654fccc4a4d8+3
413 ea10d51bcf88862dbcc36eb292017dfd+45)
415 unsigned_manifest = locators.map { |loc|
416 ". " + loc + " 0:0:foo.txt\n"
418 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
420 unsigned_manifest.length.to_s
422 # Build a manifest with both signed and unsigned locators.
424 key: Rails.configuration.blob_signing_key,
425 api_token: api_token(:active),
427 signed_locators = locators.collect do |x|
428 Blob.sign_locator x, signing_opts
431 # Leave a non-empty blob unsigned.
432 signed_locators[1] = locators[1]
434 # Leave the empty blob unsigned. This should still be allowed.
435 signed_locators[0] = locators[0]
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"
444 manifest_text: signed_manifest,
445 portable_data_hash: manifest_uuid,
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)
456 assert Blob.verify_signature m[0], signing_opts
462 test "create collection with signed manifest and explicit TTL" do
463 authorize_with :active
465 d41d8cd98f00b204e9800998ecf8427e+0
466 acbd18db4cc2f85cedef654fccc4a4d8+3
467 ea10d51bcf88862dbcc36eb292017dfd+45)
469 unsigned_manifest = locators.map { |loc|
470 ". " + loc + " 0:0:foo.txt\n"
472 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
474 unsigned_manifest.length.to_s
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.
480 key: Rails.configuration.blob_signing_key,
481 api_token: api_token(:active),
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"
491 manifest_text: signed_manifest,
492 portable_data_hash: manifest_uuid,
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)
503 assert Blob.verify_signature m[0], signing_opts
508 test "create fails with invalid signature" do
509 authorize_with :active
511 key: Rails.configuration.blob_signing_key,
512 api_token: api_token(:active),
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)
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) +
525 unsigned_manifest.length.to_s
527 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
530 manifest_text: bad_manifest,
531 portable_data_hash: manifest_uuid
538 test "create fails with uuid of signed manifest" do
539 authorize_with :active
541 key: Rails.configuration.blob_signing_key,
542 api_token: api_token(:active),
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) +
550 signed_manifest.length.to_s
554 manifest_text: signed_manifest,
555 portable_data_hash: manifest_uuid
562 test "multiple locators per line" do
563 permit_unsigned_manifests
564 authorize_with :active
566 d41d8cd98f00b204e9800998ecf8427e+0
567 acbd18db4cc2f85cedef654fccc4a4d8+3
568 ea10d51bcf88862dbcc36eb292017dfd+45)
570 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
571 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
573 manifest_text.length.to_s
576 manifest_text: manifest_text,
577 portable_data_hash: manifest_uuid,
579 post_collection = Marshal.load(Marshal.dump(test_collection))
581 collection: post_collection
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']
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
594 test "multiple signed locators per line" do
595 permit_unsigned_manifests
596 authorize_with :active
598 d41d8cd98f00b204e9800998ecf8427e+0
599 acbd18db4cc2f85cedef654fccc4a4d8+3
600 ea10d51bcf88862dbcc36eb292017dfd+45)
603 key: Rails.configuration.blob_signing_key,
604 api_token: api_token(:active),
607 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
608 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
610 unsigned_manifest.length.to_s
612 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
613 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
617 manifest_text: signed_manifest,
618 portable_data_hash: manifest_uuid,
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
635 assert_equal locators.count, returned_locator_count
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)
645 manifest_text: unsigned_manifest,
646 portable_data_hash: manifest_uuid,
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"
655 test 'List expired collection returns empty list' do
656 authorize_with :active
658 where: {name: 'expired_collection'},
660 assert_response :success
661 found = assigns(:objects)
662 assert_equal 0, found.count
665 test 'Show expired collection returns 404' do
666 authorize_with :active
668 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
673 test 'Update expired collection returns 404' do
674 authorize_with :active
676 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
678 name: "still expired"
684 test 'List collection with future expiration time succeeds' do
685 authorize_with :active
687 where: {name: 'collection_expires_in_future'},
689 found = assigns(:objects)
690 assert_equal 1, found.count
694 test 'Show collection with future expiration time succeeds' do
695 authorize_with :active
697 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
699 assert_response :success
702 test 'Update collection with future expiration time succeeds' do
703 authorize_with :active
705 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
707 name: "still not expired"
710 assert_response :success
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']
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.)"
731 authorize_with :active
733 description = 'here is a collection with a very large description'
734 while description.length < description_size
735 description = description + description
738 post :create, collection: {
739 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt\n",
740 description: description,
743 assert_response expected_response
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
752 id: collections(:replication_undesired_unconfirmed).uuid,
754 replication_desired: ask,
757 assert_response :success
758 assert_equal ask, json_response['replication_desired']
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']
770 test "create collection with properties" do
771 authorize_with :active
772 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
775 manifest_text: manifest_text,
776 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
777 properties: {'property_1' => 'value_1'}
780 assert_response :success
781 assert_not_nil json_response['uuid']
782 assert_equal 'value_1', json_response['properties']['property_1']