Merge branch 'master' into 6277-check_manifest_validity
[arvados.git] / services / api / test / unit / collection_test.rb
1 require 'test_helper'
2
3 class CollectionTest < ActiveSupport::TestCase
4   def create_collection name, enc=nil
5     txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
6     txt.force_encoding(enc) if enc
7     return Collection.create(manifest_text: txt)
8   end
9
10   test 'accept ASCII manifest_text' do
11     act_as_system_user do
12       c = create_collection 'foo', Encoding::US_ASCII
13       assert c.valid?
14     end
15   end
16
17   test 'accept UTF-8 manifest_text' do
18     act_as_system_user do
19       c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
20       assert c.valid?
21     end
22   end
23
24   test 'refuse manifest_text with invalid UTF-8 byte sequence' do
25     act_as_system_user do
26       c = create_collection "f\xc8o", Encoding::UTF_8
27       assert !c.valid?
28       assert_equal [:manifest_text], c.errors.messages.keys
29       assert_match /UTF-8/, c.errors.messages[:manifest_text].first
30     end
31   end
32
33   test 'refuse manifest_text with non-UTF-8 encoding' do
34     act_as_system_user do
35       c = create_collection "f\xc8o", Encoding::ASCII_8BIT
36       assert !c.valid?
37       assert_equal [:manifest_text], c.errors.messages.keys
38       assert_match /UTF-8/, c.errors.messages[:manifest_text].first
39     end
40   end
41
42   [
43     nil,
44     ". 0:0:foo.txt",
45     ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
46     "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
47     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
48   ].each do |manifest_text|
49     test "create collection with invalid manifest text #{manifest_text} and expect error" do
50       act_as_system_user do
51         c = Collection.create(manifest_text: manifest_text)
52         assert !c.valid?
53       end
54     end
55   end
56
57   [
58     nil,
59     ". 0:0:foo.txt",
60     ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
61     "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
62     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
63   ].each do |manifest_text|
64     test "update collection with invalid manifest text #{manifest_text} and expect error" do
65       act_as_system_user do
66         c = create_collection 'foo', Encoding::US_ASCII
67         assert c.valid?
68
69         c.update_attribute 'manifest_text', manifest_text
70         assert !c.valid?
71       end
72     end
73   end
74
75   test 'create and update collection and verify file_names' do
76     act_as_system_user do
77       c = create_collection 'foo', Encoding::US_ASCII
78       assert c.valid?
79       created_file_names = c.file_names
80       assert created_file_names
81       assert_match /foo.txt/, c.file_names
82
83       c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
84       assert_not_equal created_file_names, c.file_names
85       assert_match /foo2.txt/, c.file_names
86     end
87   end
88
89   [
90     [2**8, false],
91     [2**18, true],
92   ].each do |manifest_size, allow_truncate|
93     test "create collection with manifest size #{manifest_size} with allow_truncate=#{allow_truncate},
94           and not expect exceptions even on very large manifest texts" do
95       # file_names has a max size, hence there will be no errors even on large manifests
96       act_as_system_user do
97         manifest_text = ''
98         index = 0
99         while manifest_text.length < manifest_size
100           manifest_text += "./blurfl d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylongfilename000000000000#{index}.txt\n"
101           index += 1
102         end
103         manifest_text += "./laststreamname d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylastfilename.txt\n"
104         c = Collection.create(manifest_text: manifest_text)
105
106         assert c.valid?
107         assert c.file_names
108         assert_match /veryverylongfilename0000000000001.txt/, c.file_names
109         assert_match /veryverylongfilename0000000000002.txt/, c.file_names
110         if not allow_truncate
111           assert_match /veryverylastfilename/, c.file_names
112           assert_match /laststreamname/, c.file_names
113         end
114       end
115     end
116   end
117
118   test "full text search for collections" do
119     # file_names column does not get populated when fixtures are loaded, hence setup test data
120     act_as_system_user do
121       Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n")
122       Collection.create(manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n")
123       Collection.create(manifest_text: ". 85877ca2d7e05498dd3d109baf2df106+95+A3a4e26a366ee7e4ed3e476ccf05354761be2e4ae@545a9920 0:95:file_in_subdir1\n./subdir2/subdir3 2bbc341c702df4d8f42ec31f16c10120+64+A315d7e7bad2ce937e711fc454fae2d1194d14d64@545a9920 0:32:file1.txt 32:32:file2.txt\n./subdir2/subdir3/subdir4 2bbc341c702df4d8f42ec31f16c10120+64+A315d7e7bad2ce937e711fc454fae2d1194d14d64@545a9920 0:32:file3.txt 32:32:file4.txt\n")
124     end
125
126     [
127       ['foo', true],
128       ['foo bar', false],                     # no collection matching both
129       ['foo&bar', false],                     # no collection matching both
130       ['foo|bar', true],                      # works only no spaces between the words
131       ['Gnu public', true],                   # both prefixes found, though not consecutively
132       ['Gnu&public', true],                   # both prefixes found, though not consecutively
133       ['file4', true],                        # prefix match
134       ['file4.txt', true],                    # whole string match
135       ['filex', false],                       # no such prefix
136       ['subdir', true],                       # prefix matches
137       ['subdir2', true],
138       ['subdir2/', true],
139       ['subdir2/subdir3', true],
140       ['subdir2/subdir3/subdir4', true],
141       ['subdir2 file4', true],                # look for both prefixes
142       ['subdir4', false],                     # not a prefix match
143     ].each do |search_filter, expect_results|
144       search_filters = search_filter.split.each {|s| s.concat(':*')}.join('&')
145       results = Collection.where("#{Collection.full_text_tsvector} @@ to_tsquery(?)",
146                                  "#{search_filters}")
147       if expect_results
148         refute_empty results
149       else
150         assert_empty results
151       end
152     end
153   end
154
155   test 'portable data hash with missing size hints' do
156     [[". d41d8cd98f00b204e9800998ecf8427e+0+Bar 0:0:x\n",
157       ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"],
158      [". d41d8cd98f00b204e9800998ecf8427e+Foo 0:0:x\n",
159       ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
160      [". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n",
161       ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
162     ].each do |unportable, portable|
163       c = Collection.new(manifest_text: unportable)
164       assert c.valid?
165       assert_equal(Digest::MD5.hexdigest(portable)+"+#{portable.length}",
166                    c.portable_data_hash)
167     end
168   end
169
170   pdhmanifest = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"
171   pdhmd5 = Digest::MD5.hexdigest pdhmanifest
172   [[true, nil],
173    [true, pdhmd5],
174    [true, pdhmd5+'+12345'],
175    [true, pdhmd5+'+'+pdhmanifest.length.to_s],
176    [true, pdhmd5+'+12345+Foo'],
177    [true, pdhmd5+'+Foo'],
178    [false, Digest::MD5.hexdigest(pdhmanifest.strip)],
179    [false, Digest::MD5.hexdigest(pdhmanifest.strip)+'+'+pdhmanifest.length.to_s],
180    [false, pdhmd5[0..30]],
181    [false, pdhmd5[0..30]+'z'],
182    [false, pdhmd5[0..24]+'000000000'],
183    [false, pdhmd5[0..24]+'000000000+0']].each do |isvalid, pdh|
184     test "portable_data_hash #{pdh.inspect} valid? == #{isvalid}" do
185       c = Collection.new manifest_text: pdhmanifest, portable_data_hash: pdh
186       assert_equal isvalid, c.valid?, c.errors.full_messages.to_s
187     end
188   end
189
190   [0, 2, 4, nil].each do |ask|
191     test "set replication_desired to #{ask.inspect}" do
192       Rails.configuration.default_collection_replication = 2
193       act_as_user users(:active) do
194         c = collections(:replication_undesired_unconfirmed)
195         c.update_attributes replication_desired: ask
196         assert_equal ask, c.replication_desired
197       end
198     end
199   end
200
201   test "replication_confirmed* can be set by admin user" do
202     c = collections(:replication_desired_2_unconfirmed)
203     act_as_user users(:admin) do
204       assert c.update_attributes(replication_confirmed: 2,
205                                  replication_confirmed_at: Time.now)
206     end
207   end
208
209   test "replication_confirmed* cannot be set by non-admin user" do
210     act_as_user users(:active) do
211       c = collections(:replication_desired_2_unconfirmed)
212       # Cannot set just one at a time.
213       assert_raise ArvadosModel::PermissionDeniedError do
214         c.update_attributes replication_confirmed: 1
215       end
216       assert_raise ArvadosModel::PermissionDeniedError do
217         c.update_attributes replication_confirmed_at: Time.now
218       end
219       # Cannot set both at once, either.
220       assert_raise ArvadosModel::PermissionDeniedError do
221         c.update_attributes(replication_confirmed: 1,
222                             replication_confirmed_at: Time.now)
223       end
224     end
225   end
226
227   test "replication_confirmed* can be cleared (but only together) by non-admin user" do
228     act_as_user users(:active) do
229       c = collections(:replication_desired_2_confirmed_2)
230       # Cannot clear just one at a time.
231       assert_raise ArvadosModel::PermissionDeniedError do
232         c.update_attributes replication_confirmed: nil
233       end
234       c.reload
235       assert_raise ArvadosModel::PermissionDeniedError do
236         c.update_attributes replication_confirmed_at: nil
237       end
238       # Can clear both at once.
239       c.reload
240       assert c.update_attributes(replication_confirmed: nil,
241                                  replication_confirmed_at: nil)
242     end
243   end
244
245   test "clear replication_confirmed* when introducing a new block in manifest" do
246     c = collections(:replication_desired_2_confirmed_2)
247     act_as_user users(:active) do
248       assert c.update_attributes(manifest_text: collections(:user_agreement).signed_manifest_text)
249       assert_nil c.replication_confirmed
250       assert_nil c.replication_confirmed_at
251     end
252   end
253
254   test "don't clear replication_confirmed* when just renaming a file" do
255     c = collections(:replication_desired_2_confirmed_2)
256     act_as_user users(:active) do
257       new_manifest = c.signed_manifest_text.sub(':bar', ':foo')
258       assert c.update_attributes(manifest_text: new_manifest)
259       assert_equal 2, c.replication_confirmed
260       assert_not_nil c.replication_confirmed_at
261     end
262   end
263
264   test "don't clear replication_confirmed* when just deleting a data block" do
265     c = collections(:replication_desired_2_confirmed_2)
266     act_as_user users(:active) do
267       new_manifest = c.signed_manifest_text
268       new_manifest.sub!(/ \S+:bar/, '')
269       new_manifest.sub!(/ acbd\S+/, '')
270
271       # Confirm that we did just remove a block from the manifest (if
272       # not, this test would pass without testing the relevant case):
273       assert_operator new_manifest.length+40, :<, c.signed_manifest_text.length
274
275       assert c.update_attributes(manifest_text: new_manifest)
276       assert_equal 2, c.replication_confirmed
277       assert_not_nil c.replication_confirmed_at
278     end
279   end
280
281   test "create collection with properties" do
282     act_as_system_user do
283       c = Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n",
284                             properties: {'property_1' => 'value_1'})
285       assert c.valid?
286       assert_equal 'value_1', c.properties['property_1']
287     end
288   end
289
290   test 'create, delete, recreate collection with same name and owner' do
291     act_as_user users(:active) do
292       # create collection with name
293       c = Collection.create(manifest_text: '',
294                             name: "test collection name")
295       assert c.valid?
296       uuid = c.uuid
297
298       # mark collection as expired
299       c.update_attribute 'expires_at', Time.new.strftime("%Y-%m-%d")
300       c = Collection.where(uuid: uuid)
301       assert_empty c, 'Should not be able to find expired collection'
302
303       # recreate collection with the same name
304       c = Collection.create(manifest_text: '',
305                             name: "test collection name")
306       assert c.valid?
307     end
308   end
309
310   test "find_all_for_docker_image resolves names that look like hashes" do
311     coll_list = Collection.
312       find_all_for_docker_image('a' * 64, nil, [users(:active)])
313     coll_uuids = coll_list.map(&:uuid)
314     assert_includes(coll_uuids, collections(:docker_image).uuid)
315   end
316 end