forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_definitions_test.go
326 lines (289 loc) · 8.8 KB
/
api_definitions_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package apidef
import (
"encoding/base64"
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
schema "github.com/xeipuuv/gojsonschema"
)
func TestSchema(t *testing.T) {
schemaLoader := schema.NewBytesLoader([]byte(Schema))
spec := DummyAPI()
goLoader := schema.NewGoLoader(spec)
result, err := schema.Validate(schemaLoader, goLoader)
if err != nil {
t.Error(err)
}
if !result.Valid() {
for _, err := range result.Errors() {
t.Error(err)
}
}
}
func TestStringRegexMap(t *testing.T) {
var v StringRegexMap
assert.True(t, v.Empty())
v = StringRegexMap{MatchPattern: ".*"}
assert.False(t, v.Empty())
v = StringRegexMap{Reverse: true}
assert.False(t, v.Empty())
}
func TestRoutingTriggerOptions(t *testing.T) {
opts := NewRoutingTriggerOptions()
assert.NotNil(t, opts.HeaderMatches)
assert.NotNil(t, opts.QueryValMatches)
assert.NotNil(t, opts.PathPartMatches)
assert.NotNil(t, opts.SessionMetaMatches)
assert.NotNil(t, opts.RequestContextMatches)
assert.Empty(t, opts.PayloadMatches)
}
func TestEncodeForDB(t *testing.T) {
t.Run("EncodeForDB persist schema objects from extended path", func(t *testing.T) {
spec := DummyAPI()
spec.EncodeForDB()
var schemaNotEmpty bool
for _, version := range spec.VersionData.Versions {
for _, validateObj := range version.ExtendedPaths.ValidateJSON {
schemaNotEmpty = schemaNotEmpty || (validateObj.Schema != nil)
}
}
assert.True(t, schemaNotEmpty, "expected EncodeForDB to persist schema objects")
})
}
func TestDecodeFromDB(t *testing.T) {
t.Run("json schema validation middleware", func(t *testing.T) {
apiDef := DummyAPI()
var (
bodySchema map[string]interface{}
v1 = "v1"
v1B64 = base64.StdEncoding.EncodeToString([]byte(v1))
)
err := json.Unmarshal([]byte(`{"$schema":"http://json-schema.org/draft-04/schema#","properties":{"id":{"type":"integer"}},"required":["id"],"type":"object"}`),
&bodySchema)
assert.NoError(t, err)
apiDef.VersionData.Versions[v1] = VersionInfo{
ExtendedPaths: ExtendedPathsSet{
ValidateJSON: []ValidatePathMeta{
{
Path: "/",
Method: http.MethodPost,
Schema: bodySchema,
},
},
},
}
apiDef.EncodeForDB()
copyAPIDef := apiDef
copyAPIDef.DecodeFromDB()
assert.Equal(t, apiDef.VersionData.Versions[v1B64].ExtendedPaths.ValidateJSON[0].Schema,
copyAPIDef.VersionData.Versions[v1].ExtendedPaths.ValidateJSON[0].Schema)
assert.Empty(t, copyAPIDef.VersionData.Versions[v1].ExtendedPaths.ValidateJSON[0].SchemaB64)
})
}
func TestSchemaGraphqlConfig(t *testing.T) {
schemaLoader := schema.NewBytesLoader([]byte(Schema))
spec := DummyAPI()
spec.GraphQL.ExecutionMode = ""
goLoader := schema.NewGoLoader(spec)
result, err := schema.Validate(schemaLoader, goLoader)
if err != nil {
t.Error(err)
}
if !result.Valid() {
for _, err := range result.Errors() {
t.Error(err)
}
}
}
func TestAPIDefinition_DecodeFromDB_AuthDeprecation(t *testing.T) {
const authHeader = "authorization"
spec := DummyAPI()
spec.Auth = AuthConfig{AuthHeaderName: authHeader}
spec.UseStandardAuth = true
spec.DecodeFromDB()
assert.Equal(t, spec.AuthConfigs, map[string]AuthConfig{
"authToken": spec.Auth,
})
spec.EnableJWT = true
spec.DecodeFromDB()
assert.Equal(t, spec.AuthConfigs, map[string]AuthConfig{
"authToken": spec.Auth,
"jwt": spec.Auth,
})
}
func TestAPIDefinition_GenerateAPIID(t *testing.T) {
a := APIDefinition{}
a.GenerateAPIID()
assert.NotEmpty(t, a.APIID)
}
func TestAPIDefinition_GetScopeClaimName(t *testing.T) {
var (
scopeName = "scope"
oidcScopeName = "oidc_scope"
newScopeName = "new_scope"
newOIDCScopeName = "new_oidc_scope"
)
getAPIDef := func(deprecatedScopeName, jwtScopeName, oidcScopeName string, useOIDC bool) APIDefinition {
return APIDefinition{
UseOpenID: useOIDC,
JWTScopeClaimName: deprecatedScopeName,
Scopes: Scopes{
JWT: ScopeClaim{
ScopeClaimName: jwtScopeName,
},
OIDC: ScopeClaim{
ScopeClaimName: oidcScopeName,
},
},
}
}
testCases := []struct {
name string
deprecatedScopeName string
jwtScopeName string
oidcScopeName string
useOIDC bool
expectedScopeName string
}{
{
name: "jwt: only deprecated fields",
deprecatedScopeName: scopeName,
expectedScopeName: scopeName,
},
{
name: "jwt: only scopes.jwt",
jwtScopeName: newScopeName,
expectedScopeName: newScopeName,
},
{
name: "jwt: both scopes.jwt and scopes.oidc",
jwtScopeName: newScopeName,
oidcScopeName: newOIDCScopeName,
expectedScopeName: newScopeName,
},
{
name: "jwt: deprecated field and jwt.scopes",
deprecatedScopeName: scopeName,
jwtScopeName: newScopeName,
expectedScopeName: newScopeName,
},
{
name: "oidc: only deprecated fields",
deprecatedScopeName: oidcScopeName,
expectedScopeName: oidcScopeName,
useOIDC: true,
},
{
name: "oidc: only scopes.oidc",
oidcScopeName: newOIDCScopeName,
expectedScopeName: newOIDCScopeName,
useOIDC: true,
},
{
name: "oidc: both scopes.jwt and scopes.oidc",
jwtScopeName: newScopeName,
oidcScopeName: newOIDCScopeName,
expectedScopeName: newOIDCScopeName,
useOIDC: true,
},
{
name: "oidc: deprecated field and oidc.scopes",
deprecatedScopeName: oidcScopeName,
oidcScopeName: newOIDCScopeName,
expectedScopeName: newOIDCScopeName,
useOIDC: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
apiDef := getAPIDef(tc.deprecatedScopeName, tc.jwtScopeName, tc.oidcScopeName, tc.useOIDC)
assert.Equal(t, tc.expectedScopeName, apiDef.GetScopeClaimName())
})
}
}
func TestAPIDefinition_GetScopeToPolicyMapping(t *testing.T) {
var (
scopeToPolicyMapping = map[string]string{"jwtClaim": "pol1"}
newScopeToPolicyMapping = map[string]string{"jwtClaim1": "pol1"}
oidcScopeToPolicyMapping = map[string]string{"oidcClaim": "pol1"}
newOIDCScopeToPolicyMapping = map[string]string{"oidcClaim1": "pol1"}
)
getAPIDef := func(deprecatedScopeToPolicy, jwtScopeToPolicy, oidcScopeToPolicy map[string]string, useOIDC bool) APIDefinition {
return APIDefinition{
UseOpenID: useOIDC,
JWTScopeToPolicyMapping: deprecatedScopeToPolicy,
Scopes: Scopes{
JWT: ScopeClaim{
ScopeToPolicy: jwtScopeToPolicy,
},
OIDC: ScopeClaim{
ScopeToPolicy: oidcScopeToPolicy,
},
},
}
}
testCases := []struct {
name string
deprecatedScopeToPolicy map[string]string
jwtScopeToPolicy map[string]string
oidcScopeToPolicy map[string]string
useOIDC bool
expectedScopeToPolicy map[string]string
}{
{
name: "jwt: only deprecated fields",
deprecatedScopeToPolicy: scopeToPolicyMapping,
expectedScopeToPolicy: scopeToPolicyMapping,
},
{
name: "jwt: only scopes.jwt",
jwtScopeToPolicy: scopeToPolicyMapping,
expectedScopeToPolicy: scopeToPolicyMapping,
},
{
name: "jwt: both scopes.jwt and scopes.oidc",
jwtScopeToPolicy: scopeToPolicyMapping,
oidcScopeToPolicy: oidcScopeToPolicyMapping,
expectedScopeToPolicy: scopeToPolicyMapping,
},
{
name: "jwt: deprecated field and jwt.scopes",
deprecatedScopeToPolicy: scopeToPolicyMapping,
jwtScopeToPolicy: newScopeToPolicyMapping,
expectedScopeToPolicy: newScopeToPolicyMapping,
},
{
name: "oidc: only deprecated fields",
deprecatedScopeToPolicy: oidcScopeToPolicyMapping,
expectedScopeToPolicy: oidcScopeToPolicyMapping,
useOIDC: true,
},
{
name: "oidc: only scopes.oidc",
oidcScopeToPolicy: newOIDCScopeToPolicyMapping,
expectedScopeToPolicy: newOIDCScopeToPolicyMapping,
useOIDC: true,
},
{
name: "oidc: both scopes.jwt and scopes.oidc",
jwtScopeToPolicy: scopeToPolicyMapping,
oidcScopeToPolicy: oidcScopeToPolicyMapping,
expectedScopeToPolicy: oidcScopeToPolicyMapping,
useOIDC: true,
},
{
name: "oidc: deprecated field and oidc.scopes",
deprecatedScopeToPolicy: oidcScopeToPolicyMapping,
oidcScopeToPolicy: newOIDCScopeToPolicyMapping,
expectedScopeToPolicy: newOIDCScopeToPolicyMapping,
useOIDC: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
apiDef := getAPIDef(tc.deprecatedScopeToPolicy, tc.jwtScopeToPolicy, tc.oidcScopeToPolicy, tc.useOIDC)
assert.Equal(t, tc.expectedScopeToPolicy, apiDef.GetScopeToPolicyMapping())
})
}
}