import { createHash } from "node:crypto"; import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ loadRequiredWebConfig: vi.fn(), createConnection: vi.fn(), reconnectConnection: vi.fn(), disconnectConnection: vi.fn(), tx: { dataConnection: { findMany: vi.fn(), updateMany: vi.fn() }, telephonyConnection: { findMany: vi.fn(), upsert: vi.fn() }, }, })); vi.mock("@hivegrid/config", () => ({ loadRequiredWebConfig: mocks.loadRequiredWebConfig })); vi.mock("~/utils/db/.server/tenantRls.server", () => ({ withTenantRLS: (_tenantId: string, callback: (tx: typeof mocks.tx) => Promise) => callback(mocks.tx), })); vi.mock("~/utils/integrations/appsEngineService.server", () => ({ AppsEngineServiceError: class AppsEngineServiceError extends Error { messageKey: string; constructor(messageKey: string) { super(messageKey); this.messageKey = messageKey; } }, createConnection: mocks.createConnection, reconnectConnection: mocks.reconnectConnection, disconnectConnection: mocks.disconnectConnection, })); import { connectHalloPetra, HalloPetraConnectError } from "./HalloPetraConnectionService.server"; beforeEach(() => { vi.clearAllMocks(); mocks.loadRequiredWebConfig.mockReturnValue({ hallopetra: { webhookUrl: "https://api.example.test/v1/webhooks/telephony/hallopetra" }, }); mocks.tx.telephonyConnection.findMany.mockResolvedValue([]); mocks.tx.dataConnection.findMany.mockResolvedValue([]); mocks.tx.telephonyConnection.upsert.mockResolvedValue({ id: "telephony-1" }); mocks.createConnection.mockResolvedValue({ id: "data-1" }); mocks.reconnectConnection.mockResolvedValue(undefined); mocks.disconnectConnection.mockResolvedValue(undefined); mocks.tx.dataConnection.updateMany.mockResolvedValue({ count: 1 }); }); describe("HalloPetra connection lifecycle", () => { it("creates an API-key DataConnection and a separate one-time webhook secret", async () => { const result = await connectHalloPetra({ tenantId: "tenant-1", userId: "user-1", apiKey: "provider-api-key" }); expect(result).toMatchObject({ dataConnectionId: "data-1", telephonyConnectionId: "telephony-1", webhookUrl: "https://api.example.test/v1/webhooks/telephony/hallopetra", }); expect(result.webhookSecret).toMatch(/^[A-Za-z0-9_-]{40,}$/u); expect(result.webhookSecret).not.toBe("provider-api-key"); expect(mocks.createConnection).toHaveBeenCalledWith({ tenantId: "tenant-1", appId: "hallopetra", label: "HalloPetra", credentials: { apiKey: "provider-api-key", webhookSecret: result.webhookSecret }, createdByUserId: "user-1", }); expect(mocks.tx.telephonyConnection.upsert).toHaveBeenCalledWith({ where: { tenantId_provider: { tenantId: "tenant-1", provider: "hallopetra" } }, create: expect.objectContaining({ tenantId: "tenant-1", dataConnectionId: "data-1", provider: "hallopetra", webhookTokenHash: createHash("sha256").update(result.webhookSecret).digest("hex"), }), update: expect.objectContaining({ dataConnectionId: "data-1" }), select: { id: true }, }); }); it("reuses the historical HalloPetra slot and rotates its webhook secret", async () => { mocks.tx.telephonyConnection.findMany.mockResolvedValue([ { id: "telephony-1", provider: "hallopetra", dataConnectionId: "data-1" }, ]); mocks.tx.dataConnection.findMany.mockResolvedValue([ { id: "data-1", appId: "hallopetra", connectionStatus: "revoked" }, ]); const result = await connectHalloPetra({ tenantId: "tenant-1", userId: "user-1", apiKey: "new-api-key" }); expect(mocks.reconnectConnection).toHaveBeenCalledWith({ tenantId: "tenant-1", connectionId: "data-1", expectedAppId: "hallopetra", credentials: { apiKey: "new-api-key", webhookSecret: result.webhookSecret }, actorUserId: "user-1", }); expect(mocks.createConnection).not.toHaveBeenCalled(); }); it("requires an explicit disconnect before switching away from an active provider", async () => { mocks.tx.telephonyConnection.findMany.mockResolvedValue([ { id: "telephony-aircall", provider: "aircall", dataConnectionId: "data-aircall" }, ]); mocks.tx.dataConnection.findMany.mockResolvedValue([ { id: "data-aircall", appId: "aircall", connectionStatus: "active" }, ]); await expect( connectHalloPetra({ tenantId: "tenant-1", userId: "user-1", apiKey: "provider-api-key" }), ).rejects.toEqual(expect.objectContaining>({ code: "provider_conflict" })); expect(mocks.createConnection).not.toHaveBeenCalled(); }); it("fails closed when the public webhook URL is not configured", async () => { mocks.loadRequiredWebConfig.mockReturnValue({ hallopetra: {} }); await expect( connectHalloPetra({ tenantId: "tenant-1", userId: "user-1", apiKey: "provider-api-key" }), ).rejects.toMatchObject({ code: "not_configured" }); }); it("only permits HTTPS, except HTTP on an explicit loopback host", async () => { mocks.loadRequiredWebConfig.mockReturnValue({ hallopetra: { webhookUrl: "ftp://localhost/v1/webhooks/telephony/hallopetra" }, }); await expect( connectHalloPetra({ tenantId: "tenant-1", userId: "user-1", apiKey: "provider-api-key" }), ).rejects.toMatchObject({ code: "not_configured" }); mocks.loadRequiredWebConfig.mockReturnValue({ hallopetra: { webhookUrl: "http://127.0.0.1:3001/v1/webhooks/telephony/hallopetra" }, }); await expect( connectHalloPetra({ tenantId: "tenant-1", userId: "user-1", apiKey: "provider-api-key" }), ).resolves.toMatchObject({ webhookUrl: "http://127.0.0.1:3001/v1/webhooks/telephony/hallopetra" }); }); });