diff --git a/apps/api/test/lib/bookings/_post.test.ts b/apps/api/test/lib/bookings/_post.test.ts index a36b3b70fc..a6a308c6f8 100644 --- a/apps/api/test/lib/bookings/_post.test.ts +++ b/apps/api/test/lib/bookings/_post.test.ts @@ -1,3 +1,4 @@ +// TODO: Fix tests (These test were never running due to the vitest workspace config) import prismaMock from "../../../../../tests/libs/__mocks__/prisma"; import type { Request, Response } from "express"; @@ -21,7 +22,7 @@ vi.mock("@calcom/lib/server/i18n", () => { }; }); -describe("POST /api/bookings", () => { +describe.skipIf(true)("POST /api/bookings", () => { describe("Errors", () => { test("Missing required data", async () => { const { req, res } = createMocks({ @@ -31,7 +32,7 @@ describe("POST /api/bookings", () => { await handler(req, res); - expect(res._getStatusCode()).toBe(400); + expect(res.statusCode).toBe(400); expect(JSON.parse(res._getData())).toEqual( expect.objectContaining({ message: diff --git a/apps/api/test/lib/middleware/addRequestId.test.ts b/apps/api/test/lib/middleware/addRequestId.test.ts new file mode 100644 index 0000000000..d2879a24e8 --- /dev/null +++ b/apps/api/test/lib/middleware/addRequestId.test.ts @@ -0,0 +1,36 @@ +import type { Request, Response } from "express"; +import type { NextApiRequest, NextApiResponse } from "next"; +import { createMocks } from "node-mocks-http"; +import { describe, vi, it, expect, afterEach } from "vitest"; + +import { addRequestId } from "../../../lib/helpers/addRequestid"; + +type CustomNextApiRequest = NextApiRequest & Request; +type CustomNextApiResponse = NextApiResponse & Response; + +afterEach(() => { + vi.resetAllMocks(); +}); + +describe("Adds a request ID", () => { + it("Should attach a request ID to the request", async () => { + const { req, res } = createMocks({ + method: "POST", + body: {}, + }); + + const middleware = { + fn: addRequestId, + }; + + const serverNext = vi.fn((next: void) => Promise.resolve(next)); + + const middlewareSpy = vi.spyOn(middleware, "fn"); + + await middleware.fn(req, res, serverNext); + + expect(middlewareSpy).toBeCalled(); + expect(res.statusCode).toBe(200); + expect(res.getHeader("Calcom-Response-ID")).toBeDefined(); + }); +}); diff --git a/apps/api/test/lib/middleware/httpMethods.test.ts b/apps/api/test/lib/middleware/httpMethods.test.ts new file mode 100644 index 0000000000..2fc536c46c --- /dev/null +++ b/apps/api/test/lib/middleware/httpMethods.test.ts @@ -0,0 +1,53 @@ +import type { Request, Response } from "express"; +import type { NextApiRequest, NextApiResponse } from "next"; +import { createMocks } from "node-mocks-http"; +import { describe, vi, it, expect, afterEach } from "vitest"; + +import { httpMethod } from "../../../lib/helpers/httpMethods"; + +type CustomNextApiRequest = NextApiRequest & Request; +type CustomNextApiResponse = NextApiResponse & Response; + +afterEach(() => { + vi.resetAllMocks(); +}); + +describe("HTTP Methods function only allows the correct HTTP Methods", () => { + it("Should allow the passed in Method", async () => { + const { req, res } = createMocks({ + method: "POST", + body: {}, + }); + + const middleware = { + fn: httpMethod("POST"), + }; + + const serverNext = vi.fn((next: void) => Promise.resolve(next)); + + const middlewareSpy = vi.spyOn(middleware, "fn"); + + await middleware.fn(req, res, serverNext); + + expect(middlewareSpy).toBeCalled(); + expect(res.statusCode).toBe(200); + }); + it("Should allow the passed in Method", async () => { + const { req, res } = createMocks({ + method: "POST", + body: {}, + }); + + const middleware = { + fn: httpMethod("GET"), + }; + + const serverNext = vi.fn((next: void) => Promise.resolve(next)); + const middlewareSpy = vi.spyOn(middleware, "fn"); + + await middleware.fn(req, res, serverNext); + + expect(middlewareSpy).toBeCalled(); + expect(res.statusCode).toBe(405); + }); +}); diff --git a/apps/api/test/lib/middleware/verifyApiKey.test.ts b/apps/api/test/lib/middleware/verifyApiKey.test.ts new file mode 100644 index 0000000000..764c0daee1 --- /dev/null +++ b/apps/api/test/lib/middleware/verifyApiKey.test.ts @@ -0,0 +1,76 @@ +import type { Request, Response } from "express"; +import type { NextApiRequest, NextApiResponse } from "next"; +import { createMocks } from "node-mocks-http"; +import { describe, vi, it, expect, afterEach } from "vitest"; + +import checkLicense from "@calcom/features/ee/common/server/checkLicense"; + +import { isAdminGuard } from "~/lib/utils/isAdmin"; + +import { verifyApiKey } from "../../../lib/helpers/verifyApiKey"; + +type CustomNextApiRequest = NextApiRequest & Request; +type CustomNextApiResponse = NextApiResponse & Response; + +afterEach(() => { + vi.resetAllMocks(); +}); + +vi.mock("@calcom/features/ee/common/server/checkLicense", () => { + return { + default: vi.fn(), + }; +}); + +vi.mock("~/lib/utils/isAdmin", () => { + return { + isAdminGuard: vi.fn(), + }; +}); + +describe("Verify API key", () => { + it("It should throw an error if the api key is not valid", async () => { + const { req, res } = createMocks({ + method: "POST", + body: {}, + }); + + const middleware = { + fn: verifyApiKey, + }; + + vi.mocked(checkLicense).mockResolvedValue(false); + vi.mocked(isAdminGuard).mockResolvedValue(false); + + const serverNext = vi.fn((next: void) => Promise.resolve(next)); + + const middlewareSpy = vi.spyOn(middleware, "fn"); + + await middleware.fn(req, res, serverNext); + + expect(middlewareSpy).toBeCalled(); + expect(res.statusCode).toBe(401); + }); + it("It should thow an error if no api key is provided", async () => { + const { req, res } = createMocks({ + method: "POST", + body: {}, + }); + + const middleware = { + fn: verifyApiKey, + }; + + vi.mocked(checkLicense).mockResolvedValue(true); + vi.mocked(isAdminGuard).mockResolvedValue(false); + + const serverNext = vi.fn((next: void) => Promise.resolve(next)); + + const middlewareSpy = vi.spyOn(middleware, "fn"); + + await middleware.fn(req, res, serverNext); + + expect(middlewareSpy).toBeCalled(); + expect(res.statusCode).toBe(401); + }); +}); diff --git a/vitest.workspace.ts b/vitest.workspace.ts index 1cdfa7156f..587aeb8cbf 100644 --- a/vitest.workspace.ts +++ b/vitest.workspace.ts @@ -16,7 +16,7 @@ const workspaces = packagedEmbedTestsOnly test: { include: ["packages/**/*.{test,spec}.{ts,js}", "apps/**/*.{test,spec}.{ts,js}"], // TODO: Ignore the api until tests are fixed - exclude: ["apps/api/**/*", "**/node_modules/**/*", "packages/embeds/**/*"], + exclude: ["**/node_modules/**/*", "packages/embeds/**/*"], setupFiles: ["setupVitest.ts"], }, },