Files
calendar/packages/features/watchlist/operations/check-if-email-in-watchlist.controller.test.ts
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

248 lines
8.1 KiB
TypeScript

import { describe, test, expect, vi, beforeEach } from "vitest";
import { WatchlistType } from "@calcom/prisma/enums";
import { WatchlistFeature } from "../lib/facade/WatchlistFeature";
import type { SpanFn } from "../lib/telemetry";
import { checkIfEmailIsBlockedInWatchlistController } from "./check-if-email-in-watchlist.controller";
// Mock the DI container
vi.mock("@calcom/features/di/watchlist/containers/watchlist", () => ({
getWatchlistFeature: vi.fn(),
}));
const mockGlobalBlocking = {
isBlocked: vi.fn(),
};
const mockOrgBlocking = {
isBlocked: vi.fn(),
};
const mockWatchlistFeature = {
globalBlocking: mockGlobalBlocking,
orgBlocking: mockOrgBlocking,
watchlist: {},
audit: {},
};
describe("checkIfEmailIsBlockedInWatchlistController", () => {
beforeEach(async () => {
vi.clearAllMocks();
const { getWatchlistFeature } = await import("@calcom/features/di/watchlist/containers/watchlist");
vi.mocked(getWatchlistFeature).mockResolvedValue(mockWatchlistFeature as WatchlistFeature);
});
describe("Global blocking only", () => {
test("should return false when email not blocked globally", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
});
expect(result).toBe(false);
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalledWith("[email protected]");
expect(mockOrgBlocking.isBlocked).not.toHaveBeenCalled();
});
test("should return true when email blocked globally", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({
isBlocked: true,
reason: WatchlistType.EMAIL,
watchlistEntry: { id: "123" },
});
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
});
expect(result).toBe(true);
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalledWith("[email protected]");
expect(mockOrgBlocking.isBlocked).not.toHaveBeenCalled(); // Short-circuit
});
test("should return true when domain blocked globally", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({
isBlocked: true,
reason: WatchlistType.DOMAIN,
watchlistEntry: { id: "456" },
});
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
});
expect(result).toBe(true);
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalledWith("[email protected]");
});
test("should normalize email before checking", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
});
// Email should be normalized by the time it reaches the service
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalledWith("[email protected]");
});
});
describe("Organization-specific blocking", () => {
test("should check org blocklist when organizationId provided", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
mockOrgBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
organizationId: 123,
});
expect(result).toBe(false);
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalledWith("[email protected]");
expect(mockOrgBlocking.isBlocked).toHaveBeenCalledWith("[email protected]", 123);
});
test("should return true when blocked in org blocklist", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
mockOrgBlocking.isBlocked.mockResolvedValue({
isBlocked: true,
reason: WatchlistType.EMAIL,
watchlistEntry: { id: "org-123" },
});
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
organizationId: 123,
});
expect(result).toBe(true);
expect(mockOrgBlocking.isBlocked).toHaveBeenCalledWith("[email protected]", 123);
});
test("should short-circuit on global block (not check org)", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({
isBlocked: true,
reason: WatchlistType.EMAIL,
watchlistEntry: { id: "global-1" },
});
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
organizationId: 123,
});
expect(result).toBe(true);
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalled();
expect(mockOrgBlocking.isBlocked).not.toHaveBeenCalled(); // Global takes precedence
});
test("should handle organizationId as null (explicit no-org)", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
organizationId: null,
});
expect(result).toBe(false);
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalled();
expect(mockOrgBlocking.isBlocked).not.toHaveBeenCalled();
});
});
describe("Telemetry (span)", () => {
test("should call span when provided", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
const mockSpan: SpanFn = vi.fn((options, callback) => {
return Promise.resolve(callback());
});
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
span: mockSpan,
});
expect(result).toBe(false);
expect(mockSpan).toHaveBeenCalledWith(
{ name: "checkIfEmailInWatchlist Controller" },
expect.any(Function)
);
expect(mockSpan).toHaveBeenCalledWith(
{ name: "checkIfEmailInWatchlist Presenter", op: "serialize" },
expect.any(Function)
);
});
test("should not call span when not provided", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
// No span provided
});
expect(result).toBe(false);
// No span to verify - just ensure it doesn't crash
});
test("should execute callback inside span", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({
isBlocked: true,
reason: WatchlistType.EMAIL,
});
let callbackExecuted = false;
const mockSpan: SpanFn = vi.fn(async (options, callback) => {
const result = await callback();
callbackExecuted = true;
return result;
});
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
span: mockSpan,
});
expect(result).toBe(true);
expect(callbackExecuted).toBe(true);
});
});
describe("Edge cases", () => {
test("should handle whitespace in email", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
await checkIfEmailIsBlockedInWatchlistController({
email: " [email protected] ",
});
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalledWith("[email protected]");
});
test("should handle case-insensitive email", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
});
expect(mockGlobalBlocking.isBlocked).toHaveBeenCalledWith("[email protected]");
});
test("should handle organizationId as undefined", async () => {
mockGlobalBlocking.isBlocked.mockResolvedValue({ isBlocked: false });
const result = await checkIfEmailIsBlockedInWatchlistController({
email: "[email protected]",
organizationId: undefined,
});
expect(result).toBe(false);
expect(mockOrgBlocking.isBlocked).not.toHaveBeenCalled();
});
});
});