add e2e testing on webhooks and booking happy-path (#936)
This commit is contained in:
1 parent
86d292838c
commit
9e69029943
16 files changed
+271
-80
No files matched your search
@@ -0,0 +1,99 @@
|
||||
import dayjs from "dayjs";
|
||||
import { kont } from "kont";
|
||||
|
||||
import { loginProvider } from "./lib/loginProvider";
|
||||
import { createHttpServer, waitFor } from "./lib/testUtils";
|
||||
|
||||
jest.setTimeout(60e3);
|
||||
jest.retryTimes(3);
|
||||
|
||||
describe("webhooks", () => {
|
||||
const ctx = kont()
|
||||
.useBeforeEach(
|
||||
loginProvider({
|
||||
user: "pro",
|
||||
path: "/integrations",
|
||||
waitForSelector: '[data-testid="new_webhook"]',
|
||||
})
|
||||
)
|
||||
.done();
|
||||
|
||||
test("add webhook & test that creating an event triggers a webhook call", async () => {
|
||||
const { page } = ctx;
|
||||
const webhookReceiver = createHttpServer();
|
||||
|
||||
// --- add webhook
|
||||
await page.click('[data-testid="new_webhook"]');
|
||||
await expect(page).toHaveSelector("[data-testid='WebhookDialogForm']");
|
||||
|
||||
await page.fill('[name="subscriberUrl"]', webhookReceiver.url);
|
||||
|
||||
await page.click("[type=submit]");
|
||||
|
||||
// dialog is closed
|
||||
await expect(page).not.toHaveSelector("[data-testid='WebhookDialogForm']");
|
||||
// page contains the url
|
||||
await expect(page).toHaveSelector(`text='${webhookReceiver.url}'`);
|
||||
|
||||
// --- go to tomorrow in the pro user's "30min"-event
|
||||
const tomorrow = dayjs().add(1, "day");
|
||||
const tomorrowFormatted = tomorrow.format("YYYY-MM-DDZZ");
|
||||
|
||||
await page.goto(`http://localhost:3000/pro/30min?date=${encodeURIComponent(tomorrowFormatted)}`);
|
||||
|
||||
// click first time available
|
||||
await page.click("[data-testid=time]");
|
||||
|
||||
// --- fill form
|
||||
await page.fill('[name="name"]', "Test Testson");
|
||||
await page.fill('[name="email"]', "[email protected]");
|
||||
await page.press('[name="email"]', "Enter");
|
||||
|
||||
// --- check that webhook was called
|
||||
await waitFor(() => {
|
||||
expect(webhookReceiver.requestList.length).toBe(1);
|
||||
});
|
||||
|
||||
const [request] = webhookReceiver.requestList;
|
||||
const body = request.body as any;
|
||||
|
||||
// remove dynamic properties that differs depending on where you run the tests
|
||||
const dynamic = "[redacted/dynamic]";
|
||||
body.createdAt = dynamic;
|
||||
body.payload.startTime = dynamic;
|
||||
body.payload.endTime = dynamic;
|
||||
for (const attendee of body.payload.attendees) {
|
||||
attendee.timeZone = dynamic;
|
||||
}
|
||||
body.payload.organizer.timeZone = dynamic;
|
||||
|
||||
// if we change the shape of our webhooks, we can simply update this by clicking `u`
|
||||
expect(body).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"createdAt": "[redacted/dynamic]",
|
||||
"payload": Object {
|
||||
"attendees": Array [
|
||||
Object {
|
||||
"email": "[email protected]",
|
||||
"name": "Test Testson",
|
||||
"timeZone": "[redacted/dynamic]",
|
||||
},
|
||||
],
|
||||
"description": "",
|
||||
"endTime": "[redacted/dynamic]",
|
||||
"organizer": Object {
|
||||
"email": "[email protected]",
|
||||
"name": "Pro Example",
|
||||
"timeZone": "[redacted/dynamic]",
|
||||
},
|
||||
"startTime": "[redacted/dynamic]",
|
||||
"title": "30min with Test Testson",
|
||||
"type": "30min",
|
||||
},
|
||||
"triggerEvent": "BOOKING_CREATED",
|
||||
}
|
||||
`);
|
||||
|
||||
webhookReceiver.close();
|
||||
});
|
||||
});
|
||||
@@ -37,7 +37,7 @@ export function loginProvider(opts: {
|
||||
waitForSelector?: string;
|
||||
}): Provider<Needs, Contributes> {
|
||||
return provider<Needs, Contributes>()
|
||||
.name("page")
|
||||
.name("login")
|
||||
.before(async () => {
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export function randomString(length: number) {
|
||||
import { createServer, IncomingMessage, ServerResponse } from "http";
|
||||
|
||||
export function randomString(length = 12) {
|
||||
let result = "";
|
||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
const charactersLength = characters.length;
|
||||
@@ -7,3 +9,65 @@ export function randomString(length: number) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
type Request = IncomingMessage & { body?: unknown };
|
||||
type RequestHandlerOptions = { req: Request; res: ServerResponse };
|
||||
type RequestHandler = (opts: RequestHandlerOptions) => void;
|
||||
|
||||
export function createHttpServer(opts: { requestHandler?: RequestHandler } = {}) {
|
||||
const {
|
||||
requestHandler = ({ res }) => {
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
res.write(JSON.stringify({}));
|
||||
res.end();
|
||||
},
|
||||
} = opts;
|
||||
const requestList: Request[] = [];
|
||||
const server = createServer((req, res) => {
|
||||
const buffer: unknown[] = [];
|
||||
|
||||
req.on("data", (data) => {
|
||||
buffer.push(data);
|
||||
});
|
||||
req.on("end", () => {
|
||||
const _req: Request = req;
|
||||
// assume all incoming request bodies are json
|
||||
const json = buffer.length ? JSON.parse(buffer.join("")) : undefined;
|
||||
|
||||
_req.body = json;
|
||||
requestList.push(_req);
|
||||
requestHandler({ req: _req, res });
|
||||
});
|
||||
});
|
||||
|
||||
// listen on random port
|
||||
server.listen(0);
|
||||
const port: number = (server.address() as any).port;
|
||||
const url = `http://localhost:${port}`;
|
||||
return {
|
||||
port,
|
||||
close: () => server.close(),
|
||||
requestList,
|
||||
url,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* When in need to wait for any period of time you can use waitFor, to wait for your expectations to pass.
|
||||
*/
|
||||
export async function waitFor(fn: () => Promise<unknown> | unknown, opts: { timeout?: number } = {}) {
|
||||
let finished = false;
|
||||
const timeout = opts.timeout ?? 5000; // 5s
|
||||
const timeStart = Date.now();
|
||||
while (!finished) {
|
||||
try {
|
||||
await fn();
|
||||
finished = true;
|
||||
} catch {
|
||||
if (Date.now() - timeStart >= timeout) {
|
||||
throw new Error("waitFor timed out");
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user