13996: Update tests for cleaner config access
[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   test "search collection using full text search" do
279     # create collection to be searched for
280     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))
281     post "/arvados/v1/collections",
282       params: {
283         format: :json,
284         collection: {description: 'specific collection description', manifest_text: signed_manifest}.to_json,
285       },
286       headers: auth(:active)
287     assert_response :success
288     assert_equal true, json_response['manifest_text'].include?('file4_in_subdir4.txt')
289
290     # search using the filename
291     search_using_full_text_search 'subdir2', 0
292     search_using_full_text_search 'subdir2:*', 1
293     search_using_full_text_search 'subdir2/subdir3/subdir4', 1
294     search_using_full_text_search 'file4:*', 1
295     search_using_full_text_search 'file4_in_subdir4.txt', 1
296     search_using_full_text_search 'subdir2 file4:*', 0      # first word is incomplete
297     search_using_full_text_search 'subdir2/subdir3/subdir4 file4:*', 1
298     search_using_full_text_search 'subdir2/subdir3/subdir4 file4_in_subdir4.txt', 1
299     search_using_full_text_search 'ile4', 0                 # not a prefix match
300   end
301
302   def search_using_full_text_search search_filter, expected_items
303     get '/arvados/v1/collections',
304       params: {:filters => [['any', '@@', search_filter]].to_json},
305       headers: auth(:active)
306     assert_response :success
307     response_items = json_response['items']
308     assert_not_nil response_items
309     if expected_items == 0
310       assert_empty response_items
311     else
312       refute_empty response_items
313       first_item = response_items.first
314       assert_not_nil first_item
315     end
316   end
317
318   # search for the filename in the file_names column and expect error
319   test "full text search not supported for individual columns" do
320     get '/arvados/v1/collections',
321       params: {:filters => [['name', '@@', 'General']].to_json},
322       headers: auth(:active)
323     assert_response 422
324   end
325
326   [
327     'quick fox',
328     'quick_brown fox',
329     'brown_ fox',
330     'fox dogs',
331   ].each do |search_filter|
332     test "full text search ignores special characters and finds with filter #{search_filter}" do
333       # description: The quick_brown_fox jumps over the lazy_dog
334       # full text search treats '_' as space apparently
335       get '/arvados/v1/collections',
336         params: {:filters => [['any', '@@', search_filter]].to_json},
337         headers: auth(:active)
338       assert_response 200
339       response_items = json_response['items']
340       assert_not_nil response_items
341       first_item = response_items.first
342       refute_empty first_item
343       assert_equal first_item['description'], 'The quick_brown_fox jumps over the lazy_dog'
344     end
345   end
346
347   test "create and get collection with properties" do
348     # create collection to be searched for
349     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
350     post "/arvados/v1/collections",
351       params: {
352         format: :json,
353         collection: {manifest_text: signed_manifest}.to_json,
354       },
355       headers: auth(:active)
356     assert_response 200
357     assert_not_nil json_response['uuid']
358     assert_not_nil json_response['properties']
359     assert_empty json_response['properties']
360
361     # update collection's description
362     put "/arvados/v1/collections/#{json_response['uuid']}",
363       params: {
364         format: :json,
365         collection: { properties: {'property_1' => 'value_1'} }
366       },
367       headers: auth(:active)
368     assert_response :success
369     assert_equal 'value_1', json_response['properties']['property_1']
370   end
371 end