12298: Allow non-null log when cancelling an unrunnable container.
[arvados.git] / services / api / test / unit / group_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class GroupTest < ActiveSupport::TestCase
8
9   test "cannot set owner_uuid to object with existing ownership cycle" do
10     set_user_from_auth :active_trustedclient
11
12     # First make sure we have lots of permission on the bad group by
13     # renaming it to "{current name} is mine all mine"
14     g = groups(:bad_group_has_ownership_cycle_b)
15     g.name += " is mine all mine"
16     assert g.save, "active user should be able to modify group #{g.uuid}"
17
18     # Use the group as the owner of a new object
19     s = Specimen.
20       create(owner_uuid: groups(:bad_group_has_ownership_cycle_b).uuid)
21     assert s.valid?, "ownership should pass validation #{s.errors.messages}"
22     assert_equal false, s.save, "should not save object with #{g.uuid} as owner"
23
24     # Use the group as the new owner of an existing object
25     s = specimens(:in_aproject)
26     s.owner_uuid = groups(:bad_group_has_ownership_cycle_b).uuid
27     assert s.valid?, "ownership should pass validation"
28     assert_equal false, s.save, "should not save object with #{g.uuid} as owner"
29   end
30
31   test "cannot create a new ownership cycle" do
32     set_user_from_auth :active_trustedclient
33
34     g_foo = Group.create!(name: "foo")
35     g_bar = Group.create!(name: "bar")
36
37     g_foo.owner_uuid = g_bar.uuid
38     assert g_foo.save, lambda { g_foo.errors.messages }
39     g_bar.owner_uuid = g_foo.uuid
40     assert g_bar.valid?, "ownership cycle should not prevent validation"
41     assert_equal false, g_bar.save, "should not create an ownership loop"
42     assert g_bar.errors.messages[:owner_uuid].join(" ").match(/ownership cycle/)
43   end
44
45   test "cannot create a single-object ownership cycle" do
46     set_user_from_auth :active_trustedclient
47
48     g_foo = Group.create!(name: "foo")
49     assert g_foo.save
50
51     # Ensure I have permission to manage this group even when its owner changes
52     perm_link = Link.create!(tail_uuid: users(:active).uuid,
53                             head_uuid: g_foo.uuid,
54                             link_class: 'permission',
55                             name: 'can_manage')
56     assert perm_link.save
57
58     g_foo.owner_uuid = g_foo.uuid
59     assert_equal false, g_foo.save, "should not create an ownership loop"
60     assert g_foo.errors.messages[:owner_uuid].join(" ").match(/ownership cycle/)
61   end
62
63 end