Skip to content

Commit 2e2c011

Browse files
committed
added extra tests
1 parent b2f1b83 commit 2e2c011

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

gateway/mw_jwt_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,19 @@ func TestGetPolicyIDFromToken(t *testing.T) {
12501250
spec.OAS.ExtractTo(spec.APIDefinition)
12511251
},
12521252
},
1253+
{
1254+
name: "is classic converted to oas without filling BasePolicyClaims",
1255+
claims: jwt.MapClaims{
1256+
"policy": "mainpolicy",
1257+
},
1258+
expectedBool: true,
1259+
expected: "mainpolicy",
1260+
modifySpec: func(spec *APISpec) {
1261+
spec.APIDefinition.JWTPolicyFieldName = "policy"
1262+
spec.OAS.Fill(*spec.APIDefinition)
1263+
spec.IsOAS = true
1264+
},
1265+
},
12531266
}
12541267

12551268
for _, tc := range testCases {
@@ -2865,6 +2878,35 @@ func TestGetUserIDFromClaim(t *testing.T) {
28652878
})
28662879
})
28672880

2881+
t.Run("is classic and converted to oas", func(t *testing.T) {
2882+
userIDKey := "user_id"
2883+
var api apidef.APIDefinition
2884+
api.EnableJWT = true
2885+
api.AuthConfigs = map[string]apidef.AuthConfig{
2886+
apidef.JWTType: {
2887+
Name: "jwtAuth",
2888+
AuthHeaderName: "Authorization",
2889+
},
2890+
}
2891+
api.JWTIdentityBaseField = userIDKey
2892+
2893+
var o oas.OAS
2894+
o.Fill(api)
2895+
api.IsOAS = true
2896+
middleware := JWTMiddleware{&BaseMiddleware{Spec: &APISpec{
2897+
OAS: o,
2898+
APIDefinition: &api,
2899+
}}}
2900+
2901+
jwtClaims := jwt.MapClaims{
2902+
userIDKey: userID,
2903+
"iss": "example.com",
2904+
}
2905+
identity, err := middleware.getUserIdFromClaim(jwtClaims)
2906+
assert.NoError(t, err)
2907+
assert.Equal(t, identity, userID)
2908+
})
2909+
28682910
t.Run("is OAS", func(t *testing.T) {
28692911
testCases := []struct {
28702912
name string

0 commit comments

Comments
 (0)