|
| 1 | +import { bootstrap } from "@/app"; |
| 2 | +import { AppModule } from "@/app.module"; |
| 3 | +import { HttpExceptionFilter } from "@/filters/http-exception.filter"; |
| 4 | +import { PrismaExceptionFilter } from "@/filters/prisma-exception.filter"; |
| 5 | +import { ApiAuthGuard } from "@/modules/auth/guards/api-auth/api-auth.guard"; |
| 6 | +import { PlatformPlanGuard } from "@/modules/auth/guards/billing/platform-plan.guard"; |
| 7 | +import { IsAdminAPIEnabledGuard } from "@/modules/auth/guards/organizations/is-admin-api-enabled.guard"; |
| 8 | +import { RolesGuard } from "@/modules/auth/guards/roles/roles.guard"; |
| 9 | +import { TokensModule } from "@/modules/tokens/tokens.module"; |
| 10 | +import { UsersModule } from "@/modules/users/users.module"; |
| 11 | +import { INestApplication } from "@nestjs/common"; |
| 12 | +import { NestExpressApplication } from "@nestjs/platform-express"; |
| 13 | +import { Test } from "@nestjs/testing"; |
| 14 | +import * as request from "supertest"; |
| 15 | +import { EventTypesRepositoryFixture } from "test/fixtures/repository/event-types.repository.fixture"; |
| 16 | +import { OrganizationRepositoryFixture } from "test/fixtures/repository/organization.repository.fixture"; |
| 17 | +import { TeamRepositoryFixture } from "test/fixtures/repository/team.repository.fixture"; |
| 18 | +import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture"; |
| 19 | +import { randomString } from "test/utils/randomString"; |
| 20 | +import { withApiAuth } from "test/utils/withApiAuth"; |
| 21 | + |
| 22 | +import { SUCCESS_STATUS } from "@calcom/platform-constants"; |
| 23 | +import { CreatePrivateLinkInput } from "@calcom/platform-types"; |
| 24 | + |
| 25 | +describe("Organizations / Teams / Event Types / Private Links Endpoints", () => { |
| 26 | + let app: INestApplication; |
| 27 | + |
| 28 | + let orgFixture: OrganizationRepositoryFixture; |
| 29 | + let teamFixture: TeamRepositoryFixture; |
| 30 | + let userFixture: UserRepositoryFixture; |
| 31 | + let eventTypesFixture: EventTypesRepositoryFixture; |
| 32 | + |
| 33 | + let org: any; |
| 34 | + let team: any; |
| 35 | + let user: any; |
| 36 | + let eventType: any; |
| 37 | + |
| 38 | + const userEmail = `org-private-links-user-${randomString()}@api.com`; |
| 39 | + |
| 40 | + beforeAll(async () => { |
| 41 | + const testingModuleBuilder = withApiAuth( |
| 42 | + userEmail, |
| 43 | + Test.createTestingModule({ |
| 44 | + providers: [PrismaExceptionFilter, HttpExceptionFilter], |
| 45 | + imports: [AppModule, UsersModule, TokensModule], |
| 46 | + }) |
| 47 | + ) |
| 48 | + // Bypass org admin plan and admin API checks and roles in this e2e |
| 49 | + .overrideGuard(PlatformPlanGuard) |
| 50 | + .useValue({ canActivate: () => true }) |
| 51 | + .overrideGuard(IsAdminAPIEnabledGuard) |
| 52 | + .useValue({ canActivate: () => true }) |
| 53 | + .overrideGuard(RolesGuard) |
| 54 | + .useValue({ canActivate: () => true }) |
| 55 | + // Keep IsOrgGuard and IsTeamInOrg to validate org/team path integrity |
| 56 | + .overrideGuard(ApiAuthGuard) |
| 57 | + .useValue({ canActivate: () => true }); |
| 58 | + |
| 59 | + const moduleRef = await testingModuleBuilder.compile(); |
| 60 | + |
| 61 | + app = moduleRef.createNestApplication(); |
| 62 | + bootstrap(app as NestExpressApplication); |
| 63 | + |
| 64 | + orgFixture = new OrganizationRepositoryFixture(moduleRef); |
| 65 | + teamFixture = new TeamRepositoryFixture(moduleRef); |
| 66 | + userFixture = new UserRepositoryFixture(moduleRef); |
| 67 | + eventTypesFixture = new EventTypesRepositoryFixture(moduleRef); |
| 68 | + |
| 69 | + user = await userFixture.create({ |
| 70 | + email: userEmail, |
| 71 | + username: `org-private-links-user-${randomString()}`, |
| 72 | + name: "Test User", |
| 73 | + }); |
| 74 | + |
| 75 | + org = await orgFixture.create({ |
| 76 | + name: `org-private-links-org-${randomString()}`, |
| 77 | + slug: `org-private-links-org-${randomString()}`, |
| 78 | + isOrganization: true, |
| 79 | + }); |
| 80 | + |
| 81 | + team = await teamFixture.create({ |
| 82 | + name: `org-private-links-team-${randomString()}`, |
| 83 | + isOrganization: false, |
| 84 | + parent: { connect: { id: org.id } }, |
| 85 | + }); |
| 86 | + |
| 87 | + // Create a team-owned event type |
| 88 | + eventType = await eventTypesFixture.createTeamEventType({ |
| 89 | + title: `org-private-links-event-type-${randomString()}`, |
| 90 | + slug: `org-private-links-event-type-${randomString()}`, |
| 91 | + length: 30, |
| 92 | + locations: [], |
| 93 | + team: { connect: { id: team.id } }, |
| 94 | + }); |
| 95 | + |
| 96 | + await app.init(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("POST /v2/organizations/:orgId/teams/:teamId/event-types/:eventTypeId/private-links - create", async () => { |
| 100 | + const body: CreatePrivateLinkInput = { maxUsageCount: 5 }; |
| 101 | + const response = await request(app.getHttpServer()) |
| 102 | + .post(`/v2/organizations/${org.id}/teams/${team.id}/event-types/${eventType.id}/private-links`) |
| 103 | + .set("Authorization", "Bearer test") |
| 104 | + .send(body) |
| 105 | + .expect(201); |
| 106 | + |
| 107 | + expect(response.body.status).toBe(SUCCESS_STATUS); |
| 108 | + expect(response.body.data.linkId).toBeDefined(); |
| 109 | + expect(response.body.data.maxUsageCount).toBe(5); |
| 110 | + expect(response.body.data.usageCount).toBeDefined(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("GET /v2/organizations/:orgId/teams/:teamId/event-types/:eventTypeId/private-links - list", async () => { |
| 114 | + const response = await request(app.getHttpServer()) |
| 115 | + .get(`/v2/organizations/${org.id}/teams/${team.id}/event-types/${eventType.id}/private-links`) |
| 116 | + .set("Authorization", "Bearer test") |
| 117 | + .expect(200); |
| 118 | + |
| 119 | + expect(response.body.status).toBe(SUCCESS_STATUS); |
| 120 | + expect(Array.isArray(response.body.data)).toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it("PATCH /v2/organizations/:orgId/teams/:teamId/event-types/:eventTypeId/private-links/:linkId - update", async () => { |
| 124 | + // create first |
| 125 | + const createResp = await request(app.getHttpServer()) |
| 126 | + .post(`/v2/organizations/${org.id}/teams/${team.id}/event-types/${eventType.id}/private-links`) |
| 127 | + .set("Authorization", "Bearer test") |
| 128 | + .send({ maxUsageCount: 3 }) |
| 129 | + .expect(201); |
| 130 | + |
| 131 | + const linkId = createResp.body.data.linkId as string; |
| 132 | + |
| 133 | + const response = await request(app.getHttpServer()) |
| 134 | + .patch( |
| 135 | + `/v2/organizations/${org.id}/teams/${team.id}/event-types/${eventType.id}/private-links/${linkId}` |
| 136 | + ) |
| 137 | + .set("Authorization", "Bearer test") |
| 138 | + .send({ maxUsageCount: 10 }) |
| 139 | + .expect(200); |
| 140 | + |
| 141 | + expect(response.body.status).toBe(SUCCESS_STATUS); |
| 142 | + expect(response.body.data.maxUsageCount).toBe(10); |
| 143 | + }); |
| 144 | + |
| 145 | + it("DELETE /v2/organizations/:orgId/teams/:teamId/event-types/:eventTypeId/private-links/:linkId - delete", async () => { |
| 146 | + // create first |
| 147 | + const createResp = await request(app.getHttpServer()) |
| 148 | + .post(`/v2/organizations/${org.id}/teams/${team.id}/event-types/${eventType.id}/private-links`) |
| 149 | + .set("Authorization", "Bearer test") |
| 150 | + .send({ maxUsageCount: 2 }) |
| 151 | + .expect(201); |
| 152 | + |
| 153 | + const linkId = createResp.body.data.linkId as string; |
| 154 | + |
| 155 | + const response = await request(app.getHttpServer()) |
| 156 | + .delete( |
| 157 | + `/v2/organizations/${org.id}/teams/${team.id}/event-types/${eventType.id}/private-links/${linkId}` |
| 158 | + ) |
| 159 | + .set("Authorization", "Bearer test") |
| 160 | + .expect(200); |
| 161 | + |
| 162 | + expect(response.body.status).toBe(SUCCESS_STATUS); |
| 163 | + expect(response.body.data.linkId).toBe(linkId); |
| 164 | + }); |
| 165 | + |
| 166 | + afterAll(async () => { |
| 167 | + try { |
| 168 | + if (eventType?.id) { |
| 169 | + await eventTypesFixture.delete(eventType.id); |
| 170 | + } |
| 171 | + if (team?.id) { |
| 172 | + await teamFixture.delete(team.id); |
| 173 | + } |
| 174 | + if (org?.id) { |
| 175 | + await orgFixture.delete(org.id); |
| 176 | + } |
| 177 | + if (user?.email) { |
| 178 | + await userFixture.deleteByEmail(user.email); |
| 179 | + } |
| 180 | + } catch {} |
| 181 | + await app.close(); |
| 182 | + }); |
| 183 | +}); |
0 commit comments