MCPcopy Index your code
hub / github.com/apache/casbin / TestRBACModelWithPattern

Function TestRBACModelWithPattern

model_test.go:376–411  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

374}
375
376func TestRBACModelWithPattern(t *testing.T) {
377 e, _ := NewEnforcer("examples/rbac_with_pattern_model.conf", "examples/rbac_with_pattern_policy.csv")
378
379 // Here's a little confusing: the matching function here is not the custom function used in matcher.
380 // It is the matching function used by "g" (and "g2", "g3" if any..)
381 // You can see in policy that: "g2, /book/:id, book_group", so in "g2()" function in the matcher, instead
382 // of checking whether "/book/:id" equals the obj: "/book/1", it checks whether the pattern matches.
383 // You can see it as normal RBAC: "/book/:id" == "/book/1" becomes KeyMatch2("/book/:id", "/book/1")
384 e.AddNamedMatchingFunc("g2", "KeyMatch2", util.KeyMatch2)
385 e.AddNamedMatchingFunc("g", "KeyMatch2", util.KeyMatch2)
386 testEnforce(t, e, "any_user", "/pen3/1", "GET", true)
387 testEnforce(t, e, "/book/user/1", "/pen4/1", "GET", true)
388
389 testEnforce(t, e, "/book/user/1", "/pen4/1", "POST", true)
390
391 testEnforce(t, e, "alice", "/book/1", "GET", true)
392 testEnforce(t, e, "alice", "/book/2", "GET", true)
393 testEnforce(t, e, "alice", "/pen/1", "GET", true)
394 testEnforce(t, e, "alice", "/pen/2", "GET", false)
395 testEnforce(t, e, "bob", "/book/1", "GET", false)
396 testEnforce(t, e, "bob", "/book/2", "GET", false)
397 testEnforce(t, e, "bob", "/pen/1", "GET", true)
398 testEnforce(t, e, "bob", "/pen/2", "GET", true)
399
400 // AddMatchingFunc() is actually setting a function because only one function is allowed,
401 // so when we set "KeyMatch3", we are actually replacing "KeyMatch2" with "KeyMatch3".
402 e.AddNamedMatchingFunc("g2", "KeyMatch2", util.KeyMatch3)
403 testEnforce(t, e, "alice", "/book2/1", "GET", true)
404 testEnforce(t, e, "alice", "/book2/2", "GET", true)
405 testEnforce(t, e, "alice", "/pen2/1", "GET", true)
406 testEnforce(t, e, "alice", "/pen2/2", "GET", false)
407 testEnforce(t, e, "bob", "/book2/1", "GET", false)
408 testEnforce(t, e, "bob", "/book2/2", "GET", false)
409 testEnforce(t, e, "bob", "/pen2/1", "GET", true)
410 testEnforce(t, e, "bob", "/pen2/2", "GET", true)
411}
412
413func TestRBACModelWithDifferentTypesOfRoles(t *testing.T) {
414 e, _ := NewEnforcer("examples/rbac_with_different_types_of_roles_model.conf", "examples/rbac_with_different_types_of_roles_policy.csv")

Callers

nothing calls this directly

Calls 3

AddNamedMatchingFuncMethod · 0.95
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…