DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
test: cover apiFetch 401 redirect opt-out
Ilia Avdeeva8d7736b6d91
diff --git a/web/tests/api-auth-redirect.test.ts b/web/tests/api-auth-redirect.test.tsnew file mode 100644index 000000000..dceee0cca--- /dev/null+++ b/web/tests/api-auth-redirect.test.ts@@ -0,0 +1,111 @@+import test from "node:test";+import assert from "node:assert/strict";++// AUTH_ENABLED and API_BASE_URL are read at module-load time, so the environment+// must be configured before the module under test is imported.+process.env.NEXT_PUBLIC_API_BASE = "http://localhost:8001/api";+process.env.NEXT_PUBLIC_AUTH_ENABLED = "true";++let apiModulePromise: Promise<typeof import("../lib/api")> | null = null;++async function loadApiModule(): Promise<typeof import("../lib/api")> {+ apiModulePromise ??= import("../lib/api");+ return apiModulePromise;+}++// Install a fake `window` whose `location.href` assignment is recorded instead+// of triggering a real navigation, so we can assert whether apiFetch redirected.+function installWindow(pathname: string): {+ redirectedTo: () => string | null;+} {+ let redirect: string | null = null;+ const location = { pathname, href: "" };+ Object.defineProperty(location, "href", {+ get: () => redirect ?? "",+ set: (value: string) => {+ redirect = value;+ },+ configurable: true,+ });+ (globalThis as { window?: unknown }).window = { location };+ return { redirectedTo: () => redirect };+}++function clearWindow(): void {+ delete (globalThis as { window?: unknown }).window;+}++// Replace global fetch with one that always yields the given response.+function stubFetch(response: Response): () => void {+ const original = globalThis.fetch;+ (globalThis as { fetch: typeof fetch }).fetch = async () => response;+ return () => {+ (globalThis as { fetch: typeof fetch }).fetch = original;+ };+}++function jsonResponse(status: number, body: unknown): Response {+ return new Response(JSON.stringify(body), {+ status,+ headers: { "Content-Type": "application/json" },+ });+}++// Let pending microtasks run so apiFetch's async body reaches the redirect+// branch. The call itself must NOT be awaited there: on redirect apiFetch+// returns a promise that never resolves.+function tick(): Promise<void> {+ return new Promise((resolve) => setTimeout(resolve, 0));+}++test("apiFetch redirects to /login on 401 when auth is enabled and no opt-out", async () => {+ const { apiFetch } = await loadApiModule();+ const win = installWindow("/dashboard");+ const restore = stubFetch(jsonResponse(401, { detail: "unauthorized" }));+ try {+ // Do not await: apiFetch returns a never-resolving promise once it redirects.+ void apiFetch("http://localhost:8001/api/v1/knowledge/list");+ await tick();+ assert.equal(win.redirectedTo(), "/login?next=%2Fdashboard");+ } finally {+ restore();+ clearWindow();+ }+});++test("apiFetch does NOT redirect on 401 when skipAuthRedirect is set", async () => {+ // Regression guard: the login endpoint returns 401 for wrong credentials, and+ // that must reach the caller as an inline error instead of reloading the page.+ const { apiFetch } = await loadApiModule();+ const win = installWindow("/login");+ const restore = stubFetch(+ jsonResponse(401, { detail: "Incorrect username or password" }),+ );+ try {+ const res = await apiFetch("http://localhost:8001/api/v1/auth/login", {+ method: "POST",+ skipAuthRedirect: true,+ });+ assert.equal(res.status, 401);+ assert.equal(win.redirectedTo(), null);+ const data = (await res.json()) as { detail?: string };+ assert.equal(data.detail, "Incorrect username or password");+ } finally {+ restore();+ clearWindow();+ }+});++test("apiFetch passes successful responses through without redirecting", async () => {+ const { apiFetch } = await loadApiModule();+ const win = installWindow("/dashboard");+ const restore = stubFetch(jsonResponse(200, { ok: true }));+ try {+ const res = await apiFetch("http://localhost:8001/api/v1/auth/status");+ assert.equal(res.status, 200);+ assert.equal(win.redirectedTo(), null);+ } finally {+ restore();+ clearWindow();+ }+});