-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_adapter_test.go
157 lines (137 loc) · 4.44 KB
/
context_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
package casbinbunadapter
import (
"context"
"testing"
"time"
"github.com/agiledragon/gomonkey/v2"
"github.com/casbin/casbin/v2"
"github.com/stretchr/testify/assert"
)
func mockExecuteWithContextTimeOut(ctx context.Context, fn func() error) error {
done := make(chan error)
go func() {
time.Sleep(500 * time.Microsecond)
done <- fn()
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-done:
return err
}
}
func clearDBPolicy() (*casbin.Enforcer, *ctxBunAdapter) {
ca, err := NewCtxAdapter("mysql", "root:root@tcp(127.0.0.1:3306)/test", WithDebugMode())
if err != nil {
panic(err)
}
e, err := casbin.NewEnforcer("testdata/rbac_model.conf", ca)
if err != nil {
panic(err)
}
e.ClearPolicy()
_ = e.SavePolicy()
return e, ca
}
func TestCtxBunAdapter_LoadPolicyCtx(t *testing.T) {
e, _ := casbin.NewEnforcer("testdata/rbac_model.conf", "testdata/rbac_policy.csv")
ca, err := NewCtxAdapter("mysql", "root:root@tcp(127.0.0.1:3306)/test", WithDebugMode())
if err != nil {
panic(err)
}
_ = ca.SavePolicyCtx(context.Background(), e.GetModel())
assert.NoError(t, ca.LoadPolicyCtx(context.Background(), e.GetModel()))
e, _ = casbin.NewEnforcer("testdata/rbac_model.conf", ca)
testGetPolicy(
t,
e,
[][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
},
)
var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut)
defer p.Reset()
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
assert.EqualError(t, ca.LoadPolicyCtx(ctx, e.GetModel()), "context deadline exceeded")
}
func TestCtxBunAdapter_SavePolicyCtx(t *testing.T) {
e, ca := clearDBPolicy()
e.EnableAutoSave(false)
_, _ = e.AddPolicy("alice", "data1", "read")
assert.NoError(t, ca.SavePolicyCtx(context.Background(), e.GetModel()))
_ = e.LoadPolicy()
testGetPolicy(
t,
e,
[][]string{
{"alice", "data1", "read"},
},
)
var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut)
defer p.Reset()
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
assert.EqualError(t, ca.SavePolicyCtx(ctx, e.GetModel()), "context deadline exceeded")
}
func TestCtxBunAdapter_AddPolicyCtx(t *testing.T) {
e, ca := clearDBPolicy()
if err := ca.AddPolicyCtx(context.Background(), "p", "p", []string{"alice", "data1", "read"}); err != nil {
t.Fatalf("failed to add policy: %v", err)
}
_ = e.LoadPolicy()
testGetPolicy(
t,
e,
[][]string{
{"alice", "data1", "read"},
},
)
var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut)
defer p.Reset()
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
assert.EqualError(t, ca.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data2", "read"}), "context deadline exceeded")
}
func TestCtxBunAdapter_RemovePolicyCtx(t *testing.T) {
e, ca := clearDBPolicy()
_ = ca.AddPolicyCtx(context.Background(), "p", "p", []string{"alice", "data1", "read"})
_ = ca.AddPolicyCtx(context.Background(), "p", "p", []string{"alice", "data2", "read"})
_ = ca.RemovePolicyCtx(context.Background(), "p", "p", []string{"alice", "data1", "read"})
_ = e.LoadPolicy()
testGetPolicy(
t,
e,
[][]string{
{"alice", "data2", "read"},
},
)
var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut)
defer p.Reset()
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
assert.EqualError(t, ca.RemovePolicyCtx(ctx, "p", "p", []string{"alice", "data2", "read"}), "context deadline exceeded")
}
func TestCtxBunAdapter_RemoveFilteredPolicyCtx(t *testing.T) {
e, ca := clearDBPolicy()
_ = ca.AddPolicyCtx(context.Background(), "p", "p", []string{"alice", "data1", "read"})
_ = ca.AddPolicyCtx(context.Background(), "p", "p", []string{"alice", "data2", "read"})
_ = ca.AddPolicyCtx(context.Background(), "p", "p", []string{"bob", "data1", "read"})
_ = ca.RemoveFilteredPolicyCtx(context.Background(), "p", "p", 0, "alice")
_ = e.LoadPolicy()
testGetPolicy(
t,
e,
[][]string{
{"bob", "data1", "read"},
},
)
var p = gomonkey.ApplyFunc(executeWithContext, mockExecuteWithContextTimeOut)
defer p.Reset()
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
assert.EqualError(t, ca.RemoveFilteredPolicyCtx(ctx, "p", "p", 0, "alice"), "context deadline exceeded")
}