Compare commits

...

8 Commits

Author SHA1 Message Date
Joe Au-Yeung a88ce51ff0
Merge branch 'main' into fix/credential-sync-app-lookup-tests 2023-10-24 13:05:49 -04:00
Keith Williams 9d04d67360
Merge branch 'main' into fix/credential-sync-app-lookup-tests 2023-10-19 10:29:51 -03:00
Keith Williams 20ac080517
Merge branch 'main' into fix/credential-sync-app-lookup-tests 2023-10-04 09:08:11 -03:00
Keith Williams 289a60ba3a Building helpers for prisma mocks 2023-09-27 09:59:58 -03:00
Keith Williams b52c2c3d07 Attempting to fix types 2023-09-27 09:59:58 -03:00
Keith Williams 287c455a52 Added unit tests for app-credential webhook 2023-09-27 09:59:58 -03:00
Keith Williams 8a9dbf4457 Updated the data returned for the app 2023-09-27 09:54:10 -03:00
Keith Williams 62b3b87cda Fix: credential sync app lookup 2023-09-27 09:52:08 -03:00
2 changed files with 82 additions and 1 deletions

View File

@ -0,0 +1,67 @@
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 { buildUser } from "@calcom/lib/test/builder";
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(
buildUser({
id: 1,
username: "test",
name: "Test User",
email: "test@example.com",
})
);
prismaMock.app.findUnique.mockResolvedValue({ dirName: "office365calendar" });
await handler(req, res);
expect(res.statusCode).toBe(200);
expect(JSON.parse(res._getData()).message).toBe("Credentials created for userId: 1");
});
});

View File

@ -1,5 +1,5 @@
import { faker } from "@faker-js/faker";
import type { Booking, EventType, Prisma, Webhook } from "@prisma/client";
import type { App, Booking, EventType, Prisma, Webhook } from "@prisma/client";
import type { TFunction } from "next-i18next";
import { BookingStatus } from "@calcom/prisma/enums";
@ -227,3 +227,17 @@ export const buildUser = <T extends Partial<UserPayload>>(user?: T): UserPayload
...user,
};
};
export const buildApp = (app?: Partial<App>): App => {
return {
categories: null,
createdAt: new Date(),
updatedAt: new Date(),
credentials: null,
payments: null,
webhooks: null,
apiKeys: null,
enabled: true,
...app,
};
};