Skip to content

Commit 8d2df58

Browse files
committed
Increased test coverage to include clearing, adding and saving policies.
1 parent f66c60e commit 8d2df58

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

adapter_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,54 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
3333
}
3434
}
3535

36+
func errorExpected(t *testing.T, err error) {
37+
if err == nil {
38+
t.Error("expected error")
39+
}
40+
}
41+
3642
func TestAdapter(t *testing.T) {
3743
b, _ := ioutil.ReadFile(filepath.Join("examples", "rbac_policy.json"))
3844
a := NewAdapter(&b)
3945
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
46+
e.GetPolicy()
4047

4148
// Now the JSON Buffer has policy, so we can provide a normal use case.
4249
// Create an adapter and an enforcer.
4350
// NewEnforcer() will load the policy automatically.
4451
a = NewAdapter(&b)
4552
e, _ = casbin.NewEnforcer("examples/rbac_model.conf", a)
4653
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
54+
55+
//Test Clear Policy
56+
e.ClearPolicy()
57+
testGetPolicy(t, e, [][]string{})
58+
59+
// Test Add Policy
60+
_, _ = e.AddPolicy("alice", "data1", "read")
61+
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}})
62+
63+
// Add policies with up to 6 rule elements
64+
_, _ = e.AddPolicy("alice", "data1", "read", "indeterminate")
65+
_, _ = e.AddPolicy("alice", "domain1", "data1", "write", "indeterminate")
66+
_, _ = e.AddPolicy("alice", "domain1", "data1", "write", "indeterminate", "foo")
67+
_, _ = e.AddPolicy("alice", "domain1", "data1", "write", "indeterminate", "foo", "bar")
68+
69+
// Add grouping policy
70+
_, _ = e.AddGroupingPolicy("alice", "data2_admin")
71+
72+
// Test Save Policy
73+
expectedPolicies := len(e.GetPolicy()) + len(e.GetGroupingPolicy())
74+
_ = e.SavePolicy()
75+
if len(a.policy) != expectedPolicies {
76+
t.Errorf("expected %d policies, got %d", expectedPolicies, len(a.policy))
77+
}
78+
79+
// Not implemented methods
80+
err := a.AddPolicy("", "", []string{""})
81+
errorExpected(t, err)
82+
err = a.RemovePolicy("", "", []string{""})
83+
errorExpected(t, err)
84+
err = a.RemoveFilteredPolicy("", "", 0, "")
85+
errorExpected(t, err)
4786
}

0 commit comments

Comments
 (0)