Openstatus
www.openstatus.dev
1import { expect, test } from "bun:test";
2
3import { app } from "@/index";
4import { db, eq } from "@openstatus/db";
5import { notification } from "@openstatus/db/src/schema";
6import { NotificationSchema } from "./schema";
7
8test("create a notification", async () => {
9 const res = await app.request("/v1/notification", {
10 method: "POST",
11 headers: {
12 "x-openstatus-key": "1",
13 "content-type": "application/json",
14 },
15 body: JSON.stringify({
16 name: "OpenStatus",
17 provider: "email",
18 payload: { email: "ping@openstatus.dev" },
19 monitors: [1],
20 }),
21 });
22
23 const result = NotificationSchema.safeParse(await res.json());
24
25 expect(res.status).toBe(200);
26 expect(result.success).toBe(true);
27
28 // Cleanup: delete the created notification
29 if (result.success) {
30 await db.delete(notification).where(eq(notification.id, result.data.id));
31 }
32});
33
34test("create a notification with invalid monitor ids should return a 400", async () => {
35 const res = await app.request("/v1/notification", {
36 method: "POST",
37 headers: {
38 "x-openstatus-key": "1",
39 "content-type": "application/json",
40 },
41 body: JSON.stringify({
42 name: "OpenStatus",
43 provider: "email",
44 payload: { email: "ping@openstatus.dev" },
45 monitors: [404],
46 }),
47 });
48
49 expect(res.status).toBe(400);
50});
51
52test("create a email notification with invalid payload should return a 400", async () => {
53 const res = await app.request("/v1/notification", {
54 method: "POST",
55 headers: {
56 "x-openstatus-key": "1",
57 "content-type": "application/json",
58 },
59 body: JSON.stringify({
60 name: "OpenStatus",
61 provider: "email",
62 payload: { hello: "world" },
63 }),
64 });
65
66 expect(res.status).toBe(400);
67});
68
69test("no auth key should return 401", async () => {
70 const res = await app.request("/v1/notification", {
71 method: "POST",
72 headers: {
73 "content-type": "application/json",
74 },
75 });
76
77 expect(res.status).toBe(401);
78});