Skip to content

Commit a0b56a3

Browse files
committed
test: mock token retrieval globally
1 parent 10a9ecf commit a0b56a3

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/app/catalog/actions.test.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
import { mockedGetRegistryV01Servers } from "@mocks/fixtures/registry_v0_1_servers/get";
22
import { HttpResponse } from "msw";
3-
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { describe, expect, it } from "vitest";
44
import { getServers } from "./actions";
55

6-
// Mock the auth to bypass authentication
7-
vi.mock("@/lib/api-client", async (importOriginal) => {
8-
const original = await importOriginal<typeof import("@/lib/api-client")>();
9-
return {
10-
...original,
11-
getAuthenticatedClient: vi.fn(() =>
12-
original.getAuthenticatedClient("mock-token"),
13-
),
14-
};
15-
});
6+
// Authentication is mocked globally in vitest.setup.ts:
7+
// - auth.api.getSession returns a mock session
8+
// - getValidOidcToken returns "mock-test-token"
169

1710
describe("getServers", () => {
18-
beforeEach(() => {
19-
vi.clearAllMocks();
20-
});
21-
2211
it("returns servers from default fixture", async () => {
2312
const servers = await getServers();
2413

src/lib/auth/__tests__/token.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { HttpResponse, http } from "msw";
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import { server } from "@/mocks/node";
4-
import { getValidOidcToken } from "../token";
54
import type { OidcTokenData } from "../types";
65
import { encrypt } from "../utils";
76

7+
// Unmock @/lib/auth/token to test the real implementation
8+
// (overrides the global mock from vitest.setup.ts)
9+
vi.unmock("@/lib/auth/token");
10+
// Import after unmocking to get the real function
11+
const { getValidOidcToken } = await import("../token");
12+
813
const REFRESH_API_URL = "http://localhost:3000/api/auth/refresh-token";
914

1015
// Mock jose library

vitest.setup.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ vi.mock("@/lib/auth/auth", async (importOriginal) => {
4141
...actual.auth.api,
4242
getSession: vi.fn(() =>
4343
Promise.resolve({
44-
user: { email: "[email protected]", name: "Test User" },
44+
user: {
45+
id: "mock-user-id",
46+
47+
name: "Test User",
48+
},
4549
}),
4650
),
4751
},
@@ -73,6 +77,12 @@ vi.mock("next-themes", () => ({
7377
ThemeProvider: ({ children }: { children: React.ReactNode }) => children,
7478
}));
7579

80+
// Mock OIDC token retrieval to return a test token by default
81+
// This allows server action tests to bypass the full auth flow
82+
vi.mock("@/lib/auth/token", () => ({
83+
getValidOidcToken: vi.fn(() => Promise.resolve("mock-test-token")),
84+
}));
85+
7686
// Auth client baseline mock; individual tests can customize return values
7787
vi.mock("@/lib/auth/auth-client", () => ({
7888
authClient: {

0 commit comments

Comments
 (0)