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", {:format => :json}, auth(:active)
12 assert_response :success
13 assert_equal "arvados#collectionList", json_response['kind']
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']
22 test "get index with invalid filters (array of strings) responds 422" do
23 get "/arvados/v1/collections", {
25 :filters => ['uuid', '=', 'ad02e37b6a7f45bbe2ead3c29a109b8a+54'].to_json
28 assert_match(/nvalid element.*not an array/, json_response['errors'].join(' '))
31 test "get index with invalid filters (unsearchable column) responds 422" do
32 get "/arvados/v1/collections", {
34 :filters => [['this_column_does_not_exist', '=', 'bogus']].to_json
37 assert_match(/nvalid attribute/, json_response['errors'].join(' '))
40 test "get index with invalid filters (invalid operator) responds 422" do
41 get "/arvados/v1/collections", {
43 :filters => [['uuid', ':-(', 'displeased']].to_json
46 assert_match(/nvalid operator/, json_response['errors'].join(' '))
49 test "get index with invalid filters (invalid operand type) responds 422" do
50 get "/arvados/v1/collections", {
52 :filters => [['uuid', '=', {foo: 'bar'}]].to_json
55 assert_match(/nvalid operand type/, json_response['errors'].join(' '))
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']
64 test "get index with select= (valid attribute)" do
65 get "/arvados/v1/collections", {
67 :select => ['portable_data_hash'].to_json
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'))
74 test "get index with select= (invalid attribute) responds 422" do
75 get "/arvados/v1/collections", {
77 :select => ['bogus'].to_json
80 assert_match(/Invalid attribute.*bogus/, json_response['errors'].join(' '))
83 test "get index with select= (invalid attribute type) responds 422" do
84 get "/arvados/v1/collections", {
86 :select => [['bogus']].to_json
89 assert_match(/Invalid attribute.*bogus/, json_response['errors'].join(' '))
92 test "controller 404 response is json" do
93 get "/arvados/v1/thingsthatdonotexist", {:format => :xml}, auth(:active)
95 assert_equal 1, json_response['errors'].length
96 assert_equal true, json_response['errors'][0].is_a?(String)
99 test "object 404 response is json" do
100 get "/arvados/v1/groups/zzzzz-j7d0g-o5ba971173cup4f", {}, auth(:active)
102 assert_equal 1, json_response['errors'].length
103 assert_equal true, json_response['errors'][0].is_a?(String)
106 test "store collection as json" do
108 key: Rails.configuration.blob_signing_key,
109 api_token: api_token(:active),
111 signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
113 post "/arvados/v1/collections", {
115 collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\",\"portable_data_hash\":\"ad02e37b6a7f45bbe2ead3c29a109b8a+54\"}"
118 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
121 test "store collection with manifest_text only" do
123 key: Rails.configuration.blob_signing_key,
124 api_token: api_token(:active),
126 signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
128 post "/arvados/v1/collections", {
130 collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\"}"
133 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
136 test "store collection then update name" do
138 key: Rails.configuration.blob_signing_key,
139 api_token: api_token(:active),
141 signed_locator = Blob.sign_locator('bad42fa702ae3ea7d888fef11b46f450+44',
143 post "/arvados/v1/collections", {
145 collection: "{\"manifest_text\":\". #{signed_locator} 0:44:md5sum.txt\\n\",\"portable_data_hash\":\"ad02e37b6a7f45bbe2ead3c29a109b8a+54\"}"
148 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
150 put "/arvados/v1/collections/#{json_response['uuid']}", {
152 collection: { name: "a name" }
156 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
157 assert_equal 'a name', json_response['name']
159 get "/arvados/v1/collections/#{json_response['uuid']}", {
164 assert_equal 'ad02e37b6a7f45bbe2ead3c29a109b8a+54', json_response['portable_data_hash']
165 assert_equal 'a name', json_response['name']
168 test "update description for a collection, and search for that description" do
169 collection = collections(:multilevel_collection_1)
171 # update collection's description
172 put "/arvados/v1/collections/#{collection['uuid']}", {
174 collection: { description: "something specific" }
176 assert_response :success
177 assert_equal 'something specific', json_response['description']
179 # get the collection and verify newly added description
180 get "/arvados/v1/collections/#{collection['uuid']}", {
184 assert_equal 'something specific', json_response['description']
187 search_using_filter 'specific', 1
188 search_using_filter 'not specific enough', 0
191 test "create collection, update manifest, and search with filename" do
193 signed_manifest = Collection.sign_manifest(". bad42fa702ae3ea7d888fef11b46f450+44 0:44:my_test_file.txt\n", api_token(:active))
194 post "/arvados/v1/collections", {
196 collection: {manifest_text: signed_manifest}.to_json,
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'
202 created = json_response
204 # search using the filename
205 search_using_filter 'my_test_file.txt', 1
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']}", {
211 collection: {manifest_text: signed_manifest}.to_json,
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'
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
224 def search_using_filter search_filter, expected_items
225 get '/arvados/v1/collections', {
226 :filters => [['any', 'ilike', "%#{search_filter}%"]].to_json
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
234 refute_empty response_items
235 first_item = response_items.first
236 assert_not_nil first_item
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", {
245 collection: {description: 'specific collection description', manifest_text: signed_manifest}.to_json,
247 assert_response :success
248 assert_equal true, json_response['manifest_text'].include?('file4_in_subdir4.txt')
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
262 def search_using_full_text_search search_filter, expected_items
263 get '/arvados/v1/collections', {
264 :filters => [['any', '@@', search_filter]].to_json
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 # 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
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
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'
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", {
312 collection: {manifest_text: signed_manifest}.to_json,
315 assert_not_nil json_response['uuid']
316 assert_not_nil json_response['properties']
317 assert_empty json_response['properties']
319 # update collection's description
320 put "/arvados/v1/collections/#{json_response['uuid']}", {
322 collection: { properties: {'property_1' => 'value_1'} }
324 assert_response :success
325 assert_equal 'value_1', json_response['properties']['property_1']