fix(#411): QA-004 — HttpException for session guard + PDA-friendly auth error
getSession now throws HttpException(401) instead of raw Error. handleAuth error message updated to PDA-friendly language. headersSent branch upgraded from warn to error with request details. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -285,6 +285,45 @@ describe("auth.config", () => {
|
||||
|
||||
expect(mockGenericOAuth).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should throw if OIDC_CLIENT_ID is missing when OIDC is enabled", () => {
|
||||
process.env.OIDC_ENABLED = "true";
|
||||
process.env.OIDC_ISSUER = "https://auth.example.com/application/o/mosaic-stack/";
|
||||
process.env.OIDC_CLIENT_SECRET = "test-client-secret";
|
||||
process.env.OIDC_REDIRECT_URI = "https://app.example.com/auth/callback/authentik";
|
||||
// OIDC_CLIENT_ID deliberately not set
|
||||
|
||||
// validateOidcConfig will throw first, so we need to bypass it
|
||||
// by setting the var then deleting it after validation
|
||||
// Instead, test via the validation path which is fine — but let's
|
||||
// verify the plugin-level guard by using a direct approach:
|
||||
// Set env to pass validateOidcConfig, then delete OIDC_CLIENT_ID
|
||||
// The validateOidcConfig will catch this first, which is correct behavior
|
||||
const mockPrisma = {} as PrismaClient;
|
||||
expect(() => createAuth(mockPrisma)).toThrow("OIDC_CLIENT_ID");
|
||||
});
|
||||
|
||||
it("should throw if OIDC_CLIENT_SECRET is missing when OIDC is enabled", () => {
|
||||
process.env.OIDC_ENABLED = "true";
|
||||
process.env.OIDC_ISSUER = "https://auth.example.com/application/o/mosaic-stack/";
|
||||
process.env.OIDC_CLIENT_ID = "test-client-id";
|
||||
process.env.OIDC_REDIRECT_URI = "https://app.example.com/auth/callback/authentik";
|
||||
// OIDC_CLIENT_SECRET deliberately not set
|
||||
|
||||
const mockPrisma = {} as PrismaClient;
|
||||
expect(() => createAuth(mockPrisma)).toThrow("OIDC_CLIENT_SECRET");
|
||||
});
|
||||
|
||||
it("should throw if OIDC_ISSUER is missing when OIDC is enabled", () => {
|
||||
process.env.OIDC_ENABLED = "true";
|
||||
process.env.OIDC_CLIENT_ID = "test-client-id";
|
||||
process.env.OIDC_CLIENT_SECRET = "test-client-secret";
|
||||
process.env.OIDC_REDIRECT_URI = "https://app.example.com/auth/callback/authentik";
|
||||
// OIDC_ISSUER deliberately not set
|
||||
|
||||
const mockPrisma = {} as PrismaClient;
|
||||
expect(() => createAuth(mockPrisma)).toThrow("OIDC_ISSUER");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getTrustedOrigins", () => {
|
||||
@@ -407,7 +446,7 @@ describe("auth.config", () => {
|
||||
expect(origins).toHaveLength(5);
|
||||
});
|
||||
|
||||
it("should reject invalid URLs in TRUSTED_ORIGINS with a warning", () => {
|
||||
it("should reject invalid URLs in TRUSTED_ORIGINS with a warning including error details", () => {
|
||||
process.env.TRUSTED_ORIGINS = "not-a-url,https://valid.example.com";
|
||||
process.env.NODE_ENV = "production";
|
||||
|
||||
@@ -420,6 +459,12 @@ describe("auth.config", () => {
|
||||
expect(warnSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining('Ignoring invalid URL in TRUSTED_ORIGINS: "not-a-url"')
|
||||
);
|
||||
// Verify that error detail is included in the warning
|
||||
const warnCall = warnSpy.mock.calls.find(
|
||||
(call) => typeof call[0] === "string" && call[0].includes("not-a-url")
|
||||
);
|
||||
expect(warnCall).toBeDefined();
|
||||
expect(warnCall![0]).toMatch(/\(.*\)$/);
|
||||
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user