X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/cf1dcfb8715822f7ac5fe4fce067197dd84bad54..8f9ba1d6405c397c40de94e155cea0c84120ff0c:/services/api/test/unit/collection_test.rb diff --git a/services/api/test/unit/collection_test.rb b/services/api/test/unit/collection_test.rb index 477f9e2750..e8cd21a243 100644 --- a/services/api/test/unit/collection_test.rb +++ b/services/api/test/unit/collection_test.rb @@ -266,6 +266,67 @@ class CollectionTest < ActiveSupport::TestCase end end + # This test exposes a bug related to JSONB attributes, see #15725. + test "recently loaded collection shouldn't list changed attributes" do + col = Collection.where("properties != '{}'::jsonb").limit(1).first + refute col.properties_changed?, 'Properties field should not be seen as changed' + end + + [ + [ + true, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + {:foo=>:bar, :lst=>[1, 3, 5, 7], :hsh=>{'baz'=>'qux', :foobar=>true, 'hsh'=>{:nested=>true}}, :delete_at=>nil}, + ], + [ + true, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + {'delete_at'=>nil, 'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}}, + ], + [ + true, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + {'delete_at'=>nil, 'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'foobar'=>true, 'hsh'=>{'nested'=>true}, 'baz'=>'qux'}}, + ], + [ + false, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 42], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + ], + [ + false, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + {'foo'=>'bar', 'lst'=>[1, 3, 7, 5], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + ], + [ + false, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>false}}, 'delete_at'=>nil}, + ], + [ + false, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>nil}, + {'foo'=>'bar', 'lst'=>[1, 3, 5, 7], 'hsh'=>{'baz'=>'qux', 'foobar'=>true, 'hsh'=>{'nested'=>true}}, 'delete_at'=>1234567890}, + ], + ].each do |should_be_equal, value_1, value_2| + test "JSONB properties #{value_1} is#{should_be_equal ? '' : ' not'} equal to #{value_2}" do + act_as_user users(:active) do + # Set up initial collection + c = create_collection 'foo', Encoding::US_ASCII + assert c.valid? + c.update_attributes!({'properties' => value_1}) + c.reload + assert c.changes.keys.empty? + c.properties = value_2 + if should_be_equal + assert c.changes.keys.empty?, "Properties #{value_1.inspect} should be equal to #{value_2.inspect}" + else + refute c.changes.keys.empty?, "Properties #{value_1.inspect} should not be equal to #{value_2.inspect}" + end + end + end + end + test "older versions' modified_at indicate when they're created" do Rails.configuration.Collections.CollectionVersioning = true Rails.configuration.Collections.PreserveVersionIfIdle = 0 @@ -334,7 +395,6 @@ class CollectionTest < ActiveSupport::TestCase ['owner_uuid', 'zzzzz-tpzed-d9tiejq69daie8f', 'zzzzz-tpzed-xurymjxw79nv3jz'], ['replication_desired', 2, 3], ['storage_classes_desired', ['hot'], ['archive']], - ['is_trashed', true, false], ].each do |attr, first_val, second_val| test "sync #{attr} with older versions" do Rails.configuration.Collections.CollectionVersioning = true @@ -760,7 +820,7 @@ class CollectionTest < ActiveSupport::TestCase name: 'foo', trash_at: db_current_time + 1.years) sig_exp = /\+A[0-9a-f]{40}\@([0-9]+)/.match(c.signed_manifest_text)[1].to_i - expect_max_sig_exp = db_current_time.to_i + Rails.configuration.Collections.BlobSigningTTL + expect_max_sig_exp = db_current_time.to_i + Rails.configuration.Collections.BlobSigningTTL.to_i assert_operator c.trash_at.to_i, :>, expect_max_sig_exp assert_operator sig_exp.to_i, :<=, expect_max_sig_exp end @@ -849,7 +909,7 @@ class CollectionTest < ActiveSupport::TestCase test test_name do act_as_user users(:active) do min_exp = (db_current_time + - Rails.configuration.Collections.BlobSigningTTL.seconds) + Rails.configuration.Collections.BlobSigningTTL) if fixture_name == :expired_collection # Fixture-finder shorthand doesn't find trashed collections # because they're not in the default scope. @@ -952,4 +1012,63 @@ class CollectionTest < ActiveSupport::TestCase SweepTrashedObjects.sweep_now assert_empty Collection.where(uuid: uuid) end + + test "create collections with default properties" do + Rails.configuration.Collections.DefaultProperties = { + 'default_prop1' => {'value' => 'prop1_value'}, + 'responsible_person_uuid' => {'function' => 'original_owner'} + } + # Test collection without initial properties + act_as_user users(:active) do + c = create_collection 'foo', Encoding::US_ASCII + assert c.valid? + assert_not_empty c.properties + assert_equal 'prop1_value', c.properties['default_prop1'] + assert_equal users(:active).uuid, c.properties['responsible_person_uuid'] + end + # Test collection with default_prop1 property already set + act_as_user users(:active) do + c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", + properties: {'default_prop1' => 'custom_value'}) + assert c.valid? + assert_not_empty c.properties + assert_equal 'custom_value', c.properties['default_prop1'] + assert_equal users(:active).uuid, c.properties['responsible_person_uuid'] + end + # Test collection inside a sub project + act_as_user users(:active) do + c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", + owner_uuid: groups(:asubproject).uuid) + assert c.valid? + assert_not_empty c.properties + assert_equal users(:active).uuid, c.properties['responsible_person_uuid'] + end + end + + test "update collection with protected default properties" do + Rails.configuration.Collections.DefaultProperties = { + 'default_prop1' => {'value' => 'prop1_value', 'protected' => true}, + } + act_as_user users(:active) do + c = create_collection 'foo', Encoding::US_ASCII + assert c.valid? + assert_not_empty c.properties + assert_equal 'prop1_value', c.properties['default_prop1'] + # Add new property + c.properties['prop2'] = 'value2' + c.save! + assert_equal 'value2', c.properties['prop2'] + # Try to change protected property's value + c.properties['default_prop1'] = 'new_value' + assert_raises(ArvadosModel::PermissionDeniedError) do + c.save! + end + # Admins are allowed to change protected properties + act_as_system_user do + c.properties['default_prop1'] = 'new_value' + c.save! + assert_equal 'new_value', c.properties['default_prop1'] + end + end + end end