MCPcopy
hub / github.com/apache/casbin / TestKeyMatchModelInMemory

Function TestKeyMatchModelInMemory

enforcer_test.go:28–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestKeyMatchModelInMemory(t *testing.T) {
29 m := model.NewModel()
30 m.AddDef("r", "r", "sub, obj, act")
31 m.AddDef("p", "p", "sub, obj, act")
32 m.AddDef("e", "e", "some(where (p.eft == allow))")
33 m.AddDef("m", "m", "r.sub == p.sub && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)")
34
35 a := fileadapter.NewAdapter("examples/keymatch_policy.csv")
36
37 e, _ := NewEnforcer(m, a)
38
39 testEnforce(t, e, "alice", "/alice_data/resource1", "GET", true)
40 testEnforce(t, e, "alice", "/alice_data/resource1", "POST", true)
41 testEnforce(t, e, "alice", "/alice_data/resource2", "GET", true)
42 testEnforce(t, e, "alice", "/alice_data/resource2", "POST", false)
43 testEnforce(t, e, "alice", "/bob_data/resource1", "GET", false)
44 testEnforce(t, e, "alice", "/bob_data/resource1", "POST", false)
45 testEnforce(t, e, "alice", "/bob_data/resource2", "GET", false)
46 testEnforce(t, e, "alice", "/bob_data/resource2", "POST", false)
47
48 testEnforce(t, e, "bob", "/alice_data/resource1", "GET", false)
49 testEnforce(t, e, "bob", "/alice_data/resource1", "POST", false)
50 testEnforce(t, e, "bob", "/alice_data/resource2", "GET", true)
51 testEnforce(t, e, "bob", "/alice_data/resource2", "POST", false)
52 testEnforce(t, e, "bob", "/bob_data/resource1", "GET", false)
53 testEnforce(t, e, "bob", "/bob_data/resource1", "POST", true)
54 testEnforce(t, e, "bob", "/bob_data/resource2", "GET", false)
55 testEnforce(t, e, "bob", "/bob_data/resource2", "POST", true)
56
57 testEnforce(t, e, "cathy", "/cathy_data", "GET", true)
58 testEnforce(t, e, "cathy", "/cathy_data", "POST", true)
59 testEnforce(t, e, "cathy", "/cathy_data", "DELETE", false)
60
61 e, _ = NewEnforcer(m)
62 _ = a.LoadPolicy(e.GetModel())
63
64 testEnforce(t, e, "alice", "/alice_data/resource1", "GET", true)
65 testEnforce(t, e, "alice", "/alice_data/resource1", "POST", true)
66 testEnforce(t, e, "alice", "/alice_data/resource2", "GET", true)
67 testEnforce(t, e, "alice", "/alice_data/resource2", "POST", false)
68 testEnforce(t, e, "alice", "/bob_data/resource1", "GET", false)
69 testEnforce(t, e, "alice", "/bob_data/resource1", "POST", false)
70 testEnforce(t, e, "alice", "/bob_data/resource2", "GET", false)
71 testEnforce(t, e, "alice", "/bob_data/resource2", "POST", false)
72
73 testEnforce(t, e, "bob", "/alice_data/resource1", "GET", false)
74 testEnforce(t, e, "bob", "/alice_data/resource1", "POST", false)
75 testEnforce(t, e, "bob", "/alice_data/resource2", "GET", true)
76 testEnforce(t, e, "bob", "/alice_data/resource2", "POST", false)
77 testEnforce(t, e, "bob", "/bob_data/resource1", "GET", false)
78 testEnforce(t, e, "bob", "/bob_data/resource1", "POST", true)
79 testEnforce(t, e, "bob", "/bob_data/resource2", "GET", false)
80 testEnforce(t, e, "bob", "/bob_data/resource2", "POST", true)
81
82 testEnforce(t, e, "cathy", "/cathy_data", "GET", true)
83 testEnforce(t, e, "cathy", "/cathy_data", "POST", true)
84 testEnforce(t, e, "cathy", "/cathy_data", "DELETE", false)
85}

Callers

nothing calls this directly

Calls 6

AddDefMethod · 0.95
LoadPolicyMethod · 0.95
GetModelMethod · 0.95
NewModelFunction · 0.92
NewEnforcerFunction · 0.85
testEnforceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…