forked from casbin/casbin-pg-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter_suite_test.go
105 lines (85 loc) · 3.16 KB
/
adapter_suite_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
package bunadapter_test
import (
"context"
"database/sql"
"fmt"
"testing"
"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/util"
"github.com/stretchr/testify/suite"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
bunadapter "github.com/msales/casbin-bun-adapter"
)
// AdapterTestSuite tests all functionalities of Adapter
type AdapterTestSuite struct {
suite.Suite
conn string
db *bun.DB
enforcer *casbin.Enforcer
adapter *bunadapter.Adapter
}
func TestAdapterTestSuite(t *testing.T) {
suite.Run(t, new(AdapterTestSuite))
}
func (suite *AdapterTestSuite) SetupSuite() {
suite.conn = "postgresql://user:pass@postgres:5432/test?sslmode=disable"
db := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(suite.conn)))
suite.db = bun.NewDB(db, pgdialect.New())
suite.db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
err := suite.migrateDB()
suite.Require().NoError(err)
}
func (suite *AdapterTestSuite) SetupTest() {
var err error
suite.adapter, err = bunadapter.NewAdapter(suite.db)
suite.Require().NoError(err)
suite.prePopulateUsingPoliciesFromFile()
suite.enforcer, err = casbin.NewEnforcer("examples/rbac_model.conf", suite.adapter)
suite.Require().NoError(err)
}
func (suite *AdapterTestSuite) prePopulateUsingPoliciesFromFile() {
var err error
// This is adapter trick to save the current policy to the DB.
// We can't call temporaryFileEnforcer.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).
temporaryFileEnforcer, err := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
suite.Require().NoError(err)
err = suite.adapter.SavePolicy(temporaryFileEnforcer.GetModel()) // this truncates the table, clearing out old data
suite.Require().NoError(err)
}
func (suite *AdapterTestSuite) migrateDB() error {
err := suite.db.QueryRow("CREATE SCHEMA casbin").Err()
if err != nil {
return fmt.Errorf("failed to create casbin scheme: %w", err)
}
_, err = suite.db.NewCreateTable().Model((*bunadapter.CasbinRule)(nil)).IfNotExists().Exec(context.Background())
if err != nil {
return fmt.Errorf("failed to create casbin rules table: %w", err)
}
return nil
}
func (suite *AdapterTestSuite) assertEnforcerPolicy(res [][]string) {
suite.T().Helper()
expected := suite.enforcer.GetPolicy()
suite.Assert().True(util.Array2DEquals(expected, res), "Policy Got: %v, supposed to be %v", res, expected)
}
func (suite *AdapterTestSuite) assertEnforcerGroupingPolicy(res [][]string) {
suite.T().Helper()
expected := suite.enforcer.GetGroupingPolicy()
suite.Assert().True(util.Array2DEquals(expected, res), "Grouping Policy Got: %v, supposed to be %v", res, expected)
}
func (suite *AdapterTestSuite) assertAllowed(rvals ...interface{}) {
suite.T().Helper()
ok, err := suite.enforcer.Enforce(rvals...)
suite.NoError(err)
suite.True(ok)
}
func (suite *AdapterTestSuite) assertDisallowed(rvals ...interface{}) {
suite.T().Helper()
ok, err := suite.enforcer.Enforce(rvals...)
suite.NoError(err)
suite.False(ok)
}