Forráskód Böngészése

RHL-003-test(auth): add unit tests for logout endpoint to verify session destruction and error handling

Code_Uwe 1 hete
szülő
commit
e5d3161d75
1 módosított fájl, 40 hozzáadás és 0 törlés
  1. 40 0
      app/api/auth/logout/route.test.js

+ 40 - 0
app/api/auth/logout/route.test.js

@@ -0,0 +1,40 @@
+import { describe, it, expect, vi, beforeEach } from "vitest";
+
+// 1) Mock for destroySession
+vi.mock("@/lib/auth/session", () => ({
+	destroySession: vi.fn(),
+}));
+
+// 2) Import after mock
+import { destroySession } from "@/lib/auth/session";
+import { GET } from "./route";
+
+describe("GET /api/auth/logout", () => {
+	beforeEach(() => {
+		vi.clearAllMocks();
+	});
+
+	it("calls destroySession and returns ok: true", async () => {
+		const response = await GET();
+		const json = await response.json();
+
+		expect(destroySession).toHaveBeenCalledTimes(1);
+
+		expect(response.status).toBe(200);
+		expect(json).toEqual({ ok: true });
+	});
+
+	it("returns 500 when destroySession throws an error", async () => {
+		destroySession.mockImplementation(() => {
+			throw new Error("boom");
+		});
+
+		const response = await GET();
+		const json = await response.json();
+
+		expect(destroySession).toHaveBeenCalledTimes(1);
+
+		expect(response.status).toBe(500);
+		expect(json).toEqual({ error: "Internal server error" });
+	});
+});