Files
co-lite/solvway-admin-portal/app/api/auth/forgot-password/route.test.ts
T
ngqileandClaude Opus 5.5 da813874ec feat(admin-portal): forgot password page; own account cannot be deactivated
/forgot-password always shows the same message; the route never forwards upstream
errors or a token. /api/admin/me now returns id and username so the user form can
lock the Active switch on your own account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 21:38:42 +08:00

56 lines
2.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
import { POST } from "./route";
function req(body: unknown) {
return new Request("http://localhost/api/auth/forgot-password", {
method: "POST",
body: typeof body === "string" ? body : JSON.stringify(body),
}) as unknown as Parameters<typeof POST>[0];
}
beforeEach(() => {
mocks.callMobileApi.mockReset();
});
describe("POST /api/auth/forgot-password", () => {
it("forwards trimmed username and email with no token and answers ok", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { result: "true" } });
const res = await POST(req({ username: " jane ", email: " jane@example.com ", extra: "x" }));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ ok: true });
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/auth/forgot-password", {
method: "POST",
body: { username: "jane", email: "jane@example.com" },
});
});
it.each([
["404", { ok: false, error: "nope", status: 404 }],
["500", { ok: false, error: "boom", status: 500 }],
["network failure", { ok: false, error: "Unable to reach the server." }],
])("answers 200 ok whatever upstream returns (%s)", async (_n, upstream) => {
mocks.callMobileApi.mockResolvedValue(upstream);
const res = await POST(req({ username: "jane", email: "jane@example.com" }));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ ok: true });
});
it("drops non-string values and does not call upstream for missing ones", async () => {
const res = await POST(req({ username: { a: 1 }, email: 5 }));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ ok: true });
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("answers 200 ok on an unparseable body", async () => {
const res = await POST(req("not json"));
expect(res.status).toBe(200);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
});