|
| 1 | +import type { Definition } from "nock"; |
| 2 | +import { z } from "zod"; |
| 3 | + |
| 4 | +import { getConfig } from "@app/lib/config/env"; |
| 5 | +import { ForbiddenRequestError } from "@app/lib/errors"; |
| 6 | +import { logger } from "@app/lib/logger"; |
| 7 | +import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; |
| 8 | +import { AuthMode } from "@app/services/auth/auth-type"; |
| 9 | + |
| 10 | +// When running in production, we don't want to even import nock, because it's not needed and it increases memory usage a lots. |
| 11 | +// It once caused an outage in the production environment. |
| 12 | +// This is why we would rather to crash the app if it's not in development mode (in that case, Kubernetes should stop it from rolling out). |
| 13 | +if (process.env.NODE_ENV === "production") { |
| 14 | + throw new Error("BDD Nock API can only be enabled in development or test mode"); |
| 15 | +} |
| 16 | + |
| 17 | +export const registerBddNockRouter = async (server: FastifyZodProvider) => { |
| 18 | + const appCfg = getConfig(); |
| 19 | + const importNock = async () => { |
| 20 | + // eslint-disable-next-line import/no-extraneous-dependencies |
| 21 | + const { default: nock } = await import("nock"); |
| 22 | + return nock; |
| 23 | + }; |
| 24 | + |
| 25 | + const checkIfBddNockApiEnabled = () => { |
| 26 | + // Note: Please note that this API is only available in development mode and only for BDD tests. |
| 27 | + // This endpoint should NEVER BE ENABLED IN PRODUCTION! |
| 28 | + if (appCfg.NODE_ENV === "production" || !appCfg.isBddNockApiEnabled) { |
| 29 | + throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" }); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + server.route({ |
| 34 | + method: "POST", |
| 35 | + url: "/define", |
| 36 | + schema: { |
| 37 | + body: z.object({ definitions: z.unknown().array() }), |
| 38 | + response: { |
| 39 | + 200: z.object({ status: z.string() }) |
| 40 | + } |
| 41 | + }, |
| 42 | + onRequest: verifyAuth([AuthMode.JWT]), |
| 43 | + handler: async (req) => { |
| 44 | + checkIfBddNockApiEnabled(); |
| 45 | + const { body } = req; |
| 46 | + const { definitions } = body; |
| 47 | + logger.info(definitions, "Defining nock"); |
| 48 | + const processedDefinitions = definitions.map((definition: unknown) => { |
| 49 | + const { path, ...rest } = definition as Definition; |
| 50 | + return { |
| 51 | + ...rest, |
| 52 | + path: |
| 53 | + path !== undefined && typeof path === "string" |
| 54 | + ? path |
| 55 | + : new RegExp((path as unknown as { regex: string }).regex ?? "") |
| 56 | + } as Definition; |
| 57 | + }); |
| 58 | + |
| 59 | + const nock = await importNock(); |
| 60 | + nock.define(processedDefinitions); |
| 61 | + // Ensure we are activating the nocks, because we could have called `nock.restore()` before this call. |
| 62 | + if (!nock.isActive()) { |
| 63 | + nock.activate(); |
| 64 | + } |
| 65 | + return { status: "ok" }; |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + server.route({ |
| 70 | + method: "POST", |
| 71 | + url: "/clean-all", |
| 72 | + schema: { |
| 73 | + response: { |
| 74 | + 200: z.object({ status: z.string() }) |
| 75 | + } |
| 76 | + }, |
| 77 | + onRequest: verifyAuth([AuthMode.JWT]), |
| 78 | + handler: async () => { |
| 79 | + checkIfBddNockApiEnabled(); |
| 80 | + logger.info("Cleaning all nocks"); |
| 81 | + const nock = await importNock(); |
| 82 | + nock.cleanAll(); |
| 83 | + return { status: "ok" }; |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + server.route({ |
| 88 | + method: "POST", |
| 89 | + url: "/restore", |
| 90 | + schema: { |
| 91 | + response: { |
| 92 | + 200: z.object({ status: z.string() }) |
| 93 | + } |
| 94 | + }, |
| 95 | + onRequest: verifyAuth([AuthMode.JWT]), |
| 96 | + handler: async () => { |
| 97 | + checkIfBddNockApiEnabled(); |
| 98 | + logger.info("Restore network requests from nock"); |
| 99 | + const nock = await importNock(); |
| 100 | + nock.restore(); |
| 101 | + return { status: "ok" }; |
| 102 | + } |
| 103 | + }); |
| 104 | +}; |
0 commit comments