feat: Add spam blocker DI structure (#24040)

* --init

* --

* replace old structure with new DI

* fix type

* --

* moving stuff around

* moving stuff around again

* minor clean up

* --

* resolve conflict

* clea up

* old schema clean up

* further clean up

* removing unwanted merged responsibilities

* --

* improve DI and SOLID

* some type fixes

* --1

* clean up --cont

* fix DI in facade for test

* more fix

* fix

* checking

* o.o

* fix import

* fix failing test --2

* fix failing test --3

* fix failing test --4

* normalization use

* introduce facade container injection

* uniform async telemetry spans

* further improvements

* replace prismock with repo mocks

* ensure we don't pass prisma outside of repo

* fix import

* remove try catch from repo calls

* async await fixes

* using deps pattern

* more clean up and fixes

* more clean up

* address feedback --1

* separation of concern

* clean up

* test clean up

* feedback --2

* remove extra fetch

* remove await

* migrate _post test from prismock

* fix type

* --

* fix tokens path

* rename AuditRepo

* test --1

* update _post to integration test

* fix test

* fix test

* test fix maybe?

* --

* feedback

* feedback

* fixes

* more feedback

* NIT

* use sentry and logger imports as planned

* assertion in test

* add missing test case

* add tests for controllers and services

* NITs

* fix domain normalisation
This commit is contained in:
Syed Ali Shahbaz authored and GitHub committed 2025-10-14 10:16:18 +03:00
1 parent 52ecea964d
commit a2bee76da6
60 files changed
+3599 -461

No files matched your search

@@ -1,8 +1,5 @@
import prismock from "../../../../tests/libs/__mocks__/prisma";
import { describe, test, expect, vi, beforeEach } from "vitest";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
import { checkIfEmailIsBlockedInWatchlistController } from "@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller";
import { hashPassword } from "@calcom/lib/auth/hashPassword";
import { CreationSource } from "@calcom/prisma/enums";
@@ -24,16 +21,18 @@ vi.mock("@calcom/lib/auth/hashPassword", () => ({
hashPassword: vi.fn().mockResolvedValue("hashed-password"),
}));
const mockUserRepository = {
create: vi.fn(),
};
vi.mock("@calcom/features/users/repositories/UserRepository", () => {
return {
UserRepository: vi.fn().mockImplementation(() => ({
create: vi.fn(),
})),
UserRepository: vi.fn().mockImplementation(() => mockUserRepository),
};
});
vi.mock("@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller", (async) => ({
checkIfEmailIsBlockedInWatchlistController: vi.fn(() => false),
vi.mock("@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller", () => ({
checkIfEmailIsBlockedInWatchlistController: vi.fn().mockResolvedValue(false),
}));
const mockUserData = {
@@ -46,7 +45,6 @@ vi.stubEnv("CALCOM_LICENSE_KEY", undefined);
describe("UserCreationService", () => {
beforeEach(() => {
prismock;
vi.clearAllMocks();
});
@@ -55,17 +53,9 @@ describe("UserCreationService", () => {
username: "test",
locked: false,
organizationId: null,
} as any);
});
const mockUserRepository = vi.mocked(UserRepository);
if (mockUserRepository && typeof mockUserRepository.mockImplementation === "function") {
mockUserRepository.mockImplementation(
() =>
({
create: mockCreate,
} as any)
);
}
mockUserRepository.create = mockCreate;
const user = await UserCreationService.createUser({ data: mockUserData });
@@ -87,17 +77,9 @@ describe("UserCreationService", () => {
username: "test",
locked: true,
organizationId: null,
} as any);
});
const mockUserRepository = vi.mocked(UserRepository);
if (mockUserRepository && typeof mockUserRepository.mockImplementation === "function") {
mockUserRepository.mockImplementation(
() =>
({
create: mockCreate,
} as any)
);
}
mockUserRepository.create = mockCreate;
const user = await UserCreationService.createUser({ data: mockUserData });
@@ -118,17 +100,9 @@ describe("UserCreationService", () => {
username: "test",
locked: false,
organizationId: null,
} as any);
});
const mockUserRepository = vi.mocked(UserRepository);
if (mockUserRepository && typeof mockUserRepository.mockImplementation === "function") {
mockUserRepository.mockImplementation(
() =>
({
create: mockCreate,
} as any)
);
}
mockUserRepository.create = mockCreate;
const user = await UserCreationService.createUser({
data: { ...mockUserData, password: mockPassword },
@@ -1,3 +1,4 @@
import { sentrySpan } from "@calcom/features/watchlist/lib/telemetry";
import { checkIfEmailIsBlockedInWatchlistController } from "@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller";
import { hashPassword } from "@calcom/lib/auth/hashPassword";
import logger from "@calcom/lib/logger";
@@ -33,7 +34,11 @@ export class UserCreationService {
static async createUser({ data }: { data: CreateUserInput }) {
const { email, password, username } = data;
const shouldLockByDefault = await checkIfEmailIsBlockedInWatchlistController(email);
const shouldLockByDefault = await checkIfEmailIsBlockedInWatchlistController({
email,
organizationId: data.organizationId ?? undefined,
span: sentrySpan,
});
const hashedPassword = password ? await hashPassword(password) : null;
@@ -48,7 +53,7 @@ export class UserCreationService {
log.info(`Created user: ${user.id} with locked status of ${user.locked}`);
const { locked, ...restUser } = user;
const { locked: _locked, ...restUser } = user;
return restUser;
}