3 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
6 # Unless otherwise specified in the test, we want normal/secure behavior.
7 permit_unsigned_manifests false
11 # Reset to secure behavior after each test.
12 permit_unsigned_manifests false
15 def permit_unsigned_manifests isok=true
16 # Set security model for the life of a test.
17 Rails.configuration.permit_create_collection_with_unsigned_manifest = isok
20 test "should get index" do
21 authorize_with :active
23 assert_response :success
24 assert(assigns(:objects).andand.any?, "no Collections returned in index")
25 refute(json_response["items"].any? { |c| c.has_key?("manifest_text") },
26 "basic Collections index included manifest_text")
29 test "index with manifest_text selected returns signed locators" do
30 columns = %w(uuid owner_uuid manifest_text)
31 authorize_with :active
32 get :index, select: columns
33 assert_response :success
34 assert(assigns(:objects).andand.any?,
35 "no Collections returned for index with columns selected")
36 json_response["items"].each do |coll|
37 assert_equal(columns, columns & coll.keys,
38 "Collections index did not respect selected columns")
39 loc_regexp = / [[:xdigit:]]{32}\+\d+\S+/
41 while match = loc_regexp.match(coll["manifest_text"], pos)
42 assert_match(/\+A[[:xdigit:]]+@[[:xdigit:]]{8}\b/, match.to_s,
43 "Locator in manifest_text was not signed")
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 "get index with limit=2 offset=99999" do
72 # Assume there are not that many test fixtures.
73 authorize_with :active
74 get :index, limit: 2, offset: 99999
75 assert_response :success
76 assert_equal 0, assigns(:objects).count
77 resp = JSON.parse(@response.body)
78 assert_equal 2, resp['limit']
79 assert_equal 99999, resp['offset']
82 test "admin can create collection with unsigned manifest" do
86 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
87 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
88 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
89 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
92 test_collection[:portable_data_hash] =
93 Digest::MD5.hexdigest(test_collection[:manifest_text]) +
95 test_collection[:manifest_text].length.to_s
97 # post :create will modify test_collection in place, so we save a copy first.
98 # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
99 post_collection = Marshal.load(Marshal.dump(test_collection))
101 collection: post_collection
104 assert_response :success
105 assert_nil assigns(:objects)
107 response_collection = assigns(:object)
109 stored_collection = Collection.select([:uuid, :portable_data_hash, :manifest_text]).
110 where(portable_data_hash: response_collection['portable_data_hash']).first
112 assert_equal test_collection[:portable_data_hash], stored_collection['portable_data_hash']
114 # The manifest in the response will have had permission hints added.
115 # Remove any permission hints in the response before comparing it to the source.
116 stripped_manifest = stored_collection['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
117 assert_equal test_collection[:manifest_text], stripped_manifest
119 # TBD: create action should add permission signatures to manifest_text in the response,
120 # and we need to check those permission signatures here.
123 [:admin, :active].each do |user|
124 test "#{user} can get collection using portable data hash" do
127 foo_collection = collections(:foo_file)
129 # Get foo_file using it's portable data has
131 id: foo_collection[:portable_data_hash]
133 assert_response :success
134 assert_not_nil assigns(:object)
135 resp = assigns(:object)
136 assert_equal foo_collection[:portable_data_hash], resp['portable_data_hash']
138 # The manifest in the response will have had permission hints added.
139 # Remove any permission hints in the response before comparing it to the source.
140 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
141 assert_equal foo_collection[:manifest_text], stripped_manifest
145 test "create with owner_uuid set to owned group" do
146 permit_unsigned_manifests
147 authorize_with :active
148 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
151 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
152 manifest_text: manifest_text,
153 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
156 assert_response :success
157 resp = JSON.parse(@response.body)
158 assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
161 test "create fails with duplicate name" do
162 permit_unsigned_manifests
163 authorize_with :admin
164 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
167 owner_uuid: 'zzzzz-tpzed-000000000000000',
168 manifest_text: manifest_text,
169 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
176 test "create succeeds with with duplicate name with ensure_unique_name" do
177 permit_unsigned_manifests
178 authorize_with :admin
179 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
182 owner_uuid: 'zzzzz-tpzed-000000000000000',
183 manifest_text: manifest_text,
184 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
187 ensure_unique_name: true
189 assert_response :success
190 resp = JSON.parse(@response.body)
191 assert_equal 'foo_file (2)', resp['name']
194 test "create with owner_uuid set to group i can_manage" do
195 permit_unsigned_manifests
196 authorize_with :active
197 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
200 owner_uuid: groups(:active_user_has_can_manage).uuid,
201 manifest_text: manifest_text,
202 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
205 assert_response :success
206 resp = JSON.parse(@response.body)
207 assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
210 test "create with owner_uuid fails on group with only can_read permission" do
211 permit_unsigned_manifests
212 authorize_with :active
213 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
216 owner_uuid: groups(:all_users).uuid,
217 manifest_text: manifest_text,
218 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
224 test "create with owner_uuid fails on group with no permission" do
225 permit_unsigned_manifests
226 authorize_with :active
227 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
230 owner_uuid: groups(:public).uuid,
231 manifest_text: manifest_text,
232 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
238 test "admin create with owner_uuid set to group with no permission" do
239 permit_unsigned_manifests
240 authorize_with :admin
241 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
244 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
245 manifest_text: manifest_text,
246 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
249 assert_response :success
252 test "should create with collection passed as json" do
253 permit_unsigned_manifests
254 authorize_with :active
258 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
259 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
263 assert_response :success
266 test "should fail to create with checksum mismatch" do
267 permit_unsigned_manifests
268 authorize_with :active
272 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
273 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
280 test "collection UUID is normalized when created" do
281 permit_unsigned_manifests
282 authorize_with :active
285 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
286 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
289 assert_response :success
290 assert_not_nil assigns(:object)
291 resp = JSON.parse(@response.body)
292 assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['portable_data_hash']
295 test "get full provenance for baz file" do
296 authorize_with :active
297 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
298 assert_response :success
299 resp = JSON.parse(@response.body)
300 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
301 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
302 assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
303 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
304 assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
307 test "get no provenance for foo file" do
308 # spectator user cannot even see baz collection
309 authorize_with :spectator
310 get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
314 test "get partial provenance for baz file" do
315 # spectator user can see bar->baz job, but not foo->bar job
316 authorize_with :spectator
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['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
323 assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
324 assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
327 test "search collections with 'any' operator" do
328 authorize_with :active
330 where: { any: ['contains', '7f9102c395f4ffc5e3'] }
332 assert_response :success
333 found = assigns(:objects).collect(&:portable_data_hash)
334 assert_equal 2, found.count
335 assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
338 [false, true].each do |permit_unsigned|
339 test "create collection with signed manifest, permit_unsigned=#{permit_unsigned}" do
340 permit_unsigned_manifests permit_unsigned
341 authorize_with :active
343 d41d8cd98f00b204e9800998ecf8427e+0
344 acbd18db4cc2f85cedef654fccc4a4d8+3
345 ea10d51bcf88862dbcc36eb292017dfd+45)
347 unsigned_manifest = locators.map { |loc|
348 ". " + loc + " 0:0:foo.txt\n"
350 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
352 unsigned_manifest.length.to_s
354 # Build a manifest with both signed and unsigned locators.
356 key: Rails.configuration.blob_signing_key,
357 api_token: api_token(:active),
359 signed_locators = locators.collect do |x|
360 Blob.sign_locator x, signing_opts
363 # Leave a non-empty blob unsigned.
364 signed_locators[1] = locators[1]
366 # Leave the empty blob unsigned. This should still be allowed.
367 signed_locators[0] = locators[0]
370 ". " + signed_locators[0] + " 0:0:foo.txt\n" +
371 ". " + signed_locators[1] + " 0:0:foo.txt\n" +
372 ". " + signed_locators[2] + " 0:0:foo.txt\n"
376 manifest_text: signed_manifest,
377 portable_data_hash: manifest_uuid,
380 assert_response :success
381 assert_not_nil assigns(:object)
382 resp = JSON.parse(@response.body)
383 assert_equal manifest_uuid, resp['portable_data_hash']
384 # All of the locators in the output must be signed.
385 resp['manifest_text'].lines.each do |entry|
386 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
388 assert Blob.verify_signature m[0], signing_opts
394 test "create collection with signed manifest and explicit TTL" do
395 authorize_with :active
397 d41d8cd98f00b204e9800998ecf8427e+0
398 acbd18db4cc2f85cedef654fccc4a4d8+3
399 ea10d51bcf88862dbcc36eb292017dfd+45)
401 unsigned_manifest = locators.map { |loc|
402 ". " + loc + " 0:0:foo.txt\n"
404 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
406 unsigned_manifest.length.to_s
408 # build a manifest with both signed and unsigned locators.
409 # TODO(twp): in phase 4, all locators will need to be signed, so
410 # this test should break and will need to be rewritten. Issue #2755.
412 key: Rails.configuration.blob_signing_key,
413 api_token: api_token(:active),
417 ". " + locators[0] + " 0:0:foo.txt\n" +
418 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
419 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
423 manifest_text: signed_manifest,
424 portable_data_hash: manifest_uuid,
427 assert_response :success
428 assert_not_nil assigns(:object)
429 resp = JSON.parse(@response.body)
430 assert_equal manifest_uuid, resp['portable_data_hash']
431 # All of the locators in the output must be signed.
432 resp['manifest_text'].lines.each do |entry|
433 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
435 assert Blob.verify_signature m[0], signing_opts
440 test "create fails with invalid signature" do
441 authorize_with :active
443 key: Rails.configuration.blob_signing_key,
444 api_token: api_token(:active),
447 # Generate a locator with a bad signature.
448 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
449 bad_locator = unsigned_locator + "+Affffffff@ffffffff"
450 assert !Blob.verify_signature(bad_locator, signing_opts)
452 # Creating a collection with this locator should
453 # produce 403 Permission denied.
454 unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
455 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
457 unsigned_manifest.length.to_s
459 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
462 manifest_text: bad_manifest,
463 portable_data_hash: manifest_uuid
470 test "create fails with uuid of signed manifest" do
471 authorize_with :active
473 key: Rails.configuration.blob_signing_key,
474 api_token: api_token(:active),
477 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
478 signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
479 signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
480 manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
482 signed_manifest.length.to_s
486 manifest_text: signed_manifest,
487 portable_data_hash: manifest_uuid
494 test "multiple locators per line" do
495 permit_unsigned_manifests
496 authorize_with :active
498 d41d8cd98f00b204e9800998ecf8427e+0
499 acbd18db4cc2f85cedef654fccc4a4d8+3
500 ea10d51bcf88862dbcc36eb292017dfd+45)
502 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
503 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
505 manifest_text.length.to_s
508 manifest_text: manifest_text,
509 portable_data_hash: manifest_uuid,
511 post_collection = Marshal.load(Marshal.dump(test_collection))
513 collection: post_collection
515 assert_response :success
516 assert_not_nil assigns(:object)
517 resp = JSON.parse(@response.body)
518 assert_equal manifest_uuid, resp['portable_data_hash']
520 # The manifest in the response will have had permission hints added.
521 # Remove any permission hints in the response before comparing it to the source.
522 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
523 assert_equal manifest_text, stripped_manifest
526 test "multiple signed locators per line" do
527 permit_unsigned_manifests
528 authorize_with :active
530 d41d8cd98f00b204e9800998ecf8427e+0
531 acbd18db4cc2f85cedef654fccc4a4d8+3
532 ea10d51bcf88862dbcc36eb292017dfd+45)
535 key: Rails.configuration.blob_signing_key,
536 api_token: api_token(:active),
539 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
540 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
542 unsigned_manifest.length.to_s
544 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
545 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
549 manifest_text: signed_manifest,
550 portable_data_hash: manifest_uuid,
553 assert_response :success
554 assert_not_nil assigns(:object)
555 resp = JSON.parse(@response.body)
556 assert_equal manifest_uuid, resp['portable_data_hash']
557 # All of the locators in the output must be signed.
558 # Each line is of the form "path locator locator ... 0:0:file.txt"
559 # entry.split[1..-2] will yield just the tokens in the middle of the line
560 returned_locator_count = 0
561 resp['manifest_text'].lines.each do |entry|
562 entry.split[1..-2].each do |tok|
563 returned_locator_count += 1
564 assert Blob.verify_signature tok, signing_opts
567 assert_equal locators.count, returned_locator_count
570 test 'Reject manifest with unsigned blob' do
571 authorize_with :active
572 unsigned_manifest = ". 0cc175b9c0f1b6a831c399e269772661+1 0:1:a.txt\n"
573 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest)
576 manifest_text: unsigned_manifest,
577 portable_data_hash: manifest_uuid,
581 "Creating a collection with unsigned blobs should respond 403"
582 assert_empty Collection.where('uuid like ?', manifest_uuid+'%'),
583 "Collection should not exist in database after failed create"
586 test 'List expired collection returns empty list' do
587 authorize_with :active
589 where: {name: 'expired_collection'},
591 assert_response :success
592 found = assigns(:objects)
593 assert_equal 0, found.count
596 test 'Show expired collection returns 404' do
597 authorize_with :active
599 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
604 test 'Update expired collection returns 404' do
605 authorize_with :active
607 id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
609 name: "still expired"
615 test 'List collection with future expiration time succeeds' do
616 authorize_with :active
618 where: {name: 'collection_expires_in_future'},
620 found = assigns(:objects)
621 assert_equal 1, found.count
625 test 'Show collection with future expiration time succeeds' do
626 authorize_with :active
628 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
630 assert_response :success
633 test 'Update collection with future expiration time succeeds' do
634 authorize_with :active
636 id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
638 name: "still not expired"
641 assert_response :success