Skip to content

Commit bd68cd3

Browse files
add tests
1 parent 4855a1b commit bd68cd3

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface EncoderResult {
2+
jwt: string;
3+
signingErrors: string[] | null;
4+
}

src/features/encoder/services/token-encoder.service.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { AsymmetricKeyFormatValues } from "@/features/common/values/asymmetric-k
3636
import { useDebuggerStore } from "@/features/debugger/services/debugger.store";
3737
import { SigningAlgCategoryValues } from "@/features/common/values/signing-alg-category.values";
3838
import { EncoderInputsModel } from "@/features/debugger/models/encoder-inputs.model";
39+
import { EncoderResult } from "@/features/common/models/encoder-result.model";
3940

4041
type EncodingHeaderErrors = {
4142
headerErrors: string[] | null;
@@ -68,11 +69,6 @@ type EncodingJwtErrors = {
6869
encodingErrors: string[] | null;
6970
};
7071

71-
type EncodingResult = {
72-
jwt: string;
73-
signingErrors: string[] | null;
74-
}
75-
7672
class _TokenEncoderService {
7773
async selectEncodingExample(
7874
algorithmPickerOptionValue: string,
@@ -490,7 +486,7 @@ class _TokenEncoderService {
490486
payload: DecodedJwtPayloadModel,
491487
key: string,
492488
encodingFormat: EncodingValues,
493-
): Promise<Result<EncodingResult, DebuggerErrorModel>> {
489+
): Promise<Result<EncoderResult, DebuggerErrorModel>> {
494490
if (!isHmacAlg(header.alg)) {
495491
return err({
496492
task: DebuggerTaskValues.ENCODE,
@@ -542,7 +538,7 @@ class _TokenEncoderService {
542538
});
543539
}
544540

545-
return ok<EncodingResult>({
541+
return ok<EncoderResult>({
546542
jwt: signWithSymmetricSecretKeyResult.value,
547543
signingErrors: signingError,
548544
});
@@ -553,7 +549,7 @@ class _TokenEncoderService {
553549
payload: DecodedJwtPayloadModel,
554550
key: string,
555551
keyFormat: AsymmetricKeyFormatValues,
556-
): Promise<Result<EncodingResult, DebuggerErrorModel>> {
552+
): Promise<Result<EncoderResult, DebuggerErrorModel>> {
557553
if (isDigitalSignatureAlg(header.alg)) {
558554
if (!key) {
559555
return err({
@@ -716,7 +712,7 @@ class _TokenEncoderService {
716712
symmetricSecretKeyEncoding: EncodingValues;
717713
}): Promise<
718714
Result<
719-
EncodingResult,
715+
EncoderResult,
720716
EncodingSymmetricSecretKeyErrors
721717
>
722718
> {
@@ -892,15 +888,15 @@ class _TokenEncoderService {
892888
},
893889
): Promise<
894890
Result<
895-
EncodingResult,
891+
EncoderResult,
896892
EncodingJwtErrors
897893
>
898894
> {
899895
const algType = params.algType;
900896
const header = params.header;
901897
const payload = params.payload;
902898

903-
let encodeJWTResult: Result<EncodingResult, DebuggerErrorModel> | null = null;
899+
let encodeJWTResult: Result<EncoderResult, DebuggerErrorModel> | null = null;
904900

905901
if (algType === SigningAlgCategoryValues.ANY) {
906902
const symmetricSecretKey = params.symmetricSecretKey;
@@ -1027,7 +1023,7 @@ class _TokenEncoderService {
10271023
}
10281024
}
10291025

1030-
return ok<EncodingResult>({
1026+
return ok<EncoderResult>({
10311027
jwt: encodeJWTResult.value.jwt,
10321028
signingErrors: encodeJWTResult.value.signingErrors,
10331029
});

tests/token-encoder.service.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { EncodingValues } from "@/features/common/values/encoding.values";
2+
import { describe, expect, test } from "vitest";
3+
import { TokenEncoderService } from "@/features/encoder/services/token-encoder.service";
4+
import {
5+
DefaultTokensValues,
6+
DefaultTokenWithSecretModel,
7+
} from "@/features/common/values/default-tokens.values";
8+
import { EncoderResult } from "@/features/common/models/encoder-result.model";
9+
10+
describe("processSymmetricSecretKey", () => {
11+
describe("should encode a JWT for SYMMETRIC type with HMAC algorithm", () => {
12+
test("should return an object with a jwt and signingErrors should be null", async () => {
13+
const params = {
14+
header: JSON.stringify({ alg: "HS256", typ: "JWT" }),
15+
payload: JSON.stringify({
16+
sub: "1234567890",
17+
name: "John Doe",
18+
admin: true,
19+
iat: 1516239022,
20+
}),
21+
symmetricSecretKey: (
22+
DefaultTokensValues.HS256 as DefaultTokenWithSecretModel
23+
).secret,
24+
symmetricSecretKeyEncoding: EncodingValues.UTF8,
25+
};
26+
const result =
27+
await TokenEncoderService.processSymmetricSecretKey(params);
28+
29+
expect(result.isOk()).toBe(true);
30+
expect(result.unwrapOr({})).toEqual({
31+
jwt: DefaultTokensValues.HS256.token,
32+
signingErrors: null,
33+
});
34+
});
35+
36+
test("should return an object with a jwt and signingErrors should not be null", async () => {
37+
const params = {
38+
header: JSON.stringify({ alg: "HS256", typ: "JWT" }),
39+
payload: JSON.stringify({
40+
sub: "1234567890",
41+
name: "John Doe",
42+
admin: true,
43+
iat: 1516239022,
44+
}),
45+
symmetricSecretKey: "secret",
46+
symmetricSecretKeyEncoding: EncodingValues.UTF8,
47+
};
48+
const algSize = 256;
49+
const result =
50+
await TokenEncoderService.processSymmetricSecretKey(params);
51+
52+
expect(result.isOk()).toBe(true);
53+
expect((result.unwrapOr({}) as EncoderResult).jwt).not.toBeNull();
54+
expect((result.unwrapOr({}) as EncoderResult).signingErrors).toEqual(
55+
[`A key of ${algSize} bits or larger MUST be used with HS${algSize} as specified on [RFC 7518](https://datatracker.ietf.org/doc/html/rfc7518#section-3.2).`]
56+
);
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)