-
Notifications
You must be signed in to change notification settings - Fork 2
/
adapter_test.go
180 lines (157 loc) · 4.25 KB
/
adapter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package dynacasbin
import (
"fmt"
"os"
"sort"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/casbin/casbin/v2"
)
var config *aws.Config
var a *Adapter
var data [][]string
func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
myRes := e.GetPolicy()
res1 := make([]string, len(res))
myRes1 := make([]string, len(myRes))
for k, v := range res {
res1[k] = fmt.Sprint(v)
}
for k, v := range myRes {
myRes1[k] = fmt.Sprint(v)
}
sort.Strings(res1)
sort.Strings(myRes1)
if fmt.Sprint(res1) != fmt.Sprint(myRes1) {
t.Error("Policy: ", myRes1, ", supposed to be ", res1)
}
}
func init() {
// Initialize a DynamoDB adapter and use it in a Casbin enforcer:
config = &aws.Config{
Region: aws.String("us-east-2"),
Credentials: credentials.NewSharedCredentials("", ""),
} // Your AWS configuration
ds := "casbin-rules"
var err error
a, err = NewAdapter(config, ds) // Your aws configuration and data source.
if err != nil {
panic(err)
}
data = [][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
}
for _, item := range data {
//init dynamodb if it's empty
err = a.AddPolicy("p", "p", item)
if err != nil {
if strings.Contains(err.Error(), "ResourceNotFoundException") {
_, err = a.CreateTable()
fmt.Println("table not exist, creating... pls try it later")
if err != nil {
fmt.Println("createTable error: " + err.Error())
}
os.Exit(1)
} else {
panic(err)
}
}
}
}
func isPolicyExistInDB(rules [][3]string) bool {
res, err := a.getAllItems()
if err != nil {
panic("adapter getAllItems failed:" + err.Error())
}
isExist := false
for _, v := range res {
for _, item := range rules {
if v.V0 == item[0] && v.V1 == item[1] && v.V2 == item[2] {
isExist = true
}
}
}
return isExist
}
func TestLoadPolicy(t *testing.T) {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
t.Error("failed to create enforcer with `examples/rbac_model.conf`")
return
}
//reload policy, so you call e.LoadPolicy multi times is ok
err = e.LoadPolicy()
// but a.LoadPolicy only can load policy. call it multi times will repeat policy multi times.
//a.LoadPolicy(e.GetModel()) //Do Not Use it.
if err != nil {
t.Error("TestLoadPolicy err:" + err.Error())
return
}
res := make([][]string, 0)
items, err := a.getAllItems()
if err != nil {
panic("adapter getAllItems failed:" + err.Error())
}
for _, item := range items {
res = append(res, []string{item.V0, item.V1, item.V2})
}
testGetPolicy(t, e, res)
}
func TestAddPolicy(t *testing.T) {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
t.Error("failed to create enforcer with `examples/rbac_model.conf`")
return
}
e.AddPolicy("jack", "data3", "read")
if !isPolicyExistInDB([][3]string{{"jack", "data3", "read"}}) {
t.Error("TestAddPolicy is not ok")
}
}
func TestRemovePolicy(t *testing.T) {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
t.Error("failed to create enforcer with `examples/rbac_model.conf`")
return
}
params := []interface{}{"alice", "data1", "read"}
e.RemovePolicy(params...)
if isPolicyExistInDB([][3]string{{"alice", "data1", "read"}}) {
t.Error("TestRemovePolicy is not ok")
}
}
func TestRemoveFilteredPolicy(t *testing.T) {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
t.Error("failed to create enforcer with `examples/rbac_model.conf`")
return
}
e.RemoveFilteredPolicy(0, "data2_admin")
//check load is ok
if isPolicyExistInDB([][3]string{{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}}) {
t.Error("TestRemoveFilteredPolicy is not ok")
}
}
func TestSavePolicy(t *testing.T) {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
t.Error("failed to create enforcer with `examples/rbac_model.conf`")
return
}
items, err := a.getAllItems()
res := make([][]string, 0)
for _, item := range items {
res = append(res, []string{item.V0, item.V1, item.V2})
}
err = e.SavePolicy()
if err != nil {
t.Error("TestSavePolicy err:" + err.Error())
return
}
testGetPolicy(t, e, res)
}