forked from casbin/casbin-pg-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter_policy_test.go
268 lines (221 loc) · 7.99 KB
/
adapter_policy_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package bunadapter_test
import (
"github.com/casbin/casbin/v2"
bunadapter "github.com/msales/casbin-bun-adapter"
)
func (suite *AdapterTestSuite) TestSaveLoad() {
suite.Assert().False(suite.enforcer.IsFiltered())
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
})
}
func (suite *AdapterTestSuite) TestAutoSave() {
// AutoSave is enabled by default.
// Now we disable it.
suite.enforcer.EnableAutoSave(false)
// Because AutoSave is disabled, the policy change only affects the policy in Casbin enforcer,
// it doesn't affect the policy in the storage.
_, err := suite.enforcer.AddPolicy("alice", "data1", "write")
suite.Require().NoError(err)
// Reload the policy from the storage to see the effect.
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
// This is still the original policy.
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
})
// Now we enable the AutoSave.
suite.enforcer.EnableAutoSave(true)
// Because AutoSave is enabled, the policy change not only affects the policy in Casbin enforcer,
// but also affects the policy in the storage.
_, err = suite.enforcer.AddPolicy("alice", "data1", "write")
suite.Require().NoError(err)
// Reload the policy from the storage to see the effect.
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
// The policy has adapter new rule: {"alice", "data1", "write"}.
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"alice", "data1", "write"},
})
// Additional AddPolicy have no effect
_, err = suite.enforcer.AddPolicy("alice", "data1", "write")
suite.Require().NoError(err)
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"alice", "data1", "write"},
})
_, err = suite.enforcer.AddPolicies([][]string{
{"bob", "data2", "read"},
{"alice", "data2", "write"},
{"alice", "data2", "read"},
{"bob", "data1", "write"},
{"bob", "data1", "read"},
})
suite.Require().NoError(err)
// Reload the policy from the storage to see the effect.
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
// The policy has adapter new rule: {"alice", "data1", "write"}.
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"alice", "data1", "write"},
{"bob", "data2", "read"},
{"alice", "data2", "write"},
{"alice", "data2", "read"},
{"bob", "data1", "write"},
{"bob", "data1", "read"},
})
suite.Require().NoError(err)
}
func (suite *AdapterTestSuite) TestRemovePolicy() {
_, err := suite.enforcer.RemovePolicy("alice", "data1", "read")
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
})
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
})
_, err = suite.enforcer.RemovePolicies([][]string{
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
})
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{{"bob", "data2", "write"}})
}
func (suite *AdapterTestSuite) TestRemoveFilteredPolicy() {
_, err := suite.enforcer.RemoveFilteredPolicy(0, "", "data2")
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{{"alice", "data1", "read"}})
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{{"alice", "data1", "read"}})
}
func (suite *AdapterTestSuite) TestLoadFilteredPolicy() {
err := suite.enforcer.LoadFilteredPolicy(&bunadapter.Filter{
P: []string{"", "", "read"},
})
suite.Require().NoError(err)
suite.Assert().True(suite.enforcer.IsFiltered())
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"data2_admin", "data2", "read"},
})
}
func (suite *AdapterTestSuite) TestLoadFilteredPolicyNilFilter() {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", suite.adapter)
suite.Require().NoError(err)
err = e.LoadFilteredPolicy(nil)
suite.Require().NoError(err)
suite.Assert().False(e.IsFiltered())
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
})
}
func (suite *AdapterTestSuite) TestSavePolicyClearPreviousData() {
suite.enforcer.EnableAutoSave(false)
policies := suite.enforcer.GetPolicy()
// clone slice to avoid shuffling elements
policies = append(policies[:0:0], policies...)
for _, p := range policies {
_, err := suite.enforcer.RemovePolicy(p)
suite.Require().NoError(err)
}
policies = suite.enforcer.GetGroupingPolicy()
policies = append(policies[:0:0], policies...)
for _, p := range policies {
_, err := suite.enforcer.RemoveGroupingPolicy(p)
suite.Require().NoError(err)
}
suite.assertEnforcerPolicy([][]string{})
err := suite.enforcer.SavePolicy()
suite.Require().NoError(err)
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{})
}
func (suite *AdapterTestSuite) TestUpdatePolicy() {
var err error
suite.enforcer, err = casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
suite.Require().NoError(err)
suite.enforcer.SetAdapter(suite.adapter)
err = suite.enforcer.SavePolicy()
suite.Require().NoError(err)
_, err = suite.enforcer.UpdatePolicies([][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}}, [][]string{{"bob", "data1", "read"}, {"alice", "data2", "write"}})
suite.Require().NoError(err)
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"bob", "data1", "read"},
{"alice", "data2", "write"},
})
}
func (suite *AdapterTestSuite) TestUpdatePolicyWithLoadFilteredPolicy() {
var err error
suite.enforcer, err = casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
suite.Require().NoError(err)
suite.enforcer.SetAdapter(suite.adapter)
err = suite.enforcer.SavePolicy()
suite.Require().NoError(err)
err = suite.enforcer.LoadFilteredPolicy(&bunadapter.Filter{P: []string{"data2_admin"}})
suite.Require().NoError(err)
_, err = suite.enforcer.UpdatePolicies(suite.enforcer.GetPolicy(), [][]string{{"bob", "data2", "read"}, {"alice", "data2", "write"}})
suite.Require().NoError(err)
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"bob", "data2", "read"},
{"alice", "data2", "write"},
})
}
func (suite *AdapterTestSuite) TestUpdateFilteredPolicies() {
var err error
suite.enforcer, err = casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
suite.Require().NoError(err)
suite.enforcer.SetAdapter(suite.adapter)
err = suite.enforcer.SavePolicy()
suite.Require().NoError(err)
_, err = suite.adapter.UpdateFilteredPolicies("p", "p", [][]string{{"alice", "data2", "write"}}, 0, "alice", "data1", "read")
suite.Require().NoError(err)
_, err = suite.adapter.UpdateFilteredPolicies("p", "p", [][]string{{"bob", "data1", "read"}}, 0, "bob", "data2", "write")
suite.Require().NoError(err)
err = suite.enforcer.LoadPolicy()
suite.Require().NoError(err)
suite.assertEnforcerPolicy([][]string{
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"alice", "data2", "write"},
{"bob", "data1", "read"},
})
}