17755: Merge branch 'main' into 17755-add-singularity-to-compute-image
[arvados.git] / services / api / test / integration / collections_api_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class CollectionsApiTest < ActionDispatch::IntegrationTest
8   fixtures :all
9
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']
16   end
17
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']
24   end
25
26   test "get index with invalid filters (array of strings) responds 422" do
27     get "/arvados/v1/collections",
28       params: {
29         :format => :json,
30         :filters => ['uuid', '=', 'ad02e37b6a7f45bbe2ead3c29a109b8a+54'].to_json
31       },
32       headers: auth(:active)
33     assert_response 422
34     assert_match(/nvalid element.*not an array/, json_response['errors'].join(' '))
35   end
36
37   test "get index with invalid filters (unsearchable column) responds 422" do
38     get "/arvados/v1/collections",
39       params: {
40         :format => :json,
41         :filters => [['this_column_does_not_exist', '=', 'bogus']].to_json
42       },
43       headers: auth(:active)
44     assert_response 422
45     assert_match(/nvalid attribute/, json_response['errors'].join(' '))
46   end
47
48   test "get index with invalid filters (invalid operator) responds 422" do
49     get "/arvados/v1/collections",
50       params: {
51         :format => :json,
52         :filters => [['uuid', ':-(', 'displeased']].to_json
53       },
54       headers: auth(:active)
55     assert_response 422
56     assert_match(/nvalid operator/, json_response['errors'].join(' '))
57   end
58
59   test "get index with invalid filters (invalid operand type) responds 422" do
60     get "/arvados/v1/collections",
61       params: {
62         :format => :json,
63         :filters => [['uuid', '=', {foo: 'bar'}]].to_json
64       },
65       headers: auth(:active)
66     assert_response 422
67     assert_match(/nvalid operand type/, json_response['errors'].join(' '))
68   end
69
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']
76   end
77
78   test "get index with select= (valid attribute)" do
79     get "/arvados/v1/collections",
80       params: {
81         :format => :json,
82         :select => ['portable_data_hash'].to_json
83       },
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'))
88   end
89
90   test "get index with select= (invalid attribute) responds 422" do
91     get "/arvados/v1/collections",
92       params: {
93         :format => :json,
94         :select => ['bogus'].to_json
95       },
96       headers: auth(:active)
97     assert_response 422
98     assert_match(/Invalid attribute.*bogus/, json_response['errors'].join(' '))
99   end
100
101   test "get index with select= (invalid attribute type) responds 422" do
102     get "/arvados/v1/collections",
103       params: {
104         :format => :json,
105         :select => [['bogus']].to_json
106       },
107       headers: auth(:active)
108     assert_response 422
109     assert_match(/Invalid attribute.*bogus/, json_response['errors'].join(' '))
110   end
111
112   test "controller 404 response is json" do
113     get "/arvados/v1/thingsthatdonotexist",
114       params: {:format => :xml},
115       headers: auth(:active)
116     assert_response 404
117     assert_equal 1, json_response['errors'].length
118     assert_equal true, json_response['errors'][0].is_a?(String)
119   end
120
121   test "object 404 response is json" do
122     get "/arvados/v1/groups/zzzzz-j7d0g-o5ba971173cup4f",
123       params: {},
124       headers: auth(:active)
125     assert_response 404
126     assert_equal 1, json_response['errors'].length
127     assert_equal true, json_response['errors'][0].is_a?(String)
128   end
129
130   test "store collection as json" do
131     signing_opts = {
132       key: Rails.configuration.Collections.BlobSigningKey,
133       api_token: api_token(:active),
134     }
135     signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
136                                        signing_opts)
137     post "/arvados/v1/collections",
138       params: {
139         format: :json,
140         collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\",\"portable_data_hash\":\"ad02e37b6a7f45bbe2ead3c29a109b8a+54\"}"
141       },
142       headers: auth(:active)
143     assert_response 200
144     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
145   end
146
147   test "store collection with manifest_text only" do
148     signing_opts = {
149       key: Rails.configuration.Collections.BlobSigningKey,
150       api_token: api_token(:active),
151     }
152     signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
153                                        signing_opts)
154     post "/arvados/v1/collections",
155       params: {
156         format: :json,
157         collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\"}"
158       },
159       headers: auth(:active)
160     assert_response 200
161     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
162   end
163
164   test "store collection then update name" do
165     signing_opts = {
166       key: Rails.configuration.Collections.BlobSigningKey,
167       api_token: api_token(:active),
168     }
169     signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
170                                        signing_opts)
171     post "/arvados/v1/collections",
172       params: {
173         format: :json,
174         collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\",\"portable_data_hash\":\"ad02e37b6a7f45bbe2ead3c29a109b8a+54\"}"
175       },
176       headers: auth(:active)
177     assert_response 200
178     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
179
180     put "/arvados/v1/collections/#{json_response['uuid']}",
181       params: {
182         format: :json,
183         collection: { name: "a name" }
184       },
185       headers: auth(:active)
186
187     assert_response 200
188     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
189     assert_equal 'a name', json_response['name']
190
191     get "/arvados/v1/collections/#{json_response['uuid']}",
192       params: {format: :json},
193       headers: auth(:active)
194
195     assert_response 200
196     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
197     assert_equal 'a name', json_response['name']
198   end
199
200   test "update description for a collection, and search for that description" do
201     collection = collections(:multilevel_collection_1)
202
203     # update collection's description
204     put "/arvados/v1/collections/#{collection['uuid']}",
205       params: {
206         format: :json,
207         collection: { description: "something specific" }
208       },
209       headers: auth(:active)
210     assert_response :success
211     assert_equal 'something specific', json_response['description']
212
213     # get the collection and verify newly added description
214     get "/arvados/v1/collections/#{collection['uuid']}",
215       params: {format: :json},
216       headers: auth(:active)
217     assert_response 200
218     assert_equal 'something specific', json_response['description']
219
220     # search
221     search_using_filter 'specific', 1
222     search_using_filter 'not specific enough', 0
223   end
224
225   test "create collection, update manifest, and search with filename" do
226     # create collection
227     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
228     post "/arvados/v1/collections",
229       params: {
230         format: :json,
231         collection: {manifest_text: signed_manifest}.to_json,
232       },
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'
237
238     created = json_response
239
240     # search using the filename
241     search_using_filter 'my_test_file.txt', 1
242
243     # update the collection's manifest text
244     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_updated_test_file.txt\n", api_token(:active))
245     put "/arvados/v1/collections/#{created['uuid']}",
246       params: {
247         format: :json,
248         collection: {manifest_text: signed_manifest}.to_json,
249       },
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'
255
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
260   end
261
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
271     else
272       refute_empty response_items
273       first_item = response_items.first
274       assert_not_nil first_item
275     end
276   end
277
278   [
279     ["false", false],
280     ["0", false],
281     ["true", true],
282     ["1", true]
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
287       # Try #index first
288       post "/arvados/v1/collections",
289           params: {
290             :_method => 'GET',
291             :include_trash => param,
292             :filters => [['uuid', '=', expired_col.uuid]].to_json
293           },
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)
298       # Try #show next
299       post "/arvados/v1/collections/#{expired_col.uuid}",
300         params: {
301           :_method => 'GET',
302           :include_trash => param,
303         },
304         headers: auth(:active)
305       if truthiness
306         assert_response :success
307       else
308         assert_response 404
309       end
310     end
311   end
312
313   [
314     ["false", false],
315     ["0", false],
316     ["true", true],
317     ["1", true]
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
322       # Try #index first
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)
328       # Try #show next
329       get("/arvados/v1/collections/#{expired_col.uuid}?include_trash=#{param}",
330         headers: auth(:active))
331       if truthiness
332         assert_response :success
333       else
334         assert_response 404
335       end
336     end
337   end
338
339   [
340     ["false", false],
341     ["0", false],
342     ["true", true],
343     ["1", true]
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
348       params = [
349         ['_method', 'GET'],
350         ['include_trash', param],
351         ['filters', [['uuid','=',expired_col.uuid]].to_json],
352       ]
353       # Try #index first
354       post "/arvados/v1/collections",
355         params: URI.encode_www_form(params),
356         headers: {
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)
362       # Try #show next
363       post "/arvados/v1/collections/#{expired_col.uuid}",
364         params: URI.encode_www_form([['_method', 'GET'],['include_trash', param]]),
365         headers: {
366           "Content-type" => "application/x-www-form-urlencoded"
367         }.update(auth(:active))
368       if truthiness
369         assert_response :success
370       else
371         assert_response 404
372       end
373     end
374   end
375
376   test "search collection using full text search" do
377     # create collection to be searched for
378     signed_manifest = Collection.sign_manifest(". 85877ca2d7e05498dd3d109baf2df106+95+A3a4e26a366ee7e4ed3e476ccf05354761be2e4ae@545a9920 0:95:file_in_subdir1\n./subdir2/subdir3 2bbc341c702df4d8f42ec31f16c10120+64+A315d7e7bad2ce937e711fc454fae2d1194d14d64@545a9920 0:32:file1_in_subdir3.txt 32:32:file2_in_subdir3.txt\n./subdir2/subdir3/subdir4 2bbc341c702df4d8f42ec31f16c10120+64+A315d7e7bad2ce937e711fc454fae2d1194d14d64@545a9920 0:32:file3_in_subdir4.txt 32:32:file4_in_subdir4.txt\n", api_token(:active))
379     post "/arvados/v1/collections",
380       params: {
381         format: :json,
382         collection: {description: 'specific collection description', manifest_text: signed_manifest}.to_json,
383       },
384       headers: auth(:active)
385     assert_response :success
386     assert_equal true, json_response['manifest_text'].include?('file4_in_subdir4.txt')
387
388     # search using the filename
389     search_using_full_text_search 'subdir2', 0
390     search_using_full_text_search 'subdir2:*', 1
391     search_using_full_text_search 'subdir2/subdir3/subdir4', 1
392     search_using_full_text_search 'file4:*', 1
393     search_using_full_text_search 'file4_in_subdir4.txt', 1
394     search_using_full_text_search 'subdir2 file4:*', 0      # first word is incomplete
395     search_using_full_text_search 'subdir2/subdir3/subdir4 file4:*', 1
396     search_using_full_text_search 'subdir2/subdir3/subdir4 file4_in_subdir4.txt', 1
397     search_using_full_text_search 'ile4', 0                 # not a prefix match
398   end
399
400   def search_using_full_text_search search_filter, expected_items
401     get '/arvados/v1/collections',
402       params: {:filters => [['any', '@@', search_filter]].to_json},
403       headers: auth(:active)
404     assert_response :success
405     response_items = json_response['items']
406     assert_not_nil response_items
407     if expected_items == 0
408       assert_empty response_items
409     else
410       refute_empty response_items
411       first_item = response_items.first
412       assert_not_nil first_item
413     end
414   end
415
416   # search for the filename in the file_names column and expect error
417   test "full text search not supported for individual columns" do
418     get '/arvados/v1/collections',
419       params: {:filters => [['name', '@@', 'General']].to_json},
420       headers: auth(:active)
421     assert_response 422
422   end
423
424   [
425     'quick fox',
426     'quick_brown fox',
427     'brown_ fox',
428     'fox dogs',
429   ].each do |search_filter|
430     test "full text search ignores special characters and finds with filter #{search_filter}" do
431       # description: The quick_brown_fox jumps over the lazy_dog
432       # full text search treats '_' as space apparently
433       get '/arvados/v1/collections',
434         params: {:filters => [['any', '@@', search_filter]].to_json},
435         headers: auth(:active)
436       assert_response 200
437       response_items = json_response['items']
438       assert_not_nil response_items
439       first_item = response_items.first
440       refute_empty first_item
441       assert_equal first_item['description'], 'The quick_brown_fox jumps over the lazy_dog'
442     end
443   end
444
445   test "create and get collection with properties" do
446     # create collection to be searched for
447     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
448     post "/arvados/v1/collections",
449       params: {
450         format: :json,
451         collection: {manifest_text: signed_manifest}.to_json,
452       },
453       headers: auth(:active)
454     assert_response 200
455     assert_not_nil json_response['uuid']
456     assert_not_nil json_response['properties']
457     assert_empty json_response['properties']
458
459     # update collection's properties
460     put "/arvados/v1/collections/#{json_response['uuid']}",
461       params: {
462         format: :json,
463         collection: { properties: {'property_1' => 'value_1'} }
464       },
465       headers: auth(:active)
466     assert_response :success
467     assert_equal Hash, json_response['properties'].class, 'Collection properties attribute should be of type hash'
468     assert_equal 'value_1', json_response['properties']['property_1']
469   end
470
471   test "create collection and update it with json encoded hash properties" do
472     # create collection to be searched for
473     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
474     post "/arvados/v1/collections",
475       params: {
476         format: :json,
477         collection: {manifest_text: signed_manifest}.to_json,
478       },
479       headers: auth(:active)
480     assert_response 200
481     assert_not_nil json_response['uuid']
482     assert_not_nil json_response['properties']
483     assert_empty json_response['properties']
484
485     # update collection's properties
486     put "/arvados/v1/collections/#{json_response['uuid']}",
487       params: {
488         format: :json,
489         collection: {
490           properties: "{\"property_1\":\"value_1\"}"
491         }
492       },
493       headers: auth(:active)
494     assert_response :success
495     assert_equal Hash, json_response['properties'].class, 'Collection properties attribute should be of type hash'
496     assert_equal 'value_1', json_response['properties']['property_1']
497   end
498
499   test "update collection with versioning enabled and using preserve_version" do
500     Rails.configuration.Collections.CollectionVersioning = true
501     Rails.configuration.Collections.PreserveVersionIfIdle = -1 # Disable auto versioning
502
503     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
504     post "/arvados/v1/collections",
505       params: {
506         format: :json,
507         collection: {
508           name: 'Test collection',
509           manifest_text: signed_manifest,
510         }.to_json,
511       },
512       headers: auth(:active)
513     assert_response 200
514     assert_not_nil json_response['uuid']
515     assert_equal 1, json_response['version']
516     assert_equal false, json_response['preserve_version']
517
518     # Versionable update including preserve_version=true should create a new
519     # version that will also be persisted.
520     put "/arvados/v1/collections/#{json_response['uuid']}",
521       params: {
522         format: :json,
523         collection: {
524           name: 'Test collection v2',
525           preserve_version: true,
526         }.to_json,
527       },
528       headers: auth(:active)
529     assert_response 200
530     assert_equal 2, json_response['version']
531     assert_equal true, json_response['preserve_version']
532
533     # 2nd versionable update including preserve_version=true should create a new
534     # version that will also be persisted.
535     put "/arvados/v1/collections/#{json_response['uuid']}",
536       params: {
537         format: :json,
538         collection: {
539           name: 'Test collection v3',
540           preserve_version: true,
541         }.to_json,
542       },
543       headers: auth(:active)
544     assert_response 200
545     assert_equal 3, json_response['version']
546     assert_equal true, json_response['preserve_version']
547
548     # 3rd versionable update without including preserve_version should create a new
549     # version that will have its preserve_version attr reset to false.
550     put "/arvados/v1/collections/#{json_response['uuid']}",
551       params: {
552         format: :json,
553         collection: {
554           name: 'Test collection v4',
555         }.to_json,
556       },
557       headers: auth(:active)
558     assert_response 200
559     assert_equal 4, json_response['version']
560     assert_equal false, json_response['preserve_version']
561
562     # 4th versionable update without including preserve_version=true should NOT
563     # create a new version.
564     put "/arvados/v1/collections/#{json_response['uuid']}",
565       params: {
566         format: :json,
567         collection: {
568           name: 'Test collection v5?',
569         }.to_json,
570       },
571       headers: auth(:active)
572     assert_response 200
573     assert_equal 4, json_response['version']
574     assert_equal false, json_response['preserve_version']
575   end
576 end