Skip to content

Commit

Permalink
fix: event api accepts invalid start & end values (calcom#13203)
Browse files Browse the repository at this point in the history
* fix: event api accepts invalid start & end values

* fix: unit tests

* test: added 'event length check during booking'

* test: fix unit test

---------

Co-authored-by: Keith Williams <[email protected]>
  • Loading branch information
MehulZR and keithwillcode authored Jan 20, 2024
1 parent da026d9 commit 95e037c
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 207 deletions.
9 changes: 9 additions & 0 deletions packages/features/bookings/lib/handleNewBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,15 @@ async function handler(
throw new HttpError({ statusCode: 400, message: error.message });
}

const reqEventLength = dayjs(reqBody.end).diff(dayjs(reqBody.start), "minutes");
const validEventLengths = eventType.metadata?.multipleDuration?.length
? eventType.metadata.multipleDuration
: [eventType.length];
if (!validEventLengths.includes(reqEventLength)) {
loggerWithEventDetails.warn({ message: "NewBooking: Invalid event length" });
throw new HttpError({ statusCode: 400, message: "Invalid event length" });
}

// loadUsers allows type inferring
let users: (Awaited<ReturnType<typeof loadUsers>>[number] & {
isFixed?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -285,8 +285,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -448,8 +448,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -603,8 +603,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -724,8 +724,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -889,8 +889,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1023,8 +1023,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1111,8 +1111,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1167,6 +1167,66 @@ describe("handleNewBooking", () => {
);
});

describe("Event length check during booking", () => {
test(
`should fail if the time difference between a booking's start and end times is not equal to the event length.`,
async () => {
const handleNewBooking = (await import("@calcom/features/bookings/lib/handleNewBooking")).default;

const booker = getBooker({
email: "[email protected]",
name: "Booker",
});

const organizer = getOrganizer({
name: "Organizer",
email: "[email protected]",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});

await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
},
],
organizer,
})
);

const mockBookingData = getMockRequestDataForBooking({
data: {
start: `${getDate({ dateIncrement: 1 }).dateString}T05:00:00.000Z`,
end: `${getDate({ dateIncrement: 1 }).dateString}T05:15:00.000Z`,
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: "New York" },
},
},
});

const { req } = createMockNextJsRequest({
method: "POST",
body: mockBookingData,
});

await expect(async () => await handleNewBooking(req)).rejects.toThrowError("Invalid event length");
},
timeout
);
});

describe(
"Availability Check during booking",
() => {
Expand Down Expand Up @@ -1196,8 +1256,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand All @@ -1212,7 +1272,7 @@ describe("handleNewBooking", () => {
userId: 101,
status: BookingStatus.ACCEPTED,
startTime: `${plus1DateString}T05:00:00.000Z`,
endTime: `${plus1DateString}T05:15:00.000Z`,
endTime: `${plus1DateString}T05:30:00.000Z`,
},
],
organizer,
Expand All @@ -1221,7 +1281,7 @@ describe("handleNewBooking", () => {

const mockBookingData = getMockRequestDataForBooking({
data: {
start: `${getDate({ dateIncrement: 1 }).dateString}T04:00:00.000Z`,
start: `${getDate({ dateIncrement: 1 }).dateString}T05:00:00.000Z`,
end: `${getDate({ dateIncrement: 1 }).dateString}T05:30:00.000Z`,
eventTypeId: 1,
responses: {
Expand Down Expand Up @@ -1279,8 +1339,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1308,7 +1368,7 @@ describe("handleNewBooking", () => {

const mockBookingData = getMockRequestDataForBooking({
data: {
start: `${getDate({ dateIncrement: 1 }).dateString}T04:00:00.000Z`,
start: `${getDate({ dateIncrement: 1 }).dateString}T05:00:00.000Z`,
end: `${getDate({ dateIncrement: 1 }).dateString}T05:30:00.000Z`,
eventTypeId: 1,
responses: {
Expand Down Expand Up @@ -1380,9 +1440,9 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
slotInterval: 30,
requiresConfirmation: true,
length: 45,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1506,9 +1566,9 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
slotInterval: 30,
requiresConfirmation: true,
length: 45,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1630,15 +1690,15 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
slotInterval: 30,
requiresConfirmation: true,
metadata: {
requiresConfirmationThreshold: {
time: 30,
unit: "minutes",
},
},
length: 45,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1762,15 +1822,15 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
slotInterval: 30,
requiresConfirmation: true,
metadata: {
requiresConfirmationThreshold: {
time: 120,
unit: "hours",
},
},
length: 45,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1856,8 +1916,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -1968,8 +2028,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -2076,7 +2136,7 @@ describe("handleNewBooking", () => {
id: 1,
title: "Paid Event",
description: "It's a test Paid Event",
slotInterval: 45,
slotInterval: 30,
requiresConfirmation: false,
metadata: {
apps: {
Expand All @@ -2088,7 +2148,7 @@ describe("handleNewBooking", () => {
},
},
},
length: 45,
length: 30,
users: [
{
id: 101,
Expand Down Expand Up @@ -2233,7 +2293,7 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
slotInterval: 30,
requiresConfirmation: true,
metadata: {
apps: {
Expand All @@ -2244,7 +2304,7 @@ describe("handleNewBooking", () => {
},
},
},
length: 45,
length: 30,
users: [
{
id: 101,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
recurringEvent: recurrence,
users: [
{
Expand Down Expand Up @@ -294,8 +294,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
recurringEvent: recurrence,
users: [
{
Expand Down Expand Up @@ -426,8 +426,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
recurringEvent: recurrence,
users: [
{
Expand Down Expand Up @@ -641,8 +641,8 @@ describe("handleNewBooking", () => {
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
slotInterval: 30,
length: 30,
recurringEvent: recurrence,
users: [
{
Expand Down
Loading

0 comments on commit 95e037c

Please sign in to comment.