More mock

This commit is contained in:
Fang-Pen Lin
2025-11-13 09:21:57 -08:00
parent 89baed638d
commit 146524ac2d
@@ -7,6 +7,15 @@ import { AuthMode } from "@app/services/auth/auth-type";
import nock, { Definition } from "nock"; import nock, { Definition } from "nock";
export const registerBddNockRouter = async (server: FastifyZodProvider) => { export const registerBddNockRouter = async (server: FastifyZodProvider) => {
const checkIfBddNockApiEnabled = () => {
const appCfg = getConfig();
// Note: Please note that this API is only available in development mode and only for BDD tests.
// This endpoint should NEVER BE ENABLED IN PRODUCTION!
if (appCfg.NODE_ENV !== "development" || !appCfg.isBddNockApiEnabled) {
throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" });
}
};
server.route({ server.route({
method: "POST", method: "POST",
url: "/define", url: "/define",
@@ -18,16 +27,43 @@ export const registerBddNockRouter = async (server: FastifyZodProvider) => {
}, },
onRequest: verifyAuth([AuthMode.JWT]), onRequest: verifyAuth([AuthMode.JWT]),
handler: async (req) => { handler: async (req) => {
const appCfg = getConfig(); checkIfBddNockApiEnabled();
// Note: Please note that this API is only available in development mode and only for BDD tests.
// This endpoint should NEVER BE ENABLED IN PRODUCTION!
if (appCfg.NODE_ENV !== "development" || !appCfg.isBddNockApiEnabled) {
throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" });
}
const { body } = req; const { body } = req;
const { definition } = body; const { definition } = body;
nock.define(definition as unknown as Definition[]); nock.define(definition as unknown as Definition[]);
return { status: "ok" }; return { status: "ok" };
} }
}); });
server.route({
method: "POST",
url: "/restore",
schema: {
response: {
200: z.object({ status: z.string() })
}
},
onRequest: verifyAuth([AuthMode.JWT]),
handler: async (req) => {
checkIfBddNockApiEnabled();
nock.restore();
return { status: "ok" };
}
});
server.route({
method: "POST",
url: "/clear-all",
schema: {
response: {
200: z.object({ status: z.string() })
}
},
onRequest: verifyAuth([AuthMode.JWT]),
handler: async (req) => {
checkIfBddNockApiEnabled();
nock.cleanAll();
return { status: "ok" };
}
});
}; };