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 collection: test_collection
62 assert_response :success
63 assert_nil assigns(:objects)
66 id: test_collection[:uuid]
68 assert_response :success
69 assert_not_nil assigns(:object)
70 resp = JSON.parse(@response.body)
71 assert_equal test_collection[:uuid], resp['uuid']
72 assert_equal test_collection[:manifest_text], resp['manifest_text']
73 assert_equal 9, resp['data_size']
74 assert_equal [['.', 'foo.txt', 0],
76 ['./baz', 'bar.txt', 3]], resp['files']
79 test "list of files is correct for empty manifest" do
80 authorize_with :active
83 uuid: "d41d8cd98f00b204e9800998ecf8427e+0"
86 collection: test_collection
88 assert_response :success
91 id: "d41d8cd98f00b204e9800998ecf8427e+0"
93 assert_response :success
94 resp = JSON.parse(@response.body)
95 assert_equal [], resp['files']
98 test "create with owner_uuid set to owned group" do
99 authorize_with :active
100 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
103 owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
104 manifest_text: manifest_text,
105 uuid: "d30fe8ae534397864cb96c544f4cf102"
108 assert_response :success
109 resp = JSON.parse(@response.body)
110 assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
113 test "create with owner_uuid set to group i can_manage" do
114 authorize_with :active
115 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
118 owner_uuid: 'zzzzz-j7d0g-8ulrifv67tve5sx',
119 manifest_text: manifest_text,
120 uuid: "d30fe8ae534397864cb96c544f4cf102"
123 assert_response :success
124 resp = JSON.parse(@response.body)
125 assert_equal 'zzzzz-tpzed-000000000000000', resp['owner_uuid']
128 test "create with owner_uuid set to group with no can_manage permission" do
129 authorize_with :active
130 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
133 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
134 manifest_text: manifest_text,
135 uuid: "d30fe8ae534397864cb96c544f4cf102"
141 test "admin create with owner_uuid set to group with no permission" do
142 authorize_with :admin
143 manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
146 owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
147 manifest_text: manifest_text,
148 uuid: "d30fe8ae534397864cb96c544f4cf102"
151 assert_response :success
154 test "should create with collection passed as json" do
155 authorize_with :active
159 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
160 "uuid":"d30fe8ae534397864cb96c544f4cf102"\
164 assert_response :success
167 test "should fail to create with checksum mismatch" do
168 authorize_with :active
172 "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
173 "uuid":"d30fe8ae534397864cb96c544f4cf102"\
180 test "collection UUID is normalized when created" do
181 authorize_with :active
184 manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
185 uuid: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
188 assert_response :success
189 assert_not_nil assigns(:object)
190 resp = JSON.parse(@response.body)
191 assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['uuid']
194 test "get full provenance for baz file" do
195 authorize_with :active
196 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
197 assert_response :success
198 resp = JSON.parse(@response.body)
199 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
200 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
201 assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
202 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
203 assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
206 test "get no provenance for foo file" do
207 # spectator user cannot even see baz collection
208 authorize_with :spectator
209 get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
213 test "get partial provenance for baz file" do
214 # spectator user can see bar->baz job, but not foo->bar job
215 authorize_with :spectator
216 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
217 assert_response :success
218 resp = JSON.parse(@response.body)
219 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
220 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
221 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
222 assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
223 assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
226 test "search collections with 'any' operator" do
227 authorize_with :active
229 where: { any: ['contains', '7f9102c395f4ffc5e3'] }
231 assert_response :success
232 found = assigns(:objects).collect(&:uuid)
233 assert_equal 1, found.count
234 assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
237 test "create collection with signed manifest" do
238 authorize_with :active
240 d41d8cd98f00b204e9800998ecf8427e+0
241 acbd18db4cc2f85cedef654fccc4a4d8+3
242 ea10d51bcf88862dbcc36eb292017dfd+45)
244 unsigned_manifest = locators.map { |loc|
245 ". " + loc + " 0:0:foo.txt\n"
247 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
249 unsigned_manifest.length.to_s
251 # build a manifest with both signed and unsigned locators.
252 # TODO(twp): in phase 4, all locators will need to be signed, so
253 # this test should break and will need to be rewritten. Issue #2755.
255 key: Rails.configuration.blob_signing_key,
256 api_token: api_token(:active),
259 ". " + locators[0] + " 0:0:foo.txt\n" +
260 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
261 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
265 manifest_text: signed_manifest,
269 assert_response :success
270 assert_not_nil assigns(:object)
271 resp = JSON.parse(@response.body)
272 assert_equal manifest_uuid, resp['uuid']
273 assert_equal 48, resp['data_size']
274 # All of the locators in the output must be signed.
275 resp['manifest_text'].lines.each do |entry|
276 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
278 assert Blob.verify_signature m[0], signing_opts
283 test "create collection with signed manifest and explicit TTL" do
284 authorize_with :active
286 d41d8cd98f00b204e9800998ecf8427e+0
287 acbd18db4cc2f85cedef654fccc4a4d8+3
288 ea10d51bcf88862dbcc36eb292017dfd+45)
290 unsigned_manifest = locators.map { |loc|
291 ". " + loc + " 0:0:foo.txt\n"
293 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
295 unsigned_manifest.length.to_s
297 # build a manifest with both signed and unsigned locators.
298 # TODO(twp): in phase 4, all locators will need to be signed, so
299 # this test should break and will need to be rewritten. Issue #2755.
301 key: Rails.configuration.blob_signing_key,
302 api_token: api_token(:active),
306 ". " + locators[0] + " 0:0:foo.txt\n" +
307 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
308 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
312 manifest_text: signed_manifest,
316 assert_response :success
317 assert_not_nil assigns(:object)
318 resp = JSON.parse(@response.body)
319 assert_equal manifest_uuid, resp['uuid']
320 assert_equal 48, resp['data_size']
321 # All of the locators in the output must be signed.
322 resp['manifest_text'].lines.each do |entry|
323 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
325 assert Blob.verify_signature m[0], signing_opts
330 test "create fails with invalid signature" do
331 authorize_with :active
333 key: Rails.configuration.blob_signing_key,
334 api_token: api_token(:active),
337 # Generate a locator with a bad signature.
338 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
339 bad_locator = unsigned_locator + "+Affffffff@ffffffff"
340 assert !Blob.verify_signature(bad_locator, signing_opts)
342 # Creating a collection with this locator should
343 # produce 403 Permission denied.
344 unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
345 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
347 unsigned_manifest.length.to_s
349 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
352 manifest_text: bad_manifest,
360 test "create fails with uuid of signed manifest" do
361 authorize_with :active
363 key: Rails.configuration.blob_signing_key,
364 api_token: api_token(:active),
367 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
368 signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
369 signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
370 manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
372 signed_manifest.length.to_s
376 manifest_text: signed_manifest,
384 test "multiple locators per line" do
385 authorize_with :active
387 d41d8cd98f00b204e9800998ecf8427e+0
388 acbd18db4cc2f85cedef654fccc4a4d8+3
389 ea10d51bcf88862dbcc36eb292017dfd+45)
391 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
392 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
394 manifest_text.length.to_s
398 manifest_text: manifest_text,
402 assert_response :success
403 assert_not_nil assigns(:object)
404 resp = JSON.parse(@response.body)
405 assert_equal manifest_uuid, resp['uuid']
406 assert_equal 48, resp['data_size']
407 assert_equal resp['manifest_text'], manifest_text
410 test "multiple signed locators per line" do
411 authorize_with :active
413 d41d8cd98f00b204e9800998ecf8427e+0
414 acbd18db4cc2f85cedef654fccc4a4d8+3
415 ea10d51bcf88862dbcc36eb292017dfd+45)
418 key: Rails.configuration.blob_signing_key,
419 api_token: api_token(:active),
422 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
423 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
425 unsigned_manifest.length.to_s
427 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
428 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
432 manifest_text: signed_manifest,
436 assert_response :success
437 assert_not_nil assigns(:object)
438 resp = JSON.parse(@response.body)
439 assert_equal manifest_uuid, resp['uuid']
440 assert_equal 48, resp['data_size']
441 # All of the locators in the output must be signed.
442 # Each line is of the form "path locator locator ... 0:0:file.txt"
443 # entry.split[1..-2] will yield just the tokens in the middle of the line
444 returned_locator_count = 0
445 resp['manifest_text'].lines.each do |entry|
446 entry.split[1..-2].each do |tok|
447 returned_locator_count += 1
448 assert Blob.verify_signature tok, signing_opts
451 assert_equal locators.count, returned_locator_count