forked from casbin/casbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
management_api_test.go
117 lines (91 loc) · 4.37 KB
/
management_api_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2017 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package casbin
import (
"github.com/casbin/casbin/util"
"log"
"testing"
)
func testStringList(t *testing.T, title string, f func() []string, res []string) {
myRes := f()
log.Print(title+": ", myRes)
if !util.ArrayEquals(res, myRes) {
t.Error(title+": ", myRes, ", supposed to be ", res)
}
}
func TestGetList(t *testing.T) {
e := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
testStringList(t, "Subjects", e.GetAllSubjects, []string{"alice", "bob", "data2_admin"})
testStringList(t, "Objeccts", e.GetAllObjects, []string{"data1", "data2"})
testStringList(t, "Actions", e.GetAllActions, []string{"read", "write"})
testStringList(t, "Roles", e.GetAllRoles, []string{"data2_admin"})
}
func testGetPolicy(t *testing.T, e *Enforcer, res [][]string) {
myRes := e.GetPolicy()
log.Print("Policy: ", myRes)
if !util.Array2DEquals(res, myRes) {
t.Error("Policy: ", myRes, ", supposed to be ", res)
}
}
func testGetFilteredPolicy(t *testing.T, e *Enforcer, fieldIndex int, fieldValue string, res [][]string) {
myRes := e.GetFilteredPolicy(fieldIndex, fieldValue)
log.Print("Policy for ", fieldValue, ": ", myRes)
if !util.Array2DEquals(res, myRes) {
t.Error("Policy for ", fieldValue, ": ", myRes, ", supposed to be ", res)
}
}
func testGetGroupingPolicy(t *testing.T, e *Enforcer, res [][]string) {
myRes := e.GetGroupingPolicy()
log.Print("Grouping policy: ", myRes)
if !util.Array2DEquals(res, myRes) {
t.Error("Grouping policy: ", myRes, ", supposed to be ", res)
}
}
func TestGetPolicy(t *testing.T) {
e := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
testGetFilteredPolicy(t, e, 0, "alice", [][]string{{"alice", "data1", "read"}})
testGetFilteredPolicy(t, e, 0, "bob", [][]string{{"bob", "data2", "write"}})
testGetFilteredPolicy(t, e, 0, "data2_admin", [][]string{{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
testGetFilteredPolicy(t, e, 1, "data1", [][]string{{"alice", "data1", "read"}})
testGetFilteredPolicy(t, e, 1, "data2", [][]string{{"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
testGetFilteredPolicy(t, e, 2, "read", [][]string{{"alice", "data1", "read"}, {"data2_admin", "data2", "read"}})
testGetFilteredPolicy(t, e, 2, "write", [][]string{{"bob", "data2", "write"}, {"data2_admin", "data2", "write"}})
testGetGroupingPolicy(t, e, [][]string{{"alice", "data2_admin"}})
}
func TestModifyPolicy(t *testing.T) {
e := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
e.RemovePolicy([]string{"alice", "data1", "read"})
e.RemovePolicy([]string{"bob", "data2", "write"})
e.RemovePolicy([]string{"alice", "data1", "read"})
e.AddPolicy([]string{"eve", "data3", "read"})
testGetPolicy(t, e, [][]string{{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"eve", "data3", "read"}})
e.RemoveFilteredPolicy(1, "data2")
testGetPolicy(t, e, [][]string{{"eve", "data3", "read"}})
}
func TestModifyGroupingPolicy(t *testing.T) {
e := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
e.RemoveGroupingPolicy([]string{"alice", "data2_admin"})
e.AddGroupingPolicy([]string{"bob", "data1_admin"})
e.AddGroupingPolicy([]string{"eve", "data3_admin"})
testGetRoles(t, e, "alice", []string{})
testGetRoles(t, e, "bob", []string{"data1_admin"})
testGetRoles(t, e, "eve", []string{"data3_admin"})
testGetRoles(t, e, "non_exist", []string{})
e.RemoveFilteredGroupingPolicy(0, "bob")
testGetRoles(t, e, "alice", []string{})
testGetRoles(t, e, "bob", []string{})
testGetRoles(t, e, "eve", []string{"data3_admin"})
testGetRoles(t, e, "non_exist", []string{})
}