Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

desktopBootstrapToken.test.ts

49 lines1.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS,
5 currentDesktopBootstrapToken,
6 isValidDesktopBootstrapToken,
7} from "./desktopBootstrapToken.ts";
8​
9const SECRET = "desktop-secret";
10const WINDOW = DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS;
11​
12describe("desktop bootstrap token", () => {
13 it("rotates every window and stays accepted for one more", () => {
14 const issuedAt = 10 * WINDOW + 1;
15 const token = currentDesktopBootstrapToken(SECRET, issuedAt);
16​
17 expect(currentDesktopBootstrapToken(SECRET, issuedAt + WINDOW)).not.toBe(token);
18 expect(isValidDesktopBootstrapToken(SECRET, token, issuedAt)).toBe(true);
19 expect(isValidDesktopBootstrapToken(SECRET, token, issuedAt + WINDOW)).toBe(true);
20 expect(isValidDesktopBootstrapToken(SECRET, token, issuedAt + 2 * WINDOW)).toBe(false);
21 });
22​
23 it("accepts a token from a desktop clock slightly ahead of the backend", () => {
24 const desktopNow = 10 * WINDOW + 500;
25 const backendNow = 10 * WINDOW - 500;
26​
27 expect(
28 isValidDesktopBootstrapToken(
29 SECRET,
30 currentDesktopBootstrapToken(SECRET, desktopNow),
31 backendNow,
32 ),
33 ).toBe(true);
34 expect(
35 isValidDesktopBootstrapToken(
36 SECRET,
37 currentDesktopBootstrapToken(SECRET, desktopNow + WINDOW),
38 backendNow,
39 ),
40 ).toBe(false);
41 });
42​
43 it("rejects tokens derived from another secret", () => {
44 const token = currentDesktopBootstrapToken("other-secret", WINDOW);
45​
46 expect(isValidDesktopBootstrapToken(SECRET, token, WINDOW)).toBe(false);
47 expect(isValidDesktopBootstrapToken(SECRET, "short", WINDOW)).toBe(false);
48 });
49});