-
Notifications
You must be signed in to change notification settings - Fork 0
/
enforcer_test.go
50 lines (39 loc) · 1.13 KB
/
enforcer_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
package casbin_test
import (
"strings"
"testing"
"github.com/sdcxtech/casbin"
"github.com/sdcxtech/casbin/core"
"github.com/sdcxtech/casbin/effector"
"github.com/sdcxtech/casbin/load"
"github.com/stretchr/testify/assert"
)
func TestEnforcer(t *testing.T) {
policy, err := core.NewAssertionSchema("sub, obj, act")
assert.NoError(t, err)
request, err := core.NewAssertionSchema("sub, obj, act")
assert.NoError(t, err)
rolesSchema := make(core.RolesSchema)
rolesSchema["g"] = core.RoleSchema{
Type: core.RoleTypeWithoutDomain,
}
matchers, err := core.MatchersConfig{
Roles: rolesSchema,
Define: map[string]string{
"m": "g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act",
},
}.New()
assert.NoError(t, err)
m := core.NewModel(policy, request, rolesSchema, effector.NewAllowOverride(), matchers)
csv := `
p, staff, order, get
g, admin, staff
`
reader := strings.NewReader(csv)
itr := load.NewCSVIterator(reader)
enforcer, err := casbin.NewEnforcer(m, itr)
assert.NoError(t, err)
allow, err := enforcer.Enforce(casbin.Request("admin", "order", "get"))
assert.NoError(t, err)
assert.True(t, allow)
}