-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathtoken-encoder.service.test.ts
More file actions
59 lines (55 loc) · 2.21 KB
/
token-encoder.service.test.ts
File metadata and controls
59 lines (55 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { EncodingValues } from "@/features/common/values/encoding.values";
import { describe, expect, test } from "vitest";
import { TokenEncoderService } from "@/features/encoder/services/token-encoder.service";
import {
DefaultTokensValues,
DefaultTokenWithSecretModel,
} from "@/features/common/values/default-tokens.values";
import { EncoderResult } from "@/features/common/models/encoder-result.model";
describe("processSymmetricSecretKey", () => {
describe("should encode a JWT for SYMMETRIC type with HMAC algorithm", () => {
test("should return an object with a jwt and signingErrors should be null", async () => {
const params = {
header: JSON.stringify({ alg: "HS256", typ: "JWT" }),
payload: JSON.stringify({
sub: "1234567890",
name: "John Doe",
admin: true,
iat: 1516239022,
}),
symmetricSecretKey: (
DefaultTokensValues.HS256 as DefaultTokenWithSecretModel
).secret,
symmetricSecretKeyEncoding: EncodingValues.UTF8,
};
const result =
await TokenEncoderService.processSymmetricSecretKey(params);
expect(result.isOk()).toBe(true);
expect(result.unwrapOr({})).toEqual({
jwt: DefaultTokensValues.HS256.token,
signingErrors: null,
});
});
test("should return an object with a jwt and signingErrors should not be null", async () => {
const params = {
header: JSON.stringify({ alg: "HS256", typ: "JWT" }),
payload: JSON.stringify({
sub: "1234567890",
name: "John Doe",
admin: true,
iat: 1516239022,
}),
symmetricSecretKey: "secret",
symmetricSecretKeyEncoding: EncodingValues.UTF8,
};
const algSize = 256;
const result =
await TokenEncoderService.processSymmetricSecretKey(params);
expect(result.isOk()).toBe(true);
expect((result.unwrapOr({}) as EncoderResult).jwt).not.toBeNull();
expect((result.unwrapOr({}) as EncoderResult).signingErrors).toEqual(
[`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).`]
);
});
});
});