import { beforeEach, describe, expect, it } from "vitest"; import { prisma } from "@/lib/db"; import { protokollieren, protokollKettePruefen } from "@/server/services/protokoll"; import { datenbankLeeren } from "./hilfen/db"; /** Protokoll-Service (#005): schwaerzt vor dem Schreiben und laesst die Kette pruefen. */ const KEY = "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAA\n-----END OPENSSH PRIVATE KEY-----"; beforeEach(async () => { await datenbankLeeren(); }); describe("protokollieren", () => { it("schreibt Akteur, Aktion und Ergebnis", async () => { await protokollieren({ akteur: { id: "b1", name: "Florian" }, aktion: "schluessel.erzeugt", ergebnis: "ERFOLG", hostName: "srv-pve01", fingerprint: "SHA256:abc", }); const [eintrag] = await prisma.protokoll.findMany(); expect(eintrag).toMatchObject({ akteurName: "Florian", aktion: "schluessel.erzeugt", hostName: "srv-pve01", }); expect(await protokollKettePruefen()).toBeNull(); }); it("laesst keinen Private Key und kein Passwort hinein", async () => { await protokollieren({ akteur: "System", aktion: "test", ergebnis: "FEHLER", diff: `+ ${KEY}`, details: { passwort: "geheim123", fehler: `Parser scheiterte an ${KEY}`, privatePfad: "~/.ssh/id_x" }, }); const [eintrag] = await prisma.protokoll.findMany(); const text = JSON.stringify(eintrag, (_, wert: unknown) => typeof wert === "bigint" ? String(wert) : wert, ); expect(text).not.toContain("PRIVATE KEY"); expect(text).not.toContain("geheim123"); expect(text).toContain("~/.ssh/id_x"); }); });