fix(test): mock auditWrapper across tests and add assertions for session events

This commit is contained in:
Tyler Gillispie 2026-08-26 12:31:38 -07:00
parent 695d44e16e
commit e9060eee5a
3 changed files with 24 additions and 3 deletions

View File

@ -43,7 +43,8 @@ export let auditLog = function auditLog(
}, ${ipAddress}, ${leafHashHex}) }, ${ipAddress}, ${leafHashHex})
`; `;
} catch (error: any) { } catch (error: any) {
console.error("[Audit Logger] Failed to insert audit record:", error); const msg = error?.code || error?.message || String(error);
console.error(`[Audit Logger] Failed to insert audit record: ${msg}`);
} }
})(); })();
}; };

View File

@ -7,6 +7,7 @@ import { auditWrapper } from "../audit.ts";
import { rateLimitWrapper } from "../ratelimit.ts"; import { rateLimitWrapper } from "../ratelimit.ts";
const originalSql = sqlWrapper.sql; const originalSql = sqlWrapper.sql;
const originalAudit = auditWrapper.auditLog;
function setMockSql(mockImpl: () => Promise<any>) { function setMockSql(mockImpl: () => Promise<any>) {
sqlWrapper.sql = mockImpl as any; sqlWrapper.sql = mockImpl as any;
@ -36,7 +37,6 @@ Deno.test("Phase 4: Audit Ledger Verification - Login failed", async () => {
}); });
let auditArgs: any[] = []; let auditArgs: any[] = [];
const originalAudit = auditWrapper.auditLog;
auditWrapper.auditLog = (...args: any[]) => { auditWrapper.auditLog = (...args: any[]) => {
auditArgs = args; auditArgs = args;
}; };
@ -57,7 +57,6 @@ Deno.test("Phase 4: Audit Ledger Verification - Login failed", async () => {
assertEquals(auditArgs[1], "login_failed"); assertEquals(auditArgs[1], "login_failed");
restoreMockSql(); restoreMockSql();
auditWrapper.auditLog = originalAudit;
}); });
Deno.test("WebAuthn - /api/register/verify extracts PRF", async () => { Deno.test("WebAuthn - /api/register/verify extracts PRF", async () => {
@ -397,6 +396,7 @@ Deno.test("Ephemeral 1-Click Magic Link Redemption (/pass)", async (t) => {
} finally { } finally {
valkeyGetStub.restore(); valkeyGetStub.restore();
valkeyTtlStub.restore(); valkeyTtlStub.restore();
auditWrapper.auditLog = originalAudit;
} }
}, },
); );

View File

@ -4,8 +4,13 @@ import { app } from "../main.ts";
import { sqlWrapper } from "../db.ts"; import { sqlWrapper } from "../db.ts";
import { valkey } from "../valkey.ts"; import { valkey } from "../valkey.ts";
import { rateLimitWrapper } from "../ratelimit.ts"; import { rateLimitWrapper } from "../ratelimit.ts";
import { auditWrapper } from "../audit.ts";
const originalSql = sqlWrapper.sql; const originalSql = sqlWrapper.sql;
const originalAudit = auditWrapper.auditLog;
const auditEvents: Array<
{ action: string; resource?: string | null; details?: any }
> = [];
function setMockSql(mockImpl: () => Promise<any>) { function setMockSql(mockImpl: () => Promise<any>) {
sqlWrapper.sql = mockImpl as any; sqlWrapper.sql = mockImpl as any;
@ -16,6 +21,14 @@ function restoreMockSql() {
} }
rateLimitWrapper.checkRateLimit = () => Promise.resolve(true); rateLimitWrapper.checkRateLimit = () => Promise.resolve(true);
auditWrapper.auditLog = (
_userId: string | null,
action: string,
resource: string | null,
details: Record<string, unknown> | null,
) => {
auditEvents.push({ action, resource, details });
};
Deno.test("Zero-Trust Scope Guards", async (t) => { Deno.test("Zero-Trust Scope Guards", async (t) => {
const mockUserId = "user-id-guards"; const mockUserId = "user-id-guards";
@ -385,6 +398,7 @@ Deno.test("Agent Session Delegation & Scoped Permissions", async (t) => {
const json = await res.json(); const json = await res.json();
assert(json.success === true); assert(json.success === true);
assert(json.newExpiresAt); assert(json.newExpiresAt);
assert(auditEvents.some((e) => e.action === "session_extended"));
} finally { } finally {
sqlWrapper.sql = originalSql; sqlWrapper.sql = originalSql;
valkeyStub.restore(); valkeyStub.restore();
@ -392,4 +406,10 @@ Deno.test("Agent Session Delegation & Scoped Permissions", async (t) => {
} }
}, },
); );
// Verify full audit coverage across delegation endpoints
assert(auditEvents.some((e) => e.action === "session_delegated"));
assert(auditEvents.some((e) => e.action === "session_scopes_updated"));
auditWrapper.auditLog = originalAudit;
}); });