mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-10 20:28:54 +00:00
89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { getConfig } from "@app/lib/config/env";
|
|
import { ForbiddenRequestError } from "@app/lib/errors";
|
|
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
|
|
import { AuthMode } from "@app/services/auth/auth-type";
|
|
import { logger } from "@app/lib/logger";
|
|
import nock, { Definition } from "nock";
|
|
|
|
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({
|
|
method: "POST",
|
|
url: "/define",
|
|
schema: {
|
|
body: z.object({ definitions: z.unknown().array() }),
|
|
response: {
|
|
200: z.object({ status: z.string() })
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async (req) => {
|
|
checkIfBddNockApiEnabled();
|
|
const { body } = req;
|
|
const { definitions } = body;
|
|
logger.info(definitions, "Defining nock");
|
|
const processedDefinitions = definitions.map((definition: unknown) => {
|
|
const { path, ...rest } = definition as Definition;
|
|
return {
|
|
...rest,
|
|
path:
|
|
path !== undefined && typeof path === "string"
|
|
? path
|
|
: new RegExp((path as unknown as { regex: string }).regex ?? "")
|
|
} as Definition;
|
|
});
|
|
|
|
nock.define(processedDefinitions);
|
|
// Ensure we are activating the nocks, because we could have called `nock.restore()` before this call.
|
|
if (!nock.isActive()) {
|
|
nock.activate();
|
|
}
|
|
return { status: "ok" };
|
|
}
|
|
});
|
|
|
|
server.route({
|
|
method: "POST",
|
|
url: "/clean-all",
|
|
schema: {
|
|
response: {
|
|
200: z.object({ status: z.string() })
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async () => {
|
|
checkIfBddNockApiEnabled();
|
|
logger.info("Cleaning all nocks");
|
|
nock.cleanAll();
|
|
return { status: "ok" };
|
|
}
|
|
});
|
|
|
|
server.route({
|
|
method: "POST",
|
|
url: "/restore",
|
|
schema: {
|
|
response: {
|
|
200: z.object({ status: z.string() })
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async () => {
|
|
checkIfBddNockApiEnabled();
|
|
logger.info("Restore network requests from nock");
|
|
nock.restore();
|
|
return { status: "ok" };
|
|
}
|
|
});
|
|
};
|