Files
loki/scripts/site-template/lib/auth/methods.test.ts
CatoandClaude c83c8f473e feat(starter): kickoff and new-site.sh both produce the fleet starter (#1138)
scripts/site-template is now bitbaum/fleet templates/app — Next.js 16,
better-auth accounts (password, link, passkey, 2FA, OrangeCat/bitbaum sign-in
as env-gated OIDC), en/de/fr, nonce CSP, ai-kit/mail-kit/limitkit/bip-kit,
tests and CI — copied at a pinned fleet commit by
scripts/templates/sync-starter.sh. CI proves the copy is exact
(sync-starter.sh --check), and starter-sync.yml opens the PR when fleet's
starter moves. Never edited here.

The kickoff's "nextjs-tailwind" starter seeds the same app from
src/lib/starter-app.generated.ts (generated from the directory, pinned by
scripts/test/starter-app.ts), minus the workflows register-site.sh adds with
the site's CD. The repo is seeded in one Trees API call with inline contents
instead of a blob POST per file (~85 for this starter). The widget token rides
in .env.production; the starter's .gitleaks.toml allows NEXT_PUBLIC_* values.
The new-from-scratch picker reads the light template list, so the starter's
file bodies never reach a client bundle.

Both paths give the site its database and auth/seal secrets ON THE BOX
(scripts/hetzner/provision-db-on-box.sh: role + database, ~/.db-credentials,
/opt/<slug>/shared/.env; existing secrets never replaced; nothing printed), and
record the db in the register row so every deploy applies drizzle/.
register-site.sh seeds the starter's full workflow set to starter repos and
the golden CI floor to anything else, never overwriting another repo's ci.yml.

test-site-template.sh follows the starter (verify SSOT, design tokens instead
of a local palette) and its comment stripper is fixed: the sed range deleted
the page from the doc comment to EOF, so every day-zero check had been passing
against an empty string. check-template-contrast.mjs and the starter-lock
generator are retired with the files they checked.


Claude-Session: https://claude.ai/code/session_01W7K2Gm5wcbfCi7THtmV1Hy

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-11 13:39:14 -07:00

106 lines
3.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
isDeliverable,
oidcClients,
orangecatOidc,
placeholderEmail,
signinBroker,
signInMethods,
usableIssuer,
} from "./methods";
const BROKER = {
SIGNIN_ISSUER: "https://signin.example.ch",
SIGNIN_CLIENT_ID: "app",
SIGNIN_CLIENT_SECRET: "s3cret",
};
const ORANGECAT = { ORANGECAT_OAUTH_CLIENT_ID: "app", ORANGECAT_OAUTH_CLIENT_SECRET: "s3cret" };
describe("signInMethods", () => {
it("offers password and passkeys with no setup at all", () => {
const methods = signInMethods({ NODE_ENV: "production" }, false);
expect(methods).toMatchObject({ password: true, passkey: true, twoFactor: true });
});
it("hides every external button when its env is missing", () => {
const methods = signInMethods({ NODE_ENV: "production" }, false);
expect(methods.signin).toBe(false);
expect(methods.orangecat).toBe(false);
});
it("hides the magic link in production without mail, and never strands a sign-up", () => {
const methods = signInMethods({ NODE_ENV: "production" }, false);
expect(methods.magicLink).toBe(false);
expect(methods.verifyEmail).toBe(false);
});
it("offers the magic link and demands verification once mail works", () => {
const methods = signInMethods({ NODE_ENV: "production" }, true);
expect(methods.magicLink).toBe(true);
expect(methods.verifyEmail).toBe(true);
});
it("prints the magic link locally in development", () => {
expect(signInMethods({ NODE_ENV: "development" }, false).magicLink).toBe(true);
});
it("shows each OIDC button exactly when its three values are set", () => {
expect(signInMethods({ ...BROKER }, false).signin).toBe(true);
expect(signInMethods({ ...BROKER, SIGNIN_CLIENT_SECRET: " " }, false).signin).toBe(false);
expect(signInMethods({ ...ORANGECAT }, false).orangecat).toBe(true);
expect(signInMethods({ ORANGECAT_OAUTH_CLIENT_ID: "app" }, false).orangecat).toBe(false);
});
});
describe("OIDC clients", () => {
it("builds the discovery URL from the issuer", () => {
expect(signinBroker(BROKER)?.discoveryUrl).toBe(
"https://signin.example.ch/.well-known/openid-configuration",
);
});
it("defaults OrangeCat to its public issuer", () => {
expect(orangecatOidc(ORANGECAT)?.issuer).toBe("https://orangecat.ch");
expect(
orangecatOidc({ ...ORANGECAT, ORANGECAT_OAUTH_ISSUER: "https://oc.test/" })?.issuer,
).toBe("https://oc.test/");
});
it("refuses an issuer that is not https", () => {
expect(signinBroker({ ...BROKER, SIGNIN_ISSUER: "http://signin.example.ch" })).toBeNull();
expect(signinBroker({ ...BROKER, SIGNIN_ISSUER: "signin.example.ch" })).toBeNull();
expect(usableIssuer("https://user:pw@x.ch")).toBeNull();
expect(usableIssuer("http://localhost:3001")).toBe("http://localhost:3001");
});
it("lists the broker before OrangeCat", () => {
expect(oidcClients({ ...BROKER, ...ORANGECAT }).map((c) => c.providerId)).toEqual([
"bitbaum-signin",
"orangecat",
]);
});
});
describe("placeholderEmail", () => {
it("can never receive mail and never repeats across providers", () => {
const a = placeholderEmail("orangecat", "sub-1");
expect(a).toMatch(/^[0-9a-f]{32}@orangecat\.invalid$/);
expect(isDeliverable(a)).toBe(false);
expect(placeholderEmail("bitbaum-signin", "sub-1")).not.toBe(a);
expect(placeholderEmail("orangecat", "sub-1")).toBe(a);
});
it("does not reveal the subject", () => {
expect(placeholderEmail("orangecat", "actor-42")).not.toContain("actor-42");
});
});
describe("isDeliverable", () => {
it("accepts a real address and refuses the rest", () => {
expect(isDeliverable("person@example.ch")).toBe(true);
expect(isDeliverable("x@y.INVALID")).toBe(false);
expect(isDeliverable("")).toBe(false);
expect(isDeliverable("no-at-sign")).toBe(false);
});
});