11import { bootstrap } from "@/app" ;
22import { AppModule } from "@/app.module" ;
33import { ReassignBookingOutput_2024_08_13 } from "@/ee/bookings/2024-08-13/outputs/reassign-booking.output" ;
4+ import { BOOKING_REASSIGN_PERMISSION_ERROR } from "@/ee/bookings/2024-08-13/services/bookings.service" ;
45import { CreateScheduleInput_2024_04_15 } from "@/ee/schedules/schedules_2024_04_15/inputs/create-schedule.input" ;
56import { SchedulesModule_2024_04_15 } from "@/ee/schedules/schedules_2024_04_15/schedules.module" ;
67import { SchedulesService_2024_04_15 } from "@/ee/schedules/schedules_2024_04_15/services/schedules.service" ;
@@ -11,6 +12,7 @@ import { INestApplication } from "@nestjs/common";
1112import { NestExpressApplication } from "@nestjs/platform-express" ;
1213import { Test } from "@nestjs/testing" ;
1314import * as request from "supertest" ;
15+ import { ApiKeysRepositoryFixture } from "test/fixtures/repository/api-keys.repository.fixture" ;
1416import { BookingsRepositoryFixture } from "test/fixtures/repository/bookings.repository.fixture" ;
1517import { EventTypesRepositoryFixture } from "test/fixtures/repository/event-types.repository.fixture" ;
1618import { HostsRepositoryFixture } from "test/fixtures/repository/hosts.repository.fixture" ;
@@ -21,15 +23,11 @@ import { ProfileRepositoryFixture } from "test/fixtures/repository/profiles.repo
2123import { TeamRepositoryFixture } from "test/fixtures/repository/team.repository.fixture" ;
2224import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture" ;
2325import { randomString } from "test/utils/randomString" ;
24- import { withApiAuth } from "test/utils/withApiAuth" ;
25-
26-
2726
2827import { CAL_API_VERSION_HEADER , SUCCESS_STATUS , VERSION_2024_08_13 } from "@calcom/platform-constants" ;
2928import type { CreateBookingInput_2024_08_13 } from "@calcom/platform-types" ;
3029import type { Booking , User , PlatformOAuthClient , Team } from "@calcom/prisma/client" ;
3130
32-
3331describe ( "Bookings Endpoints 2024-08-13" , ( ) => {
3432describe ( "Reassign bookings" , ( ) => {
3533let app :INestApplication ;
@@ -47,13 +45,16 @@ describe("Bookings Endpoints 2024-08-13", () => {
4745let hostsRepositoryFixture :HostsRepositoryFixture ;
4846let organizationsRepositoryFixture :OrganizationRepositoryFixture ;
4947let profileRepositoryFixture :ProfileRepositoryFixture ;
48+ let apiKeysRepositoryFixture :ApiKeysRepositoryFixture ;
5049
5150const teamUserEmail = `reassign-bookings-2024-08-13-user1-${ randomString ( ) } @api.com` ;
5251const teamUserEmail2 = `reassign-bookings-2024-08-13-user2-${ randomString ( ) } @api.com` ;
5352const teamUserEmail3 = `reassign-bookings-2024-08-13-user3-${ randomString ( ) } @api.com` ;
5453let teamUser1 :User ;
5554let teamUser2 :User ;
5655let teamUser3 :User ;
56+ let teamUser1ApiKey :string ;
57+ let teamUser2ApiKey :string ;
5758
5859let teamRoundRobinEventTypeId :number ;
5960let teamRoundRobinFixedHostEventTypeId :number ;
@@ -69,12 +70,9 @@ describe("Bookings Endpoints 2024-08-13", () => {
6970let rescheduleReasonBookingInitialHostId :number ;
7071
7172beforeAll ( async ( ) => {
72- const moduleRef = await withApiAuth (
73- teamUserEmail ,
74- Test . createTestingModule ( {
75- imports :[ AppModule , PrismaModule , UsersModule , SchedulesModule_2024_04_15 ] ,
76- } )
77- )
73+ const moduleRef = await Test . createTestingModule ( {
74+ imports :[ AppModule , PrismaModule , UsersModule , SchedulesModule_2024_04_15 ] ,
75+ } )
7876. overrideGuard ( PermissionsGuard )
7977. useValue ( {
8078canActivate :( ) => true ,
@@ -90,6 +88,7 @@ describe("Bookings Endpoints 2024-08-13", () => {
9088profileRepositoryFixture = new ProfileRepositoryFixture ( moduleRef ) ;
9189membershipsRepositoryFixture = new MembershipRepositoryFixture ( moduleRef ) ;
9290hostsRepositoryFixture = new HostsRepositoryFixture ( moduleRef ) ;
91+ apiKeysRepositoryFixture = new ApiKeysRepositoryFixture ( moduleRef ) ;
9392schedulesService = moduleRef . get < SchedulesService_2024_04_15 > ( SchedulesService_2024_04_15 ) ;
9493
9594organization = await organizationsRepositoryFixture . create ( {
@@ -126,6 +125,12 @@ describe("Bookings Endpoints 2024-08-13", () => {
126125name :`reassign-bookings-2024-08-13-user3-${ randomString ( ) } ` ,
127126} ) ;
128127
128+ const { keyString} = await apiKeysRepositoryFixture . createApiKey ( teamUser1 . id , null ) ;
129+ teamUser1ApiKey = `cal_test_${ keyString } ` ;
130+
131+ const { keyString :keyString2 } = await apiKeysRepositoryFixture . createApiKey ( teamUser2 . id , null ) ;
132+ teamUser2ApiKey = `cal_test_${ keyString2 } ` ;
133+
129134const userSchedule :CreateScheduleInput_2024_04_15 = {
130135name :`reassign-bookings-2024-08-13-schedule-${ randomString ( ) } ` ,
131136timeZone :"Europe/Rome" ,
@@ -473,6 +478,7 @@ describe("Bookings Endpoints 2024-08-13", () => {
473478
474479return request ( app . getHttpServer ( ) )
475480. post ( `/v2/bookings/${ roundRobinBooking . uid } /reassign` )
481+ . set ( "Authorization" , `Bearer${ teamUser1ApiKey } ` )
476482. set ( CAL_API_VERSION_HEADER , VERSION_2024_08_13 )
477483. expect ( 200 )
478484. then ( async ( response ) => {
@@ -512,6 +518,7 @@ describe("Bookings Endpoints 2024-08-13", () => {
512518return request ( app . getHttpServer ( ) )
513519. post ( `/v2/bookings/${ roundRobinBooking . uid } /reassign/${ teamUser1 . id } ` )
514520. send ( body )
521+ . set ( "Authorization" , `Bearer${ teamUser1ApiKey } ` )
515522. set ( CAL_API_VERSION_HEADER , VERSION_2024_08_13 )
516523. expect ( 200 )
517524. then ( async ( response ) => {
@@ -562,6 +569,7 @@ describe("Bookings Endpoints 2024-08-13", () => {
562569
563570return request ( app . getHttpServer ( ) )
564571. post ( `/v2/bookings/${ bookingUid } /reassign` )
572+ . set ( "Authorization" , `Bearer${ teamUser2ApiKey } ` )
565573. set ( CAL_API_VERSION_HEADER , VERSION_2024_08_13 )
566574. expect ( 200 )
567575. then ( async ( response ) => {
@@ -611,6 +619,7 @@ describe("Bookings Endpoints 2024-08-13", () => {
611619
612620return request ( app . getHttpServer ( ) )
613621. post ( `/v2/bookings/${ bookingUid } /reassign/${ teamUser3 . id } ` )
622+ . set ( "Authorization" , `Bearer${ teamUser1ApiKey } ` )
614623. set ( CAL_API_VERSION_HEADER , VERSION_2024_08_13 )
615624. expect ( 200 )
616625. then ( async ( response ) => {
@@ -669,6 +678,7 @@ describe("Bookings Endpoints 2024-08-13", () => {
669678
670679return request ( app . getHttpServer ( ) )
671680. post ( `/v2/bookings/${ bookingUid } /reassign/${ reassignToHostId } ` )
681+ . set ( "Authorization" , `Bearer${ teamUser1ApiKey } ` )
672682. set ( CAL_API_VERSION_HEADER , VERSION_2024_08_13 )
673683. expect ( 200 )
674684. then ( async ( response ) => {
@@ -704,6 +714,7 @@ describe("Bookings Endpoints 2024-08-13", () => {
704714
705715return request ( app . getHttpServer ( ) )
706716. post ( `/v2/bookings/${ bookingUid } /reassign` )
717+ . set ( "Authorization" , `Bearer${ teamUser1ApiKey } ` )
707718. set ( CAL_API_VERSION_HEADER , VERSION_2024_08_13 )
708719. expect ( 200 )
709720. then ( async ( response ) => {
@@ -724,6 +735,51 @@ describe("Bookings Endpoints 2024-08-13", () => {
724735} ) ;
725736} ) ;
726737
738+ it ( "should return 403 when unauthorized user tries to reassign booking" , async ( ) => {
739+ const unauthorizedUserEmail = `fake-user-${ randomString ( ) } @api.com` ;
740+ const unauthorizedUser = await userRepositoryFixture . create ( {
741+ email :unauthorizedUserEmail ,
742+ locale :"en" ,
743+ name :`fake-user-${ randomString ( ) } ` ,
744+ } ) ;
745+
746+ const { keyString} = await apiKeysRepositoryFixture . createApiKey ( unauthorizedUser . id , null ) ;
747+ const unauthorizedApiKeyString = `cal_test_${ keyString } ` ;
748+
749+ const response = await request ( app . getHttpServer ( ) )
750+ . post ( `/v2/bookings/${ roundRobinBooking . uid } /reassign` )
751+ . set ( "Authorization" , `Bearer${ unauthorizedApiKeyString } ` )
752+ . set ( CAL_API_VERSION_HEADER , VERSION_2024_08_13 )
753+ . expect ( 403 ) ;
754+
755+ expect ( response . body . error . message ) . toBe ( BOOKING_REASSIGN_PERMISSION_ERROR ) ;
756+
757+ await userRepositoryFixture . deleteByEmail ( unauthorizedUserEmail ) ;
758+ } ) ;
759+
760+ it ( "should return 403 when unauthorized user tries to reassign booking to specific user" , async ( ) => {
761+ const unauthorizedUserEmail = `fake-user-${ randomString ( ) } @api.com` ;
762+ const unauthorizedUser = await userRepositoryFixture . create ( {
763+ email :unauthorizedUserEmail ,
764+ locale :"en" ,
765+ name :`fake-user-${ randomString ( ) } ` ,
766+ } ) ;
767+
768+ const { keyString} = await apiKeysRepositoryFixture . createApiKey ( unauthorizedUser . id , null ) ;
769+ const unauthorizedApiKeyString = `cal_test_${ keyString } ` ;
770+
771+ const response = await request ( app . getHttpServer ( ) )
772+ . post ( `/v2/bookings/${ roundRobinBooking . uid } /reassign/${ teamUser2 . id } ` )
773+ . send ( { reason :"Testing unauthorized access" } )
774+ . set ( "Authorization" , `Bearer${ unauthorizedApiKeyString } ` )
775+ . set ( CAL_API_VERSION_HEADER , VERSION_2024_08_13 )
776+ . expect ( 403 ) ;
777+
778+ expect ( response . body . error . message ) . toBe ( BOOKING_REASSIGN_PERMISSION_ERROR ) ;
779+
780+ await userRepositoryFixture . deleteByEmail ( unauthorizedUserEmail ) ;
781+ } ) ;
782+
727783async function createOAuthClient ( organizationId :number ) {
728784const data = {
729785logo :"logo-url" ,
@@ -749,4 +805,4 @@ describe("Bookings Endpoints 2024-08-13", () => {
749805await app . close ( ) ;
750806} ) ;
751807} ) ;
752- } ) ;
808+ } ) ;