diff --git a/tests/api-keys/[id]/api-key.id.delete.test.ts b/tests/api-keys/[id]/api-key.id.delete.test.ts deleted file mode 100644 index d2abfecb7a..0000000000 --- a/tests/api-keys/[id]/api-key.id.delete.test.ts +++ /dev/null @@ -1,86 +0,0 @@ -import handleDeleteApiKey from "@api/api-keys/[id]/delete"; -import { createMocks } from "node-mocks-http"; - -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 { req, res } = createMocks({ - method: "DELETE", - query: { - id: apiKey?.id, - }, - }); - // 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`, - }); - }); -}); - -// 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("DELETE /api/api-keys/[id]/delete errors if query id is number, requires a string", () => { - it("returns a message with the specified apiKeys", async () => { - const { req, res } = createMocks({ - method: "DELETE", - query: { - id: 1, // passing query as a number, which should fail as nextjs will try to parse it as a string - }, - }); - await handleDeleteApiKey(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("DELETE /api/api-keys/[id]/delete an id not present in db like 0, throws 404 not found", () => { - it("returns a message with the specified apiKeys", async () => { - const { req, res } = createMocks({ - method: "DELETE", - query: { - id: "0", // There's no apiKey with id 0 - }, - }); - await handleDeleteApiKey(req, res); - - 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", - }); - }); -}); - -describe("POST /api/api-keys/[id]/delete fails, only DELETE allowed", () => { - it("returns a message with the specified apiKeys", async () => { - const { req, res } = createMocks({ - method: "POST", // This POST method is not allowed - query: { - id: "1", - }, - }); - await handleDeleteApiKey(req, res); - - expect(res._getStatusCode()).toBe(405); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only DELETE Method allowed" }); - }); -}); diff --git a/tests/api-keys/[id]/api-key.id.edit.test.ts b/tests/api-keys/[id]/api-key.id.edit.test.ts deleted file mode 100644 index 1007181179..0000000000 --- a/tests/api-keys/[id]/api-key.id.edit.test.ts +++ /dev/null @@ -1,103 +0,0 @@ -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"; - -describe("PATCH /api/api-keys/[id]/edit with valid id and body with note", () => { - it("returns a 200 and the updated apiKey note", async () => { - 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); - expect(JSON.parse(res._getData())).toEqual({ - data: { - ...apiKey, - createdAt: stringifyISODate(apiKey?.createdAt), - expiresAt: stringifyISODate(apiKey?.expiresAt), - }, - }); - }); -}); - -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); - - 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.", - }, - }, - 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 { req, res } = createMocks({ - method: "PATCH", - query: { - id: apiKey?.id, - }, - }); - await handleapiKeyEdit(req, res); - - 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), - }, - }); - }); -}); - -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); - expect(JSON.parse(res._getData())).toStrictEqual({ - message: "Only PATCH Method allowed for updating API keys", - }); - }); -}); diff --git a/tests/api-keys/[id]/api-key.id.index.test.ts b/tests/api-keys/[id]/api-key.id.index.test.ts deleted file mode 100644 index 8d3c2655af..0000000000 --- a/tests/api-keys/[id]/api-key.id.index.test.ts +++ /dev/null @@ -1,83 +0,0 @@ -import handleApiKey from "@api/api-keys/[id]"; -import { createMocks } from "node-mocks-http"; - -import prisma from "@calcom/prisma"; - -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 () => { - const { req, res } = createMocks({ - method: "GET", - query: { - id: "cl16zg6860000wwylnsgva00b", - }, - }); - 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), - }, - }); - }); -}); - -// 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/api-keys/[id] errors if query id is number, requires a string", () => { - it("returns a message with the specified apiKeys", 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 handleApiKey(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/api-keys/[id] an id not present in db like 0, throws 404 not found", () => { - it("returns a message with the specified apiKeys", async () => { - const { req, res } = createMocks({ - method: "GET", - query: { - id: "0", // There's no apiKey with id 0 - }, - }); - await handleApiKey(req, res); - - expect(res._getStatusCode()).toBe(404); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "API key was not found" }); - }); -}); - -describe("POST /api/api-keys/[id] 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 - query: { - id: "1", - }, - }); - await handleApiKey(req, res); - - expect(res._getStatusCode()).toBe(405); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" }); - }); -}); diff --git a/tests/api-keys/api-key.index.test.ts b/tests/api-keys/api-key.index.test.ts deleted file mode 100644 index 957b749b88..0000000000 --- a/tests/api-keys/api-key.index.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -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" }); - }); -}); diff --git a/tests/api-keys/api-key.new.test.ts b/tests/api-keys/api-key.new.test.ts deleted file mode 100644 index 339b4c146b..0000000000 --- a/tests/api-keys/api-key.new.test.ts +++ /dev/null @@ -1,77 +0,0 @@ -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: [], - }, - ]); - }); -}); diff --git a/tests/bookings/[id]/booking.id.edit.test.ts b/tests/bookings/[id]/booking.id.edit.test.ts deleted file mode 100644 index 8bf32eb0e5..0000000000 --- a/tests/bookings/[id]/booking.id.edit.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -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", - }); - }); -}); diff --git a/tests/bookings/[id]/booking.id.index.test.ts b/tests/bookings/[id]/booking.id.index.test.ts deleted file mode 100644 index 4a2ff0ca0c..0000000000 --- a/tests/bookings/[id]/booking.id.index.test.ts +++ /dev/null @@ -1,84 +0,0 @@ -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" }); - }); -}); diff --git a/tests/bookings/booking.index.test.ts b/tests/bookings/booking.index.test.ts deleted file mode 100644 index 957b749b88..0000000000 --- a/tests/bookings/booking.index.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -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" }); - }); -}); diff --git a/tests/bookings/booking.new.test.ts b/tests/bookings/booking.new.test.ts deleted file mode 100644 index 339b4c146b..0000000000 --- a/tests/bookings/booking.new.test.ts +++ /dev/null @@ -1,77 +0,0 @@ -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: [], - }, - ]); - }); -}); diff --git a/tests/event-types/[id]/event-type.id.edit.test.ts b/tests/event-types/[id]/event-type.id.edit.test.ts deleted file mode 100644 index e88bcb9b49..0000000000 --- a/tests/event-types/[id]/event-type.id.edit.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -import handleEventTypeEdit from "@api/event-types/[id]/edit"; -import { createMocks } from "node-mocks-http"; - -import prisma from "@calcom/prisma"; - -describe("PATCH /api/event-types/[id]/edit with valid id and body updates an event-type", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "2", - }, - body: { - title: "Updated title", - slug: "updated-slug", - length: 1, - }, - }); - const event = await prisma.eventType.findUnique({ where: { id: parseInt(req.query.id) } }); - await handleEventTypeEdit(req, res); - - expect(res._getStatusCode()).toBe(200); - if (event) event.title = "Updated title"; - expect(JSON.parse(res._getData())).toStrictEqual({ data: event }); - }); -}); - -describe("PATCH /api/event-types/[id]/edit with invalid id returns 404", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "0", - }, - body: { - title: "Updated title", - slug: "updated-slug", - length: 1, - }, - }); - const event = await prisma.eventType.findUnique({ where: { id: parseInt(req.query.id) } }); - await handleEventTypeEdit(req, res); - - expect(res._getStatusCode()).toBe(404); - if (event) event.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/event-types/[id]/edit with valid id and no body returns 400 error and zod validation errors", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "2", - }, - }); - await handleEventTypeEdit(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/event-types/[id]/edit fails, only PATCH 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", - }, - body: { - title: "Updated title", - slug: "updated-slug", - length: 1, - }, - }); - await handleEventTypeEdit(req, res); - - expect(res._getStatusCode()).toBe(405); - expect(JSON.parse(res._getData())).toStrictEqual({ - message: "Only PATCH Method allowed for updating event-types", - }); - }); -}); diff --git a/tests/event-types/[id]/event-type.id.index.test.ts b/tests/event-types/[id]/event-type.id.index.test.ts deleted file mode 100644 index bb54d5963f..0000000000 --- a/tests/event-types/[id]/event-type.id.index.test.ts +++ /dev/null @@ -1,75 +0,0 @@ -import handleEventType from "@api/event-types/[id]"; -import { createMocks } from "node-mocks-http"; - -import prisma from "@calcom/prisma"; - -describe("GET /api/event-types/[id] with valid id as string returns an event-type", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "GET", - query: { - id: "1", - }, - }); - const event = await prisma.eventType.findUnique({ where: { id: 1 } }); - await handleEventType(req, res); - - expect(res._getStatusCode()).toBe(200); - expect(JSON.parse(res._getData())).toStrictEqual({ data: event }); - }); -}); - -// 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/event-types/[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 handleEventType(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/event-types/[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 event type with id 0 - }, - }); - await handleEventType(req, res); - - expect(res._getStatusCode()).toBe(404); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "Event type not found" }); - }); -}); - -describe("POST /api/event-types/[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 handleEventType(req, res); - - expect(res._getStatusCode()).toBe(405); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" }); - }); -}); diff --git a/tests/event-types/event-type.index.test.ts b/tests/event-types/event-type.index.test.ts deleted file mode 100644 index 7a63a59a5d..0000000000 --- a/tests/event-types/event-type.index.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import handleApiKeys from "@api/event-types"; -import { createMocks } from "node-mocks-http"; - -import prisma from "@calcom/prisma"; - -// import {stringifyISODate} from "@lib/utils/stringifyISODate"; - -describe("GET /api/event-types without any params", () => { - it("returns a message with the specified eventTypes", async () => { - const { req, res } = createMocks({ - method: "GET", - query: {}, - }); - const eventTypes = await prisma.eventType.findMany(); - await handleApiKeys(req, res); - - expect(res._getStatusCode()).toBe(200); - // eventTypes = eventTypes.map(eventType => (eventType = {...eventType, createdAt: stringifyISODate(eventType?.createdAt), expiresAt: stringifyISODate(eventType?.expiresAt)})); - expect(JSON.parse(res._getData())).toStrictEqual(JSON.parse(JSON.stringify({ data: { ...eventTypes } }))); - }); -}); - -describe("POST /api/event-types/ fails, only GET allowed", () => { - it("returns a message with the specified eventTypes", 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" }); - }); -}); diff --git a/tests/event-types/event-type.new.test.ts b/tests/event-types/event-type.new.test.ts deleted file mode 100644 index a79209ec03..0000000000 --- a/tests/event-types/event-type.new.test.ts +++ /dev/null @@ -1,70 +0,0 @@ -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: { -// fail: true -// // note: '123', -// // slug: 12, -// }, -// }); -// await handleNewApiKey(req, res); - -// expect(res._getStatusCode()).toBe(400); -// expect(JSON.parse(res._getData())).toStrictEqual({ error: "Only POST Method allowed" }); -// }); -// }); diff --git a/tests/teams/[id]/team.id.edit.test.ts b/tests/teams/[id]/team.id.edit.test.ts deleted file mode 100644 index 101deaa06e..0000000000 --- a/tests/teams/[id]/team.id.edit.test.ts +++ /dev/null @@ -1,107 +0,0 @@ -import handleTeamEdit from "@api/teams/[id]/edit"; -import { createMocks } from "node-mocks-http"; - -import prisma from "@calcom/prisma"; - -describe("PATCH /api/teams/[id]/edit with valid id and body updates a team", () => { - it("returns a message with the specified teams", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "1", - }, - body: { - name: "Updated team", - slug: "updated-team", - }, - }); - const team = await prisma.team.findUnique({ where: { id: parseInt(req.query.id) } }); - await handleTeamEdit(req, res); - - expect(res._getStatusCode()).toBe(200); - // if (team) team.name = "Updated name"; - expect(JSON.parse(res._getData())).toStrictEqual({ data: team }); - }); -}); - -describe("PATCH /api/teams/[id]/edit with invalid id returns 404", () => { - it("returns a message with the specified teams", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "0", - }, - body: { - name: "Updated name", - slug: "updated-slug", - }, - }); - const team = await prisma.team.findUnique({ where: { id: parseInt(req.query.id) } }); - await handleTeamEdit(req, res); - - expect(res._getStatusCode()).toBe(404); - 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/teams/[id]/edit with valid id and no body returns 400 error and zod validation errors", () => { - it("returns a message with the specified teams", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "2", - }, - }); - await handleTeamEdit(req, res); - - expect(res._getStatusCode()).toBe(400); - - // Ugly parsing of zod validation errors, not for final production but works for testing - expect(JSON.parse(res._getData())).toStrictEqual([ - { - code: "invalid_type", - expected: "string", - message: "Required", - path: ["slug"], - received: "undefined", - }, - { - code: "invalid_type", - expected: "string", - message: "Required", - path: ["name"], - received: "undefined", - }, - ]); - }); -}); - -describe("POST /api/teams/[id]/edit fails, only PATCH allowed", () => { - it("returns a message with the specified teams", async () => { - const { req, res } = createMocks({ - method: "POST", // This POST method is not allowed - query: { - id: "1", - }, - body: { - name: "Updated name", - slug: "updated-slug", - }, - }); - await handleTeamEdit(req, res); - - expect(res._getStatusCode()).toBe(405); - expect(JSON.parse(res._getData())).toStrictEqual({ - message: "Only PATCH Method allowed for updating teams", - }); - }); -}); diff --git a/tests/teams/[id]/team.id.index.test.ts b/tests/teams/[id]/team.id.index.test.ts deleted file mode 100644 index 016f7ec604..0000000000 --- a/tests/teams/[id]/team.id.index.test.ts +++ /dev/null @@ -1,75 +0,0 @@ -import handleTeam from "@api/teams/[id]"; -import { createMocks } from "node-mocks-http"; - -import prisma from "@calcom/prisma"; - -describe("GET /api/teams/[id] with valid id as string returns an team-type", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "GET", - query: { - id: "1", - }, - }); - const team = await prisma.team.findUnique({ where: { id: 1 } }); - await handleTeam(req, res); - - expect(res._getStatusCode()).toBe(200); - expect(JSON.parse(res._getData())).toStrictEqual({ data: team }); - }); -}); - -// 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/teams/[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 handleTeam(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/teams/[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 team type with id 0 - }, - }); - await handleTeam(req, res); - - expect(res._getStatusCode()).toBe(404); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "Event type not found" }); - }); -}); - -describe("POST /api/teams/[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 handleTeam(req, res); - - expect(res._getStatusCode()).toBe(405); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" }); - }); -}); diff --git a/tests/users/[id]/user.id.edit.test.ts b/tests/users/[id]/user.id.edit.test.ts deleted file mode 100644 index e88bcb9b49..0000000000 --- a/tests/users/[id]/user.id.edit.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -import handleEventTypeEdit from "@api/event-types/[id]/edit"; -import { createMocks } from "node-mocks-http"; - -import prisma from "@calcom/prisma"; - -describe("PATCH /api/event-types/[id]/edit with valid id and body updates an event-type", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "2", - }, - body: { - title: "Updated title", - slug: "updated-slug", - length: 1, - }, - }); - const event = await prisma.eventType.findUnique({ where: { id: parseInt(req.query.id) } }); - await handleEventTypeEdit(req, res); - - expect(res._getStatusCode()).toBe(200); - if (event) event.title = "Updated title"; - expect(JSON.parse(res._getData())).toStrictEqual({ data: event }); - }); -}); - -describe("PATCH /api/event-types/[id]/edit with invalid id returns 404", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "0", - }, - body: { - title: "Updated title", - slug: "updated-slug", - length: 1, - }, - }); - const event = await prisma.eventType.findUnique({ where: { id: parseInt(req.query.id) } }); - await handleEventTypeEdit(req, res); - - expect(res._getStatusCode()).toBe(404); - if (event) event.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/event-types/[id]/edit with valid id and no body returns 400 error and zod validation errors", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "PATCH", - query: { - id: "2", - }, - }); - await handleEventTypeEdit(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/event-types/[id]/edit fails, only PATCH 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", - }, - body: { - title: "Updated title", - slug: "updated-slug", - length: 1, - }, - }); - await handleEventTypeEdit(req, res); - - expect(res._getStatusCode()).toBe(405); - expect(JSON.parse(res._getData())).toStrictEqual({ - message: "Only PATCH Method allowed for updating event-types", - }); - }); -}); diff --git a/tests/users/[id]/user.id.index.test.ts b/tests/users/[id]/user.id.index.test.ts deleted file mode 100644 index bf79c38143..0000000000 --- a/tests/users/[id]/user.id.index.test.ts +++ /dev/null @@ -1,83 +0,0 @@ -import handleUser from "@api/users/[id]"; -import { createMocks } from "node-mocks-http"; - -import prisma from "@calcom/prisma"; - -import { stringifyISODate } from "@lib/utils/stringifyISODate"; - -describe("GET /api/users/[id] with valid id as string returns an user-type", () => { - it("returns a message with the specified events", async () => { - const { req, res } = createMocks({ - method: "GET", - query: { - id: "1", - }, - }); - const user = await prisma.user.findUnique({ where: { id: 1 } }); - await handleUser(req, res); - - expect(res._getStatusCode()).toBe(200); - expect(JSON.parse(res._getData())).toEqual({ - data: { - ...user, - createdDate: stringifyISODate(user?.createdDate), - emailVerified: stringifyISODate(user?.emailVerified), - }, - }); - }); -}); - -// 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/users/[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 handleUser(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/users/[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 user type with id 0 - }, - }); - await handleUser(req, res); - - expect(res._getStatusCode()).toBe(404); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "Event type not found" }); - }); -}); - -describe("POST /api/users/[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 handleUser(req, res); - - expect(res._getStatusCode()).toBe(405); - expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" }); - }); -});