Skip to content

Scheduling infrastructure for absolutely everyone.

test: Tests the order of middleware in API (#11852)

sean-brydonbc81f659aade
diff --git a/apps/api/lib/helpers/verifyApiKey.ts b/apps/api/lib/helpers/verifyApiKey.ts
index 6e4fa43ad4c..85bb98ad7c3 100644
--- a/apps/api/lib/helpers/verifyApiKey.ts
+++ b/apps/api/lib/helpers/verifyApiKey.ts
@@ -4,7 +4,7 @@ import { hashAPIKey } from "@calcom/features/ee/api-keys/lib/apiKeys";
import checkLicense from "@calcom/features/ee/common/server/checkLicense";
import { IS_PRODUCTION } from "@calcom/lib/constants";
-import { isAdminGuard } from "~/lib/utils/isAdmin";
+import { isAdminGuard } from "../utils/isAdmin";
// Used to check if the apiKey is not expired, could be extracted if reused. but not for now.
export const dateNotInPast = function (date: Date) {
diff --git a/apps/api/lib/helpers/withMiddleware.ts b/apps/api/lib/helpers/withMiddleware.ts
index d8cc9d6a13c..ecfa22fd4f2 100644
--- a/apps/api/lib/helpers/withMiddleware.ts
+++ b/apps/api/lib/helpers/withMiddleware.ts
@@ -16,22 +16,25 @@ import { rateLimitApiKey } from "./rateLimitApiKey";
import { verifyApiKey } from "./verifyApiKey";
import { withPagination } from "./withPagination";
-const withMiddleware = label(
- {
- HTTP_GET_OR_POST,
- HTTP_GET_DELETE_PATCH,
- HTTP_GET,
- HTTP_PATCH,
- HTTP_POST,
- HTTP_DELETE,
- addRequestId,
- verifyApiKey,
- rateLimitApiKey,
- customPrismaClient,
- extendRequest,
- pagination: withPagination,
- captureErrors,
- },
+const middleware = {
+ HTTP_GET_OR_POST,
+ HTTP_GET_DELETE_PATCH,
+ HTTP_GET,
+ HTTP_PATCH,
+ HTTP_POST,
+ HTTP_DELETE,
+ addRequestId,
+ verifyApiKey,
+ rateLimitApiKey,
+ customPrismaClient,
+ extendRequest,
+ pagination: withPagination,
+ captureErrors,
+};
+
+type Middleware = keyof typeof middleware;
+
+const middlewareOrder =
// The order here, determines the order of execution
[
"extendRequest",
@@ -41,7 +44,8 @@ const withMiddleware = label(
"verifyApiKey",
"rateLimitApiKey",
"addRequestId",
- ] // <-- Provide a list of middleware to call automatically
-);
+ ] as Middleware[]; // <-- Provide a list of middleware to call automatically
+
+const withMiddleware = label(middleware, middlewareOrder);
-export { withMiddleware };
+export { withMiddleware, middleware, middlewareOrder };
diff --git a/apps/api/test/lib/middleware/withMiddleware.test.ts b/apps/api/test/lib/middleware/withMiddleware.test.ts
new file mode 100644
index 00000000000..d8771e9719e
--- /dev/null
+++ b/apps/api/test/lib/middleware/withMiddleware.test.ts
@@ -0,0 +1,17 @@
+import { describe, vi, it, expect, afterEach } from "vitest";
+
+import { middlewareOrder } from "../../../lib/helpers/withMiddleware";
+
+afterEach(() => {
+ vi.resetAllMocks();
+});
+
+// Not sure if there is much point testing this order is actually applied via an integration test:
+// It is tested internally https://github.com/htunnicliff/next-api-middleware/blob/368b12aa30e79f4bd7cfe7aacc18da263cc3de2f/lib/label.spec.ts#L62
+describe("API - withMiddleware test", () => {
+ it("Custom prisma should be before verifyApiKey", async () => {
+ const customPrismaClientIndex = middlewareOrder.indexOf("customPrismaClient");
+ const verifyApiKeyIndex = middlewareOrder.indexOf("verifyApiKey");
+ expect(customPrismaClientIndex).toBeLessThan(verifyApiKeyIndex);
+ });
+});