Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
|
|
|
import { POST } from "./route";
|
|
|
|
function ctx(id: string) {
|
|
return { params: Promise.resolve({ id }) };
|
|
}
|
|
function req(body: unknown) {
|
|
return new Request("http://localhost/api/users/9/password", {
|
|
method: "POST",
|
|
body: JSON.stringify(body),
|
|
}) as unknown as Parameters<typeof POST>[0];
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
});
|
|
|
|
describe("POST /api/users/[id]/password", () => {
|
|
it("returns 401 when there is no session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const res = await POST(req({}), ctx("9"));
|
|
expect(res.status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("forwards only the password pair and answers 204", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: null });
|
|
const res = await POST(req({ password: "Good#Pass1", confirmPassword: "Good#Pass1", extra: 1 }), ctx("9"));
|
|
expect(res.status).toBe(204);
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/users/9/password", {
|
|
method: "POST",
|
|
body: { password: "Good#Pass1", confirmPassword: "Good#Pass1" },
|
|
token: "jwt-abc",
|
|
});
|
|
});
|
|
|
|
it("passes a refusal through with its code", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 409, error: "The user is deactivated", code: "USER_DISABLED" });
|
|
const res = await POST(req({ password: "a", confirmPassword: "a" }), ctx("9"));
|
|
expect(res.status).toBe(409);
|
|
expect(await res.json()).toMatchObject({ error: "The user is deactivated", code: "USER_DISABLED" });
|
|
});
|
|
});
|