Skip to content

Commit c006f54

Browse files
add test to address empty token
1 parent 9c8d1dc commit c006f54

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

tests/token-decoder.service.test.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ vi.mock("@/features/debugger/services/debugger.store", () => ({
5757
// Typed Mocks
5858
const viExtractJwt = vi.mocked(extractJwt);
5959
const viValidateSymmetricSecret = vi.mocked(validateSymmetricSecret);
60-
const viValidateAsymmetricKey = vi.mocked(validateAsymmetricKey);
6160
const viValidateJwtFormat = vi.mocked(validateJwtFormat);
6261
const viIsHmacAlg = vi.mocked(isHmacAlg);
6362
const viIsDigitalSignatureAlg = vi.mocked(isDigitalSignatureAlg);
@@ -71,7 +70,7 @@ const viParseStringIntoValidJsonObject = vi.mocked(
7170
const viVerifyMACedJwt = vi.mocked(verifyMACedJwt);
7271
const viDownloadPublicKeyIfPossible = vi.mocked(downloadPublicKeyIfPossible);
7372

74-
describe("TokenDecoderService.handleJwtChange", () => {
73+
describe("handleJwtChange", () => {
7574
const mockJwt =
7675
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
7776
const mockParams = {
@@ -109,7 +108,6 @@ describe("TokenDecoderService.handleJwtChange", () => {
109108
);
110109
viIsSupportedAlg.mockReturnValue(true);
111110
viValidateSymmetricSecret.mockResolvedValue(ok(new Uint8Array([1, 2, 3])));
112-
viValidateAsymmetricKey.mockResolvedValue(ok({} as CryptoKey));
113111
viGetStringifiedHeaderAndPayload.mockReturnValue(
114112
ok({
115113
header: mockStringifiedHeader,
@@ -128,6 +126,31 @@ describe("TokenDecoderService.handleJwtChange", () => {
128126
);
129127
});
130128

129+
it("should return a warning if the new token is empty", async () => {
130+
const error = {
131+
message: "JWT must not be empty.",
132+
input: DebuggerInputValues.JWT,
133+
task: DebuggerTaskValues.DECODE,
134+
};
135+
136+
viExtractJwt.mockReturnValue("");
137+
viValidateJwtFormat.mockReturnValue(err(error));
138+
139+
const result = await TokenDecoderService.handleJwtChange({
140+
...mockParams,
141+
newToken: " ",
142+
});
143+
144+
expect(viExtractJwt).toHaveBeenCalledWith(" ");
145+
expect(result.jwt).toBe("");
146+
expect(result.signatureStatus).toBe(JwtSignatureStatusValues.WARNING);
147+
expect(result.signatureWarnings).toEqual([
148+
StringValues.editor.signatureWarning,
149+
]);
150+
expect(result.decodedHeader).toBe("");
151+
expect(result.decodedPayload).toBe("");
152+
});
153+
131154
it("should return decoding errors if JWT format is invalid", async () => {
132155
const error = {
133156
message: "Invalid format",

0 commit comments

Comments
 (0)