15
15
package xormadapter
16
16
17
17
import (
18
- "github.com/casbin/casbin/v2/util"
19
18
"log"
20
19
"strings"
21
20
"testing"
22
21
23
22
"github.com/casbin/casbin/v2"
23
+ "github.com/casbin/casbin/v2/util"
24
24
_ "github.com/go-sql-driver/mysql"
25
25
_ "github.com/lib/pq"
26
26
)
@@ -45,20 +45,15 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
45
45
}
46
46
}
47
47
48
- func initPolicy (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
48
+ func initPolicy (t * testing.T , a * Adapter ) {
49
49
// Because the DB is empty at first,
50
50
// so we need to load the policy from the file adapter (.CSV) first.
51
51
e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" , "examples/rbac_policy.csv" )
52
52
53
- a , err := NewAdapter (driverName , dataSourceName , dbSpecified ... )
54
- if err != nil {
55
- panic (err )
56
- }
57
-
58
53
// This is a trick to save the current policy to the DB.
59
54
// We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
60
55
// The current policy means the policy in the Casbin enforcer (aka in memory).
61
- err = a .SavePolicy (e .GetModel ())
56
+ err : = a .SavePolicy (e .GetModel ())
62
57
if err != nil {
63
58
panic (err )
64
59
}
@@ -75,30 +70,28 @@ func initPolicy(t *testing.T, driverName string, dataSourceName string, dbSpecif
75
70
testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }, {"data2_admin" , "data2" , "read" }, {"data2_admin" , "data2" , "write" }})
76
71
}
77
72
78
- func testSaveLoad (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
73
+ func testSaveLoad (t * testing.T , a * Adapter ) {
79
74
// Initialize some policy in DB.
80
- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
75
+ initPolicy (t , a )
81
76
// Note: you don't need to look at the above code
82
77
// if you already have a working DB with policy inside.
83
78
84
79
// Now the DB has policy, so we can provide a normal use case.
85
80
// Create an adapter and an enforcer.
86
81
// NewEnforcer() will load the policy automatically.
87
- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
88
82
e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" , a )
89
83
testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }, {"data2_admin" , "data2" , "read" }, {"data2_admin" , "data2" , "write" }})
90
84
}
91
85
92
- func testAutoSave (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
86
+ func testAutoSave (t * testing.T , a * Adapter ) {
93
87
// Initialize some policy in DB.
94
- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
88
+ initPolicy (t , a )
95
89
// Note: you don't need to look at the above code
96
90
// if you already have a working DB with policy inside.
97
91
98
92
// Now the DB has policy, so we can provide a normal use case.
99
93
// Create an adapter and an enforcer.
100
94
// NewEnforcer() will load the policy automatically.
101
- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
102
95
e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" , a )
103
96
104
97
// AutoSave is enabled by default.
@@ -152,16 +145,15 @@ func testAutoSave(t *testing.T, driverName string, dataSourceName string, dbSpec
152
145
testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }})
153
146
}
154
147
155
- func testFilteredPolicy (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
148
+ func testFilteredPolicy (t * testing.T , a * Adapter ) {
156
149
// Initialize some policy in DB.
157
- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
150
+ initPolicy (t , a )
158
151
// Note: you don't need to look at the above code
159
152
// if you already have a working DB with policy inside.
160
153
161
154
// Now the DB has policy, so we can provide a normal use case.
162
155
// Create an adapter and an enforcer.
163
156
// NewEnforcer() will load the policy automatically.
164
- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
165
157
e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
166
158
// Now set the adapter
167
159
e .SetAdapter (a )
@@ -194,16 +186,15 @@ func testFilteredPolicy(t *testing.T, driverName string, dataSourceName string,
194
186
testGetPolicy (t , e , [][]string {{"alice" , "data1" , "read" }, {"bob" , "data2" , "write" }})
195
187
}
196
188
197
- func testRemovePolicies (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
189
+ func testRemovePolicies (t * testing.T , a * Adapter ) {
198
190
// Initialize some policy in DB.
199
- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
191
+ initPolicy (t , a )
200
192
// Note: you don't need to look at the above code
201
193
// if you already have a working DB with policy inside.
202
194
203
195
// Now the DB has policy, so we can provide a normal use case.
204
196
// Create an adapter and an enforcer.
205
197
// NewEnforcer() will load the policy automatically.
206
- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
207
198
e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
208
199
209
200
// Now set the adapter
@@ -236,16 +227,15 @@ func testRemovePolicies(t *testing.T, driverName string, dataSourceName string,
236
227
testGetPolicy (t , e , [][]string {{"max" , "data1" , "delete" }})
237
228
}
238
229
239
- func testAddPolicies (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
230
+ func testAddPolicies (t * testing.T , a * Adapter ) {
240
231
// Initialize some policy in DB.
241
- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
232
+ initPolicy (t , a )
242
233
// Note: you don't need to look at the above code
243
234
// if you already have a working DB with policy inside.
244
235
245
236
// Now the DB has policy, so we can provide a normal use case.
246
237
// Create an adapter and an enforcer.
247
238
// NewEnforcer() will load the policy automatically.
248
- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
249
239
e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
250
240
251
241
// Now set the adapter
@@ -268,16 +258,15 @@ func testAddPolicies(t *testing.T, driverName string, dataSourceName string, dbS
268
258
testGetPolicy (t , e , [][]string {{"max" , "data2" , "read" }, {"max" , "data1" , "write" }})
269
259
}
270
260
271
- func testUpdatePolicies (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
261
+ func testUpdatePolicies (t * testing.T , a * Adapter ) {
272
262
// Initialize some policy in DB.
273
- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
263
+ initPolicy (t , a )
274
264
// Note: you don't need to look at the above code
275
265
// if you already have a working DB with policy inside.
276
266
277
267
// Now the DB has policy, so we can provide a normal use case.
278
268
// Create an adapter and an enforcer.
279
269
// NewEnforcer() will load the policy automatically.
280
- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
281
270
e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
282
271
283
272
// Now set the adapter
@@ -301,16 +290,15 @@ func testUpdatePolicies(t *testing.T, driverName string, dataSourceName string,
301
290
testGetPolicy (t , e , [][]string {{"bob" , "data1" , "read" }, {"bob" , "data2" , "write" }, {"data2_admin" , "data2" , "read" }, {"data2_admin" , "data2" , "write" }})
302
291
}
303
292
304
- func testUpdateFilteredPolicies (t * testing.T , driverName string , dataSourceName string , dbSpecified ... bool ) {
293
+ func testUpdateFilteredPolicies (t * testing.T , a * Adapter ) {
305
294
// Initialize some policy in DB.
306
- initPolicy (t , driverName , dataSourceName , dbSpecified ... )
295
+ initPolicy (t , a )
307
296
// Note: you don't need to look at the above code
308
297
// if you already have a working DB with policy inside.
309
298
310
299
// Now the DB has policy, so we can provide a normal use case.
311
300
// Create an adapter and an enforcer.
312
301
// NewEnforcer() will load the policy automatically.
313
- a , _ := NewAdapter (driverName , dataSourceName , dbSpecified ... )
314
302
e , _ := casbin .NewEnforcer ("examples/rbac_model.conf" )
315
303
316
304
// Now set the adapter
@@ -370,23 +358,30 @@ func TestAdapters(t *testing.T) {
370
358
// You can also use the following way to use an existing DB "abc":
371
359
// testSaveLoad(t, "mysql", "root:@tcp(127.0.0.1:3306)/abc", true)
372
360
373
- testSaveLoad (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
374
- testSaveLoad (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
375
-
376
- testAutoSave (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
377
- testAutoSave (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
378
-
379
- testFilteredPolicy (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
380
-
381
- testAddPolicies (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
382
- testAddPolicies (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
383
-
384
- testRemovePolicies (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
385
- testRemovePolicies (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
386
-
387
- testUpdatePolicies (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
388
- testUpdatePolicies (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
389
-
390
- testUpdateFilteredPolicies (t , "mysql" , "root:@tcp(127.0.0.1:3306)/" )
391
- testUpdateFilteredPolicies (t , "postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
361
+ a , _ := NewAdapter ("mysql" , "root:@tcp(127.0.0.1:3306)/" )
362
+ testSaveLoad (t , a )
363
+ testAutoSave (t , a )
364
+ testFilteredPolicy (t , a )
365
+ testAddPolicies (t , a )
366
+ testRemovePolicies (t , a )
367
+ testUpdatePolicies (t , a )
368
+ testUpdateFilteredPolicies (t , a )
369
+
370
+ a , _ = NewAdapter ("postgres" , "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable" )
371
+ testSaveLoad (t , a )
372
+ testAutoSave (t , a )
373
+ testFilteredPolicy (t , a )
374
+ testAddPolicies (t , a )
375
+ testRemovePolicies (t , a )
376
+ testUpdatePolicies (t , a )
377
+ testUpdateFilteredPolicies (t , a )
378
+
379
+ a , _ = NewAdapterWithTableName ("mysql" , "root:@tcp(127.0.0.1:3306)/" , "test" , "abc" )
380
+ testSaveLoad (t , a )
381
+ testAutoSave (t , a )
382
+ testFilteredPolicy (t , a )
383
+ testAddPolicies (t , a )
384
+ testRemovePolicies (t , a )
385
+ testUpdatePolicies (t , a )
386
+ testUpdateFilteredPolicies (t , a )
392
387
}
0 commit comments