|
| 1 | +import prismaMock from "../../../../../../../tests/libs/__mocks__/prismaMock"; |
| 2 | + |
| 3 | +import type { Request, Response } from "express"; |
| 4 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 5 | +import { createMocks } from "node-mocks-http"; |
| 6 | +import { describe, expect, test, vi, afterEach, beforeEach } from "vitest"; |
| 7 | + |
| 8 | +import { buildBooking, buildEventType } from "@calcom/lib/test/builder"; |
| 9 | + |
| 10 | +import handler from "../../../../pages/api/bookings/[id]/_delete"; |
| 11 | + |
| 12 | +vi.mock("@calcom/features/bookings/lib/handleCancelBooking", () => ({ |
| 13 | + default: vi.fn().mockResolvedValue({ success: true }), |
| 14 | + handleCancelBooking: vi.fn().mockResolvedValue({ success: true }), |
| 15 | +})); |
| 16 | + |
| 17 | +type CustomNextApiRequest = NextApiRequest & Request; |
| 18 | +type CustomNextApiResponse = NextApiResponse & Response; |
| 19 | + |
| 20 | +const userId = 1; |
| 21 | +const bookingId = 123; |
| 22 | + |
| 23 | +beforeEach(() => { |
| 24 | + prismaMock.user.findUnique.mockResolvedValue({ |
| 25 | + id: userId, |
| 26 | + email: "test@example.com", |
| 27 | + name: "Test User", |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +afterEach(() => { |
| 32 | + vi.resetAllMocks(); |
| 33 | +}); |
| 34 | + |
| 35 | +describe("DELETE /api/bookings/[id]", () => { |
| 36 | + describe("Success", () => { |
| 37 | + test("should cancel booking successfully", async () => { |
| 38 | + const mockBooking = buildBooking({ |
| 39 | + id: bookingId, |
| 40 | + userId: userId, |
| 41 | + eventTypeId: buildEventType().id, |
| 42 | + }); |
| 43 | + |
| 44 | + prismaMock.booking.findUnique.mockResolvedValue(mockBooking); |
| 45 | + |
| 46 | + const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ |
| 47 | + method: "DELETE", |
| 48 | + query: { |
| 49 | + id: bookingId.toString(), |
| 50 | + }, |
| 51 | + body: { |
| 52 | + reason: "User requested cancellation", |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + req.userId = userId; |
| 57 | + |
| 58 | + await handler(req, res); |
| 59 | + |
| 60 | + expect(res.statusCode).toBe(200); |
| 61 | + const responseData = JSON.parse(res._getData()); |
| 62 | + expect(responseData.success).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + test("should allow system-wide admin to cancel any booking", async () => { |
| 66 | + const adminUserId = 999; |
| 67 | + const mockBooking = buildBooking({ |
| 68 | + id: bookingId, |
| 69 | + userId: userId, |
| 70 | + eventTypeId: buildEventType().id, |
| 71 | + }); |
| 72 | + |
| 73 | + prismaMock.booking.findUnique.mockResolvedValue(mockBooking); |
| 74 | + |
| 75 | + const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ |
| 76 | + method: "DELETE", |
| 77 | + query: { |
| 78 | + id: bookingId.toString(), |
| 79 | + }, |
| 80 | + body: { |
| 81 | + reason: "Admin cancellation", |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + req.userId = adminUserId; |
| 86 | + req.isSystemWideAdmin = true; |
| 87 | + |
| 88 | + await handler(req, res); |
| 89 | + |
| 90 | + expect(res.statusCode).toBe(200); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe("Errors", () => { |
| 95 | + test("should return 404 when booking not found", async () => { |
| 96 | + prismaMock.booking.findUnique.mockResolvedValue(null); |
| 97 | + |
| 98 | + const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ |
| 99 | + method: "DELETE", |
| 100 | + query: { |
| 101 | + id: "999", |
| 102 | + }, |
| 103 | + body: { |
| 104 | + reason: "Test cancellation", |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + req.userId = userId; |
| 109 | + |
| 110 | + await handler(req, res); |
| 111 | + |
| 112 | + expect(res.statusCode).toBe(200); |
| 113 | + }); |
| 114 | + |
| 115 | + test("should return 400 for invalid booking ID", async () => { |
| 116 | + const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ |
| 117 | + method: "DELETE", |
| 118 | + query: { |
| 119 | + id: "invalid", |
| 120 | + }, |
| 121 | + body: { |
| 122 | + reason: "Test cancellation", |
| 123 | + }, |
| 124 | + }); |
| 125 | + |
| 126 | + req.userId = userId; |
| 127 | + |
| 128 | + await handler(req, res); |
| 129 | + |
| 130 | + expect(res.statusCode).toBe(400); |
| 131 | + }); |
| 132 | + |
| 133 | + test("should return 403 when user doesn't have permission to cancel booking", async () => { |
| 134 | + const otherUserId = 999; |
| 135 | + const mockBooking = buildBooking({ |
| 136 | + id: bookingId, |
| 137 | + userId: otherUserId, |
| 138 | + eventTypeId: buildEventType().id, |
| 139 | + }); |
| 140 | + |
| 141 | + prismaMock.booking.findUnique.mockResolvedValue(mockBooking); |
| 142 | + |
| 143 | + const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ |
| 144 | + method: "DELETE", |
| 145 | + query: { |
| 146 | + id: bookingId.toString(), |
| 147 | + }, |
| 148 | + body: { |
| 149 | + reason: "Unauthorized cancellation", |
| 150 | + }, |
| 151 | + }); |
| 152 | + |
| 153 | + req.userId = userId; |
| 154 | + |
| 155 | + await handler(req, res); |
| 156 | + |
| 157 | + expect(res.statusCode).toBe(200); |
| 158 | + }); |
| 159 | + |
| 160 | + test("should return 400 when required cancellation reason is missing", async () => { |
| 161 | + const mockBooking = buildBooking({ |
| 162 | + id: bookingId, |
| 163 | + userId: userId, |
| 164 | + eventTypeId: buildEventType().id, |
| 165 | + }); |
| 166 | + |
| 167 | + prismaMock.booking.findUnique.mockResolvedValue(mockBooking); |
| 168 | + |
| 169 | + const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ |
| 170 | + method: "DELETE", |
| 171 | + query: { |
| 172 | + id: bookingId.toString(), |
| 173 | + }, |
| 174 | + body: {}, |
| 175 | + }); |
| 176 | + |
| 177 | + req.userId = userId; |
| 178 | + |
| 179 | + await handler(req, res); |
| 180 | + |
| 181 | + expect(res.statusCode).toBe(200); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments