-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscopie_test.go
195 lines (174 loc) · 4.35 KB
/
scopie_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package scopie
import (
"encoding/json"
"fmt"
"os"
"testing"
"github.com/miniscruff/scopie-go/then"
)
type testAllowedScenario struct {
ID string `json:"id"`
ActorRules []string `json:"actorRules"`
ActionScopes []string `json:"actionScopes"`
Result bool `json:"result"`
Variables map[string]string `json:"variables"`
Error string `json:"error"`
}
type testValidScenario struct {
ID string `json:"id"`
Scopes []string `json:"scopes"`
Error string `json:"error"`
}
type coreTestCase struct {
Version string `json:"version"`
IsAllowedTests []testAllowedScenario `json:"isAllowedTests"`
ScopeValidTests []testValidScenario `json:"validateScopesTests"`
Benchmarks []testAllowedScenario `json:"benchmarks"`
}
var testCases coreTestCase
func TestMain(m *testing.M) {
testFile, err := os.Open("testdata/scenarios.json")
if err != nil {
fmt.Println("unable to read scenarios", err)
os.Exit(1)
}
err = json.NewDecoder(testFile).Decode(&testCases)
if err != nil {
fmt.Println("unable to decode scenarios", err)
os.Exit(1)
}
os.Exit(m.Run())
}
func Test_IsAllowed(t *testing.T) {
for _, scenario := range testCases.IsAllowedTests {
t.Run(scenario.ID, func(t *testing.T) {
res, err := IsAllowed(scenario.ActionScopes, scenario.ActorRules, scenario.Variables)
if scenario.Error != "" {
then.NotNil(t, err)
then.Equals(t, scenario.Error, err.Error())
} else {
then.Nil(t, err)
then.Equals(t, scenario.Result, res)
}
})
}
}
func Test_IsAllowedBenchmarks(t *testing.T) {
// Also run our benchmarks as test cases separate from running benchmarks
for _, scenario := range testCases.Benchmarks {
t.Run(scenario.ID, func(t *testing.T) {
res, err := IsAllowed(scenario.ActionScopes, scenario.ActorRules, scenario.Variables)
then.Equals(t, scenario.Result, res)
then.Nil(t, err)
})
}
}
func Test_ScopeValid(t *testing.T) {
for _, scenario := range testCases.ScopeValidTests {
t.Run(scenario.ID, func(t *testing.T) {
err := ValidateScopes(scenario.Scopes)
if scenario.Error == "" {
then.Nil(t, err)
} else {
then.NotNil(t, err)
then.Equals(t, scenario.Error, err.Error())
}
})
}
}
type compareTestCase struct {
name string
actor string
action string
vars map[string]string
err error
res bool
}
func Test_CompareActorToRule(t *testing.T) {
for _, tc := range []compareTestCase{
{
name: "basic equality",
actor: "allow/alpha/beta",
action: "alpha/beta",
res: true,
},
{
name: "first inequality",
actor: "allow/alpha/beta",
action: "delta/beta",
res: false,
},
{
name: "last inequality",
actor: "allow/alpha/beta/ceti/delta",
action: "alpha/beta/ceti/epsilon",
res: false,
},
{
name: "wildcard equality",
actor: "allow/alpha/beta/*/delta",
action: "alpha/beta/ceti/delta",
res: true,
},
{
name: "super wildcard equality",
actor: "allow/alpha/beta/**",
action: "alpha/beta/ceti/delta",
res: true,
},
{
name: "variable usage",
actor: "allow/alpha/@user",
action: "alpha/our_user",
vars: map[string]string{
"user": "our_user",
},
res: true,
},
{
name: "first array value",
actor: "allow/alpha/beta|ceti|delta",
action: "alpha/beta",
res: true,
},
{
name: "last array value",
actor: "allow/alpha/beta|ceti|delta",
action: "alpha/delta", // last array value of epsilon
res: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
tc := tc
doesMatch, err := compareActorToAction(&tc.actor, &tc.action, tc.vars)
if tc.err == nil {
then.Nil(t, err)
then.Equals(t, tc.res, doesMatch)
} else {
then.Err(t, tc.err, err)
}
})
}
}
func Benchmark_Validations(b *testing.B) {
for _, scenario := range testCases.Benchmarks {
b.Run(scenario.ID, func(b *testing.B) {
b.ReportAllocs()
for range b.N {
_, err := IsAllowed(scenario.ActionScopes, scenario.ActorRules, scenario.Variables)
then.Nil(b, err)
}
})
}
}
func ExampleIsAllowed() {
userScopes := []string{"allow/blog/create|update"}
allowed, err := IsAllowed([]string{"blog/create"}, userScopes, nil)
if err != nil {
panic("invalid scopes or rules")
}
if !allowed {
panic("can not create a new blog")
}
// create the blog here
}