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 "can get non-database fields via index select" do
30 authorize_with :active
31 get(:index, filters: [["uuid", "=", collections(:foo_file).uuid]],
32 select: %w(uuid owner_uuid files))
33 assert_response :success
34 assert_equal(1, json_response["items"].andand.size,
35 "wrong number of items returned for index")
36 assert_equal([[".", "foo", 3]], json_response["items"].first["files"],
37 "wrong file list in index result")
40 test "can select only non-database fields for index" do
41 authorize_with :active
42 get(:index, select: %w(data_size files))
43 assert_response :success
44 assert(json_response["items"].andand.any?, "no items found in index")
45 json_response["items"].each do |coll|
46 assert_equal(coll["data_size"],
47 coll["files"].inject(0) { |size, fspec| size + fspec.last },
48 "mismatch between data size and file list")
52 test "index with manifest_text selected returns signed locators" do
53 columns = %w(uuid owner_uuid data_size files manifest_text)
54 authorize_with :active
55 get :index, select: columns
56 assert_response :success
57 assert(assigns(:objects).andand.any?,
58 "no Collections returned for index with columns selected")
59 json_response["items"].each do |coll|
60 assert_equal(columns, columns & coll.keys,
61 "Collections index did not respect selected columns")
62 loc_regexp = / [[:xdigit:]]{32}\+\d+\S+/
64 while match = loc_regexp.match(coll["manifest_text"], pos)
65 assert_match(/\+A[[:xdigit:]]+@[[:xdigit:]]{8}\b/, match.to_s,
66 "Locator in manifest_text was not signed")
72 [0,1,2].each do |limit|
73 test "get index with limit=#{limit}" do
74 authorize_with :active
75 get :index, limit: limit
76 assert_response :success
77 assert_equal limit, assigns(:objects).count
78 resp = JSON.parse(@response.body)
79 assert_equal limit, resp['limit']
83 test "items.count == items_available" do
84 authorize_with :active
85 get :index, limit: 100000
86 assert_response :success
87 resp = JSON.parse(@response.body)
88 assert_equal resp['items_available'], assigns(:objects).length
89 assert_equal resp['items_available'], resp['items'].count
90 unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq
91 assert_equal unique_uuids.count, resp['items'].count
94 test "get index with limit=2 offset=99999" do
95 # Assume there are not that many test fixtures.
96 authorize_with :active
97 get :index, limit: 2, offset: 99999
98 assert_response :success
99 assert_equal 0, assigns(:objects).count
100 resp = JSON.parse(@response.body)
101 assert_equal 2, resp['limit']
102 assert_equal 99999, resp['offset']
105 test "create with unsigned manifest" do
106 permit_unsigned_manifests
107 authorize_with :active
109 manifest_text: <<-EOS
110 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
111 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
112 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
113 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
116 test_collection[:portable_data_hash] =
117 Digest::MD5.hexdigest(test_collection[:manifest_text]) +
119 test_collection[:manifest_text].length.to_s
121 # post :create will modify test_collection in place, so we save a copy first.
122 # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
123 post_collection = Marshal.load(Marshal.dump(test_collection))
125 collection: post_collection
128 assert_response :success
129 assert_nil assigns(:objects)
132 id: test_collection[:portable_data_hash]
134 assert_response :success
135 assert_not_nil assigns(:object)
136 resp = JSON.parse(@response.body)
137 assert_equal test_collection[:uuid], resp['uuid']
139 # The manifest in the response will have had permission hints added.
140 # Remove any permission hints in the response before comparing it to the source.
141 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
142 assert_equal test_collection[:manifest_text], stripped_manifest
143 assert_equal 9, resp['data_size']
144 assert_equal [['.', 'foo.txt', 0],
146 ['./baz', 'bar.txt', 3]], resp['files']
149 test "list of files is correct for empty manifest" do
150 authorize_with :active
153 portable_data_hash: "d41d8cd98f00b204e9800998ecf8427e+0"
156 collection: test_collection
158 assert_response :success
161 id: "d41d8cd98f00b204e9800998ecf8427e+0"
163 assert_response :success
164 resp = JSON.parse(@response.body)
165 assert_equal [], resp['files']
168 test "create with owner_uuid set to owned group" do
169 permit_unsigned_manifests
170 authorize_with :active
171 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
174 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
175 manifest_text: manifest_text,
176 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
179 assert_response :success
180 resp = JSON.parse(@response.body)
181 assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
184 test "create fails with duplicate name" do
185 permit_unsigned_manifests
186 authorize_with :admin
187 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
190 owner_uuid: 'zzzzz-tpzed-000000000000000',
191 manifest_text: manifest_text,
192 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
199 test "create with owner_uuid set to group i can_manage" do
200 permit_unsigned_manifests
201 authorize_with :active
202 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
205 owner_uuid: groups(:active_user_has_can_manage).uuid,
206 manifest_text: manifest_text,
207 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
210 assert_response :success
211 resp = JSON.parse(@response.body)
212 assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
215 test "create with owner_uuid fails on group with only can_read permission" do
216 permit_unsigned_manifests
217 authorize_with :active
218 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
221 owner_uuid: groups(:all_users).uuid,
222 manifest_text: manifest_text,
223 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
229 test "create with owner_uuid fails on group with no permission" do
230 permit_unsigned_manifests
231 authorize_with :active
232 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
235 owner_uuid: groups(:public).uuid,
236 manifest_text: manifest_text,
237 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
243 test "admin create with owner_uuid set to group with no permission" do
244 permit_unsigned_manifests
245 authorize_with :admin
246 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
249 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
250 manifest_text: manifest_text,
251 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
254 assert_response :success
257 test "should create with collection passed as json" do
258 permit_unsigned_manifests
259 authorize_with :active
263 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
264 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
268 assert_response :success
271 test "should fail to create with checksum mismatch" do
272 permit_unsigned_manifests
273 authorize_with :active
277 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
278 "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
285 test "collection UUID is normalized when created" do
286 permit_unsigned_manifests
287 authorize_with :active
290 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
291 portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
294 assert_response :success
295 assert_not_nil assigns(:object)
296 resp = JSON.parse(@response.body)
297 assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['portable_data_hash']
300 test "get full provenance for baz file" do
301 authorize_with :active
302 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
303 assert_response :success
304 resp = JSON.parse(@response.body)
305 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
306 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
307 assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
308 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
309 assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
312 test "get no provenance for foo file" do
313 # spectator user cannot even see baz collection
314 authorize_with :spectator
315 get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
319 test "get partial provenance for baz file" do
320 # spectator user can see bar->baz job, but not foo->bar job
321 authorize_with :spectator
322 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
323 assert_response :success
324 resp = JSON.parse(@response.body)
325 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
326 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
327 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
328 assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
329 assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
332 test "search collections with 'any' operator" do
333 authorize_with :active
335 where: { any: ['contains', '7f9102c395f4ffc5e3'] }
337 assert_response :success
338 found = assigns(:objects).collect(&:portable_data_hash)
339 assert_equal 2, found.count
340 assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
343 [false, true].each do |permit_unsigned|
344 test "create collection with signed manifest, permit_unsigned=#{permit_unsigned}" do
345 permit_unsigned_manifests permit_unsigned
346 authorize_with :active
348 d41d8cd98f00b204e9800998ecf8427e+0
349 acbd18db4cc2f85cedef654fccc4a4d8+3
350 ea10d51bcf88862dbcc36eb292017dfd+45)
352 unsigned_manifest = locators.map { |loc|
353 ". " + loc + " 0:0:foo.txt\n"
355 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
357 unsigned_manifest.length.to_s
359 # Build a manifest with both signed and unsigned locators.
361 key: Rails.configuration.blob_signing_key,
362 api_token: api_token(:active),
364 signed_locators = locators.collect do |x|
365 Blob.sign_locator x, signing_opts
368 # Leave a non-empty blob unsigned.
369 signed_locators[1] = locators[1]
371 # Leave the empty blob unsigned. This should still be allowed.
372 signed_locators[0] = locators[0]
375 ". " + signed_locators[0] + " 0:0:foo.txt\n" +
376 ". " + signed_locators[1] + " 0:0:foo.txt\n" +
377 ". " + signed_locators[2] + " 0:0:foo.txt\n"
381 manifest_text: signed_manifest,
382 portable_data_hash: manifest_uuid,
385 assert_response :success
386 assert_not_nil assigns(:object)
387 resp = JSON.parse(@response.body)
388 assert_equal manifest_uuid, resp['portable_data_hash']
389 assert_equal 48, resp['data_size']
390 # All of the locators in the output must be signed.
391 resp['manifest_text'].lines.each do |entry|
392 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
394 assert Blob.verify_signature m[0], signing_opts
400 test "create collection with signed manifest and explicit TTL" do
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.
415 # TODO(twp): in phase 4, all locators will need to be signed, so
416 # this test should break and will need to be rewritten. Issue #2755.
418 key: Rails.configuration.blob_signing_key,
419 api_token: api_token(:active),
423 ". " + locators[0] + " 0:0:foo.txt\n" +
424 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
425 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
429 manifest_text: signed_manifest,
430 portable_data_hash: manifest_uuid,
433 assert_response :success
434 assert_not_nil assigns(:object)
435 resp = JSON.parse(@response.body)
436 assert_equal manifest_uuid, resp['portable_data_hash']
437 assert_equal 48, resp['data_size']
438 # All of the locators in the output must be signed.
439 resp['manifest_text'].lines.each do |entry|
440 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
442 assert Blob.verify_signature m[0], signing_opts
447 test "create fails with invalid signature" do
448 authorize_with :active
450 key: Rails.configuration.blob_signing_key,
451 api_token: api_token(:active),
454 # Generate a locator with a bad signature.
455 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
456 bad_locator = unsigned_locator + "+Affffffff@ffffffff"
457 assert !Blob.verify_signature(bad_locator, signing_opts)
459 # Creating a collection with this locator should
460 # produce 403 Permission denied.
461 unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
462 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
464 unsigned_manifest.length.to_s
466 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
469 manifest_text: bad_manifest,
470 portable_data_hash: manifest_uuid
477 test "create fails with uuid of signed manifest" do
478 authorize_with :active
480 key: Rails.configuration.blob_signing_key,
481 api_token: api_token(:active),
484 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
485 signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
486 signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
487 manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
489 signed_manifest.length.to_s
493 manifest_text: signed_manifest,
494 portable_data_hash: manifest_uuid
501 test "multiple locators per line" do
502 permit_unsigned_manifests
503 authorize_with :active
505 d41d8cd98f00b204e9800998ecf8427e+0
506 acbd18db4cc2f85cedef654fccc4a4d8+3
507 ea10d51bcf88862dbcc36eb292017dfd+45)
509 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
510 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
512 manifest_text.length.to_s
515 manifest_text: manifest_text,
516 portable_data_hash: manifest_uuid,
518 post_collection = Marshal.load(Marshal.dump(test_collection))
520 collection: post_collection
522 assert_response :success
523 assert_not_nil assigns(:object)
524 resp = JSON.parse(@response.body)
525 assert_equal manifest_uuid, resp['portable_data_hash']
526 assert_equal 48, resp['data_size']
528 # The manifest in the response will have had permission hints added.
529 # Remove any permission hints in the response before comparing it to the source.
530 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
531 assert_equal manifest_text, stripped_manifest
534 test "multiple signed locators per line" do
535 permit_unsigned_manifests
536 authorize_with :active
538 d41d8cd98f00b204e9800998ecf8427e+0
539 acbd18db4cc2f85cedef654fccc4a4d8+3
540 ea10d51bcf88862dbcc36eb292017dfd+45)
543 key: Rails.configuration.blob_signing_key,
544 api_token: api_token(:active),
547 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
548 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
550 unsigned_manifest.length.to_s
552 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
553 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
557 manifest_text: signed_manifest,
558 portable_data_hash: manifest_uuid,
561 assert_response :success
562 assert_not_nil assigns(:object)
563 resp = JSON.parse(@response.body)
564 assert_equal manifest_uuid, resp['portable_data_hash']
565 assert_equal 48, resp['data_size']
566 # All of the locators in the output must be signed.
567 # Each line is of the form "path locator locator ... 0:0:file.txt"
568 # entry.split[1..-2] will yield just the tokens in the middle of the line
569 returned_locator_count = 0
570 resp['manifest_text'].lines.each do |entry|
571 entry.split[1..-2].each do |tok|
572 returned_locator_count += 1
573 assert Blob.verify_signature tok, signing_opts
576 assert_equal locators.count, returned_locator_count
579 test 'Reject manifest with unsigned blob' do
580 authorize_with :active
581 unsigned_manifest = ". 0cc175b9c0f1b6a831c399e269772661+1 0:1:a.txt\n"
582 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest)
585 manifest_text: unsigned_manifest,
586 portable_data_hash: manifest_uuid,
590 "Creating a collection with unsigned blobs should respond 403"
591 assert_empty Collection.where('uuid like ?', manifest_uuid+'%'),
592 "Collection should not exist in database after failed create"