15
15
package jsonadapter
16
16
17
17
import (
18
+ "io/ioutil"
18
19
"log"
20
+ "path/filepath"
19
21
"testing"
20
22
21
23
"github.com/casbin/casbin/v2"
@@ -31,33 +33,54 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
31
33
}
32
34
}
33
35
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
+ }
38
41
39
- b := []byte {}
42
+ func TestAdapter (t * testing.T ) {
43
+ b , _ := ioutil .ReadFile (filepath .Join ("examples" , "rbac_policy.json" ))
40
44
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 ()
56
47
57
48
// Now the JSON Buffer has policy, so we can provide a normal use case.
58
49
// Create an adapter and an enforcer.
59
50
// NewEnforcer() will load the policy automatically.
60
51
a = NewAdapter (& b )
61
52
e , _ = casbin .NewEnforcer ("examples/rbac_model.conf" , a )
62
53
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 )
63
86
}
0 commit comments