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