Files
calendar/packages/features/users/services/userCreationService.test.ts
T
Syed Ali ShahbazandGitHub a2bee76da6 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
2025-10-14 10:16:18 +03:00

121 lines
3.2 KiB
TypeScript

import { describe, test, expect, vi, beforeEach } from "vitest";
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";
import { UserCreationService } from "./userCreationService";
vi.mock("@calcom/lib/server/i18n", () => {
return {
getTranslation: async (locale: string, namespace: string) => {
const t = (key: string) => key;
t.locale = locale;
t.namespace = namespace;
return t;
},
};
});
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(() => mockUserRepository),
};
});
vi.mock("@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller", () => ({
checkIfEmailIsBlockedInWatchlistController: vi.fn().mockResolvedValue(false),
}));
const mockUserData = {
email: "test@example.com",
username: "test",
creationSource: CreationSource.WEBAPP,
};
vi.stubEnv("CALCOM_LICENSE_KEY", undefined);
describe("UserCreationService", () => {
beforeEach(() => {
vi.clearAllMocks();
});
test("should create user", async () => {
const mockCreate = vi.fn().mockResolvedValue({
username: "test",
locked: false,
organizationId: null,
});
mockUserRepository.create = mockCreate;
const user = await UserCreationService.createUser({ data: mockUserData });
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
username: "test",
locked: false,
organizationId: null,
})
);
expect(user).not.toHaveProperty("locked");
});
test("should lock user when email is in watchlist", async () => {
vi.mocked(checkIfEmailIsBlockedInWatchlistController).mockResolvedValue(true);
const mockCreate = vi.fn().mockResolvedValue({
username: "test",
locked: true,
organizationId: null,
});
mockUserRepository.create = mockCreate;
const user = await UserCreationService.createUser({ data: mockUserData });
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
locked: true,
})
);
expect(user).not.toHaveProperty("locked");
});
test("should hash password when provided", async () => {
const mockPassword = "password";
vi.mocked(hashPassword).mockResolvedValue("hashed_password");
const mockCreate = vi.fn().mockResolvedValue({
username: "test",
locked: false,
organizationId: null,
});
mockUserRepository.create = mockCreate;
const user = await UserCreationService.createUser({
data: { ...mockUserData, password: mockPassword },
});
expect(hashPassword).toHaveBeenCalledWith(mockPassword);
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
hashedPassword: "hashed_password",
})
);
expect(user).not.toHaveProperty("locked");
});
});