Merge branch '8784-dir-listings'
[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", {:format => :json}, auth(:active)
12     assert_response :success
13     assert_equal "arvados#collectionList", json_response['kind']
14   end
15
16   test "get index with filters= (empty string)" do
17     get "/arvados/v1/collections", {:format => :json, :filters => ''}, auth(:active)
18     assert_response :success
19     assert_equal "arvados#collectionList", json_response['kind']
20   end
21
22   test "get index with invalid filters (array of strings) responds 422" do
23     get "/arvados/v1/collections", {
24       :format => :json,
25       :filters => ['uuid', '=', 'ad02e37b6a7f45bbe2ead3c29a109b8a+54'].to_json
26     }, auth(:active)
27     assert_response 422
28     assert_match(/nvalid element.*not an array/, json_response['errors'].join(' '))
29   end
30
31   test "get index with invalid filters (unsearchable column) responds 422" do
32     get "/arvados/v1/collections", {
33       :format => :json,
34       :filters => [['this_column_does_not_exist', '=', 'bogus']].to_json
35     }, auth(:active)
36     assert_response 422
37     assert_match(/nvalid attribute/, json_response['errors'].join(' '))
38   end
39
40   test "get index with invalid filters (invalid operator) responds 422" do
41     get "/arvados/v1/collections", {
42       :format => :json,
43       :filters => [['uuid', ':-(', 'displeased']].to_json
44     }, auth(:active)
45     assert_response 422
46     assert_match(/nvalid operator/, json_response['errors'].join(' '))
47   end
48
49   test "get index with invalid filters (invalid operand type) responds 422" do
50     get "/arvados/v1/collections", {
51       :format => :json,
52       :filters => [['uuid', '=', {foo: 'bar'}]].to_json
53     }, auth(:active)
54     assert_response 422
55     assert_match(/nvalid operand type/, json_response['errors'].join(' '))
56   end
57
58   test "get index with where= (empty string)" do
59     get "/arvados/v1/collections", {:format => :json, :where => ''}, auth(:active)
60     assert_response :success
61     assert_equal "arvados#collectionList", json_response['kind']
62   end
63
64   test "get index with select= (valid attribute)" do
65     get "/arvados/v1/collections", {
66           :format => :json,
67           :select => ['portable_data_hash'].to_json
68         }, auth(:active)
69     assert_response :success
70     assert json_response['items'][0].keys.include?('portable_data_hash')
71     assert not(json_response['items'][0].keys.include?('uuid'))
72   end
73
74   test "get index with select= (invalid attribute) responds 422" do
75     get "/arvados/v1/collections", {
76           :format => :json,
77           :select => ['bogus'].to_json
78         }, auth(:active)
79     assert_response 422
80     assert_match(/Invalid attribute.*bogus/, json_response['errors'].join(' '))
81   end
82
83   test "get index with select= (invalid attribute type) responds 422" do
84     get "/arvados/v1/collections", {
85           :format => :json,
86           :select => [['bogus']].to_json
87         }, auth(:active)
88     assert_response 422
89     assert_match(/Invalid attribute.*bogus/, json_response['errors'].join(' '))
90   end
91
92   test "controller 404 response is json" do
93     get "/arvados/v1/thingsthatdonotexist", {:format => :xml}, auth(:active)
94     assert_response 404
95     assert_equal 1, json_response['errors'].length
96     assert_equal true, json_response['errors'][0].is_a?(String)
97   end
98
99   test "object 404 response is json" do
100     get "/arvados/v1/groups/zzzzz-j7d0g-o5ba971173cup4f", {}, auth(:active)
101     assert_response 404
102     assert_equal 1, json_response['errors'].length
103     assert_equal true, json_response['errors'][0].is_a?(String)
104   end
105
106   test "store collection as json" do
107     signing_opts = {
108       key: Rails.configuration.blob_signing_key,
109       api_token: api_token(:active),
110     }
111     signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
112                                        signing_opts)
113     post "/arvados/v1/collections", {
114       format: :json,
115       collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\",\"portable_data_hash\":\"ad02e37b6a7f45bbe2ead3c29a109b8a+54\"}"
116     }, auth(:active)
117     assert_response 200
118     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
119   end
120
121   test "store collection with manifest_text only" do
122     signing_opts = {
123       key: Rails.configuration.blob_signing_key,
124       api_token: api_token(:active),
125     }
126     signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
127                                        signing_opts)
128     post "/arvados/v1/collections", {
129       format: :json,
130       collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\"}"
131     }, auth(:active)
132     assert_response 200
133     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
134   end
135
136   test "store collection then update name" do
137     signing_opts = {
138       key: Rails.configuration.blob_signing_key,
139       api_token: api_token(:active),
140     }
141     signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
142                                        signing_opts)
143     post "/arvados/v1/collections", {
144       format: :json,
145       collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\",\"portable_data_hash\":\"ad02e37b6a7f45bbe2ead3c29a109b8a+54\"}"
146     }, auth(:active)
147     assert_response 200
148     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
149
150     put "/arvados/v1/collections/#{json_response['uuid']}", {
151       format: :json,
152       collection: { name: "a name" }
153     }, auth(:active)
154
155     assert_response 200
156     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
157     assert_equal 'a name', json_response['name']
158
159     get "/arvados/v1/collections/#{json_response['uuid']}", {
160       format: :json,
161     }, auth(:active)
162
163     assert_response 200
164     assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
165     assert_equal 'a name', json_response['name']
166   end
167
168   test "update description for a collection, and search for that description" do
169     collection = collections(:multilevel_collection_1)
170
171     # update collection's description
172     put "/arvados/v1/collections/#{collection['uuid']}", {
173       format: :json,
174       collection: { description: "something specific" }
175     }, auth(:active)
176     assert_response :success
177     assert_equal 'something specific', json_response['description']
178
179     # get the collection and verify newly added description
180     get "/arvados/v1/collections/#{collection['uuid']}", {
181       format: :json,
182     }, auth(:active)
183     assert_response 200
184     assert_equal 'something specific', json_response['description']
185
186     # search
187     search_using_filter 'specific', 1
188     search_using_filter 'not specific enough', 0
189   end
190
191   test "create collection, update manifest, and search with filename" do
192     # create collection
193     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
194     post "/arvados/v1/collections", {
195       format: :json,
196       collection: {manifest_text: signed_manifest}.to_json,
197     }, auth(:active)
198     assert_response :success
199     assert_equal true, json_response['manifest_text'].include?('my_test_file.txt')
200     assert_includes json_response['manifest_text'], 'my_test_file.txt'
201
202     created = json_response
203
204     # search using the filename
205     search_using_filter 'my_test_file.txt', 1
206
207     # update the collection's manifest text
208     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_updated_test_file.txt\n", api_token(:active))
209     put "/arvados/v1/collections/#{created['uuid']}", {
210       format: :json,
211       collection: {manifest_text: signed_manifest}.to_json,
212     }, auth(:active)
213     assert_response :success
214     assert_equal created['uuid'], json_response['uuid']
215     assert_includes json_response['manifest_text'], 'my_updated_test_file.txt'
216     assert_not_includes json_response['manifest_text'], 'my_test_file.txt'
217
218     # search using the new filename
219     search_using_filter 'my_updated_test_file.txt', 1
220     search_using_filter 'my_test_file.txt', 0
221     search_using_filter 'there_is_no_such_file.txt', 0
222   end
223
224   def search_using_filter search_filter, expected_items
225     get '/arvados/v1/collections', {
226       :filters => [['any', 'ilike', "%#{search_filter}%"]].to_json
227     }, auth(:active)
228     assert_response :success
229     response_items = json_response['items']
230     assert_not_nil response_items
231     if expected_items == 0
232       assert_empty response_items
233     else
234       refute_empty response_items
235       first_item = response_items.first
236       assert_not_nil first_item
237     end
238   end
239
240   test "search collection using full text search" do
241     # create collection to be searched for
242     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))
243     post "/arvados/v1/collections", {
244       format: :json,
245       collection: {description: 'specific collection description', manifest_text: signed_manifest}.to_json,
246     }, auth(:active)
247     assert_response :success
248     assert_equal true, json_response['manifest_text'].include?('file4_in_subdir4.txt')
249
250     # search using the filename
251     search_using_full_text_search 'subdir2', 0
252     search_using_full_text_search 'subdir2:*', 1
253     search_using_full_text_search 'subdir2/subdir3/subdir4', 1
254     search_using_full_text_search 'file4:*', 1
255     search_using_full_text_search 'file4_in_subdir4.txt', 1
256     search_using_full_text_search 'subdir2 file4:*', 0      # first word is incomplete
257     search_using_full_text_search 'subdir2/subdir3/subdir4 file4:*', 1
258     search_using_full_text_search 'subdir2/subdir3/subdir4 file4_in_subdir4.txt', 1
259     search_using_full_text_search 'ile4', 0                 # not a prefix match
260   end
261
262   def search_using_full_text_search search_filter, expected_items
263     get '/arvados/v1/collections', {
264       :filters => [['any', '@@', search_filter]].to_json
265     }, 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   # search for the filename in the file_names column and expect error
279   test "full text search not supported for individual columns" do
280     get '/arvados/v1/collections', {
281       :filters => [['name', '@@', 'General']].to_json
282     }, auth(:active)
283     assert_response 422
284   end
285
286   [
287     'quick fox',
288     'quick_brown fox',
289     'brown_ fox',
290     'fox dogs',
291   ].each do |search_filter|
292     test "full text search ignores special characters and finds with filter #{search_filter}" do
293       # description: The quick_brown_fox jumps over the lazy_dog
294       # full text search treats '_' as space apparently
295       get '/arvados/v1/collections', {
296         :filters => [['any', '@@', search_filter]].to_json
297       }, auth(:active)
298       assert_response 200
299       response_items = json_response['items']
300       assert_not_nil response_items
301       first_item = response_items.first
302       refute_empty first_item
303       assert_equal first_item['description'], 'The quick_brown_fox jumps over the lazy_dog'
304     end
305   end
306
307   test "create and get collection with properties" do
308     # create collection to be searched for
309     signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
310     post "/arvados/v1/collections", {
311       format: :json,
312       collection: {manifest_text: signed_manifest}.to_json,
313     }, auth(:active)
314     assert_response 200
315     assert_not_nil json_response['uuid']
316     assert_not_nil json_response['properties']
317     assert_empty json_response['properties']
318
319     # update collection's description
320     put "/arvados/v1/collections/#{json_response['uuid']}", {
321       format: :json,
322       collection: { properties: {'property_1' => 'value_1'} }
323     }, auth(:active)
324     assert_response :success
325     assert_equal 'value_1', json_response['properties']['property_1']
326   end
327 end