|
| 1 | +import type { IncomingHttpHeaders } from "http"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import type { FeaturesRepository } from "@calcom/features/flags/features.repository"; |
| 5 | +import { HttpError } from "@calcom/lib/http-error"; |
| 6 | +import type { EventTypeRepository } from "@calcom/lib/server/repository/eventTypeRepository"; |
| 7 | + |
| 8 | +import { BotDetectionService } from "./BotDetectionService"; |
| 9 | + |
| 10 | +// Mock the botid/server module |
| 11 | +vi.mock("botid/server", () => ({ |
| 12 | + checkBotId: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +// Mock the logger |
| 16 | +vi.mock("@calcom/lib/logger", () => ({ |
| 17 | + default: { |
| 18 | + getSubLogger: vi.fn(() => ({ |
| 19 | + warn: vi.fn(), |
| 20 | + info: vi.fn(), |
| 21 | + error: vi.fn(), |
| 22 | + })), |
| 23 | + }, |
| 24 | +})); |
| 25 | + |
| 26 | +describe("BotDetectionService", () => { |
| 27 | + let botDetectionService: BotDetectionService; |
| 28 | + let mockFeaturesRepository: FeaturesRepository; |
| 29 | + let mockEventTypeRepository: EventTypeRepository; |
| 30 | + let mockHeaders: IncomingHttpHeaders; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + // Reset all mocks before each test |
| 34 | + vi.clearAllMocks(); |
| 35 | + |
| 36 | + // Setup mock repositories |
| 37 | + mockFeaturesRepository = { |
| 38 | + checkIfTeamHasFeature: vi.fn(), |
| 39 | + } as unknown as FeaturesRepository; |
| 40 | + |
| 41 | + mockEventTypeRepository = { |
| 42 | + getTeamIdByEventTypeId: vi.fn(), |
| 43 | + } as unknown as EventTypeRepository; |
| 44 | + |
| 45 | + mockHeaders = { |
| 46 | + "user-agent": "Mozilla/5.0", |
| 47 | + "x-forwarded-for": "192.168.1.1", |
| 48 | + }; |
| 49 | + |
| 50 | + botDetectionService = new BotDetectionService(mockFeaturesRepository, mockEventTypeRepository); |
| 51 | + |
| 52 | + // Reset environment variable |
| 53 | + delete process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER; |
| 54 | + }); |
| 55 | + |
| 56 | + describe("checkBotDetection", () => { |
| 57 | + it("should return early if BotID is not enabled at instance level", async () => { |
| 58 | + process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER = "0"; |
| 59 | + |
| 60 | + await botDetectionService.checkBotDetection({ |
| 61 | + eventTypeId: 123, |
| 62 | + headers: mockHeaders, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(mockEventTypeRepository.getTeamIdByEventTypeId).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should return early if no eventTypeId is provided", async () => { |
| 69 | + process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER = "1"; |
| 70 | + |
| 71 | + await botDetectionService.checkBotDetection({ |
| 72 | + headers: mockHeaders, |
| 73 | + }); |
| 74 | + |
| 75 | + expect(mockEventTypeRepository.getTeamIdByEventTypeId).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return early if event type has no teamId", async () => { |
| 79 | + process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER = "1"; |
| 80 | + vi.mocked(mockEventTypeRepository.getTeamIdByEventTypeId).mockResolvedValue({ |
| 81 | + teamId: null, |
| 82 | + }); |
| 83 | + |
| 84 | + await botDetectionService.checkBotDetection({ |
| 85 | + eventTypeId: 123, |
| 86 | + headers: mockHeaders, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(mockFeaturesRepository.checkIfTeamHasFeature).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should return early if BotID feature is not enabled for the team", async () => { |
| 93 | + process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER = "1"; |
| 94 | + vi.mocked(mockEventTypeRepository.getTeamIdByEventTypeId).mockResolvedValue({ |
| 95 | + teamId: 456, |
| 96 | + }); |
| 97 | + vi.mocked(mockFeaturesRepository.checkIfTeamHasFeature).mockResolvedValue(false); |
| 98 | + |
| 99 | + const { checkBotId } = await import("botid/server"); |
| 100 | + |
| 101 | + await botDetectionService.checkBotDetection({ |
| 102 | + eventTypeId: 123, |
| 103 | + headers: mockHeaders, |
| 104 | + }); |
| 105 | + |
| 106 | + expect(checkBotId).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should throw HttpError when a bot is detected", async () => { |
| 110 | + process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER = "1"; |
| 111 | + vi.mocked(mockEventTypeRepository.getTeamIdByEventTypeId).mockResolvedValue({ |
| 112 | + teamId: 456, |
| 113 | + }); |
| 114 | + vi.mocked(mockFeaturesRepository.checkIfTeamHasFeature).mockResolvedValue(true); |
| 115 | + |
| 116 | + const { checkBotId } = await import("botid/server"); |
| 117 | + vi.mocked(checkBotId).mockResolvedValue({ |
| 118 | + isBot: true, |
| 119 | + isHuman: false, |
| 120 | + isVerifiedBot: false, |
| 121 | + verifiedBotName: undefined, |
| 122 | + verifiedBotCategory: undefined, |
| 123 | + bypassed: false, |
| 124 | + classificationReason: "suspicious-patterns", |
| 125 | + }); |
| 126 | + |
| 127 | + await expect( |
| 128 | + botDetectionService.checkBotDetection({ |
| 129 | + eventTypeId: 123, |
| 130 | + headers: mockHeaders, |
| 131 | + }) |
| 132 | + ).rejects.toThrow(HttpError); |
| 133 | + |
| 134 | + await expect( |
| 135 | + botDetectionService.checkBotDetection({ |
| 136 | + eventTypeId: 123, |
| 137 | + headers: mockHeaders, |
| 138 | + }) |
| 139 | + ).rejects.toThrow("Access denied"); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should pass when a human is detected", async () => { |
| 143 | + process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER = "1"; |
| 144 | + vi.mocked(mockEventTypeRepository.getTeamIdByEventTypeId).mockResolvedValue({ |
| 145 | + teamId: 456, |
| 146 | + }); |
| 147 | + vi.mocked(mockFeaturesRepository.checkIfTeamHasFeature).mockResolvedValue(true); |
| 148 | + |
| 149 | + const { checkBotId } = await import("botid/server"); |
| 150 | + vi.mocked(checkBotId).mockResolvedValue({ |
| 151 | + isBot: false, |
| 152 | + isHuman: true, |
| 153 | + isVerifiedBot: false, |
| 154 | + verifiedBotName: undefined, |
| 155 | + verifiedBotCategory: undefined, |
| 156 | + bypassed: false, |
| 157 | + classificationReason: "human-behavior", |
| 158 | + }); |
| 159 | + |
| 160 | + await expect( |
| 161 | + botDetectionService.checkBotDetection({ |
| 162 | + eventTypeId: 123, |
| 163 | + headers: mockHeaders, |
| 164 | + }) |
| 165 | + ).resolves.not.toThrow(); |
| 166 | + }); |
| 167 | + |
| 168 | + it("should check feature flag with correct teamId", async () => { |
| 169 | + const teamId = 789; |
| 170 | + process.env.NEXT_PUBLIC_VERCEL_USE_BOTID_IN_BOOKER = "1"; |
| 171 | + vi.mocked(mockEventTypeRepository.getTeamIdByEventTypeId).mockResolvedValue({ |
| 172 | + teamId, |
| 173 | + }); |
| 174 | + vi.mocked(mockFeaturesRepository.checkIfTeamHasFeature).mockResolvedValue(false); |
| 175 | + |
| 176 | + await botDetectionService.checkBotDetection({ |
| 177 | + eventTypeId: 123, |
| 178 | + headers: mockHeaders, |
| 179 | + }); |
| 180 | + |
| 181 | + expect(mockFeaturesRepository.checkIfTeamHasFeature).toHaveBeenCalledWith(teamId, "booker-botid"); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments