forked from casbin/gorm-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_adapter_test.go
141 lines (118 loc) · 4.64 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
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gormadapter
import (
"context"
"testing"
"time"
"github.com/agiledragon/gomonkey/v2"
"github.com/anzimu/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, *ContextAdapter) {
ca, err := NewContextAdapter("mysql", "root:@tcp(127.0.0.1:3306)/", "casbin")
if err != nil {
panic(err)
}
e, err := casbin.NewEnforcer("examples/rbac_model.conf", ca)
if err != nil {
panic(err)
}
e.ClearPolicy()
_ = e.SavePolicy()
return e, ca
}
func TestContextAdapter_LoadPolicyCtx(t *testing.T) {
e, ca := clearDBPolicy()
db, _ := openDBConnection("mysql", "root:@tcp(127.0.0.1:3306)/casbin")
policy := &CasbinRule{
Ptype: "p",
V0: "alice",
V1: "data1",
V2: "read",
}
db.Create(policy)
assert.NoError(t, ca.LoadPolicyCtx(context.Background(), e.GetModel()))
e, _ = casbin.NewEnforcer(e.GetModel(), ca)
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.LoadPolicyCtx(ctx, e.GetModel()), "context deadline exceeded")
}
func TestContextAdapter_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 TestContextAdapter_AddPolicyCtx(t *testing.T) {
e, ca := clearDBPolicy()
assert.NoError(t, ca.AddPolicyCtx(context.Background(), "p", "p", []string{"alice", "data1", "read"}))
_ = 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", "data1", "read"}), "context deadline exceeded")
}
func TestContextAdapter_RemovePolicyCtx(t *testing.T) {
e, ca := clearDBPolicy()
_ = ca.AddPolicy("p", "p", []string{"alice", "data1", "read"})
_ = ca.AddPolicy("p", "p", []string{"alice", "data2", "read"})
assert.NoError(t, 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", "data1", "read"}), "context deadline exceeded")
}
func TestContextAdapter_RemoveFilteredPolicyCtx(t *testing.T) {
e, ca := clearDBPolicy()
_ = ca.AddPolicy("p", "p", []string{"alice", "data1", "read"})
_ = ca.AddPolicy("p", "p", []string{"alice", "data1", "write"})
_ = ca.AddPolicy("p", "p", []string{"alice", "data2", "read"})
assert.NoError(t, ca.RemoveFilteredPolicyCtx(context.Background(), "p", "p", 1, "data1"))
_ = 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.RemoveFilteredPolicyCtx(ctx, "p", "p", 1, "data1"), "context deadline exceeded")
}