Skip to content

Commit

Permalink
test: api-middleware (calcom#11825)
Browse files Browse the repository at this point in the history
* tests/api-middleware

* Skip if true

* Remove prisma from response mock
  • Loading branch information
sean-brydon authored Oct 11, 2023
1 parent f824490 commit ce532e0
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 3 deletions.
5 changes: 3 additions & 2 deletions apps/api/test/lib/bookings/_post.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<CustomNextApiRequest, CustomNextApiResponse>({
Expand All @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions apps/api/test/lib/middleware/addRequestId.test.ts
Original file line number Diff line number Diff line change
@@ -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<CustomNextApiRequest, CustomNextApiResponse>({
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();
});
});
53 changes: 53 additions & 0 deletions apps/api/test/lib/middleware/httpMethods.test.ts
Original file line number Diff line number Diff line change
@@ -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<CustomNextApiRequest, CustomNextApiResponse>({
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<CustomNextApiRequest, CustomNextApiResponse>({
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);
});
});
76 changes: 76 additions & 0 deletions apps/api/test/lib/middleware/verifyApiKey.test.ts
Original file line number Diff line number Diff line change
@@ -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<CustomNextApiRequest, CustomNextApiResponse>({
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<CustomNextApiRequest, CustomNextApiResponse>({
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);
});
});
2 changes: 1 addition & 1 deletion vitest.workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
},
Expand Down

0 comments on commit ce532e0

Please sign in to comment.