MCPcopy Create free account
hub / github.com/apache/casbin / TestGetAllowedObjectConditions

Function TestGetAllowedObjectConditions

rbac_api_test.go:574–607  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

572}
573
574func TestGetAllowedObjectConditions(t *testing.T) {
575 e, _ := NewEnforcer("examples/object_conditions_model.conf", "examples/object_conditions_policy.csv")
576 testGetAllowedObjectConditions(t, e, "alice", "read", "r.obj.", []string{"price < 25", "category_id = 2"}, nil)
577 testGetAllowedObjectConditions(t, e, "admin", "read", "r.obj.", []string{"category_id = 2"}, nil)
578 testGetAllowedObjectConditions(t, e, "bob", "write", "r.obj.", []string{"author = bob"}, nil)
579
580 // test ErrEmptyCondition
581 testGetAllowedObjectConditions(t, e, "alice", "write", "r.obj.", []string{}, errors.ErrEmptyCondition)
582 testGetAllowedObjectConditions(t, e, "bob", "read", "r.obj.", []string{}, errors.ErrEmptyCondition)
583
584 // test ErrObjCondition
585 // should : e.AddPolicy("alice", "r.obj.price > 50", "read")
586 ok, _ := e.AddPolicy("alice", "price > 50", "read")
587 if ok {
588 testGetAllowedObjectConditions(t, e, "alice", "read", "r.obj.", []string{}, errors.ErrObjCondition)
589 }
590
591 // test prefix
592 e.ClearPolicy()
593 err := e.GetRoleManager().DeleteLink("alice", "admin")
594 if err != nil {
595 panic(err)
596 }
597 ok, _ = e.AddPolicies([][]string{
598 {"alice", "r.book.price < 25", "read"},
599 {"admin", "r.book.category_id = 2", "read"},
600 {"bob", "r.book.author = bob", "write"},
601 })
602 if ok {
603 testGetAllowedObjectConditions(t, e, "alice", "read", "r.book.", []string{"price < 25"}, nil)
604 testGetAllowedObjectConditions(t, e, "admin", "read", "r.book.", []string{"category_id = 2"}, nil)
605 testGetAllowedObjectConditions(t, e, "bob", "write", "r.book.", []string{"author = bob"}, nil)
606 }
607}
608
609func testGetImplicitUsersForResource(t *testing.T, e *Enforcer, res [][]string, resource string, domain ...string) {
610 t.Helper()

Callers

nothing calls this directly

Calls 7

AddPolicyMethod · 0.95
ClearPolicyMethod · 0.95
GetRoleManagerMethod · 0.95
AddPoliciesMethod · 0.95
NewEnforcerFunction · 0.85
DeleteLinkMethod · 0.65

Tested by

no test coverage detected