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

Function TestLoadFilteredPolicy

filter_test.go:34–67  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32}
33
34func TestLoadFilteredPolicy(t *testing.T) {
35 e, _ := NewEnforcer()
36
37 adapter := fileadapter.NewFilteredAdapter("examples/rbac_with_domains_policy.csv")
38 _ = e.InitWithAdapter("examples/rbac_with_domains_model.conf", adapter)
39 if err := e.LoadPolicy(); err != nil {
40 t.Errorf("unexpected error in LoadPolicy: %v", err)
41 }
42
43 // validate initial conditions
44 testHasPolicy(t, e, []string{"admin", "domain1", "data1", "read"}, true)
45 testHasPolicy(t, e, []string{"admin", "domain2", "data2", "read"}, true)
46
47 if err := e.LoadFilteredPolicy(&fileadapter.Filter{
48 P: []string{"", "domain1"},
49 G: []string{"", "", "domain1"},
50 }); err != nil {
51 t.Errorf("unexpected error in LoadFilteredPolicy: %v", err)
52 }
53 if !e.IsFiltered() {
54 t.Errorf("adapter did not set the filtered flag correctly")
55 }
56
57 // only policies for domain1 should be loaded
58 testHasPolicy(t, e, []string{"admin", "domain1", "data1", "read"}, true)
59 testHasPolicy(t, e, []string{"admin", "domain2", "data2", "read"}, false)
60
61 if err := e.SavePolicy(); err == nil {
62 t.Errorf("enforcer did not prevent saving filtered policy")
63 }
64 if err := e.GetAdapter().SavePolicy(e.GetModel()); err == nil {
65 t.Errorf("adapter did not prevent saving filtered policy")
66 }
67}
68
69func TestLoadMoreTypeFilteredPolicy(t *testing.T) {
70 e, _ := NewEnforcer()

Callers

nothing calls this directly

Calls 10

InitWithAdapterMethod · 0.95
LoadPolicyMethod · 0.95
LoadFilteredPolicyMethod · 0.95
IsFilteredMethod · 0.95
SavePolicyMethod · 0.95
GetAdapterMethod · 0.95
GetModelMethod · 0.95
NewEnforcerFunction · 0.85
testHasPolicyFunction · 0.85
SavePolicyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…