Files
liqa/server/test/auth-config.spec.ts
T
agent 1830feb71b refactor(auth): address server review feedback
- Extract auth DTOs, cookie/JWT helpers and the generic duration parser.
- Configure the session cookie and map domain auth failures to HTTP responses.
- Add typed Swagger responses and focused regression coverage.
2026-11-04 17:15:00 -05:00

54 lines
1.8 KiB
TypeScript

import "reflect-metadata";
import { validateAppConfig } from "../src/config/app.config";
import { durationSeconds } from "../src/common/interval";
describe("Authentication configuration", () => {
it("defaults authentication off with a 24-hour token lifetime and cleanup schedule", () => {
const config = validateAppConfig({});
expect(config.AUTH_ENABLED).toBe(false);
expect(config.AUTH_JWT_EXPIRES_IN).toBe("24h");
expect(config.AUTH_COOKIE_NAME).toBe("liqa_auth");
expect(config.AUTH_REVOKED_TOKEN_CLEANUP_CRON).toBe("0 3 * * *");
});
it("parses explicit true and false values without JavaScript truthiness", () => {
expect(validateAppConfig({ AUTH_ENABLED: "false" }).AUTH_ENABLED).toBe(
false,
);
expect(
validateAppConfig({ AUTH_ENABLED: "true", AUTH_JWT_SECRET: "test" })
.AUTH_ENABLED,
).toBe(true);
});
it("requires a signing secret and usable token lifetime only when enabled", () => {
expect(() => validateAppConfig({ AUTH_ENABLED: "true" })).toThrow(
"AUTH_JWT_SECRET must be set when AUTH_ENABLED=true",
);
expect(() =>
validateAppConfig({
AUTH_ENABLED: "true",
AUTH_JWT_SECRET: "test",
AUTH_JWT_EXPIRES_IN: "0h",
}),
).toThrow("AUTH_JWT_EXPIRES_IN must be a positive duration");
expect(
validateAppConfig({
AUTH_ENABLED: "false",
AUTH_JWT_EXPIRES_IN: "invalid",
}).AUTH_ENABLED,
).toBe(false);
});
it("rejects non-boolean auth settings and parses common positive durations", () => {
expect(() => validateAppConfig({ AUTH_ENABLED: "yes" })).toThrow(
"AUTH_ENABLED must be either true or false",
);
expect(durationSeconds("30m")).toBe(1800);
expect(durationSeconds("24h")).toBe(86400);
expect(durationSeconds("2d")).toBe(172800);
expect(durationSeconds("0s")).toBeNull();
});
});