X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/797885e26f85f04123bcba751addcd415ed3db2e..8b873a9b3b8865a4d451263e48b49122b9c32759:/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 d9facba14e..2dd6eedcfb 100644 --- a/services/api/test/unit/collection_test.rb +++ b/services/api/test/unit/collection_test.rb @@ -272,6 +272,61 @@ class CollectionTest < ActiveSupport::TestCase 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 @@ -957,4 +1012,82 @@ class CollectionTest < ActiveSupport::TestCase SweepTrashedObjects.sweep_now assert_empty Collection.where(uuid: uuid) end + + test "empty names are exempt from name uniqueness" do + act_as_user users(:active) do + c1 = Collection.new(name: nil, manifest_text: '', owner_uuid: groups(:aproject).uuid) + assert c1.save + c2 = Collection.new(name: '', manifest_text: '', owner_uuid: groups(:aproject).uuid) + assert c2.save + c3 = Collection.new(name: '', manifest_text: '', owner_uuid: groups(:aproject).uuid) + assert c3.save + c4 = Collection.new(name: 'c4', manifest_text: '', owner_uuid: groups(:aproject).uuid) + assert c4.save + c5 = Collection.new(name: 'c4', manifest_text: '', owner_uuid: groups(:aproject).uuid) + assert_raises(ActiveRecord::RecordNotUnique) do + c5.save + end + end + end + + test "create collections with managed properties" do + Rails.configuration.Collections.ManagedProperties = { + '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 managed properties" do + Rails.configuration.Collections.ManagedProperties = { + '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! + c.reload + 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! + c.reload + assert_equal 'new_value', c.properties['default_prop1'] + end + end + end end