Skip to content

Commit 067d79c

Browse files
committed
fix: fix CI errors
1 parent a36c3c1 commit 067d79c

1 file changed

Lines changed: 65 additions & 52 deletions

File tree

adapter_test.go

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ func (s *AdapterTestSuite) assertPolicy(expected, res [][]string) {
2222
s.Assert().True(util.Array2DEquals(expected, res), "Policy Got: %v, supposed to be %v", res, expected)
2323
}
2424

25+
func (s *AdapterTestSuite) getPolicy(e *casbin.Enforcer) [][]string {
26+
policy, err := e.GetPolicy()
27+
s.Require().NoError(err)
28+
return policy
29+
}
30+
31+
func (s *AdapterTestSuite) getGroupingPolicy(e *casbin.Enforcer) [][]string {
32+
policy, err := e.GetGroupingPolicy()
33+
s.Require().NoError(err)
34+
return policy
35+
}
36+
2537
func (s *AdapterTestSuite) dropCasbinDB() {
2638
opts, err := pg.ParseURL(os.Getenv("PG_CONN"))
2739
s.Require().NoError(err)
@@ -58,7 +70,7 @@ func (s *AdapterTestSuite) TestSaveLoad() {
5870
s.Assert().False(s.e.IsFiltered())
5971
s.assertPolicy(
6072
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
61-
s.e.GetPolicy(),
73+
s.getPolicy(s.e),
6274
)
6375
}
6476

@@ -77,7 +89,7 @@ func (s *AdapterTestSuite) TestAutoSave() {
7789
// This is still the original policy.
7890
s.assertPolicy(
7991
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
80-
s.e.GetPolicy(),
92+
s.getPolicy(s.e),
8193
)
8294

8395
// Now we enable the AutoSave.
@@ -93,7 +105,7 @@ func (s *AdapterTestSuite) TestAutoSave() {
93105
// The policy has a new rule: {"alice", "data1", "write"}.
94106
s.assertPolicy(
95107
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data1", "write"}},
96-
s.e.GetPolicy(),
108+
s.getPolicy(s.e),
97109
)
98110

99111
// Aditional AddPolicy have no effect
@@ -103,7 +115,7 @@ func (s *AdapterTestSuite) TestAutoSave() {
103115
s.Require().NoError(err)
104116
s.assertPolicy(
105117
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data1", "write"}},
106-
s.e.GetPolicy(),
118+
s.getPolicy(s.e),
107119
)
108120

109121
_, err = s.e.AddPolicies([][]string{
@@ -131,7 +143,7 @@ func (s *AdapterTestSuite) TestAutoSave() {
131143
{"bob", "data1", "write"},
132144
{"bob", "data1", "read"},
133145
},
134-
s.e.GetPolicy(),
146+
s.getPolicy(s.e),
135147
)
136148

137149
s.Require().NoError(err)
@@ -150,7 +162,7 @@ func (s *AdapterTestSuite) TestConstructorOptions() {
150162

151163
s.assertPolicy(
152164
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
153-
s.e.GetPolicy(),
165+
s.getPolicy(s.e),
154166
)
155167
}
156168

@@ -160,15 +172,15 @@ func (s *AdapterTestSuite) TestRemovePolicy() {
160172

161173
s.assertPolicy(
162174
[][]string{{"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
163-
s.e.GetPolicy(),
175+
s.getPolicy(s.e),
164176
)
165177

166178
err = s.e.LoadPolicy()
167179
s.Require().NoError(err)
168180

169181
s.assertPolicy(
170182
[][]string{{"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
171-
s.e.GetPolicy(),
183+
s.getPolicy(s.e),
172184
)
173185

174186
_, err = s.e.RemovePolicies([][]string{
@@ -179,7 +191,7 @@ func (s *AdapterTestSuite) TestRemovePolicy() {
179191

180192
s.assertPolicy(
181193
[][]string{{"bob", "data2", "write"}},
182-
s.e.GetPolicy(),
194+
s.getPolicy(s.e),
183195
)
184196
}
185197

@@ -189,15 +201,15 @@ func (s *AdapterTestSuite) TestRemoveFilteredPolicy() {
189201

190202
s.assertPolicy(
191203
[][]string{{"alice", "data1", "read"}},
192-
s.e.GetPolicy(),
204+
s.getPolicy(s.e),
193205
)
194206

195207
err = s.e.LoadPolicy()
196208
s.Require().NoError(err)
197209

198210
s.assertPolicy(
199211
[][]string{{"alice", "data1", "read"}},
200-
s.e.GetPolicy(),
212+
s.getPolicy(s.e),
201213
)
202214
}
203215

@@ -212,7 +224,7 @@ func (s *AdapterTestSuite) TestLoadFilteredPolicy() {
212224
s.Assert().True(e.IsFiltered())
213225
s.assertPolicy(
214226
[][]string{{"alice", "data1", "read"}, {"data2_admin", "data2", "read"}},
215-
e.GetPolicy(),
227+
s.getPolicy(e),
216228
)
217229
}
218230

@@ -225,7 +237,7 @@ func (s *AdapterTestSuite) TestLoadFilteredGroupingPolicy() {
225237
})
226238
s.Require().NoError(err)
227239
s.Assert().True(e.IsFiltered())
228-
s.assertPolicy([][]string{}, e.GetGroupingPolicy())
240+
s.assertPolicy([][]string{}, s.getGroupingPolicy(e))
229241

230242
e, err = casbin.NewEnforcer("examples/rbac_model.conf", s.a)
231243
s.Require().NoError(err)
@@ -235,7 +247,7 @@ func (s *AdapterTestSuite) TestLoadFilteredGroupingPolicy() {
235247
})
236248
s.Require().NoError(err)
237249
s.Assert().True(e.IsFiltered())
238-
s.assertPolicy([][]string{{"alice", "data2_admin"}}, e.GetGroupingPolicy())
250+
s.assertPolicy([][]string{{"alice", "data2_admin"}}, s.getGroupingPolicy(e))
239251
}
240252

241253
func (s *AdapterTestSuite) TestLoadFilteredPolicyNilFilter() {
@@ -248,28 +260,28 @@ func (s *AdapterTestSuite) TestLoadFilteredPolicyNilFilter() {
248260
s.Assert().False(e.IsFiltered())
249261
s.assertPolicy(
250262
[][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}},
251-
s.e.GetPolicy(),
263+
s.getPolicy(s.e),
252264
)
253265
}
254266

255267
func (s *AdapterTestSuite) TestSavePolicyClearPreviousData() {
256268
s.e.EnableAutoSave(false)
257-
policies := s.e.GetPolicy()
269+
policies := s.getPolicy(s.e)
258270
// clone slice to avoid shufling elements
259271
policies = append(policies[:0:0], policies...)
260272
for _, p := range policies {
261273
_, err := s.e.RemovePolicy(p)
262274
s.Require().NoError(err)
263275
}
264-
policies = s.e.GetGroupingPolicy()
276+
policies = s.getGroupingPolicy(s.e)
265277
policies = append(policies[:0:0], policies...)
266278
for _, p := range policies {
267279
_, err := s.e.RemoveGroupingPolicy(p)
268280
s.Require().NoError(err)
269281
}
270282
s.assertPolicy(
271283
[][]string{},
272-
s.e.GetPolicy(),
284+
s.getPolicy(s.e),
273285
)
274286

275287
err := s.e.SavePolicy()
@@ -279,7 +291,7 @@ func (s *AdapterTestSuite) TestSavePolicyClearPreviousData() {
279291
s.Require().NoError(err)
280292
s.assertPolicy(
281293
[][]string{},
282-
s.e.GetPolicy(),
294+
s.getPolicy(s.e),
283295
)
284296
}
285297

@@ -299,7 +311,7 @@ func (s *AdapterTestSuite) TestUpdatePolicy() {
299311
err = s.e.LoadPolicy()
300312
s.Require().NoError(err)
301313

302-
s.assertPolicy(s.e.GetPolicy(), [][]string{{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"bob", "data1", "read"}, {"alice", "data2", "write"}})
314+
s.assertPolicy(s.getPolicy(s.e), [][]string{{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"bob", "data1", "read"}, {"alice", "data2", "write"}})
303315
}
304316

305317
func (s *AdapterTestSuite) TestUpdatePolicyWithLoadFilteredPolicy() {
@@ -315,13 +327,14 @@ func (s *AdapterTestSuite) TestUpdatePolicyWithLoadFilteredPolicy() {
315327
err = s.e.LoadFilteredPolicy(&Filter{P: []string{"data2_admin"}})
316328
s.Require().NoError(err)
317329

318-
_, err = s.e.UpdatePolicies(s.e.GetPolicy(), [][]string{{"bob", "data2", "read"}, {"alice", "data2", "write"}})
330+
currentPolicy := s.getPolicy(s.e)
331+
_, err = s.e.UpdatePolicies(currentPolicy, [][]string{{"bob", "data2", "read"}, {"alice", "data2", "write"}})
319332
s.Require().NoError(err)
320333

321334
err = s.e.LoadPolicy()
322335
s.Require().NoError(err)
323336

324-
s.assertPolicy(s.e.GetPolicy(), [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"bob", "data2", "read"}, {"alice", "data2", "write"}})
337+
s.assertPolicy(s.getPolicy(s.e), [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"bob", "data2", "read"}, {"alice", "data2", "write"}})
325338
}
326339

327340
func (s *AdapterTestSuite) TestUpdateFilteredPolicies() {
@@ -343,35 +356,35 @@ func (s *AdapterTestSuite) TestUpdateFilteredPolicies() {
343356
err = s.e.LoadPolicy()
344357
s.Require().NoError(err)
345358

346-
s.assertPolicy(s.e.GetPolicy(), [][]string{{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data2", "write"}, {"bob", "data1", "read"}})
359+
s.assertPolicy(s.getPolicy(s.e), [][]string{{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data2", "write"}, {"bob", "data1", "read"}})
347360
}
348361

349362
func (s *AdapterTestSuite) TestEmptyStringSupport() {
350363
// Test that empty strings in the middle of rules are properly stored and retrieved
351364
// Note: Casbin trims trailing empty strings, so we focus on empty strings in the middle
352365
var err error
353-
366+
354367
// Add policies with empty strings in the middle
355368
_, err = s.e.AddPolicy("alice", "", "read")
356369
s.Require().NoError(err)
357-
370+
358371
_, err = s.e.AddPolicy("bob", "", "write")
359372
s.Require().NoError(err)
360-
373+
361374
// Reload to ensure they were saved correctly
362375
err = s.e.LoadPolicy()
363376
s.Require().NoError(err)
364-
377+
365378
// Check that all policies including empty strings are present
366-
policies := s.e.GetPolicy()
367-
379+
policies := s.getPolicy(s.e)
380+
368381
// Should have original 4 policies plus 2 new ones with empty strings
369382
s.Assert().Len(policies, 6)
370-
383+
371384
// Verify the new policies with empty strings exist
372385
hasAliceEmpty := false
373386
hasBobEmpty := false
374-
387+
375388
for _, p := range policies {
376389
if len(p) == 3 && p[0] == "alice" && p[1] == "" && p[2] == "read" {
377390
hasAliceEmpty = true
@@ -380,35 +393,35 @@ func (s *AdapterTestSuite) TestEmptyStringSupport() {
380393
hasBobEmpty = true
381394
}
382395
}
383-
396+
384397
s.Assert().True(hasAliceEmpty, "Policy with alice and empty string in middle not found")
385398
s.Assert().True(hasBobEmpty, "Policy with bob and empty string in middle not found")
386-
399+
387400
// Test removing a policy with empty string
388401
_, err = s.e.RemovePolicy("alice", "", "read")
389402
s.Require().NoError(err)
390-
403+
391404
err = s.e.LoadPolicy()
392405
s.Require().NoError(err)
393-
394-
policies = s.e.GetPolicy()
406+
407+
policies = s.getPolicy(s.e)
395408
s.Assert().Len(policies, 5)
396-
409+
397410
// Verify alice policy with empty string was removed
398411
for _, p := range policies {
399412
if len(p) == 3 && p[0] == "alice" && p[1] == "" && p[2] == "read" {
400413
s.Fail("Policy with alice and empty string should have been removed")
401414
}
402415
}
403-
416+
404417
// Test updating a policy with empty string
405418
_, err = s.e.UpdatePolicy([]string{"bob", "", "write"}, []string{"bob", "", "read"})
406419
s.Require().NoError(err)
407-
420+
408421
err = s.e.LoadPolicy()
409422
s.Require().NoError(err)
410-
411-
policies = s.e.GetPolicy()
423+
424+
policies = s.getPolicy(s.e)
412425
hasBobEmptyRead := false
413426
for _, p := range policies {
414427
if len(p) == 3 && p[0] == "bob" && p[1] == "" && p[2] == "read" {
@@ -425,24 +438,24 @@ func (s *AdapterTestSuite) TestEmptyStringSupport() {
425438
func (s *AdapterTestSuite) TestEmptyStringVsWildcard() {
426439
// Test that empty strings in rules are different from wildcards in filters
427440
var err error
428-
441+
429442
// Add a policy with an empty string
430443
_, err = s.e.AddPolicy("user1", "", "write")
431444
s.Require().NoError(err)
432-
445+
433446
// Add a policy with a non-empty value
434447
_, err = s.e.AddPolicy("user1", "resource1", "write")
435448
s.Require().NoError(err)
436-
449+
437450
// Reload
438451
err = s.e.LoadPolicy()
439452
s.Require().NoError(err)
440-
453+
441454
// Both should exist
442-
policies := s.e.GetPolicy()
455+
policies := s.getPolicy(s.e)
443456
hasEmpty := false
444457
hasNonEmpty := false
445-
458+
446459
for _, p := range policies {
447460
if len(p) >= 3 && p[0] == "user1" && p[2] == "write" {
448461
if p[1] == "" {
@@ -452,18 +465,18 @@ func (s *AdapterTestSuite) TestEmptyStringVsWildcard() {
452465
}
453466
}
454467
}
455-
468+
456469
s.Assert().True(hasEmpty, "Policy with empty string should exist")
457470
s.Assert().True(hasNonEmpty, "Policy with resource1 should exist")
458-
471+
459472
// Remove using wildcard (empty string in filter) should remove both
460473
_, err = s.e.RemoveFilteredPolicy(0, "user1", "", "write")
461474
s.Require().NoError(err)
462-
475+
463476
err = s.e.LoadPolicy()
464477
s.Require().NoError(err)
465-
466-
policies = s.e.GetPolicy()
478+
479+
policies = s.getPolicy(s.e)
467480
for _, p := range policies {
468481
if len(p) >= 3 && p[0] == "user1" && p[2] == "write" {
469482
s.Fail("Policies with user1 and write should have been removed by wildcard filter")

0 commit comments

Comments
 (0)