Drop NODE_ENV for explicit config toggles (todo §0.1); app is environment-agnostic
This commit is contained in:
@@ -2,12 +2,18 @@ import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { loadConfig } from "./config.ts";
|
||||
|
||||
// Minimal valid production env: the secrets are the only thing prod must supply.
|
||||
const prodEnv = { COOKIE_SECRET: "real-cookie-secret", CSRF_SECRET: "real-csrf-secret", NODE_ENV: "production" };
|
||||
// Explicit secure-secret enforcement (no environment sniffing): secrets are the only
|
||||
// thing a hardened deploy must supply.
|
||||
const secureEnv = {
|
||||
COOKIE_SECRET: "real-cookie-secret",
|
||||
CSRF_SECRET: "real-csrf-secret",
|
||||
REQUIRE_SECURE_SECRETS: "true",
|
||||
};
|
||||
|
||||
test("loads dev defaults when the environment is empty", () => {
|
||||
const c = loadConfig({});
|
||||
assert.equal(c.port, 3000);
|
||||
assert.equal(c.cacheTemplates, false);
|
||||
assert.equal(c.kratosPublicUrl, "http://kratos:4433");
|
||||
assert.equal(c.kratosAdminUrl, "http://kratos:4434");
|
||||
assert.equal(c.ketoReadUrl, "http://keto:4466");
|
||||
@@ -17,6 +23,12 @@ test("loads dev defaults when the environment is empty", () => {
|
||||
assert.match(c.csrfSecret, /dev-insecure/);
|
||||
});
|
||||
|
||||
test("parses explicit boolean toggles and rejects non-boolean values", () => {
|
||||
assert.equal(loadConfig({ CACHE_TEMPLATES: "true" }).cacheTemplates, true);
|
||||
assert.equal(loadConfig({ CACHE_TEMPLATES: "false" }).cacheTemplates, false);
|
||||
assert.throws(() => loadConfig({ CACHE_TEMPLATES: "yes" }), /CACHE_TEMPLATES/);
|
||||
});
|
||||
|
||||
test("reads overrides from the environment", () => {
|
||||
const c = loadConfig({ COOKIE_SECRET: "x", KRATOS_PUBLIC_URL: "https://id.example.com", PORT: "8080" });
|
||||
assert.equal(c.port, 8080);
|
||||
@@ -32,18 +44,18 @@ test("rejects a malformed Ory URL", () => {
|
||||
assert.throws(() => loadConfig({ KETO_READ_URL: "not a url" }), /KETO_READ_URL/);
|
||||
});
|
||||
|
||||
test("production rejects a missing or dev-throwaway secret", () => {
|
||||
assert.throws(() => loadConfig({ NODE_ENV: "production" }), /COOKIE_SECRET/);
|
||||
assert.throws(() => loadConfig({ COOKIE_SECRET: "real", NODE_ENV: "production" }), /CSRF_SECRET/);
|
||||
test("REQUIRE_SECURE_SECRETS rejects a missing or dev-throwaway secret", () => {
|
||||
assert.throws(() => loadConfig({ REQUIRE_SECURE_SECRETS: "true" }), /COOKIE_SECRET/);
|
||||
assert.throws(() => loadConfig({ COOKIE_SECRET: "real", REQUIRE_SECURE_SECRETS: "true" }), /CSRF_SECRET/);
|
||||
assert.throws(
|
||||
() => loadConfig({ COOKIE_SECRET: "dev-insecure-cookie-secret", CSRF_SECRET: "real", NODE_ENV: "production" }),
|
||||
() => loadConfig({ COOKIE_SECRET: "dev-insecure-cookie-secret", CSRF_SECRET: "real", REQUIRE_SECURE_SECRETS: "true" }),
|
||||
/COOKIE_SECRET/,
|
||||
);
|
||||
});
|
||||
|
||||
test("production succeeds with real secrets and still defaults the Ory URLs", () => {
|
||||
const c = loadConfig(prodEnv);
|
||||
test("REQUIRE_SECURE_SECRETS succeeds with real secrets and still defaults the Ory URLs", () => {
|
||||
const c = loadConfig(secureEnv);
|
||||
assert.equal(c.cookieSecret, "real-cookie-secret");
|
||||
assert.equal(c.csrfSecret, "real-csrf-secret");
|
||||
assert.equal(c.kratosPublicUrl, "http://kratos:4433"); // only secrets are required in prod; URLs still default
|
||||
assert.equal(c.kratosPublicUrl, "http://kratos:4433"); // only secrets are enforced; URLs still default
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user