-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathencoder.spec.ts
More file actions
525 lines (397 loc) · 15.2 KB
/
encoder.spec.ts
File metadata and controls
525 lines (397 loc) · 15.2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import { expect, test } from "@playwright/test";
import {
getButtonsUiDictionary,
getPickersUiDictionary,
} from "@/features/localization/services/ui-language-dictionary.service";
import { DefaultTokensValues } from "@/features/common/values/default-tokens.values";
import { dataTestidDictionary } from "@/libs/testing/data-testid.dictionary";
import {
checkHeaderEditorStatusBarMessage,
checkJwtStatusBarMessage,
checkPayloadEditorStatusBarMessage,
checkSecretKeyEncoderEditorStatusBarMessage,
E2E_BASE_URL,
expectToBeNonNull,
getLang,
switchToEncoderTab,
} from "./e2e.utils";
import jwts from "./jwt.json" with { type: "json" };
import {
JwtDictionaryModel,
JwtSignedWithDigitalModel,
JwtSignedWithHmacModel,
} from "./e2e.models";
import {
isDigitalSignatureAlg,
isHmacAlg,
isNoneAlg,
} from "@/features/common/services/jwt.service";
import { MessageStatusValue, MessageTypeValue } from "./e2e.values";
const TestJwts = (jwts as JwtDictionaryModel).byAlgorithm;
test.describe("Can interact with header editor in JWT Encoder", () => {
test.beforeEach(async ({ page }) => {
await page.goto(E2E_BASE_URL);
await switchToEncoderTab(page);
});
test("Can read default value in header editor", async ({ page }) => {
const encoderWidget = page.getByTestId(dataTestidDictionary.encoder.id);
const headerEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.headerEditor.id,
);
const headerEditorInput = headerEditor.getByRole("textbox");
const header = {
alg: `HS256`,
typ: "JWT",
};
const headerAsString = JSON.stringify(header, null, 2);
await expect(headerEditorInput).toHaveValue(headerAsString);
});
test("Can edit value in header editor", async ({ page }) => {
const inputValue = (TestJwts.RS512 as JwtSignedWithDigitalModel).withPemKey
.header;
const lang = await getLang(page);
expectToBeNonNull(lang);
const encoderWidget = page.getByTestId(dataTestidDictionary.encoder.id);
const headerEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.headerEditor.id,
);
const headerEditorInput = headerEditor.getByRole("textbox");
await headerEditorInput.fill(inputValue);
await expect(headerEditorInput).toHaveValue(inputValue);
});
test("Can clear value in header editor", async ({ page }) => {
const lang = await getLang(page);
expectToBeNonNull(lang);
const buttonsUiDictionary = getButtonsUiDictionary(lang);
const encoder = page.getByTestId(dataTestidDictionary.encoder.id);
const headerEditor = encoder.getByTestId(
dataTestidDictionary.encoder.headerEditor.id,
);
const headerEditorInput = headerEditor.getByRole("textbox");
const clearButton = headerEditor.getByRole("button", {
name: buttonsUiDictionary.clearButton.label,
});
await clearButton.click();
await expect(headerEditorInput).toHaveValue("");
});
});
test.describe("can interact with payload editor in JWT encoder", () => {
test.beforeEach(async ({ page }) => {
await page.goto(E2E_BASE_URL);
await switchToEncoderTab(page);
});
test("can read default value in payload editor", async ({ page }) => {
const encoderWidget = page.getByTestId(dataTestidDictionary.encoder.id);
const payloadEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.payloadEditor.id,
);
const payloadEditorInput = payloadEditor.getByRole("textbox");
const payload = {
sub: "1234567890",
name: "John Doe",
admin: true,
iat: 1516239022,
};
const payloadAsString = JSON.stringify(payload, null, 2);
await expect(payloadEditorInput).toHaveValue(payloadAsString);
});
test("can edit value in payload editor", async ({ page }) => {
const inputValue = "abc";
const lang = await getLang(page);
expectToBeNonNull(lang);
const encoderWidget = page.getByTestId(dataTestidDictionary.encoder.id);
const payloadEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.payloadEditor.id,
);
const payloadEditorInput = payloadEditor.getByRole("textbox");
await payloadEditorInput.fill(inputValue);
await expect(payloadEditorInput).toHaveValue(inputValue);
});
test("can clear value in payload editor", async ({ page }) => {
const lang = await getLang(page);
expectToBeNonNull(lang);
const buttonsUiDictionary = getButtonsUiDictionary(lang);
const encoder = page.getByTestId(dataTestidDictionary.encoder.id);
const payloadEditor = encoder.getByTestId(
dataTestidDictionary.encoder.payloadEditor.id,
);
const payloadEditorInput = payloadEditor.getByRole("textbox");
const clearButton = payloadEditor.getByRole("button", {
name: buttonsUiDictionary.clearButton.label,
});
await clearButton.click();
await expect(payloadEditorInput).toHaveValue("");
});
});
test.describe("can interact with secret editor in JWT encoder", () => {
test.beforeEach(async ({ page }) => {
await page.goto(E2E_BASE_URL);
await switchToEncoderTab(page);
});
test("can read default value in secret editor", async ({ page }) => {
const encoderWidget = page.getByTestId(dataTestidDictionary.encoder.id);
const secretKeyEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.secretKeyEditor.id,
);
const secretKeyEditorInput = secretKeyEditor.getByRole("textbox");
const secret = "a-string-secret-at-least-256-bits-long";
await expect(secretKeyEditorInput).toHaveValue(secret);
});
test("can edit value in secret editor", async ({ page }) => {
const inputValue = "abc";
const lang = await getLang(page);
expectToBeNonNull(lang);
const encoderWidget = page.getByTestId(dataTestidDictionary.encoder.id);
const secretKeyEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.secretKeyEditor.id,
);
const secretKeyEditorInput = secretKeyEditor.getByRole("textbox");
await secretKeyEditorInput.fill(inputValue);
await expect(secretKeyEditorInput).toHaveValue(inputValue);
});
test("can clear value in secret editor", async ({ page }) => {
const lang = await getLang(page);
expectToBeNonNull(lang);
const buttonsUiDictionary = getButtonsUiDictionary(lang);
const encoder = page.getByTestId(dataTestidDictionary.encoder.id);
const secretKeyEditor = encoder.getByTestId(
dataTestidDictionary.encoder.secretKeyEditor.id,
);
const secretKeyEditorInput = secretKeyEditor.getByRole("textbox");
const clearButton = secretKeyEditor.getByRole("button", {
name: buttonsUiDictionary.clearButton.label,
});
await clearButton.click();
await expect(secretKeyEditorInput).toHaveValue("");
});
});
test.describe("Generate JWT encoding examples", () => {
test.beforeEach(async ({ page }) => {
await page.goto(E2E_BASE_URL);
await switchToEncoderTab(page);
});
test("Can open and close encoder example widget", async ({ page }) => {
const lang = await getLang(page);
expectToBeNonNull(lang);
const pickersUiDictionary = getPickersUiDictionary(lang);
const encoder = page.getByTestId(dataTestidDictionary.encoder.id);
const exampleButton = encoder.getByRole("button", {
name: pickersUiDictionary.exampleAlgPicker.label,
});
await exampleButton.click();
await expect(exampleButton).not.toBeVisible();
const closeButton = page.getByRole("button", {
name: pickersUiDictionary.exampleAlgPicker.closeButton.label,
});
await closeButton.click();
await expect(exampleButton).toBeVisible();
await expect(closeButton).not.toBeVisible();
});
test.describe("Can generate a JWT example", () => {
test.beforeEach(async ({ page }) => {
const lang = await getLang(page);
expectToBeNonNull(lang);
const pickersUiDictionary = getPickersUiDictionary(lang);
const encoder = page.getByTestId(dataTestidDictionary.encoder.id);
const exampleButton = encoder.getByRole("button", {
name: pickersUiDictionary.exampleAlgPicker.label,
});
await exampleButton.click();
await expect(exampleButton).not.toBeVisible();
const pickerIndicator = encoder.getByText(
pickersUiDictionary.exampleAlgPicker.defaultValue,
);
await pickerIndicator.click();
});
const options = Object.keys(DefaultTokensValues);
options.forEach((option) => {
test(`can generate a JWT encoding example for ${option}`, async ({
page,
}) => {
if (option === "Ed25519") {
return;
}
const encoder = page.getByTestId(dataTestidDictionary.encoder.id);
await page.getByRole("option", { name: option }).click();
const jwtOutput = encoder
.getByTestId(dataTestidDictionary.encoder.jwt.id)
.getByRole("textbox");
if (isNoneAlg(option)) {
await checkHeaderEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await checkJwtStatusBarMessage({
page,
type: MessageTypeValue.WARNING,
status: MessageStatusValue.VISIBLE,
});
}
await expect(jwtOutput).toHaveValue(
/^[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*$/,
);
});
});
});
});
test.describe("encode JWTs", () => {
test.beforeEach(async ({ page }) => {
await page.goto(E2E_BASE_URL);
await switchToEncoderTab(page);
});
const options = Object.keys(DefaultTokensValues);
options.forEach((option) => {
test(`Can encode and sign a JWT with ${option}`, async ({ page }) => {
if (option === "Ed25519") {
return;
}
const encoderWidget = page.getByTestId(dataTestidDictionary.encoder.id);
const headerEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.headerEditor.id,
);
const payloadEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.payloadEditor.id,
);
const secretKeyEditor = encoderWidget.getByTestId(
dataTestidDictionary.encoder.secretKeyEditor.id,
);
const jwtOutput = encoderWidget
.getByTestId(dataTestidDictionary.encoder.jwt.id)
.getByRole("textbox");
const headerEditorInput = headerEditor.getByRole("textbox");
const payloadEditorInput = payloadEditor.getByRole("textbox");
const testJwt = TestJwts[option];
if (isNoneAlg(option) && testJwt.type === "unsecured") {
await headerEditorInput.fill(testJwt.header);
await checkHeaderEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await payloadEditorInput.fill(testJwt.payload);
await checkPayloadEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await expect(jwtOutput).toHaveValue(testJwt.jwt);
await checkJwtStatusBarMessage({
page,
type: MessageTypeValue.WARNING,
status: MessageStatusValue.VISIBLE,
});
return;
}
const secretKeyEditorInput = secretKeyEditor.getByRole("textbox");
if (isHmacAlg(option) && testJwt.type === "hmac") {
const tokenWithUtf8Secret = (testJwt as JwtSignedWithHmacModel)
.withUtf8Secret;
const tokenWithBase64urlSecret = (testJwt as JwtSignedWithHmacModel)
.withBase64urlSecret;
await headerEditorInput.fill(tokenWithUtf8Secret.header);
await checkHeaderEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await payloadEditorInput.fill(tokenWithUtf8Secret.payload);
await checkPayloadEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await secretKeyEditorInput.fill(tokenWithUtf8Secret.secret);
await checkSecretKeyEncoderEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await expect(jwtOutput).toHaveValue(tokenWithUtf8Secret.jwt);
const formatPicker = secretKeyEditor.locator(
".react-select__single-value",
);
await formatPicker.click();
await page
.getByRole("option", {
name: tokenWithBase64urlSecret.secretEncoding,
})
.click();
await checkSecretKeyEncoderEditorStatusBarMessage({
page,
type: MessageTypeValue.ERROR,
status: MessageStatusValue.VISIBLE,
});
await secretKeyEditorInput.fill(tokenWithBase64urlSecret.secret);
await checkSecretKeyEncoderEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await expect(jwtOutput).toHaveValue(tokenWithBase64urlSecret.jwt);
return;
}
if (isDigitalSignatureAlg(option) && testJwt.type === "digital") {
const tokenWithPemKey = (testJwt as JwtSignedWithDigitalModel)
.withPemKey;
const tokenWithJwkKey = (testJwt as JwtSignedWithDigitalModel)
.withJwkKey;
await headerEditorInput.fill(tokenWithPemKey.header);
await checkHeaderEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await payloadEditorInput.fill(tokenWithPemKey.payload);
await checkPayloadEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
await secretKeyEditorInput.fill(tokenWithPemKey.privateKey);
await checkSecretKeyEncoderEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
if (option.includes("RS")) {
await expect(jwtOutput).toHaveValue(tokenWithPemKey.jwt);
} else {
await expect(jwtOutput).toHaveValue(
/^[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*$/,
);
}
const formatPicker = secretKeyEditor.locator(
".react-select__single-value",
);
await formatPicker.click();
await page
.getByRole("option", {
name: tokenWithJwkKey.privateKeyFormat,
})
.click();
await checkSecretKeyEncoderEditorStatusBarMessage({
page,
type: MessageTypeValue.ERROR,
status: MessageStatusValue.VISIBLE,
});
await secretKeyEditorInput.fill(
JSON.stringify(tokenWithJwkKey.privateKey, null, 2),
);
await checkSecretKeyEncoderEditorStatusBarMessage({
page,
type: MessageTypeValue.SUCCESS,
status: MessageStatusValue.VISIBLE,
});
if (option.includes("RS")) {
await expect(jwtOutput).toHaveValue(tokenWithJwkKey.jwt);
} else {
await expect(jwtOutput).toHaveValue(
/^[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*$/,
);
}
return;
}
});
});
});