forked from casbin/casbin-pg-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter_test.go
288 lines (243 loc) · 7.77 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package pgadapter
import (
"os"
"testing"
"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/util"
"github.com/go-pg/pg/v9"
"github.com/stretchr/testify/suite"
)
// AdapterTestSuite tests all functionalities of Adapter
type AdapterTestSuite struct {
suite.Suite
e *casbin.Enforcer
a *Adapter
}
func (s *AdapterTestSuite) assertPolicy(expected, res [][]string) {
s.T().Helper()
s.Assert().True(util.Array2DEquals(expected, res), "Policy Got: %v, supposed to be %v", res, expected)
}
func (s *AdapterTestSuite) dropCasbinDB() {
opts, err := pg.ParseURL(os.Getenv("PG_CONN"))
s.Require().NoError(err)
db := pg.Connect(opts)
defer db.Close()
db.Exec("DROP DATABASE casbin")
}
func (s *AdapterTestSuite) SetupTest() {
s.dropCasbinDB()
var err error
s.a, err = NewAdapter(os.Getenv("PG_CONN"))
s.Require().NoError(err)
// This is a trick to save the current policy to the DB.
// We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
// The current policy means the policy in the Casbin enforcer (aka in memory).
e, err := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
s.Require().NoError(err)
err = s.a.SavePolicy(e.GetModel())
s.Require().NoError(err)
s.e, err = casbin.NewEnforcer("examples/rbac_model.conf", s.a)
s.Require().NoError(err)
}
func (s *AdapterTestSuite) TearDownTest() {
err := s.a.Close()
s.Require().NoError(err)
}
func (s *AdapterTestSuite) TestSaveLoad() {
s.Assert().False(s.e.IsFiltered())
s.assertPolicy(
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
s.e.GetPolicy(),
)
}
func (s *AdapterTestSuite) TestAutoSave() {
// AutoSave is enabled by default.
// Now we disable it.
s.e.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 := s.e.AddPolicy("alice", "data1", "write")
s.Require().NoError(err)
// Reload the policy from the storage to see the effect.
err = s.e.LoadPolicy()
s.Require().NoError(err)
// This is still the original policy.
s.assertPolicy(
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
s.e.GetPolicy(),
)
// Now we enable the AutoSave.
s.e.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 = s.e.AddPolicy("alice", "data1", "write")
s.Require().NoError(err)
// Reload the policy from the storage to see the effect.
err = s.e.LoadPolicy()
s.Require().NoError(err)
// The policy has a new rule: {"alice", "data1", "write"}.
s.assertPolicy(
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data1", "write"}},
s.e.GetPolicy(),
)
// Aditional AddPolicy have no effect
_, err = s.e.AddPolicy("alice", "data1", "write")
s.Require().NoError(err)
err = s.e.LoadPolicy()
s.Require().NoError(err)
s.assertPolicy(
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data1", "write"}},
s.e.GetPolicy(),
)
_, err = s.e.AddPolicies([][]string{
{"bob", "data2", "read"},
{"alice", "data2", "write"},
{"alice", "data2", "read"},
{"bob", "data1", "write"},
{"bob", "data1", "read"},
})
s.Require().NoError(err)
// Reload the policy from the storage to see the effect.
err = s.e.LoadPolicy()
s.Require().NoError(err)
// The policy has a new rule: {"alice", "data1", "write"}.
s.assertPolicy(
[][]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"},
},
s.e.GetPolicy(),
)
s.Require().NoError(err)
}
func (s *AdapterTestSuite) TestConstructorOptions() {
opts, err := pg.ParseURL(os.Getenv("PG_CONN"))
s.Require().NoError(err)
a, err := NewAdapter(opts)
s.Require().NoError(err)
defer a.Close()
s.e, err = casbin.NewEnforcer("examples/rbac_model.conf", a)
s.Require().NoError(err)
s.assertPolicy(
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
s.e.GetPolicy(),
)
}
func (s *AdapterTestSuite) TestRemovePolicy() {
_, err := s.e.RemovePolicy("alice", "data1", "read")
s.Require().NoError(err)
s.assertPolicy(
[][]string{{"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
s.e.GetPolicy(),
)
err = s.e.LoadPolicy()
s.Require().NoError(err)
s.assertPolicy(
[][]string{{"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
s.e.GetPolicy(),
)
_, err = s.e.RemovePolicies([][]string{
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
})
s.Require().NoError(err)
s.assertPolicy(
[][]string{{"bob", "data2", "write"}},
s.e.GetPolicy(),
)
}
func (s *AdapterTestSuite) TestRemoveFilteredPolicy() {
_, err := s.e.RemoveFilteredPolicy(0, "", "data2")
s.Require().NoError(err)
s.assertPolicy(
[][]string{{"alice", "data1", "read"}},
s.e.GetPolicy(),
)
err = s.e.LoadPolicy()
s.Require().NoError(err)
s.assertPolicy(
[][]string{{"alice", "data1", "read"}},
s.e.GetPolicy(),
)
}
func (s *AdapterTestSuite) TestLoadFilteredPolicy() {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", s.a)
s.Require().NoError(err)
err = e.LoadFilteredPolicy(&Filter{
P: []string{"", "", "read"},
})
s.Require().NoError(err)
s.Assert().True(e.IsFiltered())
s.assertPolicy(
[][]string{{"alice", "data1", "read"}, {"data2_admin", "data2", "read"}},
e.GetPolicy(),
)
}
func (s *AdapterTestSuite) TestLoadFilteredGroupingPolicy() {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", s.a)
s.Require().NoError(err)
err = e.LoadFilteredPolicy(&Filter{
G: []string{"bob"},
})
s.Require().NoError(err)
s.Assert().True(e.IsFiltered())
s.assertPolicy([][]string{}, e.GetGroupingPolicy())
e, err = casbin.NewEnforcer("examples/rbac_model.conf", s.a)
s.Require().NoError(err)
err = e.LoadFilteredPolicy(&Filter{
G: []string{"alice"},
})
s.Require().NoError(err)
s.Assert().True(e.IsFiltered())
s.assertPolicy([][]string{{"alice", "data2_admin"}}, e.GetGroupingPolicy())
}
func (s *AdapterTestSuite) TestLoadFilteredPolicyNilFilter() {
e, err := casbin.NewEnforcer("examples/rbac_model.conf", s.a)
s.Require().NoError(err)
err = e.LoadFilteredPolicy(nil)
s.Require().NoError(err)
s.Assert().False(e.IsFiltered())
s.assertPolicy(
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
s.e.GetPolicy(),
)
}
func (s *AdapterTestSuite) TestSavePolicyClearPreviousData() {
s.e.EnableAutoSave(false)
policies := s.e.GetPolicy()
// clone slice to avoid shufling elements
policies = append(policies[:0:0], policies...)
for _, p := range policies {
_, err := s.e.RemovePolicy(p)
s.Require().NoError(err)
}
policies = s.e.GetGroupingPolicy()
policies = append(policies[:0:0], policies...)
for _, p := range policies {
_, err := s.e.RemoveGroupingPolicy(p)
s.Require().NoError(err)
}
s.assertPolicy(
[][]string{},
s.e.GetPolicy(),
)
err := s.e.SavePolicy()
s.Require().NoError(err)
err = s.e.LoadPolicy()
s.Require().NoError(err)
s.assertPolicy(
[][]string{},
s.e.GetPolicy(),
)
}
func TestAdapterTestSuite(t *testing.T) {
suite.Run(t, new(AdapterTestSuite))
}