-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole_hierarchy_test.go
65 lines (53 loc) · 1.27 KB
/
role_hierarchy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package trust
import (
"testing"
)
func TestHierarchyRole(t *testing.T) {
r := NewRoleHierarchy("role-x")
if r.Name() != "role-x" {
t.Fatal("RoleH ")
}
r1 := NewRoleHierarchy("role-1")
r2 := NewRoleHierarchy("role-2")
r.AttachRole(r1)
r.AttachRole(r2)
r21 := NewRoleHierarchy("role-2-1")
p21 := NewPermissionBase("perm-2-1")
r21.Assign(p21)
r2.AttachRole(r21)
if !r.Permit("perm-2-1") {
t.Fatal("RoleH: parent does not have role child > child role")
}
if r.Permit("role-2-2") {
t.Fatal("RoleH: has some role")
}
p := NewPermissionBase("perm-1")
r.Assign(p)
if !r.Permit("perm-1") {
t.Fatal("RoleH: cant find permission")
}
p1 := NewPermissionBase("perm-2-1[1]")
p2 := NewPermissionBase("perm-2-1[2]")
r21.Assign(p1)
r21.Assign(p2)
if !r.Permit("perm-2-1[1]") {
t.Fatal("RoleH: perm not found")
}
}
func TestHierarchyRoleErrors(t *testing.T) {
r := NewRoleHierarchy("role")
r1 := NewRoleHierarchy("role-1")
r2 := NewRoleHierarchy("role-2")
r.AttachRole(r1)
r.AttachRole(r2)
r1Bad := NewRoleBase("role-1")
err := r.AttachRole(r1Bad)
if err != ErrRoleAlreadyAttached {
t.Fatal("role must have an assigned role")
}
r21 := NewRoleBase("role")
err = r2.AttachRole(r21)
if err != ErrRecursiveRoleDetectLoop {
t.Fatal("role must have a loop")
}
}