feat: initial tests for api-keys endpoint
parent
f80f23d3c3
commit
9e8be659c5
|
@ -0,0 +1,83 @@
|
|||
import handleApiKey from "@api/api-keys/[id]";
|
||||
import { createMocks } from "node-mocks-http";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
const stringifyISODate = (date: Date|undefined): string => {
|
||||
return `${date?.toISOString()}`
|
||||
}
|
||||
describe("GET /api/api-keys/[id] with valid id as string returns an apiKey", () => {
|
||||
it("returns a message with the specified apiKeys", async () => {
|
||||
const { req, res } = createMocks({
|
||||
method: "GET",
|
||||
query: {
|
||||
id: "cl16zg6860000wwylnsgva00b",
|
||||
},
|
||||
});
|
||||
const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.id} });
|
||||
await handleApiKey(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(200);
|
||||
expect(JSON.parse(res._getData())).toEqual({ data: {...apiKey, createdAt: stringifyISODate(apiKey?.createdAt), expiresAt: stringifyISODate(apiKey?.expiresAt)} });
|
||||
});
|
||||
});
|
||||
|
||||
// This can never happen under our normal nextjs setup where query is always a string | string[].
|
||||
// But seemed a good example for testing an error validation
|
||||
describe("GET /api/api-keys/[id] errors if query id is number, requires a string", () => {
|
||||
it("returns a message with the specified apiKeys", async () => {
|
||||
const { req, res } = createMocks({
|
||||
method: "GET",
|
||||
query: {
|
||||
id: 1, // passing query as a number, which should fail as nextjs will try to parse it as a string
|
||||
},
|
||||
});
|
||||
await handleApiKey(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(400);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual([
|
||||
{
|
||||
code: "invalid_type",
|
||||
expected: "string",
|
||||
received: "number",
|
||||
path: ["id"],
|
||||
message: "Expected string, received number",
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/api-keys/[id] an id not present in db like 0, throws 404 not found", () => {
|
||||
it("returns a message with the specified apiKeys", async () => {
|
||||
const { req, res } = createMocks({
|
||||
method: "GET",
|
||||
query: {
|
||||
id: "0", // There's no apiKey with id 0
|
||||
},
|
||||
});
|
||||
await handleApiKey(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(404);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual({ message: "API key was not found" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/api-keys/[id] fails, only GET allowed", () => {
|
||||
it("returns a message with the specified apiKeys", async () => {
|
||||
const { req, res } = createMocks({
|
||||
method: "POST", // This POST method is not allowed
|
||||
query: {
|
||||
id: "1",
|
||||
},
|
||||
});
|
||||
await handleApiKey(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(405);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" });
|
||||
});
|
||||
});
|
||||
|
||||
afterAll((done) => {
|
||||
prisma.$disconnect().then();
|
||||
done();
|
||||
});
|
|
@ -1,4 +1,4 @@
|
|||
import handleEventEdit from "@api/event-types/[id]/edit";
|
||||
import handleEventTypeEdit from "@api/event-types/[id]/edit";
|
||||
import { createMocks } from "node-mocks-http";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
|
@ -17,7 +17,7 @@ describe("PATCH /api/event-types/[id]/edit with valid id and body updates an eve
|
|||
},
|
||||
});
|
||||
const event = await prisma.eventType.findUnique({ where: { id: 2 } });
|
||||
await handleEventEdit(req, res);
|
||||
await handleEventTypeEdit(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(200);
|
||||
if (event) event.title = "Updated title";
|
||||
|
@ -39,7 +39,7 @@ describe("PATCH /api/event-types/[id]/edit with invalid id returns 404", () => {
|
|||
},
|
||||
});
|
||||
const event = await prisma.eventType.findUnique({ where: { id: 2 } });
|
||||
await handleEventEdit(req, res);
|
||||
await handleEventTypeEdit(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(404);
|
||||
if (event) event.title = "Updated title";
|
||||
|
@ -62,7 +62,7 @@ describe("PATCH /api/event-types/[id]/edit with valid id and no body returns 400
|
|||
id: "2",
|
||||
},
|
||||
});
|
||||
await handleEventEdit(req, res);
|
||||
await handleEventTypeEdit(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(400);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual([{"code": "invalid_type", "expected": "string", "message": "Required", "path": ["title"], "received": "undefined"}, {"code": "invalid_type", "expected": "string", "message": "Required", "path": ["slug"], "received": "undefined"}, {"code": "invalid_type", "expected": "number", "message": "Required", "path": ["length"], "received": "undefined"}]);
|
||||
|
@ -82,7 +82,7 @@ describe("POST /api/event-types/[id]/edit fails, only PATCH allowed", () => {
|
|||
length: 1,
|
||||
},
|
||||
});
|
||||
await handleEventEdit(req, res);
|
||||
await handleEventTypeEdit(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(405);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only PATCH Method allowed for updating event-types" });
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import handleEvent from "@api/event-types/[id]";
|
||||
import handleEventType from "@api/event-types/[id]";
|
||||
import { createMocks } from "node-mocks-http";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
|
@ -12,7 +12,7 @@ describe("GET /api/event-types/[id] with valid id as string returns an event-typ
|
|||
},
|
||||
});
|
||||
const event = await prisma.eventType.findUnique({ where: { id: 1 } });
|
||||
await handleEvent(req, res);
|
||||
await handleEventType(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(200);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual({ data: event });
|
||||
|
@ -29,7 +29,7 @@ describe("GET /api/event-types/[id] errors if query id is number, requires a str
|
|||
id: 1, // passing query as a number, which should fail as nextjs will try to parse it as a string
|
||||
},
|
||||
});
|
||||
await handleEvent(req, res);
|
||||
await handleEventType(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(400);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual([
|
||||
|
@ -52,7 +52,7 @@ describe("GET /api/event-types/[id] an id not present in db like 0, throws 404 n
|
|||
id: "0", // There's no event type with id 0
|
||||
},
|
||||
});
|
||||
await handleEvent(req, res);
|
||||
await handleEventType(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(404);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Event type not found" });
|
||||
|
@ -67,7 +67,7 @@ describe("POST /api/event-types/[id] fails, only GET allowed", () => {
|
|||
id: "1",
|
||||
},
|
||||
});
|
||||
await handleEvent(req, res);
|
||||
await handleEventType(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(405);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" });
|
||||
|
|
|
@ -3,7 +3,7 @@ import { z } from "zod";
|
|||
|
||||
const schemaApiKey = z
|
||||
.object({
|
||||
expiresAt: z.string().transform((date: string) => new Date(date)).optional(), // default is 30 days
|
||||
expiresAt: z.string().optional(), // default is 30 days
|
||||
note: z.string().min(1).optional(),
|
||||
})
|
||||
.strict(); // Adding strict so that we can disallow passing in extra fields
|
||||
|
|
|
@ -19,7 +19,7 @@ export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseD
|
|||
const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (apiKey) res.status(200).json({ data: apiKey });
|
||||
if (!apiKey) res.status(404).json({ message: "Event type not found" });
|
||||
if (!apiKey) res.status(404).json({ message: "API key was not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
"jsx": "preserve",
|
||||
"paths": {
|
||||
"@api/*": ["pages/api/*"],
|
||||
"@prisma/client/*": ["@calcom/prisma/client/*"],
|
||||
"@lib/*": ["lib/*"]
|
||||
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue