1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'sweep_trashed_objects'
8 class CollectionTest < ActiveSupport::TestCase
11 def create_collection name, enc=nil
12 txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
13 txt.force_encoding(enc) if enc
14 return Collection.create(manifest_text: txt, name: name)
17 test 'accept ASCII manifest_text' do
19 c = create_collection 'foo', Encoding::US_ASCII
24 test 'accept UTF-8 manifest_text' do
26 c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
31 test 'refuse manifest_text with invalid UTF-8 byte sequence' do
33 c = create_collection "f\xc8o", Encoding::UTF_8
35 assert_equal [:manifest_text], c.errors.messages.keys
36 assert_match(/UTF-8/, c.errors.messages[:manifest_text].first)
40 test 'refuse manifest_text with non-UTF-8 encoding' do
42 c = create_collection "f\xc8o", Encoding::ASCII_8BIT
44 assert_equal [:manifest_text], c.errors.messages.keys
45 assert_match(/UTF-8/, c.errors.messages[:manifest_text].first)
51 ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
52 "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
53 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
54 ].each do |manifest_text|
55 test "create collection with invalid manifest text #{manifest_text} and expect error" do
57 c = Collection.create(manifest_text: manifest_text)
66 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
67 ].each do |manifest_text|
68 test "create collection with valid manifest text #{manifest_text.inspect} and expect success" do
70 c = Collection.create(manifest_text: manifest_text)
78 ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
79 "d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
80 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt",
81 ].each do |manifest_text|
82 test "update collection with invalid manifest text #{manifest_text} and expect error" do
84 c = create_collection 'foo', Encoding::US_ASCII
87 c.update_attribute 'manifest_text', manifest_text
96 ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
97 ].each do |manifest_text|
98 test "update collection with valid manifest text #{manifest_text.inspect} and expect success" do
100 c = create_collection 'foo', Encoding::US_ASCII
103 c.update_attribute 'manifest_text', manifest_text
109 test "auto-create version after idle setting" do
110 Rails.configuration.collection_versioning = true
111 Rails.configuration.preserve_version_if_idle = 600 # 10 minutes
112 act_as_user users(:active) do
113 # Set up initial collection
114 c = create_collection 'foo', Encoding::US_ASCII
116 assert_equal 1, c.version
117 assert_equal false, c.preserve_version
118 # Make a versionable update, it shouldn't create a new version yet
119 c.update_attributes!({'name' => 'bar'})
121 assert_equal 'bar', c.name
122 assert_equal 1, c.version
123 # Update modified_at to trigger a version auto-creation
124 fifteen_min_ago = Time.now - 15.minutes
125 c.update_column('modified_at', fifteen_min_ago) # Update without validations/callbacks
127 assert_equal fifteen_min_ago.to_i, c.modified_at.to_i
128 c.update_attributes!({'name' => 'baz'})
130 assert_equal 'baz', c.name
131 assert_equal 2, c.version
132 # Make another update, no new version should be created
133 c.update_attributes!({'name' => 'foobar'})
135 assert_equal 'foobar', c.name
136 assert_equal 2, c.version
140 test "preserve_version=false assignment is ignored while being true and not producing a new version" do
141 Rails.configuration.collection_versioning = true
142 Rails.configuration.preserve_version_if_idle = 3600
143 act_as_user users(:active) do
144 # Set up initial collection
145 c = create_collection 'foo', Encoding::US_ASCII
147 assert_equal 1, c.version
148 assert_equal false, c.preserve_version
149 # This update shouldn't produce a new version, as the idle time is not up
150 c.update_attributes!({
152 'preserve_version' => true
155 assert_equal 1, c.version
156 assert_equal 'bar', c.name
157 assert_equal true, c.preserve_version
158 # Make sure preserve_version is not disabled after being enabled, unless
159 # a new version is created.
160 c.update_attributes!({
161 'preserve_version' => false,
162 'replication_desired' => 2
165 assert_equal 1, c.version
166 assert_equal 2, c.replication_desired
167 assert_equal true, c.preserve_version
168 c.update_attributes!({'name' => 'foobar'})
170 assert_equal 2, c.version
171 assert_equal false, c.preserve_version
172 assert_equal 'foobar', c.name
178 ['current_version_uuid', 'zzzzz-4zz18-bv31uwvy3neko21'],
179 ].each do |name, new_value|
180 test "'#{name}' updates on current version collections are not allowed" do
181 act_as_user users(:active) do
182 # Set up initial collection
183 c = create_collection 'foo', Encoding::US_ASCII
185 assert_equal 1, c.version
187 assert_raises(ActiveRecord::RecordInvalid) do
188 c.update_attributes!({
196 test "uuid updates on current version make older versions update their pointers" do
197 Rails.configuration.collection_versioning = true
198 Rails.configuration.preserve_version_if_idle = 0
199 act_as_system_user do
200 # Set up initial collection
201 c = create_collection 'foo', Encoding::US_ASCII
203 assert_equal 1, c.version
204 # Make changes so that a new version is created
205 c.update_attributes!({'name' => 'bar'})
207 assert_equal 2, c.version
208 assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
209 new_uuid = 'zzzzz-4zz18-somefakeuuidnow'
210 assert_empty Collection.where(uuid: new_uuid)
211 # Update UUID on current version, check that both collections point to it
212 c.update_attributes!({'uuid' => new_uuid})
214 assert_equal new_uuid, c.uuid
215 assert_equal 2, Collection.where(current_version_uuid: new_uuid).count
219 test "older versions' modified_at indicate when they're created" do
220 Rails.configuration.collection_versioning = true
221 Rails.configuration.preserve_version_if_idle = 0
222 act_as_user users(:active) do
223 # Set up initial collection
224 c = create_collection 'foo', Encoding::US_ASCII
226 # Make changes so that a new version is created
227 c.update_attributes!({'name' => 'bar'})
229 assert_equal 2, c.version
230 # Get the old version
231 c_old = Collection.where(current_version_uuid: c.uuid, version: 1).first
234 version_creation_datetime = c_old.modified_at.to_f
235 assert_equal c.created_at.to_f, c_old.created_at.to_f
236 # Current version is updated just a few milliseconds before the version is
237 # saved on the database.
238 assert_operator c.modified_at.to_f, :<, version_creation_datetime
240 # Make update on current version so old version get the attribute synced;
241 # its modified_at should not change.
243 c.update_attributes!({'replication_desired' => new_replication})
245 assert_equal new_replication, c.replication_desired
247 assert_equal new_replication, c_old.replication_desired
248 assert_equal version_creation_datetime, c_old.modified_at.to_f
249 assert_operator c.modified_at.to_f, :>, c_old.modified_at.to_f
253 test "past versions should not be directly updatable" do
254 Rails.configuration.collection_versioning = true
255 Rails.configuration.preserve_version_if_idle = 0
256 act_as_system_user do
257 # Set up initial collection
258 c = create_collection 'foo', Encoding::US_ASCII
260 # Make changes so that a new version is created
261 c.update_attributes!({'name' => 'bar'})
263 assert_equal 2, c.version
264 # Get the old version
265 c_old = Collection.where(current_version_uuid: c.uuid, version: 1).first
267 # With collection versioning still being enabled, try to update
268 c_old.name = 'this was foo'
269 assert c_old.invalid?
271 # Try to fool the validator attempting to make c_old to look like a
272 # current version, it should also fail.
273 c_old.current_version_uuid = c_old.uuid
274 assert c_old.invalid?
276 # Now disable collection versioning, it should behave the same way
277 Rails.configuration.collection_versioning = false
278 c_old.name = 'this was foo'
279 assert c_old.invalid?
284 ['owner_uuid', 'zzzzz-tpzed-d9tiejq69daie8f', 'zzzzz-tpzed-xurymjxw79nv3jz'],
285 ['replication_desired', 2, 3],
286 ['storage_classes_desired', ['hot'], ['archive']],
287 ['is_trashed', true, false],
288 ].each do |attr, first_val, second_val|
289 test "sync #{attr} with older versions" do
290 Rails.configuration.collection_versioning = true
291 Rails.configuration.preserve_version_if_idle = 0
292 act_as_system_user do
293 # Set up initial collection
294 c = create_collection 'foo', Encoding::US_ASCII
296 assert_equal 1, c.version
297 assert_not_equal first_val, c.attributes[attr]
298 # Make changes so that a new version is created and a synced field is
300 c.update_attributes!({'name' => 'bar', attr => first_val})
302 assert_equal 2, c.version
303 assert_equal first_val, c.attributes[attr]
304 assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
305 assert_equal first_val, Collection.where(current_version_uuid: c.uuid, version: 1).first.attributes[attr]
306 # Only make an update on the same synced field & check that the previously
307 # created version also gets it.
308 c.update_attributes!({attr => second_val})
310 assert_equal 2, c.version
311 assert_equal second_val, c.attributes[attr]
312 assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
313 assert_equal second_val, Collection.where(current_version_uuid: c.uuid, version: 1).first.attributes[attr]
319 [false, 'name', 'bar', false],
320 [false, 'description', 'The quick brown fox jumps over the lazy dog', false],
321 [false, 'properties', {'new_version' => true}, false],
322 [false, 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n", false],
323 [true, 'name', 'bar', true],
324 [true, 'description', 'The quick brown fox jumps over the lazy dog', true],
325 [true, 'properties', {'new_version' => true}, true],
326 [true, 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n", true],
327 # Non-versionable attribute updates shouldn't create new versions
328 [true, 'replication_desired', 5, false],
329 [false, 'replication_desired', 5, false],
330 ].each do |versioning, attr, val, new_version_expected|
331 test "update #{attr} with versioning #{versioning ? '' : 'not '}enabled should #{new_version_expected ? '' : 'not '}create a new version" do
332 Rails.configuration.collection_versioning = versioning
333 Rails.configuration.preserve_version_if_idle = 0
334 act_as_user users(:active) do
335 # Create initial collection
336 c = create_collection 'foo', Encoding::US_ASCII
338 assert_equal 'foo', c.name
340 # Check current version attributes
341 assert_equal 1, c.version
342 assert_equal c.uuid, c.current_version_uuid
344 # Update attribute and check if version number should be incremented
345 old_value = c.attributes[attr]
346 c.update_attributes!({attr => val})
347 assert_equal new_version_expected, c.version == 2
348 assert_equal val, c.attributes[attr]
350 if versioning && new_version_expected
351 # Search for the snapshot & previous value
352 assert_equal 2, Collection.where(current_version_uuid: c.uuid).count
353 s = Collection.where(current_version_uuid: c.uuid, version: 1).first
355 assert_equal old_value, s.attributes[attr]
357 # If versioning is disabled or no versionable attribute was updated,
358 # only the current version should exist
359 assert_equal 1, Collection.where(current_version_uuid: c.uuid).count
360 assert_equal c, Collection.where(current_version_uuid: c.uuid).first
366 test 'current_version_uuid is ignored during update' do
367 Rails.configuration.collection_versioning = true
368 Rails.configuration.preserve_version_if_idle = 0
369 act_as_user users(:active) do
370 # Create 1st collection
371 col1 = create_collection 'foo', Encoding::US_ASCII
373 assert_equal 1, col1.version
375 # Create 2nd collection, update it so it becomes version:2
376 # (to avoid unique index violation)
377 col2 = create_collection 'bar', Encoding::US_ASCII
379 assert_equal 1, col2.version
380 col2.update_attributes({name: 'baz'})
381 assert_equal 2, col2.version
383 # Try to make col2 a past version of col1. It shouldn't be possible
384 col2.update_attributes({current_version_uuid: col1.uuid})
387 assert_not_equal col1.uuid, col2.current_version_uuid
391 test 'with versioning enabled, simultaneous updates increment version correctly' do
392 Rails.configuration.collection_versioning = true
393 Rails.configuration.preserve_version_if_idle = 0
394 act_as_user users(:active) do
395 # Create initial collection
396 col = create_collection 'foo', Encoding::US_ASCII
398 assert_equal 1, col.version
400 # Simulate simultaneous updates
401 c1 = Collection.where(uuid: col.uuid).first
402 assert_equal 1, c1.version
404 c2 = Collection.where(uuid: col.uuid).first
405 c2.description = 'foo collection'
407 assert_equal 1, c2.version
408 # with_lock forces a reload, so this shouldn't produce an unique violation error
410 assert_equal 3, c2.version
411 assert_equal 'foo collection', c2.description
415 test 'create and update collection and verify file_names' do
416 act_as_system_user do
417 c = create_collection 'foo', Encoding::US_ASCII
419 created_file_names = c.file_names
420 assert created_file_names
421 assert_match(/foo.txt/, c.file_names)
423 c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
424 assert_not_equal created_file_names, c.file_names
425 assert_match(/foo2.txt/, c.file_names)
432 ].each do |manifest_size, allow_truncate|
433 test "create collection with manifest size #{manifest_size} with allow_truncate=#{allow_truncate},
434 and not expect exceptions even on very large manifest texts" do
435 # file_names has a max size, hence there will be no errors even on large manifests
436 act_as_system_user do
439 while manifest_text.length < manifest_size
440 manifest_text += "./blurfl d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylongfilename000000000000#{index}.txt\n"
443 manifest_text += "./laststreamname d41d8cd98f00b204e9800998ecf8427e+0 0:0:veryverylastfilename.txt\n"
444 c = Collection.create(manifest_text: manifest_text)
448 assert_match(/veryverylongfilename0000000000001.txt/, c.file_names)
449 assert_match(/veryverylongfilename0000000000002.txt/, c.file_names)
450 if not allow_truncate
451 assert_match(/veryverylastfilename/, c.file_names)
452 assert_match(/laststreamname/, c.file_names)
458 test "full text search for collections" do
459 # file_names column does not get populated when fixtures are loaded, hence setup test data
460 act_as_system_user do
461 Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n")
462 Collection.create(manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n")
463 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")
468 ['foo bar', false], # no collection matching both
469 ['foo&bar', false], # no collection matching both
470 ['foo|bar', true], # works only no spaces between the words
471 ['Gnu public', true], # both prefixes found, though not consecutively
472 ['Gnu&public', true], # both prefixes found, though not consecutively
473 ['file4', true], # prefix match
474 ['file4.txt', true], # whole string match
475 ['filex', false], # no such prefix
476 ['subdir', true], # prefix matches
479 ['subdir2/subdir3', true],
480 ['subdir2/subdir3/subdir4', true],
481 ['subdir2 file4', true], # look for both prefixes
482 ['subdir4', false], # not a prefix match
483 ].each do |search_filter, expect_results|
484 search_filters = search_filter.split.each {|s| s.concat(':*')}.join('&')
485 results = Collection.where("#{Collection.full_text_tsvector} @@ to_tsquery(?)",
495 test 'portable data hash with missing size hints' do
496 [[". d41d8cd98f00b204e9800998ecf8427e+0+Bar 0:0:x\n",
497 ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"],
498 [". d41d8cd98f00b204e9800998ecf8427e+Foo 0:0:x\n",
499 ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
500 [". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n",
501 ". d41d8cd98f00b204e9800998ecf8427e 0:0:x\n"],
502 ].each do |unportable, portable|
503 c = Collection.new(manifest_text: unportable)
505 assert_equal(Digest::MD5.hexdigest(portable)+"+#{portable.length}",
506 c.portable_data_hash)
510 pdhmanifest = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n"
511 pdhmd5 = Digest::MD5.hexdigest pdhmanifest
514 [true, pdhmd5+'+12345'],
515 [true, pdhmd5+'+'+pdhmanifest.length.to_s],
516 [true, pdhmd5+'+12345+Foo'],
517 [true, pdhmd5+'+Foo'],
518 [false, Digest::MD5.hexdigest(pdhmanifest.strip)],
519 [false, Digest::MD5.hexdigest(pdhmanifest.strip)+'+'+pdhmanifest.length.to_s],
520 [false, pdhmd5[0..30]],
521 [false, pdhmd5[0..30]+'z'],
522 [false, pdhmd5[0..24]+'000000000'],
523 [false, pdhmd5[0..24]+'000000000+0']].each do |isvalid, pdh|
524 test "portable_data_hash #{pdh.inspect} valid? == #{isvalid}" do
525 c = Collection.new manifest_text: pdhmanifest, portable_data_hash: pdh
526 assert_equal isvalid, c.valid?, c.errors.full_messages.to_s
530 test "storage_classes_desired cannot be empty" do
531 act_as_user users(:active) do
532 c = collections(:collection_owned_by_active)
533 c.update_attributes storage_classes_desired: ["hot"]
534 assert_equal ["hot"], c.storage_classes_desired
535 assert_raise ArvadosModel::InvalidStateTransitionError do
536 c.update_attributes storage_classes_desired: []
541 test "storage classes lists should only contain non-empty strings" do
542 c = collections(:storage_classes_desired_default_unconfirmed)
543 act_as_user users(:admin) do
544 assert c.update_attributes(storage_classes_desired: ["default", "a_string"],
545 storage_classes_confirmed: ["another_string"])
547 ["storage_classes_desired", ["default", 42]],
548 ["storage_classes_confirmed", [{the_answer: 42}]],
549 ["storage_classes_desired", ["default", ""]],
550 ["storage_classes_confirmed", [""]],
551 ].each do |attr, val|
552 assert_raise ArvadosModel::InvalidStateTransitionError do
553 assert c.update_attributes({attr => val})
559 test "storage_classes_confirmed* can be set by admin user" do
560 c = collections(:storage_classes_desired_default_unconfirmed)
561 act_as_user users(:admin) do
562 assert c.update_attributes(storage_classes_confirmed: ["default"],
563 storage_classes_confirmed_at: Time.now)
567 test "storage_classes_confirmed* cannot be set by non-admin user" do
568 act_as_user users(:active) do
569 c = collections(:storage_classes_desired_default_unconfirmed)
570 # Cannot set just one at a time.
571 assert_raise ArvadosModel::PermissionDeniedError do
572 c.update_attributes storage_classes_confirmed: ["default"]
575 assert_raise ArvadosModel::PermissionDeniedError do
576 c.update_attributes storage_classes_confirmed_at: Time.now
578 # Cannot set bot at once, either.
580 assert_raise ArvadosModel::PermissionDeniedError do
581 assert c.update_attributes(storage_classes_confirmed: ["default"],
582 storage_classes_confirmed_at: Time.now)
587 test "storage_classes_confirmed* can be cleared (but only together) by non-admin user" do
588 act_as_user users(:active) do
589 c = collections(:storage_classes_desired_default_confirmed_default)
590 # Cannot clear just one at a time.
591 assert_raise ArvadosModel::PermissionDeniedError do
592 c.update_attributes storage_classes_confirmed: []
595 assert_raise ArvadosModel::PermissionDeniedError do
596 c.update_attributes storage_classes_confirmed_at: nil
598 # Can clear both at once.
600 assert c.update_attributes(storage_classes_confirmed: [],
601 storage_classes_confirmed_at: nil)
605 [0, 2, 4, nil].each do |ask|
606 test "set replication_desired to #{ask.inspect}" do
607 Rails.configuration.default_collection_replication = 2
608 act_as_user users(:active) do
609 c = collections(:replication_undesired_unconfirmed)
610 c.update_attributes replication_desired: ask
611 assert_equal ask, c.replication_desired
616 test "replication_confirmed* can be set by admin user" do
617 c = collections(:replication_desired_2_unconfirmed)
618 act_as_user users(:admin) do
619 assert c.update_attributes(replication_confirmed: 2,
620 replication_confirmed_at: Time.now)
624 test "replication_confirmed* cannot be set by non-admin user" do
625 act_as_user users(:active) do
626 c = collections(:replication_desired_2_unconfirmed)
627 # Cannot set just one at a time.
628 assert_raise ArvadosModel::PermissionDeniedError do
629 c.update_attributes replication_confirmed: 1
631 assert_raise ArvadosModel::PermissionDeniedError do
632 c.update_attributes replication_confirmed_at: Time.now
634 # Cannot set both at once, either.
635 assert_raise ArvadosModel::PermissionDeniedError do
636 c.update_attributes(replication_confirmed: 1,
637 replication_confirmed_at: Time.now)
642 test "replication_confirmed* can be cleared (but only together) by non-admin user" do
643 act_as_user users(:active) do
644 c = collections(:replication_desired_2_confirmed_2)
645 # Cannot clear just one at a time.
646 assert_raise ArvadosModel::PermissionDeniedError do
647 c.update_attributes replication_confirmed: nil
650 assert_raise ArvadosModel::PermissionDeniedError do
651 c.update_attributes replication_confirmed_at: nil
653 # Can clear both at once.
655 assert c.update_attributes(replication_confirmed: nil,
656 replication_confirmed_at: nil)
660 test "clear replication_confirmed* when introducing a new block in manifest" do
661 c = collections(:replication_desired_2_confirmed_2)
662 act_as_user users(:active) do
663 assert c.update_attributes(manifest_text: collections(:user_agreement).signed_manifest_text)
664 assert_nil c.replication_confirmed
665 assert_nil c.replication_confirmed_at
669 test "don't clear replication_confirmed* when just renaming a file" do
670 c = collections(:replication_desired_2_confirmed_2)
671 act_as_user users(:active) do
672 new_manifest = c.signed_manifest_text.sub(':bar', ':foo')
673 assert c.update_attributes(manifest_text: new_manifest)
674 assert_equal 2, c.replication_confirmed
675 assert_not_nil c.replication_confirmed_at
679 test "don't clear replication_confirmed* when just deleting a data block" do
680 c = collections(:replication_desired_2_confirmed_2)
681 act_as_user users(:active) do
682 new_manifest = c.signed_manifest_text
683 new_manifest.sub!(/ \S+:bar/, '')
684 new_manifest.sub!(/ acbd\S+/, '')
686 # Confirm that we did just remove a block from the manifest (if
687 # not, this test would pass without testing the relevant case):
688 assert_operator new_manifest.length+40, :<, c.signed_manifest_text.length
690 assert c.update_attributes(manifest_text: new_manifest)
691 assert_equal 2, c.replication_confirmed
692 assert_not_nil c.replication_confirmed_at
696 test 'signature expiry does not exceed trash_at' do
697 act_as_user users(:active) do
699 c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n", name: 'foo')
700 c.update_attributes! trash_at: (t0 + 1.hours)
702 sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
703 assert_operator sig_exp.to_i, :<=, (t0 + 1.hours).to_i
707 test 'far-future expiry date cannot be used to circumvent configured permission ttl' do
708 act_as_user users(:active) do
709 c = Collection.create!(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:x\n",
711 trash_at: db_current_time + 1.years)
712 sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i
713 expect_max_sig_exp = db_current_time.to_i + Rails.configuration.blob_signature_ttl
714 assert_operator c.trash_at.to_i, :>, expect_max_sig_exp
715 assert_operator sig_exp.to_i, :<=, expect_max_sig_exp
719 test "create collection with properties" do
720 act_as_system_user do
721 c = Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n",
722 properties: {'property_1' => 'value_1'})
724 assert_equal 'value_1', c.properties['property_1']
728 test 'create, delete, recreate collection with same name and owner' do
729 act_as_user users(:active) do
730 # create collection with name
731 c = Collection.create(manifest_text: '',
732 name: "test collection name")
736 c = Collection.readable_by(current_user).where(uuid: uuid)
737 assert_not_empty c, 'Should be able to find live collection'
739 # mark collection as expired
740 c.first.update_attributes!(trash_at: Time.new.strftime("%Y-%m-%d"))
741 c = Collection.readable_by(current_user).where(uuid: uuid)
742 assert_empty c, 'Should not be able to find expired collection'
744 # recreate collection with the same name
745 c = Collection.create(manifest_text: '',
746 name: "test collection name")
751 test 'trash_at cannot be set too far in the past' do
752 act_as_user users(:active) do
754 c = Collection.create!(manifest_text: '', name: 'foo')
755 c.update_attributes! trash_at: (t0 - 2.weeks)
757 assert_operator c.trash_at, :>, t0
762 [['trash-to-delete interval negative',
763 :collection_owned_by_active,
764 {trash_at: now+2.weeks, delete_at: now},
766 ['now-to-delete interval short',
767 :collection_owned_by_active,
768 {trash_at: now+3.days, delete_at: now+7.days},
769 {state: :trash_future}],
770 ['now-to-delete interval short, trash=delete',
771 :collection_owned_by_active,
772 {trash_at: now+3.days, delete_at: now+3.days},
773 {state: :trash_future}],
774 ['trash-to-delete interval ok',
775 :collection_owned_by_active,
776 {trash_at: now, delete_at: now+15.days},
777 {state: :trash_now}],
778 ['trash-to-delete interval short, but far enough in future',
779 :collection_owned_by_active,
780 {trash_at: now+13.days, delete_at: now+15.days},
781 {state: :trash_future}],
782 ['trash by setting is_trashed bool',
783 :collection_owned_by_active,
785 {state: :trash_now}],
786 ['trash in future by setting just trash_at',
787 :collection_owned_by_active,
788 {trash_at: now+1.week},
789 {state: :trash_future}],
790 ['trash in future by setting trash_at and delete_at',
791 :collection_owned_by_active,
792 {trash_at: now+1.week, delete_at: now+4.weeks},
793 {state: :trash_future}],
794 ['untrash by clearing is_trashed bool',
797 {state: :not_trash}],
798 ].each do |test_name, fixture_name, updates, expect|
800 act_as_user users(:active) do
801 min_exp = (db_current_time +
802 Rails.configuration.blob_signature_ttl.seconds)
803 if fixture_name == :expired_collection
804 # Fixture-finder shorthand doesn't find trashed collections
805 # because they're not in the default scope.
806 c = Collection.find_by_uuid('zzzzz-4zz18-mto52zx1s7sn3ih')
808 c = collections(fixture_name)
810 updates_ok = c.update_attributes(updates)
811 expect_valid = expect[:state] != :invalid
812 assert_equal expect_valid, updates_ok, c.errors.full_messages.to_s
818 assert_not_nil c.trash_at
819 assert_operator c.trash_at, :<=, db_current_time
820 assert_not_nil c.delete_at
821 assert_operator c.delete_at, :>=, min_exp
824 assert_not_nil c.trash_at
825 assert_operator c.trash_at, :>, db_current_time
826 assert_not_nil c.delete_at
827 assert_operator c.delete_at, :>=, c.trash_at
828 # Currently this minimum interval is needed to prevent early
829 # garbage collection:
830 assert_operator c.delete_at, :>=, min_exp
833 assert_nil c.trash_at
834 assert_nil c.delete_at
836 raise "bad expect[:state]==#{expect[:state].inspect} in test case"
842 test 'default trash interval > blob signature ttl' do
843 Rails.configuration.default_trash_lifetime = 86400 * 21 # 3 weeks
844 start = db_current_time
845 act_as_user users(:active) do
846 c = Collection.create!(manifest_text: '', name: 'foo')
847 c.update_attributes!(trash_at: start + 86400.seconds)
848 assert_operator c.delete_at, :>=, start + (86400*22).seconds
849 assert_operator c.delete_at, :<, start + (86400*22 + 30).seconds
852 c = Collection.create!(manifest_text: '', name: 'foo')
853 c.update_attributes!(is_trashed: true)
854 assert_operator c.delete_at, :>=, start + (86400*21).seconds
858 test "find_all_for_docker_image resolves names that look like hashes" do
859 coll_list = Collection.
860 find_all_for_docker_image('a' * 64, nil, [users(:active)])
861 coll_uuids = coll_list.map(&:uuid)
862 assert_includes(coll_uuids, collections(:docker_image).uuid)
865 test "move collections to trash in SweepTrashedObjects" do
866 c = collections(:trashed_on_next_sweep)
867 refute_empty Collection.where('uuid=? and is_trashed=false', c.uuid)
868 assert_raises(ActiveRecord::RecordNotUnique) do
869 act_as_user users(:active) do
870 Collection.create!(owner_uuid: c.owner_uuid,
874 SweepTrashedObjects.sweep_now
875 c = Collection.where('uuid=? and is_trashed=true', c.uuid).first
877 act_as_user users(:active) do
878 assert Collection.create!(owner_uuid: c.owner_uuid,
883 test "delete collections in SweepTrashedObjects" do
884 uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep
885 assert_not_empty Collection.where(uuid: uuid)
886 SweepTrashedObjects.sweep_now
887 assert_empty Collection.where(uuid: uuid)
890 test "delete referring links in SweepTrashedObjects" do
891 uuid = collections(:trashed_on_next_sweep).uuid
892 act_as_system_user do
893 Link.create!(head_uuid: uuid,
894 tail_uuid: system_user_uuid,
895 link_class: 'whatever',
898 past = db_current_time
899 Collection.where(uuid: uuid).
900 update_all(is_trashed: true, trash_at: past, delete_at: past)
901 assert_not_empty Collection.where(uuid: uuid)
902 SweepTrashedObjects.sweep_now
903 assert_empty Collection.where(uuid: uuid)