2022-03-26 04:28:53 +00:00
|
|
|
import handleDeleteApiKey from "@api/api-keys/[id]/delete";
|
2022-03-25 23:42:12 +00:00
|
|
|
import { createMocks } from "node-mocks-http";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2022-03-26 04:28:53 +00:00
|
|
|
describe("DELETE /api/api-keys/[id]/delete with valid id as string returns an apiKey", () => {
|
2022-03-25 23:42:12 +00:00
|
|
|
it("returns a message with the specified apiKeys", async () => {
|
2022-03-30 12:17:55 +00:00
|
|
|
const apiKey = await prisma.apiKey.findFirst();
|
2022-03-25 23:42:12 +00:00
|
|
|
const { req, res } = createMocks({
|
2022-03-26 04:28:53 +00:00
|
|
|
method: "DELETE",
|
2022-03-25 23:42:12 +00:00
|
|
|
query: {
|
2022-03-26 04:28:53 +00:00
|
|
|
id: apiKey?.id,
|
2022-03-25 23:42:12 +00:00
|
|
|
},
|
|
|
|
});
|
2022-03-26 04:28:53 +00:00
|
|
|
// const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.id} });
|
|
|
|
await handleDeleteApiKey(req, res);
|
|
|
|
expect(res._getStatusCode()).toBe(204);
|
2022-03-30 12:17:55 +00:00
|
|
|
expect(JSON.parse(res._getData())).toEqual({
|
|
|
|
message: `api-key with id: ${apiKey?.id} deleted successfully`,
|
|
|
|
});
|
2022-03-25 23:42:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
2022-03-26 04:28:53 +00:00
|
|
|
describe("DELETE /api/api-keys/[id]/delete errors if query id is number, requires a string", () => {
|
2022-03-25 23:42:12 +00:00
|
|
|
it("returns a message with the specified apiKeys", async () => {
|
|
|
|
const { req, res } = createMocks({
|
2022-03-26 04:28:53 +00:00
|
|
|
method: "DELETE",
|
2022-03-25 23:42:12 +00:00
|
|
|
query: {
|
|
|
|
id: 1, // passing query as a number, which should fail as nextjs will try to parse it as a string
|
|
|
|
},
|
|
|
|
});
|
2022-03-26 04:28:53 +00:00
|
|
|
await handleDeleteApiKey(req, res);
|
2022-03-25 23:42:12 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-26 04:28:53 +00:00
|
|
|
describe("DELETE /api/api-keys/[id]/delete an id not present in db like 0, throws 404 not found", () => {
|
2022-03-25 23:42:12 +00:00
|
|
|
it("returns a message with the specified apiKeys", async () => {
|
|
|
|
const { req, res } = createMocks({
|
2022-03-26 04:28:53 +00:00
|
|
|
method: "DELETE",
|
2022-03-25 23:42:12 +00:00
|
|
|
query: {
|
|
|
|
id: "0", // There's no apiKey with id 0
|
|
|
|
},
|
|
|
|
});
|
2022-03-26 04:28:53 +00:00
|
|
|
await handleDeleteApiKey(req, res);
|
2022-03-25 23:42:12 +00:00
|
|
|
|
|
|
|
expect(res._getStatusCode()).toBe(404);
|
2022-03-26 04:28:53 +00:00
|
|
|
expect(JSON.parse(res._getData())).toStrictEqual({
|
2022-03-30 12:17:55 +00:00
|
|
|
error: {
|
|
|
|
clientVersion: "3.10.0",
|
|
|
|
code: "P2025",
|
|
|
|
meta: {
|
|
|
|
cause: "Record to delete does not exist.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
message: "Resource with id:0 was not found",
|
|
|
|
});
|
2022-03-25 23:42:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-26 04:28:53 +00:00
|
|
|
describe("POST /api/api-keys/[id]/delete fails, only DELETE allowed", () => {
|
2022-03-25 23:42:12 +00:00
|
|
|
it("returns a message with the specified apiKeys", async () => {
|
|
|
|
const { req, res } = createMocks({
|
|
|
|
method: "POST", // This POST method is not allowed
|
|
|
|
query: {
|
|
|
|
id: "1",
|
|
|
|
},
|
|
|
|
});
|
2022-03-26 04:28:53 +00:00
|
|
|
await handleDeleteApiKey(req, res);
|
2022-03-25 23:42:12 +00:00
|
|
|
|
|
|
|
expect(res._getStatusCode()).toBe(405);
|
2022-03-26 04:28:53 +00:00
|
|
|
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only DELETE Method allowed" });
|
2022-03-25 23:42:12 +00:00
|
|
|
});
|
|
|
|
});
|