This commit is contained in:
Agusti Fernandez Pardo committed 2022-03-30 14:17:55 +02:00
1 parent 39ae2aed37
commit 1de7bc4146
126 files changed
+1240 -992

No files matched your search

+13 -13
View File
@@ -5,7 +5,7 @@ import prisma from "@calcom/prisma";
describe("DELETE /api/api-keys/[id]/delete with valid id as string returns an apiKey", () => {
it("returns a message with the specified apiKeys", async () => {
const apiKey = await prisma.apiKey.findFirst()
const apiKey = await prisma.apiKey.findFirst();
const { req, res } = createMocks({
method: "DELETE",
query: {
@@ -15,7 +15,9 @@ describe("DELETE /api/api-keys/[id]/delete with valid id as string returns an ap
// const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.id} });
await handleDeleteApiKey(req, res);
expect(res._getStatusCode()).toBe(204);
expect(JSON.parse(res._getData())).toEqual({message: `api-key with id: ${apiKey?.id} deleted successfully`});
expect(JSON.parse(res._getData())).toEqual({
message: `api-key with id: ${apiKey?.id} deleted successfully`,
});
});
});
@@ -56,15 +58,15 @@ describe("DELETE /api/api-keys/[id]/delete an id not present in db like 0, throw
expect(res._getStatusCode()).toBe(404);
expect(JSON.parse(res._getData())).toStrictEqual({
"error": {
"clientVersion": "3.10.0",
"code": "P2025",
"meta": {
"cause": "Record to delete does not exist.",
},
},
"message": "Resource with id:0 was not found",
});
error: {
clientVersion: "3.10.0",
code: "P2025",
meta: {
cause: "Record to delete does not exist.",
},
},
message: "Resource with id:0 was not found",
});
});
});
@@ -82,5 +84,3 @@ describe("POST /api/api-keys/[id]/delete fails, only DELETE allowed", () => {
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only DELETE Method allowed" });
});
});
+29 -15
View File
@@ -2,7 +2,8 @@ import handleapiKeyEdit from "@api/api-keys/[id]/edit";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
import {stringifyISODate} from "@lib/utils/stringifyISODate";
import { stringifyISODate } from "@lib/utils/stringifyISODate";
describe("PATCH /api/api-keys/[id]/edit with valid id and body with note", () => {
it("returns a 200 and the updated apiKey note", async () => {
@@ -19,7 +20,13 @@ describe("PATCH /api/api-keys/[id]/edit with valid id and body with note", () =>
await handleapiKeyEdit(req, res);
expect(res._getStatusCode()).toBe(200);
expect(JSON.parse(res._getData())).toEqual({ data: {...apiKey, createdAt: stringifyISODate(apiKey?.createdAt), expiresAt: stringifyISODate(apiKey?.expiresAt)} });
expect(JSON.parse(res._getData())).toEqual({
data: {
...apiKey,
createdAt: stringifyISODate(apiKey?.createdAt),
expiresAt: stringifyISODate(apiKey?.expiresAt),
},
});
});
});
@@ -39,20 +46,22 @@ describe("PATCH /api/api-keys/[id]/edit with invalid id returns 404", () => {
expect(res._getStatusCode()).toBe(404);
if (apiKey) apiKey.note = "Updated note";
expect(JSON.parse(res._getData())).toStrictEqual({ "error": {
"clientVersion": "3.10.0",
"code": "P2025",
"meta": {
"cause": "Record to update not found.",
},
expect(JSON.parse(res._getData())).toStrictEqual({
error: {
clientVersion: "3.10.0",
code: "P2025",
meta: {
cause: "Record to update not found.",
},
},
"message": "apiKey with ID cl16zg6860000wwylnsgva00a not found and wasn't updated", });
message: "apiKey with ID cl16zg6860000wwylnsgva00a not found and wasn't updated",
});
});
});
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 () => {
const apiKey = await prisma.apiKey.create({data:{} });
const apiKey = await prisma.apiKey.create({ data: {} });
const { req, res } = createMocks({
method: "PATCH",
query: {
@@ -63,8 +72,13 @@ describe("PATCH /api/api-keys/[id]/edit with valid id and no body returns 200 wi
expect(apiKey?.note).toBeNull();
expect(res._getStatusCode()).toBe(200);
expect(JSON.parse(res._getData())).toEqual({ data: {...apiKey, createdAt: stringifyISODate(apiKey?.createdAt), expiresAt: stringifyISODate(apiKey?.expiresAt)} });
expect(JSON.parse(res._getData())).toEqual({
data: {
...apiKey,
createdAt: stringifyISODate(apiKey?.createdAt),
expiresAt: stringifyISODate(apiKey?.expiresAt),
},
});
});
});
@@ -82,8 +96,8 @@ describe("POST /api/api-keys/[id]/edit fails, only PATCH allowed", () => {
await handleapiKeyEdit(req, res);
expect(res._getStatusCode()).toBe(405);
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only PATCH Method allowed for updating API keys" });
expect(JSON.parse(res._getData())).toStrictEqual({
message: "Only PATCH Method allowed for updating API keys",
});
});
});
+10 -5
View File
@@ -2,7 +2,8 @@ import handleApiKey from "@api/api-keys/[id]";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
import {stringifyISODate} from "@lib/utils/stringifyISODate";
import { stringifyISODate } from "@lib/utils/stringifyISODate";
describe("GET /api/api-keys/[id] with valid id as string returns an apiKey", () => {
it("returns a message with the specified apiKeys", async () => {
@@ -12,11 +13,17 @@ describe("GET /api/api-keys/[id] with valid id as string returns an apiKey", ()
id: "cl16zg6860000wwylnsgva00b",
},
});
const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.id} });
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)} });
expect(JSON.parse(res._getData())).toEqual({
data: {
...apiKey,
createdAt: stringifyISODate(apiKey?.createdAt),
expiresAt: stringifyISODate(apiKey?.expiresAt),
},
});
});
});
@@ -74,5 +81,3 @@ describe("POST /api/api-keys/[id] fails, only GET allowed", () => {
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" });
});
});
+11 -3
View File
@@ -2,7 +2,8 @@ import handleApiKeys from "@api/api-keys";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
import {stringifyISODate} from "@lib/utils/stringifyISODate";
import { stringifyISODate } from "@lib/utils/stringifyISODate";
describe("GET /api/api-keys without any params", () => {
it("returns a message with the specified apiKeys", async () => {
@@ -14,8 +15,15 @@ describe("GET /api/api-keys without any params", () => {
await handleApiKeys(req, res);
expect(res._getStatusCode()).toBe(200);
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} })));
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 } })));
});
});
+18 -12
View File
@@ -28,13 +28,17 @@ describe("POST /api/api-keys/new with a slug param", () => {
await handleNewApiKey(req, res);
expect(res._getStatusCode()).toBe(400);
expect(JSON.parse(res._getData())).toStrictEqual(
[{"code": "unrecognized_keys", "keys": ["slug"], "message": "Unrecognized key(s) in object: 'slug'", "path": []}]
);
expect(JSON.parse(res._getData())).toStrictEqual([
{
code: "unrecognized_keys",
keys: ["slug"],
message: "Unrecognized key(s) in object: 'slug'",
path: [],
},
]);
});
});
describe("GET /api/api-keys/new fails, only POST allowed", () => {
it("returns a message with the specified apiKeys", async () => {
const { req, res } = createMocks({
@@ -47,14 +51,13 @@ describe("GET /api/api-keys/new fails, only POST allowed", () => {
});
});
// FIXME: test 405 when prisma fails look for how to test prisma errors
describe("GET /api/api-keys/new fails, only POST allowed", () => {
it("returns a message with the specified apiKeys", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
body: {
nonExistentParam: true
nonExistentParam: true,
// note: '123',
// slug: 12,
},
@@ -62,10 +65,13 @@ describe("GET /api/api-keys/new fails, only POST allowed", () => {
await handleNewApiKey(req, res);
expect(res._getStatusCode()).toBe(400);
expect(JSON.parse(res._getData())).toStrictEqual([{
"code": "unrecognized_keys",
"keys": ["nonExistentParam"],
"message": "Unrecognized key(s) in object: 'nonExistentParam'", "path": []
}]);
expect(JSON.parse(res._getData())).toStrictEqual([
{
code: "unrecognized_keys",
keys: ["nonExistentParam"],
message: "Unrecognized key(s) in object: 'nonExistentParam'",
path: [],
},
]);
});
});
});