Files
Jyotisha/frontend/tests/identity-config.test.ts
Jesse 2697d16ef1
Staging Backend Quality Gate / validate (push) Has been cancelled
Staging Backend Quality Gate / publish (push) Has been cancelled
feat(identity): test-only fixed OTP channel and rectification candidate cards
- IDENTITY_TEST_OTP env: when set to a 6-digit code, no real email is
  delivered; login page surfaces the fixed code so testers can register
  and sign in without a mailbox. Opt-in, never set in production.
- email-otp-login: test-channel notice with the pinned code.
- Rectification candidate list restyled with the project design system
  (warm canvas, action color, display serif, soft shadow, hover lift).
- Regression tests for config parsing, pinned OTP generation, login UI
  notice, and candidate card styles.
2026-08-10 19:38:27 +08:00

80 lines
3.8 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { isSelfHostedIdentityEnabled, readIdentityConfig, readSelfHostedIdentityConfig } from "../src/modules/identity/config.ts";
const selfHostedEnvironment = {
AUTH_PROVIDER: "self-hosted",
SELF_HOSTED_IDENTITY_ENABLED: "true",
IDENTITY_DATABASE_URL: "postgresql://identity_runtime:test-password@postgres:5432/jyotisha",
AUTH_USER_ORIGIN: "https://staging.jyotisha.chat",
ADMIN_USER_ORIGIN: "https://admin.staging.jyotisha.chat",
BETTER_AUTH_USER_SECRET: "user-secret-that-is-at-least-32-bytes-long",
RESEND_API_KEY: "re_test_key_that_must_not_be_printed",
RESEND_FROM_EMAIL: "Jyotisha Staging <login@staging.jyotisha.chat>",
};
test("identity provider defaults to supabase", () => {
assert.deepEqual(readIdentityConfig({}), { provider: "supabase" });
assert.equal(isSelfHostedIdentityEnabled({}), false);
});
test("identity config accepts the two exact self-hosted origins and one secret", () => {
const config = readIdentityConfig(selfHostedEnvironment);
assert.equal(config.provider, "self-hosted");
if (config.provider !== "self-hosted") assert.fail();
assert.equal(config.userOrigin, "https://staging.jyotisha.chat");
assert.equal(config.adminOrigin, "https://admin.staging.jyotisha.chat");
assert.equal(config.userSecret, selfHostedEnvironment.BETTER_AUTH_USER_SECRET);
assert.equal("adminSecret" in config, false);
});
test("self-hosted identity ignores retired admin-surface variables", () => {
const config = readSelfHostedIdentityConfig({
...selfHostedEnvironment,
AUTH_ADMIN_ORIGIN: "invalid-retired-value",
BETTER_AUTH_ADMIN_SECRET: "short",
});
assert.equal(config.userOrigin, selfHostedEnvironment.AUTH_USER_ORIGIN);
assert.equal(config.adminOrigin, selfHostedEnvironment.ADMIN_USER_ORIGIN);
});
test("self-hosted provider requires its enable flag", () => {
assert.throws(() => readIdentityConfig({ ...selfHostedEnvironment, SELF_HOSTED_IDENTITY_ENABLED: "false" }), /must be true/);
});
test("self-hosted identity validates active database, origin, secret, and sender", () => {
assert.throws(() => readIdentityConfig({ ...selfHostedEnvironment, IDENTITY_DATABASE_URL: "https://invalid" }), /PostgreSQL URL/);
assert.throws(() => readIdentityConfig({ ...selfHostedEnvironment, AUTH_USER_ORIGIN: "http://staging.jyotisha.chat" }), /must use HTTPS/);
assert.throws(() => readIdentityConfig({ ...selfHostedEnvironment, ADMIN_USER_ORIGIN: "https://admin.staging.jyotisha.chat/path" }), /without a path/);
assert.throws(() => readIdentityConfig({ ...selfHostedEnvironment, ADMIN_USER_ORIGIN: selfHostedEnvironment.AUTH_USER_ORIGIN }), /must differ/);
assert.throws(() => readIdentityConfig({ ...selfHostedEnvironment, BETTER_AUTH_USER_SECRET: "short" }), /at least 32/);
assert.throws(() => readIdentityConfig({ ...selfHostedEnvironment, RESEND_FROM_EMAIL: "invalid" }), /valid email/);
});
test("localhost may use two distinct HTTP origins", () => {
const config = readIdentityConfig({
...selfHostedEnvironment,
AUTH_USER_ORIGIN: "http://localhost:3000",
ADMIN_USER_ORIGIN: "http://admin.localhost:3000",
});
assert.equal(config.provider, "self-hosted");
});
test("identity test OTP channel is opt-in and strictly six digits", () => {
const without = readIdentityConfig(selfHostedEnvironment);
assert.equal(without.provider === "self-hosted" ? without.testOtp : "unexpected", null);
const withOtp = readIdentityConfig({ ...selfHostedEnvironment, IDENTITY_TEST_OTP: "123456" });
assert.equal(withOtp.provider === "self-hosted" ? withOtp.testOtp : null, "123456");
assert.throws(
() => readIdentityConfig({ ...selfHostedEnvironment, IDENTITY_TEST_OTP: "12345" }),
/must be a 6-digit/,
);
assert.throws(
() => readIdentityConfig({ ...selfHostedEnvironment, IDENTITY_TEST_OTP: "abcdef" }),
/must be a 6-digit/,
);
});