Files
Jyotisha/frontend/tests/identity-config.test.ts
T
linmeng f4e35974c6
Deploy staging to test server / deploy (push) Failing after 14m49s
Revert "merge: sync GitHub staging to Gitea"
This reverts commit a55c69115d, reversing
changes made to 02c9c9f3d6.
2026-07-30 14:38:17 +08:00

55 lines
2.6 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",
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 one self-hosted user origin and 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.userSecret, selfHostedEnvironment.BETTER_AUTH_USER_SECRET);
assert.equal("adminOrigin" in config, false);
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);
});
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, BETTER_AUTH_USER_SECRET: "short" }), /at least 32/);
assert.throws(() => readIdentityConfig({ ...selfHostedEnvironment, RESEND_FROM_EMAIL: "invalid" }), /valid email/);
});
test("localhost may use HTTP", () => {
const config = readIdentityConfig({ ...selfHostedEnvironment, AUTH_USER_ORIGIN: "http://localhost:3000" });
assert.equal(config.provider, "self-hosted");
});