feat: add forcedSlowMode rate limit to Team and Organization DELETE endpoints (#27258)

* feat: add forcedSlowMode rate limit to Team and Organization DELETE endpoints

Co-Authored-By: [email protected] <[email protected]>

* test: add NoOpThrottlerGuard to disable rate limiting in tests

- Create NoOpThrottlerGuard class that always allows requests through
- Create withNoThrottler helper function to override CustomThrottlerGuard
- Update teams.controller.e2e-spec.ts to use withNoThrottler
- Update organizations-teams.controller.e2e-spec.ts to use withNoThrottler
- Update organizations-organizations.controller.e2e-spec.ts to use withNoThrottler

This prevents race conditions when tests fire rapidly against rate-limited endpoints.

Co-Authored-By: [email protected] <[email protected]>

* fix: use jest.spyOn to mock throttler guard in tests

Changed approach from overrideGuard to jest.spyOn for mocking
CustomThrottlerGuard.handleRequest to always return true. This
properly disables rate limiting in tests for the DELETE endpoints.

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Morgan <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-01-27 17:24:30 +02:00
1 parent 0bcb110d20
commit 7ce1ee5cea
7 files changed
+298 -273

No files matched your search

@@ -29,6 +29,7 @@ import { OrganizationRepositoryFixture } from "test/fixtures/repository/organiza
import { ProfileRepositoryFixture } from "test/fixtures/repository/profiles.repository.fixture"; import { ProfileRepositoryFixture } from "test/fixtures/repository/profiles.repository.fixture";
import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture"; import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture";
import { randomString } from "test/utils/randomString"; import { randomString } from "test/utils/randomString";
import { mockThrottlerGuard } from "test/utils/withNoThrottler";
import { AppModule } from "@/app.module"; import { AppModule } from "@/app.module";
import { bootstrap } from "@/bootstrap"; import { bootstrap } from "@/bootstrap";
import { getEnv } from "@/env"; import { getEnv } from "@/env";
@@ -91,12 +92,14 @@ describe("Organizations Organizations Endpoints", () => {
const newDate = new Date(2035, 0, 9, 15, 0, 0); const newDate = new Date(2035, 0, 9, 15, 0, 0);
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ mockThrottlerGuard();
imports: [AppModule, PrismaModule, UsersModule, TokensModule],
}).compile();
userRepositoryFixture = new UserRepositoryFixture(moduleRef); const moduleRef = await Test.createTestingModule({
imports: [AppModule, PrismaModule, UsersModule, TokensModule],
}).compile();
userRepositoryFixture = new UserRepositoryFixture(moduleRef);
organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef); organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef);
membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef); membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
platformBillingRepositoryFixture = new PlatformBillingRepositoryFixture(moduleRef); platformBillingRepositoryFixture = new PlatformBillingRepositoryFixture(moduleRef);
@@ -1,5 +1,23 @@
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import { SkipTakePagination } from "@calcom/platform-types";
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Param,
ParseIntPipe,
Patch,
Post,
Query,
UseGuards,
} from "@nestjs/common";
import { ApiHeader, ApiOperation, ApiTags as DocsTags } from "@nestjs/swagger";
import { API_VERSIONS_VALUES } from "@/lib/api-versions"; import { API_VERSIONS_VALUES } from "@/lib/api-versions";
import { X_CAL_CLIENT_ID_HEADER, X_CAL_SECRET_KEY_HEADER } from "@/lib/docs/headers"; import { X_CAL_CLIENT_ID_HEADER, X_CAL_SECRET_KEY_HEADER } from "@/lib/docs/headers";
import { Throttle } from "@/lib/endpoint-throttler-decorator";
import { PlatformPlan } from "@/modules/auth/decorators/billing/platform-plan.decorator"; import { PlatformPlan } from "@/modules/auth/decorators/billing/platform-plan.decorator";
import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator"; import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator";
import { Roles } from "@/modules/auth/decorators/roles/roles.decorator"; import { Roles } from "@/modules/auth/decorators/roles/roles.decorator";
@@ -17,24 +35,6 @@ import { CreateManagedOrganizationOutput } from "@/modules/organizations/organiz
import { GetManagedOrganizationOutput } from "@/modules/organizations/organizations/outputs/get-managed-organization.output"; import { GetManagedOrganizationOutput } from "@/modules/organizations/organizations/outputs/get-managed-organization.output";
import { GetManagedOrganizationsOutput } from "@/modules/organizations/organizations/outputs/get-managed-organizations.output"; import { GetManagedOrganizationsOutput } from "@/modules/organizations/organizations/outputs/get-managed-organizations.output";
import { ManagedOrganizationsService } from "@/modules/organizations/organizations/services/managed-organizations.service"; import { ManagedOrganizationsService } from "@/modules/organizations/organizations/services/managed-organizations.service";
import {
Controller,
UseGuards,
Param,
ParseIntPipe,
Post,
Body,
Get,
Patch,
HttpCode,
HttpStatus,
Delete,
Query,
} from "@nestjs/common";
import { ApiHeader, ApiOperation, ApiTags as DocsTags } from "@nestjs/swagger";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import { SkipTakePagination } from "@calcom/platform-types";
const SCALE = "SCALE"; const SCALE = "SCALE";
@@ -142,6 +142,7 @@ export class OrganizationsOrganizationsController {
@Roles("ORG_ADMIN") @Roles("ORG_ADMIN")
@PlatformPlan(SCALE) @PlatformPlan(SCALE)
@Delete("/:managedOrganizationId") @Delete("/:managedOrganizationId")
@Throttle({ limit: 1, ttl: 30000, blockDuration: 30000, name: "organizations_delete" })
@ApiOperation({ @ApiOperation({
summary: "Delete an organization within an organization", summary: "Delete an organization within an organization",
description: description:
@@ -151,9 +152,8 @@ export class OrganizationsOrganizationsController {
async deleteOrganization( async deleteOrganization(
@Param("managedOrganizationId", ParseIntPipe) managedOrganizationId: number @Param("managedOrganizationId", ParseIntPipe) managedOrganizationId: number
): Promise<GetManagedOrganizationOutput> { ): Promise<GetManagedOrganizationOutput> {
const organization = await this.managedOrganizationsService.deleteManagedOrganization( const organization =
managedOrganizationId await this.managedOrganizationsService.deleteManagedOrganization(managedOrganizationId);
);
return { return {
status: SUCCESS_STATUS, status: SUCCESS_STATUS,
data: organization, data: organization,
@@ -12,6 +12,7 @@ import { TeamRepositoryFixture } from "test/fixtures/repository/team.repository.
import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture"; import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture";
import { randomString } from "test/utils/randomString"; import { randomString } from "test/utils/randomString";
import { withApiAuth } from "test/utils/withApiAuth"; import { withApiAuth } from "test/utils/withApiAuth";
import { mockThrottlerGuard } from "test/utils/withNoThrottler";
import { AppModule } from "@/app.module"; import { AppModule } from "@/app.module";
import { bootstrap } from "@/bootstrap"; import { bootstrap } from "@/bootstrap";
import { CreateOrgTeamDto } from "@/modules/organizations/teams/index/inputs/create-organization-team.input"; import { CreateOrgTeamDto } from "@/modules/organizations/teams/index/inputs/create-organization-team.input";
@@ -39,42 +40,44 @@ describe("Organizations Team Endpoints", () => {
const userEmail = `organizations-teams-admin-${randomString()}@api.com`; const userEmail = `organizations-teams-admin-${randomString()}@api.com`;
let user: User; let user: User;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await withApiAuth( mockThrottlerGuard();
userEmail,
Test.createTestingModule({
imports: [AppModule, PrismaModule, UsersModule, TokensModule],
})
).compile();
userRepositoryFixture = new UserRepositoryFixture(moduleRef); const moduleRef = await withApiAuth(
organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef); userEmail,
teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef); Test.createTestingModule({
membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef); imports: [AppModule, PrismaModule, UsersModule, TokensModule],
})
).compile();
user = await userRepositoryFixture.create({ userRepositoryFixture = new UserRepositoryFixture(moduleRef);
email: userEmail, organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef);
username: userEmail, teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef);
}); membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
org = await organizationsRepositoryFixture.create({ user = await userRepositoryFixture.create({
name: `organizations-teams-organization-${randomString()}`, email: userEmail,
isOrganization: true, username: userEmail,
}); });
await membershipsRepositoryFixture.create({ org = await organizationsRepositoryFixture.create({
role: "ADMIN", name: `organizations-teams-organization-${randomString()}`,
user: { connect: { id: user.id } }, isOrganization: true,
team: { connect: { id: org.id } }, });
});
team = await teamsRepositoryFixture.create({ await membershipsRepositoryFixture.create({
name: `organizations-teams-team1-${randomString()}`, role: "ADMIN",
isOrganization: false, user: { connect: { id: user.id } },
parent: { connect: { id: org.id } }, team: { connect: { id: org.id } },
}); });
team2 = await teamsRepositoryFixture.create({ team = await teamsRepositoryFixture.create({
name: `organizations-teams-team1-${randomString()}`,
isOrganization: false,
parent: { connect: { id: org.id } },
});
team2 = await teamsRepositoryFixture.create({
name: `organizations-teams-team2-${randomString()}`, name: `organizations-teams-team2-${randomString()}`,
isOrganization: false, isOrganization: false,
parent: { connect: { id: org.id } }, parent: { connect: { id: org.id } },
@@ -300,93 +303,95 @@ describe("Organizations Team Endpoints", () => {
const userEmail = `organizations-teams-member-${randomString()}@api.com`; const userEmail = `organizations-teams-member-${randomString()}@api.com`;
let user: User; let user: User;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await withApiAuth( mockThrottlerGuard();
userEmail,
Test.createTestingModule({
imports: [AppModule, PrismaModule, UsersModule, TokensModule],
})
).compile();
userRepositoryFixture = new UserRepositoryFixture(moduleRef); const moduleRef = await withApiAuth(
organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef); userEmail,
teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef); Test.createTestingModule({
membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef); imports: [AppModule, PrismaModule, UsersModule, TokensModule],
})
).compile();
user = await userRepositoryFixture.create({ userRepositoryFixture = new UserRepositoryFixture(moduleRef);
email: userEmail, organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef);
username: userEmail, teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef);
}); membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
org = await organizationsRepositoryFixture.create({ user = await userRepositoryFixture.create({
name: `organizations-teams-organization-${randomString()}`, email: userEmail,
isOrganization: true, username: userEmail,
}); });
await membershipsRepositoryFixture.create({ org = await organizationsRepositoryFixture.create({
role: "MEMBER", name: `organizations-teams-organization-${randomString()}`,
user: { connect: { id: user.id } }, isOrganization: true,
team: { connect: { id: org.id } }, });
});
team = await teamsRepositoryFixture.create({ await membershipsRepositoryFixture.create({
name: `organizations-teams-team1-${randomString()}`, role: "MEMBER",
isOrganization: false, user: { connect: { id: user.id } },
parent: { connect: { id: org.id } }, team: { connect: { id: org.id } },
}); });
team2 = await teamsRepositoryFixture.create({ team = await teamsRepositoryFixture.create({
name: `organizations-teams-team2-${randomString()}`, name: `organizations-teams-team1-${randomString()}`,
isOrganization: false, isOrganization: false,
parent: { connect: { id: org.id } }, parent: { connect: { id: org.id } },
}); });
app = moduleRef.createNestApplication(); team2 = await teamsRepositoryFixture.create({
bootstrap(app as NestExpressApplication); name: `organizations-teams-team2-${randomString()}`,
isOrganization: false,
parent: { connect: { id: org.id } },
});
await app.init(); app = moduleRef.createNestApplication();
}); bootstrap(app as NestExpressApplication);
it("should be defined", () => { await app.init();
expect(userRepositoryFixture).toBeDefined(); });
expect(teamsRepositoryFixture).toBeDefined();
expect(user).toBeDefined();
expect(org).toBeDefined();
});
it("should deny get all the teams of the org", async () => { it("should be defined", () => {
return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams`).expect(403); expect(userRepositoryFixture).toBeDefined();
}); expect(teamsRepositoryFixture).toBeDefined();
expect(user).toBeDefined();
expect(org).toBeDefined();
});
it("should deny get all the teams of the org paginated", async () => { it("should deny get all the teams of the org", async () => {
return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams?skip=1&take=1`).expect(403); return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams`).expect(403);
}); });
it("should deny get the team of the org", async () => { it("should deny get all the teams of the org paginated", async () => {
return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams/${team.id}`).expect(403); return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams?skip=1&take=1`).expect(403);
}); });
it("should deny create the team of the org", async () => { it("should deny get the team of the org", async () => {
return request(app.getHttpServer()) return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams/${team.id}`).expect(403);
.post(`/v2/organizations/${org.id}/teams`) });
.send({
name: `organizations-teams-api-team1-${randomString()}`,
} satisfies CreateOrgTeamDto)
.expect(403);
});
it("should deny update the team of the org", async () => { it("should deny create the team of the org", async () => {
return request(app.getHttpServer()) return request(app.getHttpServer())
.patch(`/v2/organizations/${org.id}/teams/${team.id}`) .post(`/v2/organizations/${org.id}/teams`)
.send({ .send({
name: `organizations-teams-api-team1-${randomString()}-updated`, name: `organizations-teams-api-team1-${randomString()}`,
} satisfies CreateOrgTeamDto) } satisfies CreateOrgTeamDto)
.expect(403); .expect(403);
}); });
it("should deny delete the team of the org we created via api", async () => { it("should deny update the team of the org", async () => {
return request(app.getHttpServer()).delete(`/v2/organizations/${org.id}/teams/${team2.id}`).expect(403); return request(app.getHttpServer())
}); .patch(`/v2/organizations/${org.id}/teams/${team.id}`)
.send({
name: `organizations-teams-api-team1-${randomString()}-updated`,
} satisfies CreateOrgTeamDto)
.expect(403);
});
it("should deny delete the team of the org we created via api", async () => {
return request(app.getHttpServer()).delete(`/v2/organizations/${org.id}/teams/${team2.id}`).expect(403);
});
afterAll(async () => { afterAll(async () => {
await userRepositoryFixture.deleteByEmail(user.email); await userRepositoryFixture.deleteByEmail(user.email);
@@ -414,105 +419,107 @@ describe("Organizations Team Endpoints", () => {
const userEmail = `organizations-teams-owner-${randomString()}@api.com`; const userEmail = `organizations-teams-owner-${randomString()}@api.com`;
let user: User; let user: User;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await withApiAuth( mockThrottlerGuard();
userEmail,
Test.createTestingModule({
imports: [AppModule, PrismaModule, UsersModule, TokensModule],
})
).compile();
userRepositoryFixture = new UserRepositoryFixture(moduleRef); const moduleRef = await withApiAuth(
organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef); userEmail,
teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef); Test.createTestingModule({
membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef); imports: [AppModule, PrismaModule, UsersModule, TokensModule],
})
).compile();
user = await userRepositoryFixture.create({ userRepositoryFixture = new UserRepositoryFixture(moduleRef);
email: userEmail, organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef);
username: userEmail, teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef);
}); membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
org = await organizationsRepositoryFixture.create({ user = await userRepositoryFixture.create({
name: `organizations-teams-organization-${randomString()}`, email: userEmail,
isOrganization: true, username: userEmail,
}); });
await membershipsRepositoryFixture.create({ org = await organizationsRepositoryFixture.create({
role: "MEMBER", name: `organizations-teams-organization-${randomString()}`,
user: { connect: { id: user.id } }, isOrganization: true,
team: { connect: { id: org.id } }, });
});
team = await teamsRepositoryFixture.create({ await membershipsRepositoryFixture.create({
name: `organizations-teams-team1-${randomString()}`, role: "MEMBER",
isOrganization: false, user: { connect: { id: user.id } },
parent: { connect: { id: org.id } }, team: { connect: { id: org.id } },
}); });
team2 = await teamsRepositoryFixture.create({ team = await teamsRepositoryFixture.create({
name: `organizations-teams-team2-${randomString()}`, name: `organizations-teams-team1-${randomString()}`,
isOrganization: false, isOrganization: false,
parent: { connect: { id: org.id } }, parent: { connect: { id: org.id } },
}); });
await membershipsRepositoryFixture.create({ team2 = await teamsRepositoryFixture.create({
role: "OWNER", name: `organizations-teams-team2-${randomString()}`,
user: { connect: { id: user.id } }, isOrganization: false,
team: { connect: { id: team.id } }, parent: { connect: { id: org.id } },
}); });
await membershipsRepositoryFixture.create({ await membershipsRepositoryFixture.create({
role: "OWNER", role: "OWNER",
user: { connect: { id: user.id } }, user: { connect: { id: user.id } },
team: { connect: { id: team2.id } }, team: { connect: { id: team.id } },
}); });
app = moduleRef.createNestApplication(); await membershipsRepositoryFixture.create({
bootstrap(app as NestExpressApplication); role: "OWNER",
user: { connect: { id: user.id } },
team: { connect: { id: team2.id } },
});
await app.init(); app = moduleRef.createNestApplication();
}); bootstrap(app as NestExpressApplication);
it("should be defined", () => { await app.init();
expect(userRepositoryFixture).toBeDefined(); });
expect(organizationsRepositoryFixture).toBeDefined();
expect(user).toBeDefined();
expect(org).toBeDefined();
});
it("should deny get all the teams of the org", async () => { it("should be defined", () => {
return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams`).expect(403); expect(userRepositoryFixture).toBeDefined();
}); expect(organizationsRepositoryFixture).toBeDefined();
expect(user).toBeDefined();
expect(org).toBeDefined();
});
it("should deny get all the teams of the org paginated", async () => { it("should deny get all the teams of the org", async () => {
return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams?skip=1&take=1`).expect(403); return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams`).expect(403);
}); });
it("should get the team of the org for which the user is team owner", async () => { it("should deny get all the teams of the org paginated", async () => {
return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams/${team.id}`).expect(200); return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams?skip=1&take=1`).expect(403);
}); });
it("should deny create the team of the org", async () => { it("should get the team of the org for which the user is team owner", async () => {
return request(app.getHttpServer()) return request(app.getHttpServer()).get(`/v2/organizations/${org.id}/teams/${team.id}`).expect(200);
.post(`/v2/organizations/${org.id}/teams`) });
.send({
name: `organizations-teams-api-team1-${randomString()}`,
} satisfies CreateOrgTeamDto)
.expect(403);
});
it("should deny update the team of the org", async () => { it("should deny create the team of the org", async () => {
return request(app.getHttpServer()) return request(app.getHttpServer())
.patch(`/v2/organizations/${org.id}/teams/${team.id}`) .post(`/v2/organizations/${org.id}/teams`)
.send({ .send({
name: `organizations-teams-api-team1-${randomString()}-updated`, name: `organizations-teams-api-team1-${randomString()}`,
} satisfies CreateOrgTeamDto) } satisfies CreateOrgTeamDto)
.expect(403); .expect(403);
}); });
it("should deny delete the team of the org we created via api", async () => { it("should deny update the team of the org", async () => {
return request(app.getHttpServer()).delete(`/v2/organizations/${org.id}/teams/${team2.id}`).expect(403); return request(app.getHttpServer())
}); .patch(`/v2/organizations/${org.id}/teams/${team.id}`)
.send({
name: `organizations-teams-api-team1-${randomString()}-updated`,
} satisfies CreateOrgTeamDto)
.expect(403);
});
it("should deny delete the team of the org we created via api", async () => {
return request(app.getHttpServer()).delete(`/v2/organizations/${org.id}/teams/${team2.id}`).expect(403);
});
afterAll(async () => { afterAll(async () => {
await userRepositoryFixture.deleteByEmail(user.email); await userRepositoryFixture.deleteByEmail(user.email);
@@ -544,43 +551,45 @@ describe("Organizations Team Endpoints", () => {
const userEmail = `organizations-teams-platform-owner-${randomString()}@api.com`; const userEmail = `organizations-teams-platform-owner-${randomString()}@api.com`;
let user: User; let user: User;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await withApiAuth( mockThrottlerGuard();
userEmail,
Test.createTestingModule({
imports: [AppModule, PrismaModule, UsersModule, TokensModule],
})
).compile();
userRepositoryFixture = new UserRepositoryFixture(moduleRef); const moduleRef = await withApiAuth(
teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef); userEmail,
membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef); Test.createTestingModule({
oauthClientRepositoryFixture = new OAuthClientRepositoryFixture(moduleRef); imports: [AppModule, PrismaModule, UsersModule, TokensModule],
orgRepositoryFixture = new OrganizationRepositoryFixture(moduleRef); })
user = await userRepositoryFixture.create({ ).compile();
email: userEmail,
username: userEmail,
});
org = await orgRepositoryFixture.create({ userRepositoryFixture = new UserRepositoryFixture(moduleRef);
name: `organizations-teams-platform-organization-${randomString()}`, teamsRepositoryFixture = new TeamRepositoryFixture(moduleRef);
isOrganization: true, membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
}); oauthClientRepositoryFixture = new OAuthClientRepositoryFixture(moduleRef);
orgRepositoryFixture = new OrganizationRepositoryFixture(moduleRef);
user = await userRepositoryFixture.create({
email: userEmail,
username: userEmail,
});
await membershipsRepositoryFixture.create({ org = await orgRepositoryFixture.create({
role: "ADMIN", name: `organizations-teams-platform-organization-${randomString()}`,
user: { connect: { id: user.id } }, isOrganization: true,
team: { connect: { id: org.id } }, });
});
oAuthClient1 = await createOAuthClient(org.id); await membershipsRepositoryFixture.create({
oAuthClient2 = await createOAuthClient(org.id); role: "ADMIN",
user: { connect: { id: user.id } },
team: { connect: { id: org.id } },
});
app = moduleRef.createNestApplication(); oAuthClient1 = await createOAuthClient(org.id);
bootstrap(app as NestExpressApplication); oAuthClient2 = await createOAuthClient(org.id);
await app.init(); app = moduleRef.createNestApplication();
}); bootstrap(app as NestExpressApplication);
await app.init();
});
async function createOAuthClient(organizationId: number) { async function createOAuthClient(organizationId: number) {
const data = { const data = {
@@ -1,9 +1,29 @@
import { SUCCESS_STATUS, X_CAL_CLIENT_ID } from "@calcom/platform-constants";
import { OrgTeamOutputDto, SkipTakePagination } from "@calcom/platform-types";
import type { Team } from "@calcom/prisma/client";
import {
Body,
Controller,
Delete,
Get,
Param,
ParseIntPipe,
Patch,
Post,
Query,
Req,
UseGuards,
} from "@nestjs/common";
import { ApiHeader, ApiOperation, ApiTags as DocsTags } from "@nestjs/swagger";
import { plainToClass } from "class-transformer";
import { Request } from "express";
import { API_VERSIONS_VALUES } from "@/lib/api-versions"; import { API_VERSIONS_VALUES } from "@/lib/api-versions";
import { import {
OPTIONAL_API_KEY_HEADER, OPTIONAL_API_KEY_HEADER,
OPTIONAL_X_CAL_CLIENT_ID_HEADER, OPTIONAL_X_CAL_CLIENT_ID_HEADER,
OPTIONAL_X_CAL_SECRET_KEY_HEADER, OPTIONAL_X_CAL_SECRET_KEY_HEADER,
} from "@/lib/docs/headers"; } from "@/lib/docs/headers";
import { Throttle } from "@/lib/endpoint-throttler-decorator";
import { PlatformPlan } from "@/modules/auth/decorators/billing/platform-plan.decorator"; import { PlatformPlan } from "@/modules/auth/decorators/billing/platform-plan.decorator";
import { GetTeam } from "@/modules/auth/decorators/get-team/get-team.decorator"; import { GetTeam } from "@/modules/auth/decorators/get-team/get-team.decorator";
import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator"; import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator";
@@ -25,26 +45,6 @@ import {
} from "@/modules/organizations/teams/index/outputs/organization-team.output"; } from "@/modules/organizations/teams/index/outputs/organization-team.output";
import { OrganizationsTeamsService } from "@/modules/organizations/teams/index/services/organizations-teams.service"; import { OrganizationsTeamsService } from "@/modules/organizations/teams/index/services/organizations-teams.service";
import { UserWithProfile } from "@/modules/users/users.repository"; import { UserWithProfile } from "@/modules/users/users.repository";
import {
Controller,
UseGuards,
Get,
Param,
ParseIntPipe,
Query,
Delete,
Patch,
Post,
Body,
Req,
} from "@nestjs/common";
import { ApiHeader, ApiOperation, ApiTags as DocsTags } from "@nestjs/swagger";
import { plainToClass } from "class-transformer";
import { Request } from "express";
import { SUCCESS_STATUS, X_CAL_CLIENT_ID } from "@calcom/platform-constants";
import { OrgTeamOutputDto, SkipTakePagination } from "@calcom/platform-types";
import type { Team } from "@calcom/prisma/client";
@Controller({ @Controller({
path: "/v2/organizations/:orgId/teams", path: "/v2/organizations/:orgId/teams",
@@ -121,6 +121,7 @@ export class OrganizationsTeamsController {
@Roles("ORG_ADMIN") @Roles("ORG_ADMIN")
@PlatformPlan("ESSENTIALS") @PlatformPlan("ESSENTIALS")
@Delete("/:teamId") @Delete("/:teamId")
@Throttle({ limit: 1, ttl: 30000, blockDuration: 30000, name: "org_teams_delete" })
@ApiOperation({ summary: "Delete a team" }) @ApiOperation({ summary: "Delete a team" })
async deleteTeam( async deleteTeam(
@Param("orgId", ParseIntPipe) orgId: number, @Param("orgId", ParseIntPipe) orgId: number,
@@ -12,6 +12,7 @@ import { MembershipRepositoryFixture } from "test/fixtures/repository/membership
import { TeamRepositoryFixture } from "test/fixtures/repository/team.repository.fixture"; import { TeamRepositoryFixture } from "test/fixtures/repository/team.repository.fixture";
import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture"; import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture";
import { randomString } from "test/utils/randomString"; import { randomString } from "test/utils/randomString";
import { mockThrottlerGuard } from "test/utils/withNoThrottler";
import { AppModule } from "@/app.module"; import { AppModule } from "@/app.module";
import { bootstrap } from "@/bootstrap"; import { bootstrap } from "@/bootstrap";
import { StripeService } from "@/modules/stripe/stripe.service"; import { StripeService } from "@/modules/stripe/stripe.service";
@@ -40,12 +41,14 @@ describe("Teams endpoint", () => {
let team1: TeamOutputDto; let team1: TeamOutputDto;
let team2: TeamOutputDto; let team2: TeamOutputDto;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ mockThrottlerGuard();
imports: [AppModule, TeamsModule],
}).compile();
jest.spyOn(StripeService.prototype, "getStripe").mockImplementation(() => ({}) as unknown as Stripe); const moduleRef = await Test.createTestingModule({
imports: [AppModule, TeamsModule],
}).compile();
jest.spyOn(StripeService.prototype, "getStripe").mockImplementation(() => ({}) as unknown as Stripe);
userRepositoryFixture = new UserRepositoryFixture(moduleRef); userRepositoryFixture = new UserRepositoryFixture(moduleRef);
teamRepositoryFixture = new TeamRepositoryFixture(moduleRef); teamRepositoryFixture = new TeamRepositoryFixture(moduleRef);
@@ -1,5 +1,11 @@
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import { TeamOutputDto } from "@calcom/platform-types";
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, UseGuards } from "@nestjs/common";
import { ApiHeader, ApiOperation, ApiTags as DocsTags } from "@nestjs/swagger";
import { plainToClass } from "class-transformer";
import { API_VERSIONS_VALUES } from "@/lib/api-versions"; import { API_VERSIONS_VALUES } from "@/lib/api-versions";
import { API_KEY_HEADER } from "@/lib/docs/headers"; import { API_KEY_HEADER } from "@/lib/docs/headers";
import { Throttle } from "@/lib/endpoint-throttler-decorator";
import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator"; import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator";
import { Roles } from "@/modules/auth/decorators/roles/roles.decorator"; import { Roles } from "@/modules/auth/decorators/roles/roles.decorator";
import { ApiAuthGuard } from "@/modules/auth/guards/api-auth/api-auth.guard"; import { ApiAuthGuard } from "@/modules/auth/guards/api-auth/api-auth.guard";
@@ -14,12 +20,6 @@ import { UpdateTeamOutput } from "@/modules/teams/teams/outputs/teams/update-tea
import { TeamsService } from "@/modules/teams/teams/services/teams.service"; import { TeamsService } from "@/modules/teams/teams/services/teams.service";
import { TeamsRepository } from "@/modules/teams/teams/teams.repository"; import { TeamsRepository } from "@/modules/teams/teams/teams.repository";
import { UserWithProfile } from "@/modules/users/users.repository"; import { UserWithProfile } from "@/modules/users/users.repository";
import { Controller, UseGuards, Get, Param, ParseIntPipe, Delete, Patch, Post, Body } from "@nestjs/common";
import { ApiHeader, ApiOperation, ApiTags as DocsTags } from "@nestjs/swagger";
import { plainToClass } from "class-transformer";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import { TeamOutputDto } from "@calcom/platform-types";
@Controller({ @Controller({
path: "/v2/teams", path: "/v2/teams",
@@ -29,7 +29,10 @@ import { TeamOutputDto } from "@calcom/platform-types";
@DocsTags("Teams") @DocsTags("Teams")
@ApiHeader(API_KEY_HEADER) @ApiHeader(API_KEY_HEADER)
export class TeamsController { export class TeamsController {
constructor(private teamsService: TeamsService, private teamsRepository: TeamsRepository) {} constructor(
private teamsService: TeamsService,
private teamsRepository: TeamsRepository
) {}
@Post() @Post()
@ApiOperation({ summary: "Create a team" }) @ApiOperation({ summary: "Create a team" })
@@ -95,6 +98,7 @@ export class TeamsController {
@UseGuards(RolesGuard) @UseGuards(RolesGuard)
@Delete("/:teamId") @Delete("/:teamId")
@Throttle({ limit: 1, ttl: 30000, blockDuration: 30000, name: "teams_delete" })
@ApiOperation({ summary: "Delete a team" }) @ApiOperation({ summary: "Delete a team" })
@Roles("TEAM_OWNER") @Roles("TEAM_OWNER")
async deleteTeam(@Param("teamId", ParseIntPipe) teamId: number): Promise<OrgTeamOutputResponseDto> { async deleteTeam(@Param("teamId", ParseIntPipe) teamId: number): Promise<OrgTeamOutputResponseDto> {
@@ -0,0 +1,5 @@
import { CustomThrottlerGuard } from "@/lib/throttler-guard";
export const mockThrottlerGuard = (): void => {
jest.spyOn(CustomThrottlerGuard.prototype, "handleRequest").mockResolvedValue(true);
};