forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tests/api-middleware * Skip if true * Remove prisma from response mock
- Loading branch information
1 parent
f824490
commit ce532e0
Showing
5 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters