107 lines
2.8 KiB
TypeScript
107 lines
2.8 KiB
TypeScript
import { expect, test, vi } from "vitest";
|
|
|
|
import { createAuditWriter, type AuditRepository } from "./write";
|
|
|
|
function createMockRepo() {
|
|
const inserted: unknown[] = [];
|
|
const repository: AuditRepository = {
|
|
async insert(record) {
|
|
inserted.push(record);
|
|
},
|
|
};
|
|
const spy = vi.spyOn(repository, "insert");
|
|
return { repository, inserted, spy };
|
|
}
|
|
|
|
test("redacts secrets before writing audit metadata", async () => {
|
|
const { repository, spy } = createMockRepo();
|
|
const writer = createAuditWriter(repository);
|
|
|
|
await writer.writeAudit({
|
|
type: "CONFIG_CHECK",
|
|
entityType: "system",
|
|
entityId: "config",
|
|
metadata: { apiKey: "secret", nested: { password: "secret" }, albumId: "a1" },
|
|
});
|
|
|
|
expect(spy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
metadata: {
|
|
apiKey: "[REDACTED]",
|
|
nested: { password: "[REDACTED]" },
|
|
albumId: "a1",
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
|
|
test("redacts secrets in previousValues and newValues", async () => {
|
|
const { repository, spy } = createMockRepo();
|
|
const writer = createAuditWriter(repository);
|
|
|
|
await writer.writeAudit({
|
|
type: "STATUS_CHANGED",
|
|
entityType: "editorial",
|
|
entityId: "e1",
|
|
previousValues: { status: "active", token: "abc" },
|
|
newValues: { status: "suspended", authorization: "bearer xyz" },
|
|
});
|
|
|
|
expect(spy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
previousValues: { status: "active", token: "[REDACTED]" },
|
|
newValues: { status: "suspended", authorization: "[REDACTED]" },
|
|
}),
|
|
);
|
|
});
|
|
|
|
test("passes through non-secret values unchanged", async () => {
|
|
const { repository, spy } = createMockRepo();
|
|
const writer = createAuditWriter(repository);
|
|
|
|
await writer.writeAudit({
|
|
type: "USAGE_CREATED",
|
|
entityType: "usage",
|
|
entityId: "u1",
|
|
metadata: { albumId: "a1", priceCents: 1500, items: [{ id: "x1" }] },
|
|
});
|
|
|
|
expect(spy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
metadata: { albumId: "a1", priceCents: 1500, items: [{ id: "x1" }] },
|
|
}),
|
|
);
|
|
});
|
|
|
|
test("redacts secrets inside arrays", async () => {
|
|
const { repository, spy } = createMockRepo();
|
|
const writer = createAuditWriter(repository);
|
|
|
|
await writer.writeAudit({
|
|
type: "BULK",
|
|
entityType: "system",
|
|
entityId: "bulk",
|
|
metadata: { entries: [{ apiKey: "secret", name: "foo" }] },
|
|
});
|
|
|
|
expect(spy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
metadata: { entries: [{ apiKey: "[REDACTED]", name: "foo" }] },
|
|
}),
|
|
);
|
|
});
|
|
|
|
test("handles null and undefined metadata gracefully", async () => {
|
|
const { repository, spy } = createMockRepo();
|
|
const writer = createAuditWriter(repository);
|
|
|
|
await writer.writeAudit({
|
|
type: "SIMPLE",
|
|
entityType: "system",
|
|
entityId: "s1",
|
|
});
|
|
|
|
expect(spy).toHaveBeenCalledWith(
|
|
expect.objectContaining({ metadata: undefined }),
|
|
);
|
|
}); |