diff --git a/apps/web/test/pages/api/webhook/app-credential.test.ts b/apps/web/test/pages/api/webhook/app-credential.test.ts new file mode 100644 index 0000000000..f76eda52b6 --- /dev/null +++ b/apps/web/test/pages/api/webhook/app-credential.test.ts @@ -0,0 +1,69 @@ +import prismaMock from "../../../../../../tests/libs/__mocks__/prisma"; + +import type { Request, Response } from "express"; +import type { NextApiRequest, NextApiResponse } from "next"; +import { createMocks } from "node-mocks-http"; +import { afterAll, describe, expect, test, vi } from "vitest"; + +import { symmetricEncrypt } from "@calcom/lib/crypto"; +import prisma from "@calcom/prisma"; + +import handler from "../../../../pages/api/webhook/app-credential"; + +type CustomNextApiRequest = NextApiRequest & Request; +type CustomNextApiResponse = NextApiResponse & Response; + +vi.mock("@calcom/lib/constants", () => ({ + APP_CREDENTIAL_SHARING_ENABLED: true, +})); + +const originalWebhookSecret = process.env.CALCOM_WEBHOOK_SECRET; +const originalCredentialEncryptionKey = process.env.CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY; + +describe("app-credential webhook", () => { + afterAll(() => { + process.env.CALCOM_WEBHOOK_SECRET = originalWebhookSecret; + process.env.CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY = originalCredentialEncryptionKey; + }); + test("Finds app by slug", async () => { + process.env.CALCOM_WEBHOOK_SECRET = "test-secret"; + process.env.CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY = "0wiKhjWZ2LGAFV9D/p2FgnPSth+7x1gY"; + + const keys = symmetricEncrypt( + JSON.stringify({ key: "test-keys" }), + process.env.CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY + ); + + const { req, res } = createMocks({ + method: "POST", + body: { + userId: 1, + appSlug: "office365-calendar", + keys, + }, + headers: { + "calcom-webhook-secret": "test-secret", + }, + prisma, + }); + + prismaMock.user.findUnique.mockResolvedValue({ id: 1 }); + prismaMock.app.findUnique.mockResolvedValue({ + slug: "office365-calendar", + dirName: "office365calendar", + keys: "test-keys", + }); + + prismaMock.credential.create.mockImplementation(() => { + return { + id: 1, + userId: 1, + }; + }); + + await handler(req, res); + + expect(res.statusCode).toBe(200); + expect(JSON.parse(res._getData()).message).toBe("Credentials created for userId: 1"); + }); +});