import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { ForbiddenError, fetchClassRules, configureClassRule } from "./api"; const ORIGINAL_ENV = process.env; beforeEach(() => { process.env = { ...ORIGINAL_ENV, NEXT_PUBLIC_RETENTION_API_URL: "http://backend.test" }; }); afterEach(() => { process.env = ORIGINAL_ENV; vi.restoreAllMocks(); }); // Akzeptanzkriterium 3 ("nur berechtigten Rollen zugaenglich"): der // Negativfall MUSS explizit gepruft werden, nicht nur der Erfolgsfall - // Frontend wirft ForbiddenError, wenn RET-06-API (real gegen RBAC-06, // siehe RET-08) mit 403 antwortet. describe("api client - 403-Nachweis (keine berechtigte Rolle)", () => { it("fetchClassRules wirft ForbiddenError bei 403-Antwort", async () => { vi.stubGlobal( "fetch", vi.fn().mockResolvedValue(new Response(null, { status: 403 })) ); await expect(fetchClassRules()).rejects.toBeInstanceOf(ForbiddenError); }); it("configureClassRule wirft ForbiddenError bei 403-Antwort", async () => { vi.stubGlobal( "fetch", vi.fn().mockResolvedValue(new Response(null, { status: 403 })) ); await expect(configureClassRule("klasse-x", "1 year")).rejects.toBeInstanceOf(ForbiddenError); }); }); describe("api client - erlaubte Rolle", () => { it("fetchClassRules liefert die Liste bei 200-Antwort", async () => { const rules = [{ RetentionClass: "klasse-a", Duration: "5 years", Active: true }]; vi.stubGlobal( "fetch", vi.fn().mockResolvedValue( new Response(JSON.stringify(rules), { status: 200, headers: { "Content-Type": "application/json" } }) ) ); await expect(fetchClassRules()).resolves.toEqual(rules); }); });