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 test "admin can create collection with unsigned manifest" do
98 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
99 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
100 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
101 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
104 test_collection[:portable_data_hash] =
105 Digest::MD5.hexdigest(test_collection[:manifest_text]) +
107 test_collection[:manifest_text].length.to_s
109 # post :create will modify test_collection in place, so we save a copy first.
110 # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
111 post_collection = Marshal.load(Marshal.dump(test_collection))
113 collection: post_collection
116 assert_response :success
117 assert_nil assigns(:objects)
119 response_collection = assigns(:object)
121 stored_collection = Collection.select([:uuid, :portable_data_hash, :manifest_text]).
122 where(portable_data_hash: response_collection['portable_data_hash']).first
124 assert_equal test_collection[:portable_data_hash], stored_collection['portable_data_hash']
126 # The manifest in the response will have had permission hints added.
127 # Remove any permission hints in the response before comparing it to the source.
128 stripped_manifest = stored_collection['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
129 assert_equal test_collection[:manifest_text], stripped_manifest
131 # TBD: create action should add permission signatures to manifest_text in the response,
132 # and we need to check those permission signatures here.
135 [:admin, :active].each do |user|
136 test "#{user} can get collection using portable data hash" do
139 foo_collection = collections(:foo_file)
141 # Get foo_file using its portable data hash
143 id: foo_collection[:portable_data_hash]
145 assert_response :success
146 assert_not_nil assigns(:object)
147 resp = assigns(:object)
148 assert_equal foo_collection[:portable_data_hash], resp['portable_data_hash']
149 assert_signed_manifest resp['manifest_text']
151 # The manifest in the response will have had permission hints added.
152 # Remove any permission hints in the response before comparing it to the source.
153 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
154 assert_equal foo_collection[:manifest_text], stripped_manifest
158 test "create with owner_uuid set to owned group" do
159 permit_unsigned_manifests
160 authorize_with :active
161 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
164 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
165 manifest_text: manifest_text,
166 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
169 assert_response :success
170 resp = JSON.parse(@response.body)
171 assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
174 test "create fails with duplicate name" do
175 permit_unsigned_manifests
176 authorize_with :admin
177 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
180 owner_uuid: 'zzzzz-tpzed-000000000000000',
181 manifest_text: manifest_text,
182 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
187 response_errors = json_response['errors']
188 assert_not_nil response_errors, 'Expected error in response'
189 assert(response_errors.first.include?('duplicate key'),
190 "Expected 'duplicate key' error in #{response_errors.first}")
193 [false, true].each do |unsigned|
194 test "create with duplicate name, ensure_unique_name, unsigned=#{unsigned}" do
195 permit_unsigned_manifests unsigned
196 authorize_with :active
197 manifest_text = ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:0:foo.txt\n"
199 manifest_text = Collection.sign_manifest manifest_text, api_token(:active)
203 owner_uuid: users(:active).uuid,
204 manifest_text: manifest_text,
205 name: "owned_by_active"
207 ensure_unique_name: true
209 assert_response :success
210 assert_equal 'owned_by_active (2)', json_response['name']
214 test "create with owner_uuid set to group i can_manage" do
215 permit_unsigned_manifests
216 authorize_with :active
217 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
220 owner_uuid: groups(:active_user_has_can_manage).uuid,
221 manifest_text: manifest_text,
222 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
225 assert_response :success
226 resp = JSON.parse(@response.body)
227 assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
230 test "create with owner_uuid fails on group with only can_read permission" do
231 permit_unsigned_manifests
232 authorize_with :active
233 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
236 owner_uuid: groups(:all_users).uuid,
237 manifest_text: manifest_text,
238 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
244 test "create with owner_uuid fails on group with no permission" do
245 permit_unsigned_manifests
246 authorize_with :active
247 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
250 owner_uuid: groups(:public).uuid,
251 manifest_text: manifest_text,
252 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
258 test "admin create with owner_uuid set to group with no permission" do
259 permit_unsigned_manifests
260 authorize_with :admin
261 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
264 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
265 manifest_text: manifest_text,
266 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
269 assert_response :success
272 test "should create with collection passed as json" do
273 permit_unsigned_manifests
274 authorize_with :active
278 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
279 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
283 assert_response :success
286 test "should fail to create with checksum mismatch" do
287 permit_unsigned_manifests
288 authorize_with :active
292 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
293 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
300 test "collection UUID is normalized when created" do
301 permit_unsigned_manifests
302 authorize_with :active
305 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
306 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
309 assert_response :success
310 assert_not_nil assigns(:object)
311 resp = JSON.parse(@response.body)
312 assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['portable_data_hash']
315 test "get full provenance for baz file" do
316 authorize_with :active
317 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
318 assert_response :success
319 resp = JSON.parse(@response.body)
320 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
321 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
322 assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
323 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
324 assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
327 test "get no provenance for foo file" do
328 # spectator user cannot even see baz collection
329 authorize_with :spectator
330 get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
334 test "get partial provenance for baz file" do
335 # spectator user can see bar->baz job, but not foo->bar job
336 authorize_with :spectator
337 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
338 assert_response :success
339 resp = JSON.parse(@response.body)
340 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
341 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
342 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
343 assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
344 assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
347 test "search collections with 'any' operator" do
348 expect_pdh = collections(:docker_image).portable_data_hash
349 authorize_with :active
351 where: { any: ['contains', expect_pdh[5..25]] }
353 assert_response :success
354 found = assigns(:objects)
355 assert_equal 1, found.count
356 assert_equal expect_pdh, found.first.portable_data_hash
359 [false, true].each do |permit_unsigned|
360 test "create collection with signed manifest, permit_unsigned=#{permit_unsigned}" do
361 permit_unsigned_manifests permit_unsigned
362 authorize_with :active
364 d41d8cd98f00b204e9800998ecf8427e+0
365 acbd18db4cc2f85cedef654fccc4a4d8+3
366 ea10d51bcf88862dbcc36eb292017dfd+45)
368 unsigned_manifest = locators.map { |loc|
369 ". " + loc + " 0:0:foo.txt\n"
371 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
373 unsigned_manifest.length.to_s
375 # Build a manifest with both signed and unsigned locators.
377 key: Rails.configuration.blob_signing_key,
378 api_token: api_token(:active),
380 signed_locators = locators.collect do |x|
381 Blob.sign_locator x, signing_opts
384 # Leave a non-empty blob unsigned.
385 signed_locators[1] = locators[1]
387 # Leave the empty blob unsigned. This should still be allowed.
388 signed_locators[0] = locators[0]
391 ". " + signed_locators[0] + " 0:0:foo.txt\n" +
392 ". " + signed_locators[1] + " 0:0:foo.txt\n" +
393 ". " + signed_locators[2] + " 0:0:foo.txt\n"
397 manifest_text: signed_manifest,
398 portable_data_hash: manifest_uuid,
401 assert_response :success
402 assert_not_nil assigns(:object)
403 resp = JSON.parse(@response.body)
404 assert_equal manifest_uuid, resp['portable_data_hash']
405 # All of the locators in the output must be signed.
406 resp['manifest_text'].lines.each do |entry|
407 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
409 assert Blob.verify_signature m[0], signing_opts
415 test "create collection with signed manifest and explicit TTL" do
416 authorize_with :active
418 d41d8cd98f00b204e9800998ecf8427e+0
419 acbd18db4cc2f85cedef654fccc4a4d8+3
420 ea10d51bcf88862dbcc36eb292017dfd+45)
422 unsigned_manifest = locators.map { |loc|
423 ". " + loc + " 0:0:foo.txt\n"
425 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
427 unsigned_manifest.length.to_s
429 # build a manifest with both signed and unsigned locators.
430 # TODO(twp): in phase 4, all locators will need to be signed, so
431 # this test should break and will need to be rewritten. Issue #2755.
433 key: Rails.configuration.blob_signing_key,
434 api_token: api_token(:active),
438 ". " + locators[0] + " 0:0:foo.txt\n" +
439 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
440 ". " + Blob.sign_locator(locators[2], signing_opts) + " 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
461 test "create fails with invalid signature" do
462 authorize_with :active
464 key: Rails.configuration.blob_signing_key,
465 api_token: api_token(:active),
468 # Generate a locator with a bad signature.
469 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
470 bad_locator = unsigned_locator + "+Affffffff@ffffffff"
471 assert !Blob.verify_signature(bad_locator, signing_opts)
473 # Creating a collection with this locator should
474 # produce 403 Permission denied.
475 unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
476 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
478 unsigned_manifest.length.to_s
480 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
483 manifest_text: bad_manifest,
484 portable_data_hash: manifest_uuid
491 test "create fails with uuid of signed manifest" do
492 authorize_with :active
494 key: Rails.configuration.blob_signing_key,
495 api_token: api_token(:active),
498 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
499 signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
500 signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
501 manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
503 signed_manifest.length.to_s
507 manifest_text: signed_manifest,
508 portable_data_hash: manifest_uuid
515 test "multiple locators per line" do
516 permit_unsigned_manifests
517 authorize_with :active
519 d41d8cd98f00b204e9800998ecf8427e+0
520 acbd18db4cc2f85cedef654fccc4a4d8+3
521 ea10d51bcf88862dbcc36eb292017dfd+45)
523 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
524 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
526 manifest_text.length.to_s
529 manifest_text: manifest_text,
530 portable_data_hash: manifest_uuid,
532 post_collection = Marshal.load(Marshal.dump(test_collection))
534 collection: post_collection
536 assert_response :success
537 assert_not_nil assigns(:object)
538 resp = JSON.parse(@response.body)
539 assert_equal manifest_uuid, resp['portable_data_hash']
541 # The manifest in the response will have had permission hints added.
542 # Remove any permission hints in the response before comparing it to the source.
543 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
544 assert_equal manifest_text, stripped_manifest
547 test "multiple signed locators per line" do
548 permit_unsigned_manifests
549 authorize_with :active
551 d41d8cd98f00b204e9800998ecf8427e+0
552 acbd18db4cc2f85cedef654fccc4a4d8+3
553 ea10d51bcf88862dbcc36eb292017dfd+45)
556 key: Rails.configuration.blob_signing_key,
557 api_token: api_token(:active),
560 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
561 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
563 unsigned_manifest.length.to_s
565 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
566 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
570 manifest_text: signed_manifest,
571 portable_data_hash: manifest_uuid,
574 assert_response :success
575 assert_not_nil assigns(:object)
576 resp = JSON.parse(@response.body)
577 assert_equal manifest_uuid, resp['portable_data_hash']
578 # All of the locators in the output must be signed.
579 # Each line is of the form "path locator locator ... 0:0:file.txt"
580 # entry.split[1..-2] will yield just the tokens in the middle of the line
581 returned_locator_count = 0
582 resp['manifest_text'].lines.each do |entry|
583 entry.split[1..-2].each do |tok|
584 returned_locator_count += 1
585 assert Blob.verify_signature tok, signing_opts
588 assert_equal locators.count, returned_locator_count
591 test 'Reject manifest with unsigned blob' do
592 permit_unsigned_manifests false
593 authorize_with :active
594 unsigned_manifest = ". 0cc175b9c0f1b6a831c399e269772661+1 0:1:a.txt\n"
595 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest)
598 manifest_text: unsigned_manifest,
599 portable_data_hash: manifest_uuid,
603 "Creating a collection with unsigned blobs should respond 403"
604 assert_empty Collection.where('uuid like ?', manifest_uuid+'%'),
605 "Collection should not exist in database after failed create"
608 test 'List expired collection returns empty list' do
609 authorize_with :active
611 where: {name: 'expired_collection'},
613 assert_response :success
614 found = assigns(:objects)
615 assert_equal 0, found.count
618 test 'Show expired collection returns 404' do
619 authorize_with :active
621 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
626 test 'Update expired collection returns 404' do
627 authorize_with :active
629 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
631 name: "still expired"
637 test 'List collection with future expiration time succeeds' do
638 authorize_with :active
640 where: {name: 'collection_expires_in_future'},
642 found = assigns(:objects)
643 assert_equal 1, found.count
647 test 'Show collection with future expiration time succeeds' do
648 authorize_with :active
650 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
652 assert_response :success
655 test 'Update collection with future expiration time succeeds' do
656 authorize_with :active
658 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
660 name: "still not expired"
663 assert_response :success
666 test "get collection and verify that file_names is not included" do
667 authorize_with :active
668 get :show, {id: collections(:foo_file).uuid}
669 assert_response :success
670 assert_equal collections(:foo_file).uuid, json_response['uuid']
671 assert_nil json_response['file_names']
672 assert json_response['manifest_text']
678 ].each do |description_size, expected_response|
679 test "create collection with description size #{description_size}
680 and expect response #{expected_response}" do
681 skip "(Descriptions are not part of search indexes. Skip until full-text search
682 is implemented, at which point replace with a search in description.)"
684 authorize_with :active
686 description = 'here is a collection with a very large description'
687 while description.length < description_size
688 description = description + description
691 post :create, collection: {
692 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt\n",
693 description: description,
696 assert_response expected_response
700 [1, 5, nil].each do |ask|
701 test "Set replication_desired=#{ask.inspect}" do
702 Rails.configuration.default_collection_replication = 2
703 authorize_with :active
705 id: collections(:replication_undesired_unconfirmed).uuid,
707 replication_desired: ask,
710 assert_response :success
711 assert_equal ask, json_response['replication_desired']
715 test "get collection with properties" do
716 authorize_with :active
717 get :show, {id: collections(:collection_with_one_property).uuid}
718 assert_response :success
719 assert_not_nil json_response['uuid']
720 assert_equal 'value1', json_response['properties']['property1']
723 test "create collection with properties" do
724 authorize_with :active
725 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
728 manifest_text: manifest_text,
729 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
730 properties: {'property_1' => 'value_1'}
733 assert_response :success
734 assert_not_nil json_response['uuid']
735 assert_equal 'value_1', json_response['properties']['property_1']