From bb55c658a534d5304cc5023c43d53b47bd8c56a6 Mon Sep 17 00:00:00 2001 From: Flea Flicker Date: Sat, 22 Aug 2026 08:39:25 +0000 Subject: [PATCH] fix(test): de-flake authProvider.test.ts OOBE test-connection timeout (GRO-2745) Mock global.fetch in the test-connection test so it no longer hits the real network. The route has a 10s fetch timeout, but the test used the default 5s vitest timeout, causing intermittent failures when DNS/network was slow. Add afterEach(vi.restoreAllMocks) to clean up between tests, and reconcile the misleading "returns 400" test title to match the 200 assertion the route actually produces. Co-Authored-By: Paperclip --- apps/api/src/__tests__/authProvider.test.ts | 12 +++++++++--- src/__tests__/authProvider.test.ts | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/apps/api/src/__tests__/authProvider.test.ts b/apps/api/src/__tests__/authProvider.test.ts index 4a7ae90..4b52732 100644 --- a/apps/api/src/__tests__/authProvider.test.ts +++ b/apps/api/src/__tests__/authProvider.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi, beforeEach } from "vitest"; +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import { Hono } from "hono"; import { authProviderRouter } from "../routes/authProvider.js"; @@ -227,6 +227,7 @@ describe("PUT /admin/auth-provider", () => { describe("POST /admin/auth-provider/test", () => { beforeEach(resetMock); + afterEach(() => vi.restoreAllMocks()); it("returns ok=false for unreachable issuer", async () => { const app = makeApp(mockSuperUser); @@ -242,7 +243,12 @@ describe("POST /admin/auth-provider/test", () => { expect(body.error).toBeTruthy(); }, 15000); // timeout must exceed the 10s fetch timeout in the route handler - it("returns 400 for missing clientSecret (not required for test)", async () => { + it("returns 200 when clientSecret is omitted (not required by test-connection schema)", async () => { + // Mock fetch so the route does not make a real network request. + vi.spyOn(global, "fetch").mockResolvedValueOnce({ + ok: true, + json: async () => ({ issuer: "https://auth.example.com" }), + } as Response); const app = makeApp(mockSuperUser); const { status } = await post(app, "/admin/auth-provider/test", { providerId: "authentik", @@ -250,7 +256,7 @@ describe("POST /admin/auth-provider/test", () => { issuerUrl: "https://auth.example.com", clientId: "client", }, mockSuperUser); - expect(status).toBe(200); // clientSecret omitted intentionally for test + expect(status).toBe(200); }); }); diff --git a/src/__tests__/authProvider.test.ts b/src/__tests__/authProvider.test.ts index c09754d..0ce239b 100644 --- a/src/__tests__/authProvider.test.ts +++ b/src/__tests__/authProvider.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi, beforeEach } from "vitest"; +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import { Hono } from "hono"; import { authProviderRouter } from "../routes/authProvider.js"; @@ -227,6 +227,7 @@ describe("PUT /admin/auth-provider", () => { describe("POST /admin/auth-provider/test", () => { beforeEach(resetMock); + afterEach(() => vi.restoreAllMocks()); it("returns ok=false for unreachable issuer", async () => { const app = makeApp(mockSuperUser); @@ -242,7 +243,12 @@ describe("POST /admin/auth-provider/test", () => { expect(body.error).toBeTruthy(); }, 15000); // timeout must exceed the 10s fetch timeout in the route handler - it("returns 400 for missing clientSecret (not required for test)", async () => { + it("returns 200 when clientSecret is omitted (not required by test-connection schema)", async () => { + // Mock fetch so the route does not make a real network request. + vi.spyOn(global, "fetch").mockResolvedValueOnce({ + ok: true, + json: async () => ({ issuer: "https://auth.example.com" }), + } as Response); const app = makeApp(mockSuperUser); const { status } = await post(app, "/admin/auth-provider/test", { providerId: "authentik", @@ -250,7 +256,7 @@ describe("POST /admin/auth-provider/test", () => { issuerUrl: "https://auth.example.com", clientId: "client", }, mockSuperUser); - expect(status).toBe(200); // clientSecret omitted intentionally for test + expect(status).toBe(200); }); });