1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class CollectionsApiTest < ActionDispatch::IntegrationTest
10 test "should get index" do
11 get "/arvados/v1/collections",
12 params: {:format => :json},
13 headers: auth(:active)
14 assert_response :success
15 assert_equal "arvados#collectionList", json_response['kind']
18 test "get index with filters= (empty string)" do
19 get "/arvados/v1/collections",
20 params: {:format => :json, :filters => ''},
21 headers: auth(:active)
22 assert_response :success
23 assert_equal "arvados#collectionList", json_response['kind']
26 test "get index with invalid filters (array of strings) responds 422" do
27 get "/arvados/v1/collections",
30 :filters => ['uuid', '=', 'ad02e37b6a7f45bbe2ead3c29a109b8a+54'].to_json
32 headers: auth(:active)
34 assert_match(/nvalid element.*not an array/, json_response['errors'].join(' '))
37 test "get index with invalid filters (unsearchable column) responds 422" do
38 get "/arvados/v1/collections",
41 :filters => [['this_column_does_not_exist', '=', 'bogus']].to_json
43 headers: auth(:active)
45 assert_match(/nvalid attribute/, json_response['errors'].join(' '))
48 test "get index with invalid filters (invalid operator) responds 422" do
49 get "/arvados/v1/collections",
52 :filters => [['uuid', ':-(', 'displeased']].to_json
54 headers: auth(:active)
56 assert_match(/nvalid operator/, json_response['errors'].join(' '))
59 test "get index with invalid filters (invalid operand type) responds 422" do
60 get "/arvados/v1/collections",
63 :filters => [['uuid', '=', {foo: 'bar'}]].to_json
65 headers: auth(:active)
67 assert_match(/nvalid operand type/, json_response['errors'].join(' '))
70 test "get index with where= (empty string)" do
71 get "/arvados/v1/collections",
72 params: {:format => :json, :where => ''},
73 headers: auth(:active)
74 assert_response :success
75 assert_equal "arvados#collectionList", json_response['kind']
78 test "get index with select= (valid attribute)" do
79 get "/arvados/v1/collections",
82 :select => ['portable_data_hash'].to_json
84 headers: auth(:active)
85 assert_response :success
86 assert json_response['items'][0].keys.include?('portable_data_hash')
87 assert not(json_response['items'][0].keys.include?('uuid'))
90 test "get index with select= (invalid attribute) responds 422" do
91 get "/arvados/v1/collections",
94 :select => ['bogus'].to_json
96 headers: auth(:active)
98 assert_match(/Invalid attribute.*bogus/, json_response['errors'].join(' '))
101 test "get index with select= (invalid attribute type) responds 422" do
102 get "/arvados/v1/collections",
105 :select => [['bogus']].to_json
107 headers: auth(:active)
109 assert_match(/Invalid attribute.*bogus/, json_response['errors'].join(' '))
112 test "controller 404 response is json" do
113 get "/arvados/v1/thingsthatdonotexist",
114 params: {:format => :xml},
115 headers: auth(:active)
117 assert_equal 1, json_response['errors'].length
118 assert_equal true, json_response['errors'][0].is_a?(String)
121 test "object 404 response is json" do
122 get "/arvados/v1/groups/zzzzz-j7d0g-o5ba971173cup4f",
124 headers: auth(:active)
126 assert_equal 1, json_response['errors'].length
127 assert_equal true, json_response['errors'][0].is_a?(String)
130 test "store collection as json" do
132 key: Rails.configuration.Collections.BlobSigningKey,
133 api_token: api_token(:active),
135 signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
137 post "/arvados/v1/collections",
140 collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\",\"portable_data_hash\":\"ad02e37b6a7f45bbe2ead3c29a109b8a+54\"}"
142 headers: auth(:active)
144 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
147 test "store collection with manifest_text only" do
149 key: Rails.configuration.Collections.BlobSigningKey,
150 api_token: api_token(:active),
152 signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
154 post "/arvados/v1/collections",
157 collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\"}"
159 headers: auth(:active)
161 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
164 test "store collection then update name" do
166 key: Rails.configuration.Collections.BlobSigningKey,
167 api_token: api_token(:active),
169 signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
171 post "/arvados/v1/collections",
174 collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\",\"portable_data_hash\":\"ad02e37b6a7f45bbe2ead3c29a109b8a+54\"}"
176 headers: auth(:active)
178 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
180 put "/arvados/v1/collections/#{json_response['uuid']}",
183 collection: { name: "a name" }
185 headers: auth(:active)
188 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
189 assert_equal 'a name', json_response['name']
191 get "/arvados/v1/collections/#{json_response['uuid']}",
192 params: {format: :json},
193 headers: auth(:active)
196 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
197 assert_equal 'a name', json_response['name']
200 test "update description for a collection, and search for that description" do
201 collection = collections(:multilevel_collection_1)
203 # update collection's description
204 put "/arvados/v1/collections/#{collection['uuid']}",
207 collection: { description: "something specific" }
209 headers: auth(:active)
210 assert_response :success
211 assert_equal 'something specific', json_response['description']
213 # get the collection and verify newly added description
214 get "/arvados/v1/collections/#{collection['uuid']}",
215 params: {format: :json},
216 headers: auth(:active)
218 assert_equal 'something specific', json_response['description']
221 search_using_filter 'specific', 1
222 search_using_filter 'not specific enough', 0
225 test "create collection, update manifest, and search with filename" do
227 signed_manifest = Collection.sign_manifest_only_for_tests(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
228 post "/arvados/v1/collections",
231 collection: {manifest_text: signed_manifest}.to_json,
233 headers: auth(:active)
234 assert_response :success
235 assert_equal true, json_response['manifest_text'].include?('my_test_file.txt')
236 assert_includes json_response['manifest_text'], 'my_test_file.txt'
238 created = json_response
240 # search using the filename
241 search_using_filter 'my_test_file.txt', 1
243 # update the collection's manifest text
244 signed_manifest = Collection.sign_manifest_only_for_tests(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_updated_test_file.txt\n", api_token(:active))
245 put "/arvados/v1/collections/#{created['uuid']}",
248 collection: {manifest_text: signed_manifest}.to_json,
250 headers: auth(:active)
251 assert_response :success
252 assert_equal created['uuid'], json_response['uuid']
253 assert_includes json_response['manifest_text'], 'my_updated_test_file.txt'
254 assert_not_includes json_response['manifest_text'], 'my_test_file.txt'
256 # search using the new filename
257 search_using_filter 'my_updated_test_file.txt', 1
258 search_using_filter 'my_test_file.txt', 0
259 search_using_filter 'there_is_no_such_file.txt', 0
262 def search_using_filter search_filter, expected_items
263 get '/arvados/v1/collections',
264 params: {:filters => [['any', 'ilike', "%#{search_filter}%"]].to_json},
265 headers: auth(:active)
266 assert_response :success
267 response_items = json_response['items']
268 assert_not_nil response_items
269 if expected_items == 0
270 assert_empty response_items
272 refute_empty response_items
273 first_item = response_items.first
274 assert_not_nil first_item
283 ].each do |param, truthiness|
284 test "include_trash=#{param.inspect} param JSON-encoded should be interpreted as include_trash=#{truthiness}" do
285 expired_col = collections(:expired_collection)
286 assert expired_col.is_trashed
288 post "/arvados/v1/collections",
291 :include_trash => param,
292 :filters => [['uuid', '=', expired_col.uuid]].to_json
294 headers: auth(:active)
295 assert_response :success
296 assert_not_nil json_response['items']
297 assert_equal truthiness, json_response['items'].collect {|c| c['uuid']}.include?(expired_col.uuid)
299 post "/arvados/v1/collections/#{expired_col.uuid}",
302 :include_trash => param,
304 headers: auth(:active)
306 assert_response :success
318 ].each do |param, truthiness|
319 test "include_trash=#{param.inspect} param encoding via query string should be interpreted as include_trash=#{truthiness}" do
320 expired_col = collections(:expired_collection)
321 assert expired_col.is_trashed
323 get("/arvados/v1/collections?include_trash=#{param}&filters=#{[['uuid','=',expired_col.uuid]].to_json}",
324 headers: auth(:active))
325 assert_response :success
326 assert_not_nil json_response['items']
327 assert_equal truthiness, json_response['items'].collect {|c| c['uuid']}.include?(expired_col.uuid)
329 get("/arvados/v1/collections/#{expired_col.uuid}?include_trash=#{param}",
330 headers: auth(:active))
332 assert_response :success
344 ].each do |param, truthiness|
345 test "include_trash=#{param.inspect} form-encoded param should be interpreted as include_trash=#{truthiness}" do
346 expired_col = collections(:expired_collection)
347 assert expired_col.is_trashed
350 ['include_trash', param],
351 ['filters', [['uuid','=',expired_col.uuid]].to_json],
354 post "/arvados/v1/collections",
355 params: URI.encode_www_form(params),
357 "Content-type" => "application/x-www-form-urlencoded"
358 }.update(auth(:active))
359 assert_response :success
360 assert_not_nil json_response['items']
361 assert_equal truthiness, json_response['items'].collect {|c| c['uuid']}.include?(expired_col.uuid)
363 post "/arvados/v1/collections/#{expired_col.uuid}",
364 params: URI.encode_www_form([['_method', 'GET'],['include_trash', param]]),
366 "Content-type" => "application/x-www-form-urlencoded"
367 }.update(auth(:active))
369 assert_response :success
376 test "create and get collection with properties" do
377 # create collection to be searched for
378 signed_manifest = Collection.sign_manifest_only_for_tests(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
379 post "/arvados/v1/collections",
382 collection: {manifest_text: signed_manifest}.to_json,
384 headers: auth(:active)
386 assert_not_nil json_response['uuid']
387 assert_not_nil json_response['properties']
388 assert_empty json_response['properties']
390 # update collection's properties
391 put "/arvados/v1/collections/#{json_response['uuid']}",
394 collection: { properties: {'property_1' => 'value_1'} }
396 headers: auth(:active)
397 assert_response :success
398 assert_equal Hash, json_response['properties'].class, 'Collection properties attribute should be of type hash'
399 assert_equal 'value_1', json_response['properties']['property_1']
402 test "create collection and update it with json encoded hash properties" do
403 # create collection to be searched for
404 signed_manifest = Collection.sign_manifest_only_for_tests(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
405 post "/arvados/v1/collections",
408 collection: {manifest_text: signed_manifest}.to_json,
410 headers: auth(:active)
412 assert_not_nil json_response['uuid']
413 assert_not_nil json_response['properties']
414 assert_empty json_response['properties']
416 # update collection's properties
417 put "/arvados/v1/collections/#{json_response['uuid']}",
421 properties: "{\"property_1\":\"value_1\"}"
424 headers: auth(:active)
425 assert_response :success
426 assert_equal Hash, json_response['properties'].class, 'Collection properties attribute should be of type hash'
427 assert_equal 'value_1', json_response['properties']['property_1']
430 test "update collection with versioning enabled and using preserve_version" do
431 Rails.configuration.Collections.CollectionVersioning = true
432 Rails.configuration.Collections.PreserveVersionIfIdle = -1 # Disable auto versioning
434 signed_manifest = Collection.sign_manifest_only_for_tests(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
435 post "/arvados/v1/collections",
439 name: 'Test collection',
440 manifest_text: signed_manifest,
443 headers: auth(:active)
445 assert_not_nil json_response['uuid']
446 assert_equal 1, json_response['version']
447 assert_equal false, json_response['preserve_version']
449 # Versionable update including preserve_version=true should create a new
450 # version that will also be persisted.
451 put "/arvados/v1/collections/#{json_response['uuid']}",
455 name: 'Test collection v2',
456 preserve_version: true,
459 headers: auth(:active)
461 assert_equal 2, json_response['version']
462 assert_equal true, json_response['preserve_version']
464 # 2nd versionable update including preserve_version=true should create a new
465 # version that will also be persisted.
466 put "/arvados/v1/collections/#{json_response['uuid']}",
470 name: 'Test collection v3',
471 preserve_version: true,
474 headers: auth(:active)
476 assert_equal 3, json_response['version']
477 assert_equal true, json_response['preserve_version']
479 # 3rd versionable update without including preserve_version should create a new
480 # version that will have its preserve_version attr reset to false.
481 put "/arvados/v1/collections/#{json_response['uuid']}",
485 name: 'Test collection v4',
488 headers: auth(:active)
490 assert_equal 4, json_response['version']
491 assert_equal false, json_response['preserve_version']
493 # 4th versionable update without including preserve_version=true should NOT
494 # create a new version.
495 put "/arvados/v1/collections/#{json_response['uuid']}",
499 name: 'Test collection v5?',
502 headers: auth(:active)
504 assert_equal 4, json_response['version']
505 assert_equal false, json_response['preserve_version']