cal.pub0.org/tests/api-keys/api-key.index.test.ts

40 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-03-26 21:29:30 +00:00
import handleApiKeys from "@api/api-keys";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
2022-03-30 12:17:55 +00:00
import { stringifyISODate } from "@lib/utils/stringifyISODate";
2022-03-26 21:29:30 +00:00
describe("GET /api/api-keys without any params", () => {
it("returns a message with the specified apiKeys", async () => {
const { req, res } = createMocks({
method: "GET",
query: {},
});
let apiKeys = await prisma.apiKey.findMany();
await handleApiKeys(req, res);
expect(res._getStatusCode()).toBe(200);
2022-03-30 12:17:55 +00:00
apiKeys = apiKeys.map(
(apiKey) =>
(apiKey = {
...apiKey,
createdAt: stringifyISODate(apiKey?.createdAt),
expiresAt: stringifyISODate(apiKey?.expiresAt),
})
);
expect(JSON.parse(res._getData())).toStrictEqual(JSON.parse(JSON.stringify({ data: { ...apiKeys } })));
2022-03-26 21:29:30 +00:00
});
});
describe("POST /api/api-keys/ 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
});
await handleApiKeys(req, res);
expect(res._getStatusCode()).toBe(405);
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" });
});
});