forked from zpatrick/rbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob_test.go
94 lines (87 loc) · 1.9 KB
/
glob_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
package rbac
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
/*
func ExampleNewGlobPermission() {
role := Role{
Permissions: []Permission{
NewGlobPermission("delete:user", "*Doe"),
NewGlobPermission("read:*", "*"),
NewGlobPermission("*", "user_123"),
},
}
fmt.Println(role.Can("read", "comment"))
fmt.Println(role.Can("write", "books"))
// Output:
// [action: "delete:user"] [target: "John Doe"] => true
// [action: "delete:user"] [target: "Jane Doe"] => true
// [action: "delete:user"] [target: "John Smith"] => false
// [action: "read:comment"] [target: "comment_123"] => true
// [action: "read:article"] [target: "article_123"] => true
// [action: "edit:user"] [target: "user_123"] => true
// [action: "edit:user"] [target: "user_456"] => false
// [action: "delete:user"] [target: "user_123"] => true
}
*/
func TestGlobMatch(t *testing.T) {
cases := map[string]map[string]bool{
"": {
"": true,
"alpha": false,
"beta": false,
"charlie": false,
},
"*": {
"": true,
"alpha": true,
"beta": true,
"charlie": true,
},
"alpha": {
"": false,
"alpha": true,
"beta": false,
"charlie": false,
},
"a*": {
"": false,
"alpha": true,
"beta": false,
"charlie": false,
},
"*a": {
"": false,
"alpha": true,
"beta": true,
"charlie": false,
},
"*a*": {
"": false,
"alpha": true,
"beta": true,
"charlie": true,
},
"delta": {
"": false,
"alpha": false,
"beta": false,
"charlie": false,
},
}
for pattern, inputs := range cases {
matcher := GlobMatch(pattern)
for input, expected := range inputs {
name := fmt.Sprintf("%s/%s", pattern, input)
t.Run(name, func(t *testing.T) {
result, err := matcher(input)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected, result)
})
}
}
}