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.
6.6 kB ยท 233 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234import { db, eq, inArray } from "@openstatus/db";import { privateLocation, privateLocationToMonitors, workspace,} from "@openstatus/db/src/schema";import { createMonitor, createWorkspace,} from "@openstatus/db/src/test/factories";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";
// Passing both `user` and `workspace` trips the NODE_ENV=test escape hatch in// the authed middleware, so the caller is scoped to ownWorkspaceId instead of// resolving the seeded workspace for the session user.function callerFor() { const ctx = createInnerTRPCContext({ req: undefined, session: { user: { id: "1" } }, // @ts-expect-error - minimal user for test user: { id: 1 }, // @ts-expect-error - minimal workspace for test workspace: { id: ownWorkspaceId }, }); return edgeRouter.createCaller(ctx);}
// Own workspaces/monitors per suite โ sharing the seeded monitor 5 / workspace// 3 races sibling suites that read or mutate that monitor's associations.let ownWorkspaceId: number;let otherWorkspaceId: number;let ownMonitorId: number;let otherMonitorId: number;let otherWorkspaceLocationId: number;let ownWorkspaceLocationId: number;
beforeAll(async () => { const own = await createWorkspace(); const other = await createWorkspace(); ownWorkspaceId = own.id; otherWorkspaceId = other.id;
const ownMonitor = await createMonitor(own.id); const otherMonitor = await createMonitor(other.id); ownMonitorId = ownMonitor.id; otherMonitorId = otherMonitor.id;
const otherLoc = await db .insert(privateLocation) .values({ workspaceId: other.id, name: "Other workspace location", token: `test-token-idor-${otherWorkspaceId}`, }) .returning() .get(); otherWorkspaceLocationId = otherLoc.id;
await db .insert(privateLocationToMonitors) .values({ privateLocationId: otherWorkspaceLocationId, monitorId: otherMonitorId, }) .run();
const ownLoc = await db .insert(privateLocation) .values({ workspaceId: own.id, name: "Own workspace location", token: `test-token-own-${ownWorkspaceId}`, }) .returning() .get(); ownWorkspaceLocationId = ownLoc.id;});
afterAll(async () => { await db .delete(privateLocationToMonitors) .where( inArray(privateLocationToMonitors.privateLocationId, [ otherWorkspaceLocationId, ownWorkspaceLocationId, ]), ) .catch(() => undefined); await db .delete(privateLocation) .where( inArray(privateLocation.id, [ otherWorkspaceLocationId, ownWorkspaceLocationId, ]), ) .catch(() => undefined); await db .delete(workspace) .where(inArray(workspace.id, [ownWorkspaceId, otherWorkspaceId])) .catch(() => undefined);});
test("privateLocation.update rejects location from another workspace", async () => { const caller = callerFor();
try { await caller.privateLocation.update({ id: otherWorkspaceLocationId, name: "Unauthorized Name Change", 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.privateLocationToMonitors.findMany({ where: eq( privateLocationToMonitors.privateLocationId, otherWorkspaceLocationId, ), }); expect(associations.length).toBe(1);});
test("privateLocation.update succeeds for own workspace location", async () => { const caller = callerFor();
const result = await caller.privateLocation.update({ id: ownWorkspaceLocationId, name: "Updated Location Name", monitors: [ownMonitorId], });
expect(result).toBeDefined(); expect(result.name).toBe("Updated Location Name");});
test("privateLocation.new rejects monitors from another workspace", async () => { const caller = callerFor();
try { await caller.privateLocation.new({ name: "Injected Location", token: "test-token-inject", monitors: [otherMonitorId], }); throw new Error("Should have thrown"); } catch (e) { expect(e).toBeInstanceOf(TRPCError); expect((e as TRPCError).code).toBe("FORBIDDEN"); }});
test("privateLocation.new succeeds with own workspace monitors", async () => { const caller = callerFor();
const result = await caller.privateLocation.new({ name: "Valid Location", token: "test-token-valid-new", monitors: [ownMonitorId], });
expect(result).toBeDefined(); expect(result.name).toBe("Valid Location");
// Verify the monitor association was created const associations = await db.query.privateLocationToMonitors.findMany({ where: eq(privateLocationToMonitors.privateLocationId, result.id), }); expect(associations.length).toBe(1); expect(associations[0].monitorId).toBe(ownMonitorId);
// Cleanup await db .delete(privateLocationToMonitors) .where(eq(privateLocationToMonitors.privateLocationId, result.id)); await db.delete(privateLocation).where(eq(privateLocation.id, result.id));});
test("privateLocation.update rejects monitors from another workspace", async () => { const caller = callerFor();
try { await caller.privateLocation.update({ id: ownWorkspaceLocationId, name: "Updated Location Name", monitors: [otherMonitorId], }); throw new Error("Should have thrown"); } catch (e) { expect(e).toBeInstanceOf(TRPCError); expect((e as TRPCError).code).toBe("FORBIDDEN"); }});
test("privateLocation.new persists metadata and defaults status", async () => { const caller = callerFor();
const result = await caller.privateLocation.new({ name: "Metadata Location", token: "test-token-metadata", monitors: [], metadata: { datacenter: "eu-west" }, });
expect(result.metadata).toEqual({ datacenter: "eu-west" }); expect(result.status).toBe("error");
await db.delete(privateLocation).where(eq(privateLocation.id, result.id));});
test("privateLocation.update replaces metadata", async () => { const caller = callerFor();
const result = await caller.privateLocation.update({ id: ownWorkspaceLocationId, name: "Metadata Update", monitors: [ownMonitorId], metadata: { env: "prod" }, });
expect(result.metadata).toEqual({ env: "prod" }); expect(result.status).toBe("error");});