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