3 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
5 test "should get index" do
8 assert_response :success
9 assert_not_nil assigns(:objects)
12 [0,1,2].each do |limit|
13 test "get index with limit=#{limit}" do
14 authorize_with :active
15 get :index, limit: limit
16 assert_response :success
17 assert_equal limit, assigns(:objects).count
18 resp = JSON.parse(@response.body)
19 assert_equal limit, resp['limit']
23 test "items.count == items_available" do
24 authorize_with :active
25 get :index, limit: 100000
26 assert_response :success
27 resp = JSON.parse(@response.body)
28 assert_equal resp['items_available'], assigns(:objects).length
29 assert_equal resp['items_available'], resp['items'].count
30 unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq
31 assert_equal unique_uuids.count, resp['items'].count
34 test "get index with limit=2 offset=99999" do
35 # Assume there are not that many test fixtures.
36 authorize_with :active
37 get :index, limit: 2, offset: 99999
38 assert_response :success
39 assert_equal 0, assigns(:objects).count
40 resp = JSON.parse(@response.body)
41 assert_equal 2, resp['limit']
42 assert_equal 99999, resp['offset']
45 test "should create" do
46 authorize_with :active
49 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
50 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
51 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
52 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
55 test_collection[:uuid] =
56 Digest::MD5.hexdigest(test_collection[:manifest_text]) +
58 test_collection[:manifest_text].length.to_s
60 # post :create will modify test_collection in place, so we save a copy first.
61 # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
62 post_collection = Marshal.load(Marshal.dump(test_collection))
64 collection: post_collection
67 assert_response :success
68 assert_nil assigns(:objects)
71 id: test_collection[:uuid]
73 assert_response :success
74 assert_not_nil assigns(:object)
75 resp = JSON.parse(@response.body)
76 assert_equal test_collection[:uuid], resp['uuid']
78 # The manifest in the response will have had permission hints added.
79 # Remove any permission hints in the response before comparing it to the source.
80 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
81 assert_equal test_collection[:manifest_text], stripped_manifest
82 assert_equal 9, resp['data_size']
83 assert_equal [['.', 'foo.txt', 0],
85 ['./baz', 'bar.txt', 3]], resp['files']
88 test "list of files is correct for empty manifest" do
89 authorize_with :active
92 uuid: "d41d8cd98f00b204e9800998ecf8427e+0"
95 collection: test_collection
97 assert_response :success
100 id: "d41d8cd98f00b204e9800998ecf8427e+0"
102 assert_response :success
103 resp = JSON.parse(@response.body)
104 assert_equal [], resp['files']
107 test "create with owner_uuid set to owned group" do
108 authorize_with :active
109 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
112 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
113 manifest_text: manifest_text,
114 uuid: "d30fe8ae534397864cb96c544f4cf102"
117 assert_response :success
118 resp = JSON.parse(@response.body)
119 assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
122 test "create with owner_uuid set to group i can_manage" do
123 authorize_with :active
124 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
127 owner_uuid: 'zzzzz-j7d0g-8ulrifv67tve5sx',
128 manifest_text: manifest_text,
129 uuid: "d30fe8ae534397864cb96c544f4cf102"
132 assert_response :success
133 resp = JSON.parse(@response.body)
134 assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
137 test "create with owner_uuid set to group with no can_manage permission" do
138 authorize_with :active
139 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
142 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
143 manifest_text: manifest_text,
144 uuid: "d30fe8ae534397864cb96c544f4cf102"
150 test "admin create with owner_uuid set to group with no permission" do
151 authorize_with :admin
152 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
155 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
156 manifest_text: manifest_text,
157 uuid: "d30fe8ae534397864cb96c544f4cf102"
160 assert_response :success
163 test "should create with collection passed as json" do
164 authorize_with :active
168 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
169 "uuid":"d30fe8ae534397864cb96c544f4cf102"\
173 assert_response :success
176 test "should fail to create with checksum mismatch" do
177 authorize_with :active
181 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
182 "uuid":"d30fe8ae534397864cb96c544f4cf102"\
189 test "collection UUID is normalized when created" do
190 authorize_with :active
193 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
194 uuid: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
197 assert_response :success
198 assert_not_nil assigns(:object)
199 resp = JSON.parse(@response.body)
200 assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['uuid']
203 test "get full provenance for baz file" do
204 authorize_with :active
205 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
206 assert_response :success
207 resp = JSON.parse(@response.body)
208 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
209 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
210 assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
211 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
212 assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
215 test "get no provenance for foo file" do
216 # spectator user cannot even see baz collection
217 authorize_with :spectator
218 get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
222 test "get partial provenance for baz file" do
223 # spectator user can see bar->baz job, but not foo->bar job
224 authorize_with :spectator
225 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
226 assert_response :success
227 resp = JSON.parse(@response.body)
228 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
229 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
230 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
231 assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
232 assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
235 test "search collections with 'any' operator" do
236 authorize_with :active
238 where: { any: ['contains', '7f9102c395f4ffc5e3'] }
240 assert_response :success
241 found = assigns(:objects).collect(&:uuid)
242 assert_equal 1, found.count
243 assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
246 test "create collection with signed manifest" do
247 authorize_with :active
249 d41d8cd98f00b204e9800998ecf8427e+0
250 acbd18db4cc2f85cedef654fccc4a4d8+3
251 ea10d51bcf88862dbcc36eb292017dfd+45)
253 unsigned_manifest = locators.map { |loc|
254 ". " + loc + " 0:0:foo.txt\n"
256 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
258 unsigned_manifest.length.to_s
260 # build a manifest with both signed and unsigned locators.
261 # TODO(twp): in phase 4, all locators will need to be signed, so
262 # this test should break and will need to be rewritten. Issue #2755.
264 key: Rails.configuration.blob_signing_key,
265 api_token: api_token(:active),
268 ". " + locators[0] + " 0:0:foo.txt\n" +
269 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
270 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
274 manifest_text: signed_manifest,
278 assert_response :success
279 assert_not_nil assigns(:object)
280 resp = JSON.parse(@response.body)
281 assert_equal manifest_uuid, resp['uuid']
282 assert_equal 48, resp['data_size']
283 # All of the locators in the output must be signed.
284 resp['manifest_text'].lines.each do |entry|
285 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
287 assert Blob.verify_signature m[0], signing_opts
292 test "create collection with signed manifest and explicit TTL" do
293 authorize_with :active
295 d41d8cd98f00b204e9800998ecf8427e+0
296 acbd18db4cc2f85cedef654fccc4a4d8+3
297 ea10d51bcf88862dbcc36eb292017dfd+45)
299 unsigned_manifest = locators.map { |loc|
300 ". " + loc + " 0:0:foo.txt\n"
302 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
304 unsigned_manifest.length.to_s
306 # build a manifest with both signed and unsigned locators.
307 # TODO(twp): in phase 4, all locators will need to be signed, so
308 # this test should break and will need to be rewritten. Issue #2755.
310 key: Rails.configuration.blob_signing_key,
311 api_token: api_token(:active),
315 ". " + locators[0] + " 0:0:foo.txt\n" +
316 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
317 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
321 manifest_text: signed_manifest,
325 assert_response :success
326 assert_not_nil assigns(:object)
327 resp = JSON.parse(@response.body)
328 assert_equal manifest_uuid, resp['uuid']
329 assert_equal 48, resp['data_size']
330 # All of the locators in the output must be signed.
331 resp['manifest_text'].lines.each do |entry|
332 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
334 assert Blob.verify_signature m[0], signing_opts
339 test "create fails with invalid signature" do
340 authorize_with :active
342 key: Rails.configuration.blob_signing_key,
343 api_token: api_token(:active),
346 # Generate a locator with a bad signature.
347 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
348 bad_locator = unsigned_locator + "+Affffffff@ffffffff"
349 assert !Blob.verify_signature(bad_locator, signing_opts)
351 # Creating a collection with this locator should
352 # produce 403 Permission denied.
353 unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
354 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
356 unsigned_manifest.length.to_s
358 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
361 manifest_text: bad_manifest,
369 test "create fails with uuid of signed manifest" do
370 authorize_with :active
372 key: Rails.configuration.blob_signing_key,
373 api_token: api_token(:active),
376 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
377 signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
378 signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
379 manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
381 signed_manifest.length.to_s
385 manifest_text: signed_manifest,
393 test "multiple locators per line" do
394 authorize_with :active
396 d41d8cd98f00b204e9800998ecf8427e+0
397 acbd18db4cc2f85cedef654fccc4a4d8+3
398 ea10d51bcf88862dbcc36eb292017dfd+45)
400 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
401 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
403 manifest_text.length.to_s
406 manifest_text: manifest_text,
409 post_collection = Marshal.load(Marshal.dump(test_collection))
411 collection: post_collection
413 assert_response :success
414 assert_not_nil assigns(:object)
415 resp = JSON.parse(@response.body)
416 assert_equal manifest_uuid, resp['uuid']
417 assert_equal 48, resp['data_size']
419 # The manifest in the response will have had permission hints added.
420 # Remove any permission hints in the response before comparing it to the source.
421 stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
422 assert_equal manifest_text, stripped_manifest
425 test "multiple signed locators per line" do
426 authorize_with :active
428 d41d8cd98f00b204e9800998ecf8427e+0
429 acbd18db4cc2f85cedef654fccc4a4d8+3
430 ea10d51bcf88862dbcc36eb292017dfd+45)
433 key: Rails.configuration.blob_signing_key,
434 api_token: api_token(:active),
437 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
438 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
440 unsigned_manifest.length.to_s
442 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
443 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
447 manifest_text: signed_manifest,
451 assert_response :success
452 assert_not_nil assigns(:object)
453 resp = JSON.parse(@response.body)
454 assert_equal manifest_uuid, resp['uuid']
455 assert_equal 48, resp['data_size']
456 # All of the locators in the output must be signed.
457 # Each line is of the form "path locator locator ... 0:0:file.txt"
458 # entry.split[1..-2] will yield just the tokens in the middle of the line
459 returned_locator_count = 0
460 resp['manifest_text'].lines.each do |entry|
461 entry.split[1..-2].each do |tok|
462 returned_locator_count += 1
463 assert Blob.verify_signature tok, signing_opts
466 assert_equal locators.count, returned_locator_count