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