Skip to content

Commit f7cccc9

Browse files
committed
fix jwt service tests
1 parent 1470b37 commit f7cccc9

File tree

1 file changed

+146
-88
lines changed

1 file changed

+146
-88
lines changed

tests/jwt.service.test.ts

Lines changed: 146 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { describe, expect, test } from "vitest";
22
import { DefaultTokensValues } from "@/features/common/values/default-tokens.values";
33
import { validateJwtFormat } from "@/features/common/services/jwt.service";
44
import { JwtTypeValues } from "@/features/common/values/jwt-type.values";
5+
import { DebuggerTaskValues } from "@/features/common/values/debugger-task.values";
6+
import { DebuggerInputValues } from "@/features/common/values/debugger-input.values";
57

68
describe("validateJwtFormat", () => {
7-
const tokenHS256 = DefaultTokensValues.hs256.token;
8-
const tokenHS384 = DefaultTokensValues.hs384.token;
9-
const tokenHS512 = DefaultTokensValues.hs512.token;
10-
const tokenRS256 = DefaultTokensValues.rs256.token;
11-
const tokenRS384 = DefaultTokensValues.rs384.token;
12-
const tokenRS512 = DefaultTokensValues.rs512.token;
9+
const tokenHS256 = DefaultTokensValues.HS256.token;
10+
const tokenHS384 = DefaultTokensValues.HS384.token;
11+
const tokenHS512 = DefaultTokensValues.HS512.token;
12+
const tokenRS256 = DefaultTokensValues.RS256.token;
13+
const tokenRS384 = DefaultTokensValues.RS384.token;
14+
const tokenRS512 = DefaultTokensValues.RS512.token;
1315
const unsecured =
1416
"eyJhbGciOiJub25lIn0.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.";
1517

@@ -24,64 +26,76 @@ describe("validateJwtFormat", () => {
2426
const invalidToken8 =
2527
"eyJhbGciOiJIUzI1N9.dGVzdA.Yysa_W8n99vc_zcHxetNl4qo8gNx1qZu63I0H5UTYAI";
2628
const invalidToken9 =
27-
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c.abc";
29+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c.abc";
2830

2931
test("input is a valid JWT", () => {
3032
const result1 = validateJwtFormat(tokenHS256);
3133
expect(result1.isErr()).toBe(false);
3234
expect(result1.isOk()).toBe(true);
3335
result1.map((value) =>
3436
expect(value).toStrictEqual({
35-
signingAlgorithm: "HS256",
36-
type: JwtTypeValues.MACed,
37-
encoded: {
38-
token:
39-
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
40-
header: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
41-
payload:
42-
"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ",
43-
signature: "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
44-
},
4537
decoded: {
4638
header: {
4739
alg: "HS256",
4840
typ: "JWT",
4941
},
42+
payload: {
43+
admin: true,
44+
sub: "1234567890",
45+
name: "John Doe",
46+
iat: 1516239022,
47+
},
48+
},
49+
signingAlgorithm: "HS256",
50+
type: JwtTypeValues.MACed,
51+
})
52+
);
53+
54+
const result2 = validateJwtFormat(tokenHS384);
55+
expect(result2.isErr()).toBe(false);
56+
expect(result2.isOk()).toBe(true);
57+
result2.map((value) =>
58+
expect(value).toStrictEqual({
59+
type: JwtTypeValues.MACed,
60+
signingAlgorithm: "HS384",
61+
decoded: {
62+
header: { alg: "HS384", typ: "JWT" },
63+
payload: {
64+
sub: "1234567890",
65+
name: "John Doe",
66+
admin: true,
67+
iat: 1516239022,
68+
},
69+
},
70+
})
71+
);
72+
73+
const result3 = validateJwtFormat(tokenHS512);
74+
expect(result3.isErr()).toBe(false);
75+
expect(result3.isOk()).toBe(true);
76+
result3.map((value) =>
77+
expect(value).toStrictEqual({
78+
type: JwtTypeValues.MACed,
79+
signingAlgorithm: "HS512",
80+
decoded: {
81+
header: { alg: "HS512", typ: "JWT" },
5082
payload: {
5183
sub: "1234567890",
5284
name: "John Doe",
85+
admin: true,
5386
iat: 1516239022,
5487
},
5588
},
56-
}),
57-
);
58-
59-
// const result2 = validateJwtFormat(tokenHS384);
60-
// expect(result2.isErr()).toBe(false);
61-
// expect(result2.isOk()).toBe(true);
62-
// result2.map((value) => expect(value).toBe(tokenHS384));
63-
//
64-
// const result3 = validateJwtFormat(tokenHS512);
65-
// expect(result3.isErr()).toBe(false);
66-
// expect(result3.isOk()).toBe(true);
67-
// result3.map((value) => expect(value).toBe(tokenHS512));
68-
//
89+
})
90+
);
91+
6992
const result4 = validateJwtFormat(tokenRS256);
7093
expect(result4.isErr()).toBe(false);
7194
expect(result4.isOk()).toBe(true);
7295
result4.map((value) =>
7396
expect(value).toStrictEqual({
7497
signingAlgorithm: "RS256",
7598
type: JwtTypeValues.DigitallySigned,
76-
encoded: {
77-
token:
78-
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.NHVaYe26MbtOYhSKkoKYdFVomg4i8ZJd8_-RU8VNbftc4TSMb4bXP3l3YlNWACwyXPGffz5aXHc6lty1Y2t4SWRqGteragsVdZufDn5BlnJl9pdR_kdVFUsra2rWKEofkZeIC4yWytE58sMIihvo9H1ScmmVwBcQP6XETqYd0aSHp1gOa9RdUPDvoXQ5oqygTqVtxaDr6wUFKrKItgBMzWIdNZ6y7O9E0DhEPTbE9rfBo6KTFsHAZnMg4k68CDp2woYIaXbmYTWcvbzIuHO7_37GT79XdIwkm95QJ7hYC9RiwrV7mesbY4PAahERJawntho0my942XheVLmGwLMBkQ",
79-
header: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9",
80-
payload:
81-
"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0",
82-
signature:
83-
"NHVaYe26MbtOYhSKkoKYdFVomg4i8ZJd8_-RU8VNbftc4TSMb4bXP3l3YlNWACwyXPGffz5aXHc6lty1Y2t4SWRqGteragsVdZufDn5BlnJl9pdR_kdVFUsra2rWKEofkZeIC4yWytE58sMIihvo9H1ScmmVwBcQP6XETqYd0aSHp1gOa9RdUPDvoXQ5oqygTqVtxaDr6wUFKrKItgBMzWIdNZ6y7O9E0DhEPTbE9rfBo6KTFsHAZnMg4k68CDp2woYIaXbmYTWcvbzIuHO7_37GT79XdIwkm95QJ7hYC9RiwrV7mesbY4PAahERJawntho0my942XheVLmGwLMBkQ",
84-
},
8599
decoded: {
86100
header: {
87101
alg: "RS256",
@@ -94,34 +108,54 @@ describe("validateJwtFormat", () => {
94108
iat: 1516239022,
95109
},
96110
},
97-
}),
98-
);
99-
100-
// const result5 = validateJwtFormat(tokenRS384);
101-
// expect(result5.isErr()).toBe(false);
102-
// expect(result5.isOk()).toBe(true);
103-
// result5.map((value) => expect(value).toBe(tokenRS384));
104-
//
105-
// const result6 = validateJwtFormat(tokenRS512);
106-
// expect(result6.isErr()).toBe(false);
107-
// expect(result6.isOk()).toBe(true);
108-
// result6.map((value) => expect(value).toBe(tokenRS512));
109-
//
111+
})
112+
);
113+
114+
const result5 = validateJwtFormat(tokenRS384);
115+
expect(result5.isErr()).toBe(false);
116+
expect(result5.isOk()).toBe(true);
117+
result5.map((value) =>
118+
expect(value).toStrictEqual({
119+
type: JwtTypeValues.DigitallySigned,
120+
signingAlgorithm: "RS384",
121+
decoded: {
122+
header: { alg: "RS384", typ: "JWT" },
123+
payload: {
124+
sub: "1234567890",
125+
name: "John Doe",
126+
admin: true,
127+
iat: 1516239022,
128+
},
129+
},
130+
})
131+
);
132+
133+
const result6 = validateJwtFormat(tokenRS512);
134+
expect(result6.isErr()).toBe(false);
135+
expect(result6.isOk()).toBe(true);
136+
result6.map((value) =>
137+
expect(value).toStrictEqual({
138+
type: JwtTypeValues.DigitallySigned,
139+
signingAlgorithm: "RS512",
140+
decoded: {
141+
header: { alg: "RS512", typ: "JWT" },
142+
payload: {
143+
sub: "1234567890",
144+
name: "John Doe",
145+
admin: true,
146+
iat: 1516239022,
147+
},
148+
},
149+
})
150+
);
151+
110152
const result7 = validateJwtFormat(unsecured);
111153
expect(result7.isErr()).toBe(false);
112154
expect(result7.isOk()).toBe(true);
113155
result7.map((value) =>
114156
expect(value).toStrictEqual({
115157
signingAlgorithm: "none",
116158
type: JwtTypeValues.Unsecured,
117-
encoded: {
118-
token:
119-
"eyJhbGciOiJub25lIn0.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.",
120-
header: "eyJhbGciOiJub25lIn0",
121-
payload:
122-
"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",
123-
signature: "",
124-
},
125159
decoded: {
126160
header: {
127161
alg: "none",
@@ -132,7 +166,7 @@ describe("validateJwtFormat", () => {
132166
"http://example.com/is_root": true,
133167
},
134168
},
135-
}),
169+
})
136170
);
137171
});
138172

@@ -141,81 +175,105 @@ describe("validateJwtFormat", () => {
141175
expect(result1.isErr()).toBe(true);
142176
expect(result1.isOk()).toBe(false);
143177
result1.mapErr((error) =>
144-
expect(error).toStrictEqual([
145-
"The first segment, the JWT header, and the second segment, the JWT payload, must represent a completely valid JSON object conforming to RFC 7159.",
146-
]),
178+
expect(error).toStrictEqual({
179+
task: DebuggerTaskValues.DECODE,
180+
input: DebuggerInputValues.JWT,
181+
message: `This tool only supports a JWT that uses the JWS Compact Serialization, which must have three base64url-encoded segments separated by two period ('.') characters as defined on [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-3.3)`,
182+
})
147183
);
148184

149185
const result2 = validateJwtFormat(invalidToken2);
150186
expect(result2.isErr()).toBe(true);
151187
expect(result2.isOk()).toBe(false);
152188
result2.mapErr((error) =>
153-
expect(error).toStrictEqual([
154-
"The first segment, the JWT header, and the second segment, the JWT payload, must represent a completely valid JSON object conforming to RFC 7159.",
155-
]),
189+
expect(error).toStrictEqual({
190+
task: DebuggerTaskValues.DECODE,
191+
input: DebuggerInputValues.JWT,
192+
message: `This tool only supports a JWT that uses the JWS Compact Serialization, which must have three base64url-encoded segments separated by two period ('.') characters as defined on [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-3.3)`,
193+
})
156194
);
157195

158196
const result3 = validateJwtFormat(invalidToken3);
159197
expect(result3.isErr()).toBe(true);
160198
expect(result3.isOk()).toBe(false);
161199
result3.mapErr((error) =>
162-
expect(error).toStrictEqual([
163-
"The second (payload) segment cannot be an empty string.",
164-
]),
200+
expect(error).toStrictEqual({
201+
task: DebuggerTaskValues.DECODE,
202+
input: DebuggerInputValues.JWT,
203+
message: `This tool only supports a JWT that uses the JWS Compact Serialization, which must have three base64url-encoded segments separated by two period ('.') characters as defined on [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-3.3)`,
204+
})
165205
);
166206

167207
const result4 = validateJwtFormat(invalidToken4);
168208
expect(result4.isErr()).toBe(true);
169209
expect(result4.isOk()).toBe(false);
170210
result4.mapErr((error) =>
171-
expect(error).toStrictEqual([
172-
"The JWT must contain at least one period ('.') character. Source: https://datatracker.ietf.org/doc/html/rfc7519#section-7.2",
173-
]),
211+
expect(error).toStrictEqual({
212+
task: DebuggerTaskValues.DECODE,
213+
input: DebuggerInputValues.JWT,
214+
message: `This tool only supports a JWT that uses the JWS Compact Serialization, which must have three base64url-encoded segments separated by two period ('.') characters as defined on [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-3.3)`,
215+
})
174216
);
175217

176218
const result5 = validateJwtFormat(invalidToken5);
177219
expect(result5.isErr()).toBe(true);
178220
expect(result5.isOk()).toBe(false);
179221
result5.mapErr((error) =>
180-
expect(error).toStrictEqual([
181-
"The JWT must contain at least one period ('.') character. Source: https://datatracker.ietf.org/doc/html/rfc7519#section-7.2",
182-
]),
222+
expect(error).toStrictEqual({
223+
task: DebuggerTaskValues.DECODE,
224+
input: DebuggerInputValues.JWT,
225+
message: `JWT must not be empty.`,
226+
})
183227
);
184228

185229
const result6 = validateJwtFormat(invalidToken6);
186230
expect(result6.isErr()).toBe(true);
187231
expect(result6.isOk()).toBe(false);
188232
result6.mapErr((error) =>
189-
expect(error).toStrictEqual([
190-
"Each JWT segment must be a base64url-encoded. The third (signature) segment isn't.",
191-
]),
233+
expect(error).toStrictEqual({
234+
task: DebuggerTaskValues.DECODE,
235+
input: DebuggerInputValues.JWT,
236+
message: `This tool only supports a JWT that uses the JWS Compact Serialization, which must have three base64url-encoded segments separated by two period ('.') characters as defined on [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-3.3)`,
237+
})
192238
);
193239

194240
const result7 = validateJwtFormat(invalidToken7);
195241
expect(result7.isErr()).toBe(true);
196242
expect(result7.isOk()).toBe(false);
197243
result7.mapErr((error) =>
198-
expect(error).toStrictEqual([
199-
"The second segment, the JWT payload, must represent a completely valid JSON object conforming to RFC 7159.",
200-
]),
244+
expect(error).toStrictEqual({
245+
task: DebuggerTaskValues.DECODE,
246+
input: DebuggerInputValues.JWT,
247+
message: `The second segment, the JWT payload, must represent a completely valid JSON object conforming to [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519#section-3).`,
248+
data: {
249+
header: {
250+
alg: "HS256",
251+
},
252+
payload: "test",
253+
},
254+
})
201255
);
202256

203257
const result8 = validateJwtFormat(invalidToken8);
204258
expect(result8.isErr()).toBe(true);
205259
expect(result8.isOk()).toBe(false);
206260
result8.mapErr((error) =>
207-
expect(error).toStrictEqual([
208-
"The first segment, the JWT header, and the second segment, the JWT payload, must represent a completely valid JSON object conforming to RFC 7159.",
209-
]),
261+
expect(error).toStrictEqual({
262+
task: DebuggerTaskValues.DECODE,
263+
input: DebuggerInputValues.JWT,
264+
message: `This tool only supports a JWT that uses the JWS Compact Serialization, which must have three base64url-encoded segments separated by two period ('.') characters as defined on [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-3.3)`,
265+
})
210266
);
211267

212268
const result9 = validateJwtFormat(invalidToken9);
213269
expect(result9.isErr()).toBe(true);
214270
expect(result9.isOk()).toBe(false);
215271
result9.mapErr((error) =>
216-
expect(error).toStrictEqual([
217-
"This tool only supports a JWT that uses the JWS Compact Serialization, which must have three base64url-encoded segments separated by two period ('.') characters. Source: https://datatracker.ietf.org/doc/html/rfc7516#section-9",
218-
]),
272+
expect(error).toStrictEqual({
273+
task: DebuggerTaskValues.DECODE,
274+
input: DebuggerInputValues.JWT,
275+
message: `This tool only supports a JWT that uses the JWS Compact Serialization, which must have three base64url-encoded segments separated by two period ('.') characters as defined on [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-3.3)`,
276+
})
219277
);
220278
});
221279
});

0 commit comments

Comments
 (0)