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 "get full provenance for baz file" do
181 authorize_with :active
182 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
183 assert_response :success
184 resp = JSON.parse(@response.body)
185 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
186 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
187 assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
188 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
189 assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
192 test "get no provenance for foo file" do
193 # spectator user cannot even see baz collection
194 authorize_with :spectator
195 get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
199 test "get partial provenance for baz file" do
200 # spectator user can see bar->baz job, but not foo->bar job
201 authorize_with :spectator
202 get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
203 assert_response :success
204 resp = JSON.parse(@response.body)
205 assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
206 assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
207 assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
208 assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
209 assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
212 test "search collections with 'any' operator" do
213 authorize_with :active
215 where: { any: ['contains', '7f9102c395f4ffc5e3'] }
217 assert_response :success
218 found = assigns(:objects).collect(&:uuid)
219 assert_equal 1, found.count
220 assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
223 test "create collection with signed manifest" do
224 authorize_with :active
226 d41d8cd98f00b204e9800998ecf8427e+0
227 acbd18db4cc2f85cedef654fccc4a4d8+3
228 ea10d51bcf88862dbcc36eb292017dfd+45)
230 unsigned_manifest = locators.map { |loc|
231 ". " + loc + " 0:0:foo.txt\n"
233 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
235 unsigned_manifest.length.to_s
237 # build a manifest with both signed and unsigned locators.
238 # TODO(twp): in phase 4, all locators will need to be signed, so
239 # this test should break and will need to be rewritten. Issue #2755.
241 key: Rails.configuration.blob_signing_key,
242 api_token: api_token(:active),
245 ". " + locators[0] + " 0:0:foo.txt\n" +
246 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
247 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
251 manifest_text: signed_manifest,
255 assert_response :success
256 assert_not_nil assigns(:object)
257 resp = JSON.parse(@response.body)
258 assert_equal manifest_uuid, resp['uuid']
259 assert_equal 48, resp['data_size']
260 # All of the locators in the output must be signed.
261 resp['manifest_text'].lines.each do |entry|
262 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
264 assert Blob.verify_signature m[0], signing_opts
269 test "create collection with signed manifest and explicit TTL" do
270 authorize_with :active
272 d41d8cd98f00b204e9800998ecf8427e+0
273 acbd18db4cc2f85cedef654fccc4a4d8+3
274 ea10d51bcf88862dbcc36eb292017dfd+45)
276 unsigned_manifest = locators.map { |loc|
277 ". " + loc + " 0:0:foo.txt\n"
279 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
281 unsigned_manifest.length.to_s
283 # build a manifest with both signed and unsigned locators.
284 # TODO(twp): in phase 4, all locators will need to be signed, so
285 # this test should break and will need to be rewritten. Issue #2755.
287 key: Rails.configuration.blob_signing_key,
288 api_token: api_token(:active),
292 ". " + locators[0] + " 0:0:foo.txt\n" +
293 ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
294 ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
298 manifest_text: signed_manifest,
302 assert_response :success
303 assert_not_nil assigns(:object)
304 resp = JSON.parse(@response.body)
305 assert_equal manifest_uuid, resp['uuid']
306 assert_equal 48, resp['data_size']
307 # All of the locators in the output must be signed.
308 resp['manifest_text'].lines.each do |entry|
309 m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
311 assert Blob.verify_signature m[0], signing_opts
316 test "create fails with invalid signature" do
317 authorize_with :active
319 key: Rails.configuration.blob_signing_key,
320 api_token: api_token(:active),
323 # Generate a locator with a bad signature.
324 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
325 bad_locator = unsigned_locator + "+Affffffff@ffffffff"
326 assert !Blob.verify_signature(bad_locator, signing_opts)
328 # Creating a collection with this locator should
329 # produce 403 Permission denied.
330 unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
331 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
333 unsigned_manifest.length.to_s
335 bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
338 manifest_text: bad_manifest,
346 test "create fails with uuid of signed manifest" do
347 authorize_with :active
349 key: Rails.configuration.blob_signing_key,
350 api_token: api_token(:active),
353 unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
354 signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
355 signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
356 manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
358 signed_manifest.length.to_s
362 manifest_text: signed_manifest,
370 test "multiple locators per line" do
371 authorize_with :active
373 d41d8cd98f00b204e9800998ecf8427e+0
374 acbd18db4cc2f85cedef654fccc4a4d8+3
375 ea10d51bcf88862dbcc36eb292017dfd+45)
377 manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
378 manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
380 manifest_text.length.to_s
384 manifest_text: manifest_text,
388 assert_response :success
389 assert_not_nil assigns(:object)
390 resp = JSON.parse(@response.body)
391 assert_equal manifest_uuid, resp['uuid']
392 assert_equal 48, resp['data_size']
393 assert_equal resp['manifest_text'], manifest_text
396 test "multiple signed locators per line" do
397 authorize_with :active
399 d41d8cd98f00b204e9800998ecf8427e+0
400 acbd18db4cc2f85cedef654fccc4a4d8+3
401 ea10d51bcf88862dbcc36eb292017dfd+45)
404 key: Rails.configuration.blob_signing_key,
405 api_token: api_token(:active),
408 unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
409 manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
411 unsigned_manifest.length.to_s
413 signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
414 signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
418 manifest_text: signed_manifest,
422 assert_response :success
423 assert_not_nil assigns(:object)
424 resp = JSON.parse(@response.body)
425 assert_equal manifest_uuid, resp['uuid']
426 assert_equal 48, resp['data_size']
427 # All of the locators in the output must be signed.
428 # Each line is of the form "path locator locator ... 0:0:file.txt"
429 # entry.split[1..-2] will yield just the tokens in the middle of the line
430 returned_locator_count = 0
431 resp['manifest_text'].lines.each do |entry|
432 entry.split[1..-2].each do |tok|
433 returned_locator_count += 1
434 assert Blob.verify_signature tok, signing_opts
437 assert_equal locators.count, returned_locator_count