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