15306: Adds testing on collections#show that expose the bug.
[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 encoding via query string 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       get("/arvados/v1/collections?include_trash=#{param}&filters=#{[['uuid','=',expired_col.uuid]].to_json}",
289           headers: auth(:active))
290       assert_response :success
291       assert_not_nil json_response['items']
292       assert_equal truthiness, json_response['items'].collect {|c| c['uuid']}.include?(expired_col.uuid)
293       # Try #show next
294       get("/arvados/v1/collections/#{expired_col.uuid}/?include_trash=#{param}",
295         headers: auth(:active))
296       if truthiness
297         assert_response :success
298       else
299         assert_response 404
300       end
301     end
302   end
303
304   [
305     ["false", false],
306     ["0", false],
307     ["true", true],
308     ["1", true]
309   ].each do |param, truthiness|
310     test "include_trash=#{param.inspect} form-encoded param should be interpreted as include_trash=#{truthiness}" do
311       expired_col = collections(:expired_collection)
312       assert expired_col.is_trashed
313       params = [
314         ['include_trash', param],
315         ['filters', [['uuid','=',expired_col.uuid]].to_json],
316       ]
317       # Try #index first
318       get "/arvados/v1/collections",
319         params: URI.encode_www_form(params),
320         headers: {
321           "Content-type" => "application/x-www-form-urlencoded"
322         }.update(auth(:active))
323       assert_response :success
324       assert_not_nil json_response['items']
325       assert_equal truthiness, json_response['items'].collect {|c| c['uuid']}.include?(expired_col.uuid)
326       # Try #show next
327       get "/arvados/v1/collections",
328         params: URI.encode_www_form([['include_trash', param]]),
329         headers: {
330           "Content-type" => "application/x-www-form-urlencoded"
331         }.update(auth(:active))
332       if truthiness
333         assert_response :success
334       else
335         assert_response 404
336       end
337     end
338   end
339
340   test "search collection using full text search" do
341     # create collection to be searched for
342     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))
343     post "/arvados/v1/collections",
344       params: {
345         format: :json,
346         collection: {description: 'specific collection description', manifest_text: signed_manifest}.to_json,
347       },
348       headers: auth(:active)
349     assert_response :success
350     assert_equal true, json_response['manifest_text'].include?('file4_in_subdir4.txt')
351
352     # search using the filename
353     search_using_full_text_search 'subdir2', 0
354     search_using_full_text_search 'subdir2:*', 1
355     search_using_full_text_search 'subdir2/subdir3/subdir4', 1
356     search_using_full_text_search 'file4:*', 1
357     search_using_full_text_search 'file4_in_subdir4.txt', 1
358     search_using_full_text_search 'subdir2 file4:*', 0      # first word is incomplete
359     search_using_full_text_search 'subdir2/subdir3/subdir4 file4:*', 1
360     search_using_full_text_search 'subdir2/subdir3/subdir4 file4_in_subdir4.txt', 1
361     search_using_full_text_search 'ile4', 0                 # not a prefix match
362   end
363
364   def search_using_full_text_search search_filter, expected_items
365     get '/arvados/v1/collections',
366       params: {:filters => [['any', '@@', search_filter]].to_json},
367       headers: auth(:active)
368     assert_response :success
369     response_items = json_response['items']
370     assert_not_nil response_items
371     if expected_items == 0
372       assert_empty response_items
373     else
374       refute_empty response_items
375       first_item = response_items.first
376       assert_not_nil first_item
377     end
378   end
379
380   # search for the filename in the file_names column and expect error
381   test "full text search not supported for individual columns" do
382     get '/arvados/v1/collections',
383       params: {:filters => [['name', '@@', 'General']].to_json},
384       headers: auth(:active)
385     assert_response 422
386   end
387
388   [
389     'quick fox',
390     'quick_brown fox',
391     'brown_ fox',
392     'fox dogs',
393   ].each do |search_filter|
394     test "full text search ignores special characters and finds with filter #{search_filter}" do
395       # description: The quick_brown_fox jumps over the lazy_dog
396       # full text search treats '_' as space apparently
397       get '/arvados/v1/collections',
398         params: {:filters => [['any', '@@', search_filter]].to_json},
399         headers: auth(:active)
400       assert_response 200
401       response_items = json_response['items']
402       assert_not_nil response_items
403       first_item = response_items.first
404       refute_empty first_item
405       assert_equal first_item['description'], 'The quick_brown_fox jumps over the lazy_dog'
406     end
407   end
408
409   test "create and get collection with properties" do
410     # create collection to be searched for
411     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
412     post "/arvados/v1/collections",
413       params: {
414         format: :json,
415         collection: {manifest_text: signed_manifest}.to_json,
416       },
417       headers: auth(:active)
418     assert_response 200
419     assert_not_nil json_response['uuid']
420     assert_not_nil json_response['properties']
421     assert_empty json_response['properties']
422
423     # update collection's properties
424     put "/arvados/v1/collections/#{json_response['uuid']}",
425       params: {
426         format: :json,
427         collection: { properties: {'property_1' => 'value_1'} }
428       },
429       headers: auth(:active)
430     assert_response :success
431     assert_equal Hash, json_response['properties'].class, 'Collection properties attribute should be of type hash'
432     assert_equal 'value_1', json_response['properties']['property_1']
433   end
434
435   test "create collection and update it with json encoded hash properties" do
436     # create collection to be searched for
437     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
438     post "/arvados/v1/collections",
439       params: {
440         format: :json,
441         collection: {manifest_text: signed_manifest}.to_json,
442       },
443       headers: auth(:active)
444     assert_response 200
445     assert_not_nil json_response['uuid']
446     assert_not_nil json_response['properties']
447     assert_empty json_response['properties']
448
449     # update collection's properties
450     put "/arvados/v1/collections/#{json_response['uuid']}",
451       params: {
452         format: :json,
453         collection: {
454           properties: "{\"property_1\":\"value_1\"}"
455         }
456       },
457       headers: auth(:active)
458     assert_response :success
459     assert_equal Hash, json_response['properties'].class, 'Collection properties attribute should be of type hash'
460     assert_equal 'value_1', json_response['properties']['property_1']
461   end
462 end