feat: adds availabilitesi and attendees endpoints, some cleanup less try/catch more if/else

This commit is contained in:
Agusti Fernandez Pardo committed 2022-03-27 00:58:22 +01:00
1 parent ac307f7161
commit 936572e7e1
48 files changed
+1365 -172

No files matched your search

@@ -0,0 +1,92 @@
import handleBookingEdit from "@api/bookings/[id]/edit";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
describe("PATCH /api/bookings/[id]/edit with valid id and body updates an booking", () => {
it("returns a message with the specified bookings", async () => {
const { req, res } = createMocks({
method: "PATCH",
query: {
id: "2",
},
body: {
title: "Updated title",
slug: "updated-slug",
length: 1,
},
});
const booking = await prisma.booking.findUnique({ where: { id: parseInt(req.query.id) } });
await handleBookingEdit(req, res);
expect(res._getStatusCode()).toBe(200);
if (booking) booking.title = "Updated title";
expect(JSON.parse(res._getData())).toStrictEqual({ data: booking });
});
});
describe("PATCH /api/bookings/[id]/edit with invalid id returns 404", () => {
it("returns a message with the specified bookings", async () => {
const { req, res } = createMocks({
method: "PATCH",
query: {
id: "0",
},
body: {
title: "Updated title",
slug: "updated-slug",
length: 1,
},
});
const booking = await prisma.booking.findUnique({ where: { id: parseInt(req.query.id) } });
await handleBookingEdit(req, res);
expect(res._getStatusCode()).toBe(404);
if (booking) booking.title = "Updated title";
expect(JSON.parse(res._getData())).toStrictEqual({ "error": {
"clientVersion": "3.10.0",
"code": "P2025",
"meta": {
"cause": "Record to update not found.",
},
},
"message": "Event type with ID 0 not found and wasn't updated", });
});
});
describe("PATCH /api/bookings/[id]/edit with valid id and no body returns 400 error and zod validation errors", () => {
it("returns a message with the specified bookings", async () => {
const { req, res } = createMocks({
method: "PATCH",
query: {
id: "2",
},
});
await handleBookingEdit(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"}]);
});
});
describe("POST /api/bookings/[id]/edit fails, only PATCH allowed", () => {
it("returns a message with the specified bookings", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
query: {
id: "1",
},
body: {
title: "Updated title",
slug: "updated-slug",
length: 1,
},
});
await handleBookingEdit(req, res);
expect(res._getStatusCode()).toBe(405);
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only PATCH Method allowed for updating bookings" });
});
});
@@ -0,0 +1,85 @@
import handleBooking from "@api/bookings/[id]";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
import { stringifyISODate } from "@lib/utils/stringifyISODate";
describe("GET /api/bookings/[id] with valid id as string returns an booking", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "GET",
query: {
id: "1",
},
});
const booking = await prisma.booking.findUnique({ where: { id: 1 } });
await handleBooking(req, res);
expect(res._getStatusCode()).toBe(200);
expect(JSON.parse(res._getData())).toEqual({
data: {
...booking,
createdAt: stringifyISODate(booking?.createdAt),
startTime: stringifyISODate(booking?.startTime),
endTime: stringifyISODate(booking?.endTime)
}
});
});
});
// 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/bookings/[id] errors if query id is number, requires a string", () => {
it("returns a message with the specified events", 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 handleBooking(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/bookings/[id] an id not present in db like 0, throws 404 not found", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "GET",
query: {
id: "0", // There's no booking type with id 0
},
});
await handleBooking(req, res);
expect(res._getStatusCode()).toBe(404);
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Event type not found" });
});
});
describe("POST /api/bookings/[id] fails, only GET allowed", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
query: {
id: "1",
},
});
await handleBooking(req, res);
expect(res._getStatusCode()).toBe(405);
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" });
});
});
+31
View File
@@ -0,0 +1,31 @@
import handleApiKeys from "@api/api-keys";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
import {stringifyISODate} from "@lib/utils/stringifyISODate";
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);
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} })));
});
});
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" });
});
});
+71
View File
@@ -0,0 +1,71 @@
import handleNewApiKey from "@api/api-keys/new";
import { createMocks } from "node-mocks-http";
describe("POST /api/api-keys/new with a note", () => {
it("returns a 201, and the created api key", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
body: {
note: "Updated note",
},
});
await handleNewApiKey(req, res);
expect(res._getStatusCode()).toBe(201);
expect(JSON.parse(res._getData()).data.note).toStrictEqual("Updated note");
});
});
describe("POST /api/api-keys/new with a slug param", () => {
it("returns error 400, and the details about invalid slug body param", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
body: {
note: "Updated note",
slug: "slug",
},
});
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": []}]
);
});
});
describe("GET /api/api-keys/new fails, only POST allowed", () => {
it("returns a message with the specified apiKeys", async () => {
const { req, res } = createMocks({
method: "GET", // This POST method is not allowed
});
await handleNewApiKey(req, res);
expect(res._getStatusCode()).toBe(405);
expect(JSON.parse(res._getData())).toStrictEqual({ error: "Only POST Method 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
// note: '123',
// slug: 12,
},
});
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": []
}]);
});
});
File renamed without changes.