Merge branch '11127-delete-trash-with-links'
[arvados.git] / services / api / test / unit / collection_test.rb
1 require 'test_helper'
2 require 'sweep_trashed_collections'
3
4 class CollectionTest < ActiveSupport::TestCase
5   include DbCurrentTime
6
7   def create_collection name, enc=nil
8     txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
9     txt.force_encoding(enc) if enc
10     return Collection.create(manifest_text: txt)
11   end
12
13   test 'accept ASCII manifest_text' do
14     act_as_system_user do
15       c = create_collection 'foo', Encoding::US_ASCII
16       assert c.valid?
17     end
18   end
19
20   test 'accept UTF-8 manifest_text' do
21     act_as_system_user do
22       c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
23       assert c.valid?
24     end
25   end
26
27   test 'refuse manifest_text with invalid UTF-8 byte sequence' do
28     act_as_system_user do
29       c = create_collection "f\xc8o", Encoding::UTF_8
30       assert !c.valid?
31       assert_equal [:manifest_text], c.errors.messages.keys
32       assert_match(/UTF-8/, c.errors.messages[:manifest_text].first)
33     end
34   end
35
36   test 'refuse manifest_text with non-UTF-8 encoding' do
37     act_as_system_user do
38       c = create_collection "f\xc8o", Encoding::ASCII_8BIT
39       assert !c.valid?
40       assert_equal [:manifest_text], c.errors.messages.keys
41       assert_match(/UTF-8/, c.errors.messages[:manifest_text].first)
42     end
43   end
44
45   [
46     ". 0:0:foo.txt",
47     ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
48     "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
49     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
50   ].each do |manifest_text|
51     test "create collection with invalid manifest text #{manifest_text} and expect error" do
52       act_as_system_user do
53         c = Collection.create(manifest_text: manifest_text)
54         assert !c.valid?
55       end
56     end
57   end
58
59   [
60     nil,
61     "",
62     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
63   ].each do |manifest_text|
64     test "create collection with valid manifest text #{manifest_text.inspect} and expect success" do
65       act_as_system_user do
66         c = Collection.create(manifest_text: manifest_text)
67         assert c.valid?
68       end
69     end
70   end
71
72   [
73     ". 0:0:foo.txt",
74     ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
75     "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
76     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
77   ].each do |manifest_text|
78     test "update collection with invalid manifest text #{manifest_text} and expect error" do
79       act_as_system_user do
80         c = create_collection 'foo', Encoding::US_ASCII
81         assert c.valid?
82
83         c.update_attribute 'manifest_text', manifest_text
84         assert !c.valid?
85       end
86     end
87   end
88
89   [
90     nil,
91     "",
92     ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
93   ].each do |manifest_text|
94     test "update collection with valid manifest text #{manifest_text.inspect} and expect success" do
95       act_as_system_user do
96         c = create_collection 'foo', Encoding::US_ASCII
97         assert c.valid?
98
99         c.update_attribute 'manifest_text', manifest_text
100         assert c.valid?
101       end
102     end
103   end
104
105   test 'create and update collection and verify file_names' do
106     act_as_system_user do
107       c = create_collection 'foo', Encoding::US_ASCII
108       assert c.valid?
109       created_file_names = c.file_names
110       assert created_file_names
111       assert_match(/foo.txt/, c.file_names)
112
113       c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
114       assert_not_equal created_file_names, c.file_names
115       assert_match(/foo2.txt/, c.file_names)
116     end
117   end
118
119   [
120     [2**8, false],
121     [2**18, true],
122   ].each do |manifest_size, allow_truncate|
123     test "create collection with manifest size #{manifest_size} with allow_truncate=#{allow_truncate},
124           and not expect exceptions even on very large manifest texts" do
125       # file_names has a max size, hence there will be no errors even on large manifests
126       act_as_system_user do
127         manifest_text = ''
128         index = 0
129         while manifest_text.length < manifest_size
130           manifest_text += "./blurfl d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylongfilename000000000000#{index}.txt\n"
131           index += 1
132         end
133         manifest_text += "./laststreamname d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylastfilename.txt\n"
134         c = Collection.create(manifest_text: manifest_text)
135
136         assert c.valid?
137         assert c.file_names
138         assert_match(/veryverylongfilename0000000000001.txt/, c.file_names)
139         assert_match(/veryverylongfilename0000000000002.txt/, c.file_names)
140         if not allow_truncate
141           assert_match(/veryverylastfilename/, c.file_names)
142           assert_match(/laststreamname/, c.file_names)
143         end
144       end
145     end
146   end
147
148   test "full text search for collections" do
149     # file_names column does not get populated when fixtures are loaded, hence setup test data
150     act_as_system_user do
151       Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n")
152       Collection.create(manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n")
153       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")
154     end
155
156     [
157       ['foo', true],
158       ['foo bar', false],                     # no collection matching both
159       ['foo&bar', false],                     # no collection matching both
160       ['foo|bar', true],                      # works only no spaces between the words
161       ['Gnu public', true],                   # both prefixes found, though not consecutively
162       ['Gnu&public', true],                   # both prefixes found, though not consecutively
163       ['file4', true],                        # prefix match
164       ['file4.txt', true],                    # whole string match
165       ['filex', false],                       # no such prefix
166       ['subdir', true],                       # prefix matches
167       ['subdir2', true],
168       ['subdir2/', true],
169       ['subdir2/subdir3', true],
170       ['subdir2/subdir3/subdir4', true],
171       ['subdir2 file4', true],                # look for both prefixes
172       ['subdir4', false],                     # not a prefix match
173     ].each do |search_filter, expect_results|
174       search_filters = search_filter.split.each {|s| s.concat(':*')}.join('&')
175       results = Collection.where("#{Collection.full_text_tsvector} @@ to_tsquery(?)",
176                                  "#{search_filters}")
177       if expect_results
178         refute_empty results
179       else
180         assert_empty results
181       end
182     end
183   end
184
185   test 'portable data hash with missing size hints' do
186     [[". d41d8cd98f00b204e9800998ecf8427e+0+Bar 0:0:x\n",
187       ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"],
188      [". d41d8cd98f00b204e9800998ecf8427e+Foo 0:0:x\n",
189       ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
190      [". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n",
191       ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
192     ].each do |unportable, portable|
193       c = Collection.new(manifest_text: unportable)
194       assert c.valid?
195       assert_equal(Digest::MD5.hexdigest(portable)+"+#{portable.length}",
196                    c.portable_data_hash)
197     end
198   end
199
200   pdhmanifest = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"
201   pdhmd5 = Digest::MD5.hexdigest pdhmanifest
202   [[true, nil],
203    [true, pdhmd5],
204    [true, pdhmd5+'+12345'],
205    [true, pdhmd5+'+'+pdhmanifest.length.to_s],
206    [true, pdhmd5+'+12345+Foo'],
207    [true, pdhmd5+'+Foo'],
208    [false, Digest::MD5.hexdigest(pdhmanifest.strip)],
209    [false, Digest::MD5.hexdigest(pdhmanifest.strip)+'+'+pdhmanifest.length.to_s],
210    [false, pdhmd5[0..30]],
211    [false, pdhmd5[0..30]+'z'],
212    [false, pdhmd5[0..24]+'000000000'],
213    [false, pdhmd5[0..24]+'000000000+0']].each do |isvalid, pdh|
214     test "portable_data_hash #{pdh.inspect} valid? == #{isvalid}" do
215       c = Collection.new manifest_text: pdhmanifest, portable_data_hash: pdh
216       assert_equal isvalid, c.valid?, c.errors.full_messages.to_s
217     end
218   end
219
220   [0, 2, 4, nil].each do |ask|
221     test "set replication_desired to #{ask.inspect}" do
222       Rails.configuration.default_collection_replication = 2
223       act_as_user users(:active) do
224         c = collections(:replication_undesired_unconfirmed)
225         c.update_attributes replication_desired: ask
226         assert_equal ask, c.replication_desired
227       end
228     end
229   end
230
231   test "replication_confirmed* can be set by admin user" do
232     c = collections(:replication_desired_2_unconfirmed)
233     act_as_user users(:admin) do
234       assert c.update_attributes(replication_confirmed: 2,
235                                  replication_confirmed_at: Time.now)
236     end
237   end
238
239   test "replication_confirmed* cannot be set by non-admin user" do
240     act_as_user users(:active) do
241       c = collections(:replication_desired_2_unconfirmed)
242       # Cannot set just one at a time.
243       assert_raise ArvadosModel::PermissionDeniedError do
244         c.update_attributes replication_confirmed: 1
245       end
246       assert_raise ArvadosModel::PermissionDeniedError do
247         c.update_attributes replication_confirmed_at: Time.now
248       end
249       # Cannot set both at once, either.
250       assert_raise ArvadosModel::PermissionDeniedError do
251         c.update_attributes(replication_confirmed: 1,
252                             replication_confirmed_at: Time.now)
253       end
254     end
255   end
256
257   test "replication_confirmed* can be cleared (but only together) by non-admin user" do
258     act_as_user users(:active) do
259       c = collections(:replication_desired_2_confirmed_2)
260       # Cannot clear just one at a time.
261       assert_raise ArvadosModel::PermissionDeniedError do
262         c.update_attributes replication_confirmed: nil
263       end
264       c.reload
265       assert_raise ArvadosModel::PermissionDeniedError do
266         c.update_attributes replication_confirmed_at: nil
267       end
268       # Can clear both at once.
269       c.reload
270       assert c.update_attributes(replication_confirmed: nil,
271                                  replication_confirmed_at: nil)
272     end
273   end
274
275   test "clear replication_confirmed* when introducing a new block in manifest" do
276     c = collections(:replication_desired_2_confirmed_2)
277     act_as_user users(:active) do
278       assert c.update_attributes(manifest_text: collections(:user_agreement).signed_manifest_text)
279       assert_nil c.replication_confirmed
280       assert_nil c.replication_confirmed_at
281     end
282   end
283
284   test "don't clear replication_confirmed* when just renaming a file" do
285     c = collections(:replication_desired_2_confirmed_2)
286     act_as_user users(:active) do
287       new_manifest = c.signed_manifest_text.sub(':bar', ':foo')
288       assert c.update_attributes(manifest_text: new_manifest)
289       assert_equal 2, c.replication_confirmed
290       assert_not_nil c.replication_confirmed_at
291     end
292   end
293
294   test "don't clear replication_confirmed* when just deleting a data block" do
295     c = collections(:replication_desired_2_confirmed_2)
296     act_as_user users(:active) do
297       new_manifest = c.signed_manifest_text
298       new_manifest.sub!(/ \S+:bar/, '')
299       new_manifest.sub!(/ acbd\S+/, '')
300
301       # Confirm that we did just remove a block from the manifest (if
302       # not, this test would pass without testing the relevant case):
303       assert_operator new_manifest.length+40, :<, c.signed_manifest_text.length
304
305       assert c.update_attributes(manifest_text: new_manifest)
306       assert_equal 2, c.replication_confirmed
307       assert_not_nil c.replication_confirmed_at
308     end
309   end
310
311   test 'signature expiry does not exceed trash_at' do
312     act_as_user users(:active) do
313       t0 = db_current_time
314       c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n", name: 'foo')
315       c.update_attributes! trash_at: (t0 + 1.hours)
316       c.reload
317       sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
318       assert_operator sig_exp.to_i, :<=, (t0 + 1.hours).to_i
319     end
320   end
321
322   test 'far-future expiry date cannot be used to circumvent configured permission ttl' do
323     act_as_user users(:active) do
324       c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n",
325                              name: 'foo',
326                              trash_at: db_current_time + 1.years)
327       sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
328       expect_max_sig_exp = db_current_time.to_i + Rails.configuration.blob_signature_ttl
329       assert_operator c.trash_at.to_i, :>, expect_max_sig_exp
330       assert_operator sig_exp.to_i, :<=, expect_max_sig_exp
331     end
332   end
333
334   test "create collection with properties" do
335     act_as_system_user do
336       c = Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n",
337                             properties: {'property_1' => 'value_1'})
338       assert c.valid?
339       assert_equal 'value_1', c.properties['property_1']
340     end
341   end
342
343   test 'create, delete, recreate collection with same name and owner' do
344     act_as_user users(:active) do
345       # create collection with name
346       c = Collection.create(manifest_text: '',
347                             name: "test collection name")
348       assert c.valid?
349       uuid = c.uuid
350
351       # mark collection as expired
352       c.update_attributes!(trash_at: Time.new.strftime("%Y-%m-%d"))
353       c = Collection.where(uuid: uuid)
354       assert_empty c, 'Should not be able to find expired collection'
355
356       # recreate collection with the same name
357       c = Collection.create(manifest_text: '',
358                             name: "test collection name")
359       assert c.valid?
360     end
361   end
362
363   test 'trash_at cannot be set too far in the past' do
364     act_as_user users(:active) do
365       t0 = db_current_time
366       c = Collection.create!(manifest_text: '', name: 'foo')
367       c.update_attributes! trash_at: (t0 - 2.weeks)
368       c.reload
369       assert_operator c.trash_at, :>, t0
370     end
371   end
372
373   [['trash-to-delete interval negative',
374     :collection_owned_by_active,
375     {trash_at: Time.now+2.weeks, delete_at: Time.now},
376     {state: :invalid}],
377    ['trash-to-delete interval too short',
378     :collection_owned_by_active,
379     {trash_at: Time.now+3.days, delete_at: Time.now+7.days},
380     {state: :invalid}],
381    ['trash-to-delete interval ok',
382     :collection_owned_by_active,
383     {trash_at: Time.now, delete_at: Time.now+15.days},
384     {state: :trash_now}],
385    ['trash-to-delete interval short, but far enough in future',
386     :collection_owned_by_active,
387     {trash_at: Time.now+13.days, delete_at: Time.now+15.days},
388     {state: :trash_future}],
389    ['trash by setting is_trashed bool',
390     :collection_owned_by_active,
391     {is_trashed: true},
392     {state: :trash_now}],
393    ['trash in future by setting just trash_at',
394     :collection_owned_by_active,
395     {trash_at: Time.now+1.week},
396     {state: :trash_future}],
397    ['trash in future by setting trash_at and delete_at',
398     :collection_owned_by_active,
399     {trash_at: Time.now+1.week, delete_at: Time.now+4.weeks},
400     {state: :trash_future}],
401    ['untrash by clearing is_trashed bool',
402     :expired_collection,
403     {is_trashed: false},
404     {state: :not_trash}],
405   ].each do |test_name, fixture_name, updates, expect|
406     test test_name do
407       act_as_user users(:active) do
408         min_exp = (db_current_time +
409                    Rails.configuration.blob_signature_ttl.seconds)
410         if fixture_name == :expired_collection
411           # Fixture-finder shorthand doesn't find trashed collections
412           # because they're not in the default scope.
413           c = Collection.unscoped.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3ih')
414         else
415           c = collections(fixture_name)
416         end
417         updates_ok = c.update_attributes(updates)
418         expect_valid = expect[:state] != :invalid
419         assert_equal updates_ok, expect_valid, c.errors.full_messages.to_s
420         case expect[:state]
421         when :invalid
422           refute c.valid?
423         when :trash_now
424           assert c.is_trashed
425           assert_not_nil c.trash_at
426           assert_operator c.trash_at, :<=, db_current_time
427           assert_not_nil c.delete_at
428           assert_operator c.delete_at, :>=, min_exp
429         when :trash_future
430           refute c.is_trashed
431           assert_not_nil c.trash_at
432           assert_operator c.trash_at, :>, db_current_time
433           assert_not_nil c.delete_at
434           assert_operator c.delete_at, :>=, c.trash_at
435           # Currently this minimum interval is needed to prevent early
436           # garbage collection:
437           assert_operator c.delete_at, :>=, min_exp
438         when :not_trash
439           refute c.is_trashed
440           assert_nil c.trash_at
441           assert_nil c.delete_at
442         else
443           raise "bad expect[:state]==#{expect[:state].inspect} in test case"
444         end
445       end
446     end
447   end
448
449   test 'default trash interval > blob signature ttl' do
450     Rails.configuration.default_trash_lifetime = 86400 * 21 # 3 weeks
451     start = db_current_time
452     act_as_user users(:active) do
453       c = Collection.create!(manifest_text: '', name: 'foo')
454       c.update_attributes!(trash_at: start + 86400.seconds)
455       assert_operator c.delete_at, :>=, start + (86400*22).seconds
456       assert_operator c.delete_at, :<, start + (86400*22 + 30).seconds
457       c.destroy
458
459       c = Collection.create!(manifest_text: '', name: 'foo')
460       c.update_attributes!(is_trashed: true)
461       assert_operator c.delete_at, :>=, start + (86400*21).seconds
462     end
463   end
464
465   test "find_all_for_docker_image resolves names that look like hashes" do
466     coll_list = Collection.
467       find_all_for_docker_image('a' * 64, nil, [users(:active)])
468     coll_uuids = coll_list.map(&:uuid)
469     assert_includes(coll_uuids, collections(:docker_image).uuid)
470   end
471
472   test "move to trash in SweepTrashedCollections" do
473     c = collections(:trashed_on_next_sweep)
474     refute_empty Collection.where('uuid=? and is_trashed=false', c.uuid)
475     assert_raises(ActiveRecord::RecordNotUnique) do
476       act_as_user users(:active) do
477         Collection.create!(owner_uuid: c.owner_uuid,
478                            name: c.name)
479       end
480     end
481     SweepTrashedCollections.sweep_now
482     c = Collection.unscoped.where('uuid=? and is_trashed=true', c.uuid).first
483     assert c
484     act_as_user users(:active) do
485       assert Collection.create!(owner_uuid: c.owner_uuid,
486                                 name: c.name)
487     end
488   end
489
490   test "delete in SweepTrashedCollections" do
491     uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep
492     assert_not_empty Collection.unscoped.where(uuid: uuid)
493     SweepTrashedCollections.sweep_now
494     assert_empty Collection.unscoped.where(uuid: uuid)
495   end
496
497   test "delete referring links in SweepTrashedCollections" do
498     uuid = collections(:trashed_on_next_sweep).uuid
499     act_as_system_user do
500       Link.create!(head_uuid: uuid,
501                    tail_uuid: system_user_uuid,
502                    link_class: 'whatever',
503                    name: 'something')
504     end
505     past = db_current_time
506     Collection.unscoped.where(uuid: uuid).
507       update_all(is_trashed: true, trash_at: past, delete_at: past)
508     assert_not_empty Collection.unscoped.where(uuid: uuid)
509     SweepTrashedCollections.sweep_now
510     assert_empty Collection.unscoped.where(uuid: uuid)
511   end
512 end