Adding more coverage (#268)

This commit is contained in:
Christian Banse 2023-02-20 23:00:46 +01:00 committed by GitHub
parent 81449672cc
commit fa7a12b45f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 2 deletions

View File

@ -134,3 +134,56 @@ func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) {
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
}
}
func TestMapClaims_ParseString(t *testing.T) {
type args struct {
key string
}
tests := []struct {
name string
m MapClaims
args args
want string
wantErr bool
}{
{
name: "missing key",
m: MapClaims{},
args: args{
key: "mykey",
},
want: "",
wantErr: false,
},
{
name: "wrong key type",
m: MapClaims{"mykey": 4},
args: args{
key: "mykey",
},
want: "",
wantErr: true,
},
{
name: "correct key type",
m: MapClaims{"mykey": "mystring"},
args: args{
key: "mykey",
},
want: "mystring",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.m.ParseString(tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("MapClaims.ParseString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("MapClaims.ParseString() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -56,6 +56,28 @@ var jwtTestData = []struct {
parser *jwt.Parser
signingMethod jwt.SigningMethod // The method to sign the JWT token for test purpose
}{
{
"invalid JWT",
"thisisnotreallyajwt",
defaultKeyFunc,
nil,
false,
jwt.ValidationErrorMalformed,
[]error{jwt.ErrTokenMalformed},
nil,
jwt.SigningMethodRS256,
},
{
"bearer in JWT",
"bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
defaultKeyFunc,
nil,
false,
jwt.ValidationErrorMalformed,
[]error{jwt.ErrTokenMalformed},
nil,
jwt.SigningMethodRS256,
},
{
"basic",
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJmb28iOiJiYXIifQ.FhkiHkoESI_cG3NPigFrxEk9Z60_oXrOT2vGm9Pn6RDgYNovYORQmmA0zs1AoAOf09ly2Nx2YAg6ABqAYga1AcMFkJljwxTT5fYphTuqpWdy4BELeSYJx5Ty2gmr8e7RonuUztrdD5WfPqLKMm1Ozp_T6zALpRmwTIW0QPnaBXaQD90FplAg46Iy1UlDKr-Eupy0i5SLch5Q-p2ZpaL_5fnTIUDlxC3pWhJTyx_71qDI-mAA_5lE_VdroOeflG56sSmDxopPEG3bFlSu1eowyBfxtu0_CuVd-M42RU75Zc4Gsj6uV77MBtbMrf4_7M_NUTSgoIF3fRqxrj0NzihIBg",
@ -371,10 +393,12 @@ func TestParser_Parse(t *testing.T) {
token, err = parser.ParseWithClaims(data.tokenString, jwt.MapClaims{}, data.keyfunc)
case *jwt.RegisteredClaims:
token, err = parser.ParseWithClaims(data.tokenString, &jwt.RegisteredClaims{}, data.keyfunc)
case nil:
token, err = parser.ParseWithClaims(data.tokenString, nil, data.keyfunc)
}
// Verify result matches expectation
if !reflect.DeepEqual(data.claims, token.Claims) {
if data.claims != nil && !reflect.DeepEqual(data.claims, token.Claims) {
t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims)
}
@ -386,7 +410,10 @@ func TestParser_Parse(t *testing.T) {
t.Errorf("[%v] Invalid token passed validation", data.name)
}
if (err == nil && !token.Valid) || (err != nil && token.Valid) {
// Since the returned token is nil in the ErrTokenMalformed, we
// cannot make the comparison here
if !errors.Is(err, jwt.ErrTokenMalformed) &&
((err == nil && !token.Valid) || (err != nil && token.Valid)) {
t.Errorf("[%v] Inconsistent behavior between returned error and token.Valid", data.name)
}