Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
3.5 kB ยท 118 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119import { db, eq } from "@openstatus/db";import { notification, notificationsToMonitors,} from "@openstatus/db/src/schema";import { expect } from "@std/expect";import { afterAll, beforeAll, test } from "@std/testing/bdd";import { TRPCError } from "@trpc/server";
import { edgeRouter } from "../edge";import { createInnerTRPCContext } from "../trpc";
let otherWorkspaceNotifId: number;
beforeAll(async () => { const notif = await db .insert(notification) .values({ provider: "email", name: "workspace 2 idor test notification", data: '{"email":"test@openstatus.dev"}', workspaceId: 3, }) .returning() .get(); otherWorkspaceNotifId = notif.id;
// Link the other workspace's notification to a monitor so we can verify // the junction table is not wiped by a cross-workspace updateNotifier call await db .insert(notificationsToMonitors) .values({ notificationId: otherWorkspaceNotifId, monitorId: 5 }) .run();});
afterAll(async () => { await db .delete(notificationsToMonitors) .where(eq(notificationsToMonitors.notificationId, otherWorkspaceNotifId)); await db .delete(notification) .where(eq(notification.id, otherWorkspaceNotifId));});
test("notification.delete does not delete a notification from another workspace", async () => { const ctx = createInnerTRPCContext({ req: undefined, session: { user: { id: "1" } }, // @ts-expect-error - minimal workspace for test workspace: { id: 1 }, }); const caller = edgeRouter.createCaller(ctx);
await caller.notification.delete({ id: otherWorkspaceNotifId });
const notifAfter = await db.query.notification.findFirst({ where: eq(notification.id, otherWorkspaceNotifId), }); expect(notifAfter).toBeDefined(); expect(notifAfter?.id).toBe(otherWorkspaceNotifId);});
test("notification.delete succeeds for own workspace notification", async () => { const tempNotif = await db .insert(notification) .values({ provider: "email", name: "temp test notification", data: '{"email":"temp@openstatus.dev"}', workspaceId: 1, }) .returning() .get();
const ctx = createInnerTRPCContext({ req: undefined, session: { user: { id: "1" } }, // @ts-expect-error - minimal workspace for test workspace: { id: 1 }, }); const caller = edgeRouter.createCaller(ctx);
await caller.notification.delete({ id: tempNotif.id });
const notifAfter = await db.query.notification.findFirst({ where: eq(notification.id, tempNotif.id), }); expect(notifAfter).toBeUndefined();});
test("notification.updateNotifier rejects notification from another workspace", async () => { const ctx = createInnerTRPCContext({ req: undefined, session: { user: { id: "1" } }, // @ts-expect-error - minimal workspace for test workspace: { id: 1 }, }); const caller = edgeRouter.createCaller(ctx);
try { await caller.notification.updateNotifier({ id: otherWorkspaceNotifId, name: "Hacked notification", data: { email: "hacker@evil.com" }, monitors: [], }); throw new Error("Should have thrown"); } catch (e) { expect(e).toBeInstanceOf(TRPCError); expect((e as TRPCError).code).toBe("NOT_FOUND"); }
// Verify monitor associations were NOT deleted const associations = await db.query.notificationsToMonitors.findMany({ where: eq(notificationsToMonitors.notificationId, otherWorkspaceNotifId), }); expect(associations.length).toBe(1);});