From b8610dc2d1f48a383a0281d0550b11fcfd59c6d1 Mon Sep 17 00:00:00 2001 From: Vinicius Vieira <64455111+viniciusvieira00@users.noreply.github.com> Date: Tue, 23 Jul 2024 09:19:36 -0300 Subject: [PATCH] test: Create 4 test cases to POST Attendees (#15856) * Create 4 test cases to POST Attendees * Adjusted commented tips on PR * Adjusted isAdmin to isSystemWideAdmin * Removed req.isAdmin * Delete test-insights.csv --------- Co-authored-by: Amit Sharma <74371312+Amit91848@users.noreply.github.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com> --- apps/api/v1/test/lib/attendees/_post.test.ts | 112 +++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 apps/api/v1/test/lib/attendees/_post.test.ts diff --git a/apps/api/v1/test/lib/attendees/_post.test.ts b/apps/api/v1/test/lib/attendees/_post.test.ts new file mode 100644 index 0000000000..aea3154b5a --- /dev/null +++ b/apps/api/v1/test/lib/attendees/_post.test.ts @@ -0,0 +1,112 @@ +import prismaMock from "../../../../../../tests/libs/__mocks__/prismaMock"; + +import type { Request, Response } from "express"; +import type { NextApiRequest, NextApiResponse } from "next"; +import { createMocks } from "node-mocks-http"; +import { describe, expect, test } from "vitest"; + +import handler from "../../../pages/api/attendees/_post"; + +type CustomNextApiRequest = NextApiRequest & Request; +type CustomNextApiResponse = NextApiResponse & Response; + +describe("POST /api/attendees", () => { + describe("Errors", () => { + test("Returns 403 if user is not admin and has no booking", async () => { + const { req, res } = createMocks({ + method: "POST", + body: { + bookingId: 1, + email: "test@example.com", + name: "Test User", + timeZone: "UTC", + }, + }); + + prismaMock.booking.findFirst.mockResolvedValue(null); + + req.userId = 123; + // req.isAdmin = false; + await handler(req, res); + + expect(res.statusCode).toBe(403); + expect(JSON.parse(res._getData()).message).toBe("Forbidden"); + }); + + test("Returns 200 if user is admin", async () => { + const { req, res } = createMocks({ + method: "POST", + body: { + bookingId: 1, + email: "test@example.com", + name: "Test User", + timeZone: "UTC", + }, + }); + + const attendeeData = { + id: 1, + email: "test@example.com", + name: "Test User", + timeZone: "UTC", + bookingId: 1, + }; + + prismaMock.attendee.create.mockResolvedValue(attendeeData); + req.isSystemWideAdmin = true; + req.userId = 123; + await handler(req, res); + + expect(res.statusCode).toBe(200); + expect(JSON.parse(res._getData()).attendee).toEqual(attendeeData); + expect(JSON.parse(res._getData()).message).toBe("Attendee created successfully"); + }); + + test("Returns 200 if user is not admin but has a booking", async () => { + const { req, res } = createMocks({ + method: "POST", + body: { + bookingId: 1, + email: "test@example.com", + name: "Test User", + timeZone: "UTC", + }, + }); + + const userBooking = { id: 1 }; + + prismaMock.booking.findFirst.mockResolvedValue(userBooking); + + const attendeeData = { + id: 1, + email: "test@example.com", + name: "Test User", + timeZone: "UTC", + bookingId: 1, + }; + + prismaMock.attendee.create.mockResolvedValue(attendeeData); + + req.userId = 123; + await handler(req, res); + + expect(res.statusCode).toBe(200); + expect(JSON.parse(res._getData()).attendee).toEqual(attendeeData); + expect(JSON.parse(res._getData()).message).toBe("Attendee created successfully"); + }); + + test("Returns 400 if request body is invalid", async () => { + const { req, res } = createMocks({ + method: "POST", + body: { + // Missing required fields + }, + }); + + req.userId = 123; + await handler(req, res); + + expect(res.statusCode).toBe(400); + }); + }); +});