Repository navigation
Expand file tree
/
Copy pathlogout.test.ts
More file actions
348 lines (283 loc) · 14 KB
/
Copy pathlogout.test.ts
File metadata and controls
348 lines (283 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { withBasePathEnv } from "../../helpers/base-path";
import { describe, test, expect, mock, spyOn, beforeEach, afterEach } from "bun:test";
import { SignJWT } from "jose";
import { createMockCookies, parseResponseJSON } from "../../helpers/mock-next";
// ─── Mock @/lib/auth BEFORE importing the route ─────────────────────────────
const mockLogout = mock(async () => {});
const mockGetSession = mock(
async () => null as { role: string; username: string; idpSubject?: string; channel?: "bearer" } | null,
);
mock.module("@/lib/auth", () => ({
login: mock(async () => {}),
signJWT: mock(async () => "mock-token"),
verifyJWT: mock(async () => null),
getSession: mockGetSession,
logout: mockLogout,
}));
let cookieStore = createMockCookies();
mock.module("next/headers", () => ({
cookies: mock(async () => cookieStore),
headers: mock(async () => new Headers()),
}));
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const mockBuildLogoutUrl = mock(async (_returnTo: string, _idTokenHint?: string) => null as string | null);
const mockGetPublicOrigin = mock((req: Request) => new URL(req.url).origin);
mock.module("@/lib/oidc", () => ({
buildLogoutUrl: mockBuildLogoutUrl,
getPublicOrigin: mockGetPublicOrigin,
getOIDCConfig: mock(() => ({})),
discoverProvider: mock(async () => ({})),
generateAuthUrl: mock(async () => ({})),
encryptState: mock(async () => ""),
decryptState: mock(async () => ({})),
exchangeCode: mock(async () => ({})),
mapOIDCRole: mock(() => "user"),
resetDiscoveryCache: mock(() => {}),
}));
// ─── Import route handler AFTER mocking ─────────────────────────────────────
const { POST } = await import("@/app/api/auth/logout/route");
function makeRequest(url = "http://localhost:3000/api/auth/logout") {
return new Request(url, { method: "POST" });
}
// ─── Tests (local auth mode) ────────────────────────────────────────────────
describe("POST /api/auth/logout (local)", () => {
beforeEach(() => {
mockLogout.mockClear();
mockBuildLogoutUrl.mockClear();
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "local";
});
test("returns 200 with success true", async () => {
const res = await POST(makeRequest() as never);
const data = await parseResponseJSON<{ success: boolean }>(res);
expect(res.status).toBe(200);
expect(data.success).toBe(true);
});
test("calls logout() once", async () => {
await POST(makeRequest() as never);
expect(mockLogout).toHaveBeenCalledTimes(1);
});
test("does not return redirectUrl in local mode", async () => {
const res = await POST(makeRequest() as never);
const data = await parseResponseJSON<{ success: boolean; redirectUrl?: string }>(res);
expect(data.redirectUrl).toBeUndefined();
});
test("returns 500 when logout throws", async () => {
mockLogout.mockImplementationOnce(async () => {
throw new Error("Cookie store unavailable");
});
const logSpy = spyOn(console, "log").mockImplementation(() => {});
try {
const res = await POST(makeRequest() as never);
const data = await parseResponseJSON<{ error: string }>(res);
expect(res.status).toBe(500);
expect(data.error).toBe("Cookie store unavailable");
// A failed logout must never produce a line asserting a logout that did not happen.
expect(logSpy).not.toHaveBeenCalled();
} finally {
logSpy.mockRestore();
}
});
test("multiple logouts all succeed", async () => {
const res1 = await POST(makeRequest() as never);
const res2 = await POST(makeRequest() as never);
const res3 = await POST(makeRequest() as never);
const data1 = await parseResponseJSON<{ success: boolean }>(res1);
const data2 = await parseResponseJSON<{ success: boolean }>(res2);
const data3 = await parseResponseJSON<{ success: boolean }>(res3);
expect(data1.success).toBe(true);
expect(data2.success).toBe(true);
expect(data3.success).toBe(true);
expect(mockLogout).toHaveBeenCalledTimes(3);
});
});
// ─── Tests (OIDC auth mode) ─────────────────────────────────────────────────
describe("POST /api/auth/logout (oidc)", () => {
beforeEach(() => {
mockLogout.mockClear();
mockBuildLogoutUrl.mockClear();
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "oidc";
});
afterEach(() => {
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "local";
});
test("returns redirectUrl when OIDC logout URL is available", async () => {
mockBuildLogoutUrl.mockResolvedValue(
"https://auth0.com/v2/logout?client_id=abc&returnTo=http://localhost:3000/login",
);
const res = await POST(makeRequest() as never);
const data = await parseResponseJSON<{ success: boolean; redirectUrl?: string }>(res);
expect(res.status).toBe(200);
expect(data.success).toBe(true);
expect(data.redirectUrl).toBe("https://auth0.com/v2/logout?client_id=abc&returnTo=http://localhost:3000/login");
});
test("calls buildLogoutUrl with correct returnTo", async () => {
mockBuildLogoutUrl.mockResolvedValue("https://auth0.com/v2/logout");
await POST(makeRequest("http://localhost:3000/api/auth/logout") as never);
expect(mockBuildLogoutUrl).toHaveBeenCalledWith("http://localhost:3000/login", undefined);
});
test("returns success without redirectUrl when buildLogoutUrl returns null", async () => {
mockBuildLogoutUrl.mockResolvedValue(null);
const res = await POST(makeRequest() as never);
const data = await parseResponseJSON<{ success: boolean; redirectUrl?: string }>(res);
expect(res.status).toBe(200);
expect(data.success).toBe(true);
expect(data.redirectUrl).toBeUndefined();
});
test("calls logout() even in OIDC mode", async () => {
mockBuildLogoutUrl.mockResolvedValue("https://auth0.com/v2/logout");
await POST(makeRequest() as never);
expect(mockLogout).toHaveBeenCalledTimes(1);
});
test("OIDC logout returns to login under the build's prefix", async () => {
const previous = process.env.NEXT_PUBLIC_AUTH_PROVIDER;
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "oidc";
try {
await withBasePathEnv("/tools/libredb", async () => {
mockBuildLogoutUrl.mockClear();
await POST(new Request("https://studio.example/tools/libredb/api/auth/logout", { method: "POST" }) as never);
expect(mockBuildLogoutUrl).toHaveBeenCalledWith("https://studio.example/tools/libredb/login", undefined);
});
} finally {
if (previous === undefined) delete process.env.NEXT_PUBLIC_AUTH_PROVIDER;
else process.env.NEXT_PUBLIC_AUTH_PROVIDER = previous;
}
});
});
// ─── Tests (OIDC_LOGOUT_HINT) ───────────────────────────────────────────────
/** An ID token as the provider signed it; the route only decodes it, so any key will do. */
async function idToken(sub: string): Promise<string> {
return await new SignJWT({ sub, iss: "https://idp.example/realms/libredb" })
.setProtectedHeader({ alg: "HS256" })
.sign(new TextEncoder().encode("a-key-the-route-never-verifies-with"));
}
const KEYCLOAK_LOGOUT = "https://idp.example/realms/libredb/protocol/openid-connect/logout";
describe("POST /api/auth/logout (OIDC_LOGOUT_HINT)", () => {
const originalHint = process.env.OIDC_LOGOUT_HINT;
beforeEach(() => {
cookieStore = createMockCookies();
mockBuildLogoutUrl.mockClear();
mockBuildLogoutUrl.mockImplementation(async (returnTo: string, idTokenHint?: string) => {
const url = new URL(KEYCLOAK_LOGOUT);
url.searchParams.set("post_logout_redirect_uri", returnTo);
if (idTokenHint !== undefined) url.searchParams.set("id_token_hint", idTokenHint);
return url.toString();
});
mockGetSession.mockImplementation(async () => ({ role: "user", username: "alice", idpSubject: "subject-alice" }));
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "oidc";
process.env.OIDC_LOGOUT_HINT = "true";
});
afterEach(() => {
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "local";
if (originalHint === undefined) delete process.env.OIDC_LOGOUT_HINT;
else process.env.OIDC_LOGOUT_HINT = originalHint;
mockGetSession.mockImplementation(async () => null);
});
async function redirectUrl(): Promise<URL> {
const res = await POST(makeRequest() as never);
const data = await parseResponseJSON<{ success: boolean; redirectUrl?: string }>(res);
expect(res.status).toBe(200);
return new URL(data.redirectUrl!);
}
test("redirectUrl carries id_token_hint when the hint cookie belongs to the session subject", async () => {
const hint = await idToken("subject-alice");
cookieStore.set("oidc-logout-hint", hint);
const url = await redirectUrl();
expect(url.searchParams.get("id_token_hint")).toBe(hint);
expect(mockBuildLogoutUrl).toHaveBeenCalledWith("http://localhost:3000/login", hint);
expect(cookieStore._deleted).toEqual([{ name: "oidc-logout-hint", path: "/api/auth/logout" }]);
});
test("a hint whose sub differs from the session is not sent and the cookie is still deleted", async () => {
cookieStore.set("oidc-logout-hint", await idToken("subject-mallory"));
const url = await redirectUrl();
expect(url.searchParams.has("id_token_hint")).toBe(false);
expect(mockBuildLogoutUrl).toHaveBeenCalledWith("http://localhost:3000/login", undefined);
expect(cookieStore._deleted).toEqual([{ name: "oidc-logout-hint", path: "/api/auth/logout" }]);
});
test("a hint is not sent without a session, nor for a session that has no idpSubject", async () => {
cookieStore.set("oidc-logout-hint", await idToken("subject-alice"));
mockGetSession.mockImplementation(async () => null);
expect((await redirectUrl()).searchParams.has("id_token_hint")).toBe(false);
cookieStore.set("oidc-logout-hint", await idToken("subject-alice"));
mockGetSession.mockImplementation(async () => ({ role: "user", username: "alice" }));
expect((await redirectUrl()).searchParams.has("id_token_hint")).toBe(false);
expect(cookieStore._deleted).toHaveLength(2);
});
test("a hint cookie that is not a JWT is not sent, the cookie is deleted and the logout succeeds", async () => {
cookieStore.set("oidc-logout-hint", "not-a-jwt");
const warnSpy = spyOn(console, "warn").mockImplementation(() => {});
try {
const url = await redirectUrl();
expect(url.searchParams.has("id_token_hint")).toBe(false);
expect(cookieStore._deleted).toEqual([{ name: "oidc-logout-hint", path: "/api/auth/logout" }]);
} finally {
warnSpy.mockRestore();
}
});
test("the cookie is deleted under the build's prefix", async () => {
await withBasePathEnv("/tools/libredb", async () => {
cookieStore.set("oidc-logout-hint", await idToken("subject-alice"));
await POST(new Request("https://studio.example/tools/libredb/api/auth/logout", { method: "POST" }) as never);
expect(cookieStore._deleted).toEqual([{ name: "oidc-logout-hint", path: "/tools/libredb/api/auth/logout" }]);
});
});
test("with OIDC_LOGOUT_HINT off a matching hint is not sent, and a leftover cookie is still deleted", async () => {
delete process.env.OIDC_LOGOUT_HINT;
cookieStore.set("oidc-logout-hint", await idToken("subject-alice"));
const url = await redirectUrl();
expect(url.searchParams.has("id_token_hint")).toBe(false);
expect(mockBuildLogoutUrl).toHaveBeenCalledWith("http://localhost:3000/login", undefined);
expect(cookieStore._deleted).toEqual([{ name: "oidc-logout-hint", path: "/api/auth/logout" }]);
});
test("without a hint cookie nothing is deleted", async () => {
const url = await redirectUrl();
expect(url.searchParams.has("id_token_hint")).toBe(false);
expect(cookieStore._deleted).toEqual([]);
});
test("local auth deletes a leftover hint cookie too", async () => {
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "local";
cookieStore.set("oidc-logout-hint", await idToken("subject-alice"));
await POST(makeRequest() as never);
expect(mockBuildLogoutUrl).not.toHaveBeenCalled();
expect(cookieStore._deleted).toEqual([{ name: "oidc-logout-hint", path: "/api/auth/logout" }]);
});
});
// A native client's session stands on the identity provider's access token (src/lib/oidc-bearer.ts). It holds no
// Studio cookie and ends its provider session itself, so the route answers with neither a cookie nor an end-session URL.
describe("POST /api/auth/logout (bearer access token)", () => {
beforeEach(() => {
cookieStore = createMockCookies();
mockLogout.mockClear();
mockBuildLogoutUrl.mockClear();
mockBuildLogoutUrl.mockResolvedValue(KEYCLOAK_LOGOUT);
mockGetSession.mockImplementation(async () => ({
role: "user",
username: "ada@example.test",
idpSubject: "subject-ada",
channel: "bearer",
}));
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "oidc";
});
afterEach(() => {
process.env.NEXT_PUBLIC_AUTH_PROVIDER = "local";
mockGetSession.mockImplementation(async () => null);
});
test("answers success with no redirectUrl, touches no cookie and records the logout", async () => {
cookieStore.set("auth-token", "a-browser-session-on-the-same-device");
const logSpy = spyOn(console, "log").mockImplementation(() => {});
try {
const res = await POST(makeRequest() as never);
expect(res.status).toBe(200);
expect(await parseResponseJSON(res)).toEqual({ success: true });
expect(res.headers.get("set-cookie")).toBeNull();
expect(mockLogout).not.toHaveBeenCalled();
expect(mockBuildLogoutUrl).not.toHaveBeenCalled();
expect(cookieStore._deleted).toEqual([]);
const lines = logSpy.mock.calls.map((call) => String(call[0]));
expect(
lines.some((line) => line.includes('"event":"logout"') && line.includes('"actor":"ada@example.test"')),
).toBe(true);
} finally {
logSpy.mockRestore();
}
});
});