Added unit tests for app-credential webhook

fix/credential-sync-app-lookup-tests
Keith Williams 2023-09-26 08:12:41 -03:00
parent 8a9dbf4457
commit 287c455a52
1 changed files with 69 additions and 0 deletions

View File

@ -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<CustomNextApiRequest, CustomNextApiResponse>({
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");
});
});