2022-03-26 00:40:43 +00:00
|
|
|
import handleapiKeyEdit from "@api/api-keys/[id]/edit";
|
|
|
|
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 00:40:43 +00:00
|
|
|
|
2022-03-26 04:28:53 +00:00
|
|
|
describe("PATCH /api/api-keys/[id]/edit with valid id and body with note", () => {
|
|
|
|
it("returns a 200 and the updated apiKey note", async () => {
|
2022-03-26 00:40:43 +00:00
|
|
|
const { req, res } = createMocks({
|
|
|
|
method: "PATCH",
|
|
|
|
query: {
|
|
|
|
id: "cl16zg6860000wwylnsgva00b",
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
note: "Updated note",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.id } });
|
|
|
|
await handleapiKeyEdit(req, res);
|
|
|
|
|
|
|
|
expect(res._getStatusCode()).toBe(200);
|
2022-03-30 12:17:55 +00:00
|
|
|
expect(JSON.parse(res._getData())).toEqual({
|
|
|
|
data: {
|
|
|
|
...apiKey,
|
|
|
|
createdAt: stringifyISODate(apiKey?.createdAt),
|
|
|
|
expiresAt: stringifyISODate(apiKey?.expiresAt),
|
|
|
|
},
|
|
|
|
});
|
2022-03-26 00:40:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-26 21:29:30 +00:00
|
|
|
describe("PATCH /api/api-keys/[id]/edit with invalid id returns 404", () => {
|
|
|
|
it("returns a message with the specified apiKeys", async () => {
|
|
|
|
const { req, res } = createMocks({
|
|
|
|
method: "PATCH",
|
|
|
|
query: {
|
|
|
|
id: "cl16zg6860000wwylnsgva00a",
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
note: "Updated note",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.id } });
|
|
|
|
await handleapiKeyEdit(req, res);
|
2022-03-26 00:40:43 +00:00
|
|
|
|
2022-03-26 21:29:30 +00:00
|
|
|
expect(res._getStatusCode()).toBe(404);
|
|
|
|
if (apiKey) apiKey.note = "Updated note";
|
2022-03-30 12:17:55 +00:00
|
|
|
expect(JSON.parse(res._getData())).toStrictEqual({
|
|
|
|
error: {
|
|
|
|
clientVersion: "3.10.0",
|
|
|
|
code: "P2025",
|
|
|
|
meta: {
|
|
|
|
cause: "Record to update not found.",
|
|
|
|
},
|
2022-03-26 21:29:30 +00:00
|
|
|
},
|
2022-03-30 12:17:55 +00:00
|
|
|
message: "apiKey with ID cl16zg6860000wwylnsgva00a not found and wasn't updated",
|
|
|
|
});
|
2022-03-26 21:29:30 +00:00
|
|
|
});
|
|
|
|
});
|
2022-03-26 00:40:43 +00:00
|
|
|
|
|
|
|
describe("PATCH /api/api-keys/[id]/edit with valid id and no body returns 200 with an apiKey with no note and default expireAt", () => {
|
|
|
|
it("returns a message with the specified apiKeys", async () => {
|
2022-03-30 12:17:55 +00:00
|
|
|
const apiKey = await prisma.apiKey.create({ data: {} });
|
2022-03-26 00:40:43 +00:00
|
|
|
const { req, res } = createMocks({
|
|
|
|
method: "PATCH",
|
|
|
|
query: {
|
|
|
|
id: apiKey?.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await handleapiKeyEdit(req, res);
|
|
|
|
|
|
|
|
expect(apiKey?.note).toBeNull();
|
|
|
|
expect(res._getStatusCode()).toBe(200);
|
2022-03-30 12:17:55 +00:00
|
|
|
expect(JSON.parse(res._getData())).toEqual({
|
|
|
|
data: {
|
|
|
|
...apiKey,
|
|
|
|
createdAt: stringifyISODate(apiKey?.createdAt),
|
|
|
|
expiresAt: stringifyISODate(apiKey?.expiresAt),
|
|
|
|
},
|
|
|
|
});
|
2022-03-26 00:40:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("POST /api/api-keys/[id]/edit fails, only PATCH allowed", () => {
|
|
|
|
it("returns a message with the specified apiKeys", async () => {
|
|
|
|
const { req, res } = createMocks({
|
|
|
|
method: "POST", // This POST method is not allowed
|
|
|
|
query: {
|
|
|
|
id: "cl16zg6860000wwylnsgva00b",
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
note: "Updated note",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await handleapiKeyEdit(req, res);
|
|
|
|
|
|
|
|
expect(res._getStatusCode()).toBe(405);
|
2022-03-30 12:17:55 +00:00
|
|
|
expect(JSON.parse(res._getData())).toStrictEqual({
|
|
|
|
message: "Only PATCH Method allowed for updating API keys",
|
|
|
|
});
|
2022-03-26 00:40:43 +00:00
|
|
|
});
|
|
|
|
});
|