-
Notifications
You must be signed in to change notification settings - Fork 2
/
access_model_test.go
69 lines (63 loc) · 1.92 KB
/
access_model_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
package rbac
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAccess(t *testing.T) {
tests := map[string]struct {
input string
app string
res string
verb string
}{
"simple": {input: "a:b:c", app: "a", res: "b", verb: "c"},
"empty": {input: "", app: "", res: "", verb: ""},
"bad sep": {input: "a/b/c", app: "", res: "", verb: ""},
"too few segments": {input: "a:b", app: "", res: "", verb: ""},
"too many segments": {input: "a:b:c:d", app: "", res: "", verb: ""},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
acc := Access{Permission: tc.input}
assert.Equal(t, tc.app, acc.Application())
assert.Equal(t, tc.res, acc.Resource())
assert.Equal(t, tc.verb, acc.Verb())
})
}
}
func TestIsAllowed(t *testing.T) {
tests := map[string]struct {
input AccessList
app string
res string
verb string
allowed bool
}{
"single": {input: AccessList{
Access{Permission: "a:b:c"},
}, app: "a", res: "b", verb: "c", allowed: true},
"multiple": {input: AccessList{
Access{Permission: "a:b:c"},
Access{Permission: "d:e:f"},
}, app: "d", res: "e", verb: "f", allowed: true},
"wildcard resource": {input: AccessList{
Access{Permission: "a:*:c"},
}, app: "a", res: "b", verb: "c", allowed: true},
"wildcard verb": {input: AccessList{
Access{Permission: "a:b:*"},
}, app: "a", res: "b", verb: "c", allowed: true},
"wildcard both": {input: AccessList{
Access{Permission: "a:*:*"},
}, app: "a", res: "b", verb: "c", allowed: true},
"empty": {input: AccessList{}, app: "a", res: "b", verb: "c", allowed: false},
"not allowed": {input: AccessList{
Access{Permission: "a:b:c"},
}, app: "d", res: "e", verb: "f", allowed: false},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := tc.input.IsAllowed(tc.app, tc.res, tc.verb)
assert.Equal(t, tc.allowed, got)
})
}
}