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.blob_signing_key,
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.blob_signing_key,
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.blob_signing_key,
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(". 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(". 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
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",
284 collection: {description: 'specific collection description', manifest_text: signed_manifest}.to_json,
286 headers: auth(:active)
287 assert_response :success
288 assert_equal true, json_response['manifest_text'].include?('file4_in_subdir4.txt')
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
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
312 refute_empty response_items
313 first_item = response_items.first
314 assert_not_nil first_item
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)
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)
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'
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",
353 collection: {manifest_text: signed_manifest}.to_json,
355 headers: auth(:active)
357 assert_not_nil json_response['uuid']
358 assert_not_nil json_response['properties']
359 assert_empty json_response['properties']
361 # update collection's description
362 put "/arvados/v1/collections/#{json_response['uuid']}",
365 collection: { properties: {'property_1' => 'value_1'} }
367 headers: auth(:active)
368 assert_response :success
369 assert_equal 'value_1', json_response['properties']['property_1']