-
Notifications
You must be signed in to change notification settings - Fork 5
/
rethinkadapter_test.go
140 lines (117 loc) · 4.74 KB
/
rethinkadapter_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
package rethinkadapter
import (
"os"
"reflect"
"testing"
"github.com/casbin/casbin"
r "gopkg.in/gorethink/gorethink.v4"
)
func getConnect() r.QueryExecutor {
url := os.Getenv("RETHINKDB_URL") //Get the Rethinkdb url from system env
if url == "" {
url = "localhost:28015"
}
session, _ := r.Connect(r.ConnectOpts{
Address: url,
})
return session
}
func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
myRes := e.GetPolicy()
// t.Log("Policy: ", myRes)
if reflect.DeepEqual(res, myRes) {
t.Error("Policy: ", myRes, ", supposed to be ", res)
}
}
func initPolicy(t *testing.T) {
// Because the DB is empty at first,
// so we need to load the policy from the file adapter (.CSV) first.
e := casbin.NewEnforcer("examples/casbinmodel.conf", "examples/casbinpolicy.csv")
session := getConnect()
a := NewAdapter(session)
// 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).
err := a.SavePolicy(e.GetModel())
if err != nil {
panic(err)
}
// Clear the current policy.
e.ClearPolicy()
testGetPolicy(t, e, [][]string{})
// Load the policy from DB.
err = a.LoadPolicy(e.GetModel())
if err != nil {
panic(err)
}
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
}
func TestAdapter(t *testing.T) {
initPolicy(t)
// Note: you don't need to look at the above code
// if you already have a working DB with policy inside.
// Now the DB has policy, so we can provide a normal use case.
// Create an adapter and an enforcer.
// NewEnforcer() will load the policy automatically.
session := getConnect()
a := NewAdapter(session)
e := casbin.NewEnforcer("examples/casbinmodel.conf", a)
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
// AutoSave is enabled by default.
// Now we disable it.
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.
e.AddPolicy("alice", "data1", "write")
// Reload the policy from the storage to see the effect.
if err := e.LoadPolicy(); err != nil {
t.Errorf("Expected LoadPolicy() to be successful; got %v", err)
}
// This is still the original policy.
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
// Now we enable the AutoSave.
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.
e.AddPolicy("alice", "data1", "write")
// Reload the policy from the storage to see the effect.
if err := e.LoadPolicy(); err != nil {
t.Errorf("Expected LoadPolicy() to be successful; got %v", err)
}
// The policy has a new rule: {"alice", "data1", "write"}.
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data1", "write"}})
// Remove the added rule.
e.RemovePolicy("alice", "data1", "write")
if err := a.RemovePolicy("p", "p", []string{"alice", "data1", "write"}); err != nil {
t.Errorf("Expected RemovePolicy() to be successful; got %v", err)
}
if err := e.LoadPolicy(); err != nil {
t.Errorf("Expected LoadPolicy() to be successful; got %v", err)
}
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
// Remove "data2_admin" related policy rules via a filter.
// Two rules: {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"} are deleted.
e.RemoveFilteredPolicy(0, "data2_admin")
if err := e.LoadPolicy(); err != nil {
t.Errorf("Expected LoadPolicy() to be successful; got %v", err)
}
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}})
e.RemoveFilteredPolicy(1, "data1")
if err := e.LoadPolicy(); err != nil {
t.Errorf("Expected LoadPolicy() to be successful; got %v", err)
}
testGetPolicy(t, e, [][]string{{"bob", "data2", "write"}})
e.RemoveFilteredPolicy(2, "write")
if err := e.LoadPolicy(); err != nil {
t.Errorf("Expected LoadPolicy() to be successful; got %v", err)
}
testGetPolicy(t, e, [][]string{})
}
func TestNewAdapterWithInvalidURL(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected recovery from panic")
}
}()
_ = NewAdapter(r.QueryExecutor(nil))
}