-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
syncing up to 6958704e221de3f6d410f1491cff571ab4c287fc
Co-authored-by: Bruce Yu <[email protected]>
- Loading branch information
1 parent
1063129
commit 935252e
Showing
4 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1262,3 +1262,53 @@ func DatasourceConfig(authType string, authConfig map[string]interface{}) *struc | |
} | ||
return s | ||
} | ||
|
||
func TestDecodeJwt(t *testing.T) { | ||
tm := &tokenManager{logger: zaptest.NewLogger(t)} | ||
|
||
tests := []struct { | ||
name string | ||
token string | ||
wantErr bool | ||
wantClaims map[string]interface{} | ||
errContains string | ||
}{ | ||
{ | ||
name: "empty token", | ||
token: "", | ||
wantErr: true, | ||
errContains: "empty token", | ||
}, | ||
{ | ||
name: "invalid token", | ||
token: "invalid.token", | ||
wantErr: true, | ||
errContains: "failed to parse JWT", | ||
}, | ||
{ | ||
name: "valid token", | ||
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZW1haWwiOiJmb29Ac3VwZXJibG9ja3MuY29tIiwiaWF0IjoxNTE2MjM5MDIyfQ.acxuPTE4HrmSFMY9v73QY5qgQWrXsRrbWdLo5Ss7fgU", | ||
wantClaims: map[string]interface{}{ | ||
"sub": "1234567890", | ||
"name": "John Doe", | ||
"email": "[email protected]", | ||
"iat": float64(1516239022), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
claims, err := tm.decodeJwt(context.Background(), tt.token) | ||
|
||
if tt.wantErr { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.errContains) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, tt.wantClaims, claims.AsMap()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters