1515package jsonadapter
1616
1717import (
18+ "io/ioutil"
1819 "log"
20+ "path/filepath"
1921 "testing"
2022
2123 "github.com/casbin/casbin/v2"
@@ -31,33 +33,54 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
3133 }
3234}
3335
34- func TestAdapter (t * testing.T ) {
35- // Because the JSON Buffer is empty at first,
36- // so we need to load the policy from the file adapter (.CSV) first.
37- e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" , "examples/rbac_policy.csv" )
36+ func errorExpected (t * testing.T , err error ) {
37+ if err == nil {
38+ t .Error ("expected error" )
39+ }
40+ }
3841
39- b := []byte {}
42+ func TestAdapter (t * testing.T ) {
43+ b , _ := ioutil .ReadFile (filepath .Join ("examples" , "rbac_policy.json" ))
4044 a := NewAdapter (& b )
41- // This is a trick to save the current policy to the JSON Buffer.
42- // We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
43- // The current policy means the policy in the Casbin enforcer (aka in memory).
44- a .SavePolicy (e .GetModel ())
45-
46- // Clear the current policy.
47- e .ClearPolicy ()
48- testGetPolicy (t , e , [][]string {})
49-
50- // Load the policy from JSON Buffer.
51- a .LoadPolicy (e .GetModel ())
52- testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }, {"data2_admin" , "data2" , "read" }, {"data2_admin" , "data2" , "write" }})
53-
54- // Note: you don't need to look at the above code
55- // if you already have a working JSON Buffer with policy inside.
45+ e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" , a )
46+ e .GetPolicy ()
5647
5748 // Now the JSON Buffer has policy, so we can provide a normal use case.
5849 // Create an adapter and an enforcer.
5950 // NewEnforcer() will load the policy automatically.
6051 a = NewAdapter (& b )
6152 e , _ = casbin .NewEnforcer ("examples/rbac_model.conf" , a )
6253 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 )
6386}
0 commit comments