mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
tests/kit/ holds typed fixture factories and callRoute(handler, options). The factories write through the application's Prisma client, so a fixture has the shape production stores and the compiler checks it; sessions and Bearer tokens are minted by createSession and issueApiToken, so a fixture cannot drift into a shape authentication no longer accepts. callRoute builds a real NextRequest, fills the next/headers stand-in from the auth fixture and runs the exported handler, apiHandler included. Every call is also a contract check: the status must be declared for that path and method in the OpenAPI registry, a JSON body must parse with the registered schema, and a key the schema does not declare fails the call. The scope gate's generic 403, documented on the shared 401, is the one answer accepted without its own entry. kit-contract.test.ts proves each refusal can fire. The token list and revoke move onto the kit. The mocked unit test asserted the list's where clause and proved the API switch is not consulted by reading api-handler.ts for a string; the kit test authenticates a Bearer token while the switch is off, revokes it, and shows it refused on the next request.
112 lines
3.2 KiB
TypeScript
112 lines
3.2 KiB
TypeScript
/**
|
|
* The route kit's contract check, proven able to fail.
|
|
*
|
|
* Every kit test leans on `callRoute` refusing a response the published
|
|
* contract does not describe. A check that passes whatever it is handed would
|
|
* turn every one of them into a weaker test than it claims to be, so each
|
|
* refusal is exercised here against a handler that answers exactly the wrong
|
|
* thing, wrapped in the real `apiHandler`.
|
|
*/
|
|
import { NextResponse } from "next/server";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { apiHandler } from "@/lib/api-handler";
|
|
|
|
import { ContractViolation, anonymous, callRoute } from "../../kit/call-route";
|
|
|
|
const token = {
|
|
id: "tok-1",
|
|
name: "phone",
|
|
permissions: ["*"],
|
|
lastUsedAt: null,
|
|
expiresAt: null,
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
revoked: false,
|
|
};
|
|
|
|
function answering(status: number, body: unknown) {
|
|
return apiHandler(async () => NextResponse.json(body, { status }));
|
|
}
|
|
|
|
describe("callRoute's contract check", () => {
|
|
it("accepts a body the registered schema describes", async () => {
|
|
const res = await callRoute(
|
|
answering(200, { data: [token], error: null }),
|
|
{
|
|
method: "GET",
|
|
route: "/api/tokens",
|
|
auth: anonymous,
|
|
},
|
|
);
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
it("refuses a key the registered schema does not declare", async () => {
|
|
await expect(
|
|
callRoute(
|
|
answering(200, {
|
|
data: [{ ...token, tokenHash: "abc" }],
|
|
error: null,
|
|
}),
|
|
{ method: "GET", route: "/api/tokens", auth: anonymous },
|
|
),
|
|
).rejects.toThrow(/does not declare: data\[0\]\.tokenHash/);
|
|
});
|
|
|
|
it("refuses a body of the wrong shape", async () => {
|
|
await expect(
|
|
callRoute(
|
|
answering(200, { data: [{ ...token, revoked: "no" }], error: null }),
|
|
{
|
|
method: "GET",
|
|
route: "/api/tokens",
|
|
auth: anonymous,
|
|
},
|
|
),
|
|
).rejects.toBeInstanceOf(ContractViolation);
|
|
});
|
|
|
|
it("refuses a status the operation does not declare", async () => {
|
|
await expect(
|
|
callRoute(answering(409, { data: null, error: "conflict" }), {
|
|
method: "GET",
|
|
route: "/api/tokens",
|
|
auth: anonymous,
|
|
}),
|
|
).rejects.toThrow(/answered 409, which the contract does not declare/);
|
|
});
|
|
|
|
it("refuses a 403 that is not the generic scope refusal", async () => {
|
|
await expect(
|
|
callRoute(
|
|
answering(403, {
|
|
data: null,
|
|
error: "nope",
|
|
meta: { errorCode: "http.forbidden" },
|
|
}),
|
|
{ method: "GET", route: "/api/tokens", auth: anonymous },
|
|
),
|
|
).rejects.toThrow(/answered 403/);
|
|
});
|
|
|
|
it("refuses a route the registry does not know", async () => {
|
|
await expect(
|
|
callRoute(answering(200, { data: null, error: null }), {
|
|
method: "GET",
|
|
route: "/api/not-a-route",
|
|
auth: anonymous,
|
|
}),
|
|
).rejects.toThrow(/is not in the OpenAPI registry/);
|
|
});
|
|
|
|
it("skips the check only when told why", async () => {
|
|
const res = await callRoute(answering(200, { anything: true }), {
|
|
method: "GET",
|
|
route: "/api/not-a-route",
|
|
auth: anonymous,
|
|
contract: { skip: "exercising the escape hatch itself" },
|
|
});
|
|
expect(res.body).toEqual({ anything: true });
|
|
});
|
|
});
|