Skip to content

Open-source status page, incident response and uptime monitoring

fix: webhook test (#1614)

Maximilian Kaske63c595a4a840
diff --git a/apps/dashboard/src/components/forms/notifications/form-webhook.tsx b/apps/dashboard/src/components/forms/notifications/form-webhook.tsx
index 817745d9f..9ab575079 100644
--- a/apps/dashboard/src/components/forms/notifications/form-webhook.tsx
+++ b/apps/dashboard/src/components/forms/notifications/form-webhook.tsx
@@ -98,24 +98,12 @@ export function FormWebhook({
startTransition(async () => {
try {
const provider = form.getValues("provider");
- const data = form.getValues("data");
- const promise = config[provider].sendTest(
- data as unknown as {
- url: string;
- headers?: { key: string; value: string }[];
- },
- );
- toast.promise(promise, {
+ const data = form.getValues("data.endpoint");
+ toast.promise(config[provider].sendTest({ url: data }), {
loading: "Sending test...",
success: "Test sent",
- error: (error) => {
- if (error instanceof Error) {
- return error.message;
- }
- return "Failed to send test";
- },
+ error: "Failed to send test",
});
- await promise;
} catch (error) {
console.error(error);
}
diff --git a/packages/notifications/webhook/package.json b/packages/notifications/webhook/package.json
index 5504d1050..315557890 100644
--- a/packages/notifications/webhook/package.json
+++ b/packages/notifications/webhook/package.json
@@ -2,6 +2,9 @@
"name": "@openstatus/notification-webhook",
"version": "1.0.0",
"main": "src/index.ts",
+ "scripts": {
+ "test": "bun test"
+ },
"dependencies": {
"@openstatus/db": "workspace:*",
"@openstatus/utils": "workspace:*",
@@ -10,6 +13,7 @@
"devDependencies": {
"@openstatus/tsconfig": "workspace:*",
"@types/node": "22.10.2",
+ "bun-types": "1.3.1",
"typescript": "5.7.2"
}
}
diff --git a/packages/notifications/webhook/src/index.test.ts b/packages/notifications/webhook/src/index.test.ts
new file mode 100644
index 000000000..cd18c958b
--- /dev/null
+++ b/packages/notifications/webhook/src/index.test.ts
@@ -0,0 +1,124 @@
+import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
+import { sendTest } from "./index";
+
+describe("Webhook sendTest", () => {
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
+ let fetchMock: any = undefined;
+
+ beforeEach(() => {
+ fetchMock = spyOn(global, "fetch");
+ });
+
+ afterEach(() => {
+ if (fetchMock) {
+ fetchMock.mockClear();
+ fetchMock.mockRestore();
+ }
+ });
+
+ test("should send test webhook successfully", async () => {
+ const url = "https://example.com/webhook";
+ fetchMock.mockResolvedValue(new Response(null, { status: 200 }));
+
+ const result = await sendTest({ url });
+
+ expect(result).toBe(true);
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ expect(fetchMock).toHaveBeenCalledWith(
+ url,
+ expect.objectContaining({
+ method: "post",
+ body: expect.any(String),
+ headers: undefined,
+ }),
+ );
+
+ const callArgs = fetchMock.mock.calls[0];
+ const body = JSON.parse(callArgs[1].body);
+ expect(body).toMatchObject({
+ monitor: {
+ id: 1,
+ name: "test",
+ url: "http://openstat.us",
+ },
+ status: "recovered",
+ statusCode: 200,
+ latency: 1337,
+ });
+ expect(body.cronTimestamp).toBeTypeOf("number");
+ });
+
+ test("should send test webhook with headers", async () => {
+ const url = "https://example.com/webhook";
+ const headers = [
+ { key: "Authorization", value: "Bearer token123" },
+ { key: "Content-Type", value: "application/json" },
+ ];
+ fetchMock.mockResolvedValue(new Response(null, { status: 200 }));
+
+ const result = await sendTest({ url, headers });
+
+ expect(result).toBe(true);
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ expect(fetchMock).toHaveBeenCalledWith(
+ url,
+ expect.objectContaining({
+ method: "post",
+ body: expect.any(String),
+ headers: {
+ Authorization: "Bearer token123",
+ "Content-Type": "application/json",
+ },
+ }),
+ );
+ });
+
+ test("should throw error when response is not ok", async () => {
+ const url = "https://example.com/webhook";
+ fetchMock.mockResolvedValue(new Response(null, { status: 400 }));
+
+ await expect(sendTest({ url })).rejects.toThrow("Failed to send test");
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ });
+
+ test("should throw error when fetch fails", async () => {
+ const url = "https://example.com/webhook";
+ const networkError = new Error("Network error");
+ fetchMock.mockRejectedValue(networkError);
+
+ await expect(sendTest({ url })).rejects.toThrow("Failed to send test");
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ });
+
+ test("should send test webhook with empty headers array", async () => {
+ const url = "https://example.com/webhook";
+ const headers: { key: string; value: string }[] = [];
+ fetchMock.mockResolvedValue(new Response(null, { status: 200 }));
+
+ const result = await sendTest({ url, headers });
+
+ expect(result).toBe(true);
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ const callArgs = fetchMock.mock.calls[0];
+ // Empty headers array should result in empty object after transformHeaders
+ expect(callArgs[1].headers).toEqual({});
+ });
+
+ test("should send test webhook with 500 status code", async () => {
+ const url = "https://example.com/webhook";
+ fetchMock.mockResolvedValue(new Response(null, { status: 500 }));
+
+ await expect(sendTest({ url })).rejects.toThrow("Failed to send test");
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ });
+
+ test("should send test webhook with 201 status code (success)", async () => {
+ const url = "https://example.com/webhook";
+ fetchMock.mockResolvedValue(new Response(null, { status: 201 }));
+
+ const result = await sendTest({ url });
+
+ expect(result).toBe(true);
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/packages/notifications/webhook/src/index.ts b/packages/notifications/webhook/src/index.ts
index e6413c743..b6dbd9508 100644
--- a/packages/notifications/webhook/src/index.ts
+++ b/packages/notifications/webhook/src/index.ts
@@ -153,14 +153,17 @@ export const sendTest = async ({
latency: 1337,
});
try {
- await fetch(url, {
+ const response = await fetch(url, {
method: "post",
body: JSON.stringify(body),
headers: headers ? transformHeaders(headers) : undefined,
});
+ if (!response.ok) {
+ throw new Error("Failed to send test");
+ }
+ return true;
} catch (err) {
console.log(err);
- return false;
+ throw new Error("Failed to send test");
}
- return true;
};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ca736bd2b..c8a62ba8c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1620,6 +1620,9 @@ importers:
'@types/node':
specifier: 22.10.2
version: 22.10.2
+ bun-types:
+ specifier: 1.3.1
+ version: 1.3.1(@types/[email protected])
typescript:
specifier: 5.7.2
version: 5.7.2