X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/43773cb247a1fb744b57070b715bfa5d53a00822..4077a9af0985d3c85f2f2de2bb7a0f6be581e71e:/services/api/test/unit/group_test.rb diff --git a/services/api/test/unit/group_test.rb b/services/api/test/unit/group_test.rb index 778eb0c581..9e0e4fc546 100644 --- a/services/api/test/unit/group_test.rb +++ b/services/api/test/unit/group_test.rb @@ -1,7 +1,59 @@ require 'test_helper' class GroupTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + + test "cannot set owner_uuid to object with existing ownership cycle" do + set_user_from_auth :active_trustedclient + + # First make sure we have lots of permission on the bad group by + # renaming it to "{current name} is mine all mine" + g = groups(:bad_group_has_ownership_cycle_b) + g.name += " is mine all mine" + assert g.save, "active user should be able to modify group #{g.uuid}" + + # Use the group as the owner of a new object + s = Specimen. + create(owner_uuid: groups(:bad_group_has_ownership_cycle_b).uuid) + assert s.valid?, "ownership should pass validation #{s.errors.messages}" + assert_equal false, s.save, "should not save object with #{g.uuid} as owner" + + # Use the group as the new owner of an existing object + s = specimens(:in_aproject) + s.owner_uuid = groups(:bad_group_has_ownership_cycle_b).uuid + assert s.valid?, "ownership should pass validation" + assert_equal false, s.save, "should not save object with #{g.uuid} as owner" + end + + test "cannot create a new ownership cycle" do + set_user_from_auth :active_trustedclient + + g_foo = Group.create!(name: "foo") + g_bar = Group.create!(name: "bar") + + g_foo.owner_uuid = g_bar.uuid + assert g_foo.save, lambda { g_foo.errors.messages } + g_bar.owner_uuid = g_foo.uuid + assert g_bar.valid?, "ownership cycle should not prevent validation" + assert_equal false, g_bar.save, "should not create an ownership loop" + assert g_bar.errors.messages[:owner_uuid].join(" ").match(/ownership cycle/) + end + + test "cannot create a single-object ownership cycle" do + set_user_from_auth :active_trustedclient + + g_foo = Group.create!(name: "foo") + assert g_foo.save + + # Ensure I have permission to manage this group even when its owner changes + perm_link = Link.create!(tail_uuid: users(:active).uuid, + head_uuid: g_foo.uuid, + link_class: 'permission', + name: 'can_manage') + assert perm_link.save + + g_foo.owner_uuid = g_foo.uuid + assert_equal false, g_foo.save, "should not create an ownership loop" + assert g_foo.errors.messages[:owner_uuid].join(" ").match(/ownership cycle/) + end + end