返回 CodeWhale
public-auth-routes.test.ts
根目录 / web / lib / public-auth-routes.test.ts
1 import { createHash } from "node:crypto";
2 import { readFileSync } from "node:fs";
3 import { describe, expect, it } from "vitest";
4 import { APP_LOGIN_URL, APP_URL } from "./i18n/links";
5 import {
6 APP_AUTH_CALLBACK_URL,
7 CANONICAL_MARK_SHA256,
8 CANONICAL_MARK_SRC,
9 canonicalPublicAuthPath,
10 publicAuthAppDestination,
11 publicAuthCallbackDestination,
12 publicAuthKind,
13 publicAuthRemainder,
14 } from "./public-auth-routes";
15
16 const GEIST_SANS_SHA256 = "5c5588027e59e74e205b9d6c7fd877b261091cee367f6bb3a8d505da14cae512";
17 const GEIST_SANS_BYTES = 69576;
18 const GEIST_MONO_SHA256 = "cf5bf55f69e043b1ecf04b0719119b7298e94add67ebcaa709a925b945b46b25";
19 const GEIST_MONO_BYTES = 71272;
20 const GEIST_OFL_SHA256 = "930853ee1daa68554d9e35c8a9175affb74f699fad9a5da6ee5ebe76379d9137";
21 const GEIST_OFL_BYTES = 4368;
22
23 describe("public auth remainders and kinds", () => {
24 it("recognizes the observed 404 paths with and without a locale prefix", () => {
25 expect(publicAuthRemainder("/signin")).toBe("signin");
26 expect(publicAuthRemainder("/en/signin")).toBe("signin");
27 expect(publicAuthRemainder("/pt-BR/signup")).toBe("signup");
28 expect(publicAuthRemainder("/zh/auth/callback")).toBe("auth/callback");
29 expect(publicAuthKind("/signin")).toBe("sign-in");
30 expect(publicAuthKind("/en/signin")).toBe("sign-in");
31 expect(publicAuthKind("/signup")).toBe("sign-up");
32 expect(publicAuthKind("/ja/signup")).toBe("sign-up");
33 expect(publicAuthKind("/auth/callback")).toBe("callback");
34 expect(publicAuthKind("/en/auth/callback")).toBe("callback");
35 expect(publicAuthKind("/install")).toBeNull();
36 expect(publicAuthKind("/en/docs")).toBeNull();
37 });
38
39 it("folds login/register aliases onto the public sign-in route", () => {
40 expect(canonicalPublicAuthPath("/login")).toBe("/signin");
41 expect(canonicalPublicAuthPath("/en/login")).toBe("/en/signin");
42 expect(canonicalPublicAuthPath("/register")).toBe("/signin");
43 expect(canonicalPublicAuthPath("/zh/register")).toBe("/zh/signin");
44 expect(canonicalPublicAuthPath("/create-account")).toBe("/signin");
45 expect(canonicalPublicAuthPath("/signin")).toBeNull();
46 expect(canonicalPublicAuthPath("/en/signup")).toBeNull();
47 });
48 });
49
50 describe("CWC destinations", () => {
51 it("sends English (and other) public pages to the default app entry", () => {
52 expect(publicAuthAppDestination("en")).toBe(APP_LOGIN_URL);
53 expect(publicAuthAppDestination("ja")).toBe(APP_LOGIN_URL);
54 expect(publicAuthAppDestination("zh")).toBe(`${APP_URL}/zh/login`);
55 });
56
57 it("hops auth callbacks to the CWC app and preserves the query string", () => {
58 expect(APP_AUTH_CALLBACK_URL).toBe(`${APP_URL}/auth/callback`);
59 expect(
60 publicAuthCallbackDestination(new URL("https://codewhale.net/auth/callback?code=abc&state=1")),
61 ).toBe(`${APP_AUTH_CALLBACK_URL}?code=abc&state=1`);
62 expect(
63 publicAuthCallbackDestination(new URL("https://codewhale.net/en/auth/callback?code=abc")),
64 ).toBe(`${APP_AUTH_CALLBACK_URL}?code=abc`);
65 expect(
66 publicAuthCallbackDestination(new URL("https://codewhale.net/en/signin")),
67 ).toBeNull();
68 });
69 });
70
71 describe("canonical mark", () => {
72 it("ships the pinned raster generated from the canonical vector", () => {
73 const bytes = readFileSync(new URL("../public/brand/codewhale-mark.png", import.meta.url));
74 expect(createHash("sha256").update(bytes).digest("hex")).toBe(CANONICAL_MARK_SHA256);
75 expect(CANONICAL_MARK_SRC).toBe("/brand/codewhale-mark.png");
76 });
77 });
78
79 describe("website font assets", () => {
80 it("ships the pinned Geist faces with the complete adjacent OFL notice", () => {
81 const read = (name: string) => readFileSync(new URL(`../public/brand/fonts/${name}`, import.meta.url));
82 const sha = (data: Buffer) => createHash("sha256").update(data).digest("hex");
83 const [sans, mono, license] = [read("Geist-Variable.woff2"), read("GeistMono-Variable.woff2"), read("OFL.txt")];
84 expect([sans.length, sha(sans)]).toEqual([GEIST_SANS_BYTES, GEIST_SANS_SHA256]);
85 expect([mono.length, sha(mono)]).toEqual([GEIST_MONO_BYTES, GEIST_MONO_SHA256]);
86 expect([license.length, sha(license)]).toEqual([GEIST_OFL_BYTES, GEIST_OFL_SHA256]);
87 });
88 });
89
89 lines TYPESCRIPT