Skip to content

Open-source status page, incident response and uptime monitoring

add some test (#1708)

Thibault Le Ouay512b0519d5be
diff --git a/apps/server/src/routes/v1/maintenances/get_all.test.ts b/apps/server/src/routes/v1/maintenances/get_all.test.ts
index e957b2d75..c2e087511 100644
--- a/apps/server/src/routes/v1/maintenances/get_all.test.ts
+++ b/apps/server/src/routes/v1/maintenances/get_all.test.ts
@@ -11,7 +11,7 @@ test("return all maintenances", async () => {
});
const result = MaintenanceSchema.array().safeParse(await res.json());
-
+ console.log(result);
expect(res.status).toBe(200);
expect(result.success).toBe(true);
expect(result.data?.length).toBeGreaterThan(0);
diff --git a/packages/api/src/router/maintenance.test.ts b/packages/api/src/router/maintenance.test.ts
new file mode 100644
index 000000000..62c85ae1d
--- /dev/null
+++ b/packages/api/src/router/maintenance.test.ts
@@ -0,0 +1,125 @@
+import { expect, test } from "bun:test";
+import { appRouter } from "../root";
+import { createInnerTRPCContext } from "../trpc";
+
+function getTestContext() {
+ return createInnerTRPCContext({
+ req: undefined,
+ session: {
+ user: {
+ // @ts-expect-error
+ id: 1,
+ },
+ },
+ });
+}
+
+const from = new Date();
+const to = new Date(from.getTime() + 1000 * 60 * 60);
+
+test("Create Maintenance", async () => {
+ const ctx = getTestContext();
+ const caller = appRouter.createCaller(ctx);
+
+ const createdMaintenance = await caller.maintenance.new({
+ title: "Test Maintenance",
+ message: "This is a test maintenance.",
+ startDate: from,
+ endDate: to,
+ pageId: 1,
+ });
+
+ expect(createdMaintenance).toMatchObject({
+ id: expect.any(Number),
+ title: "Test Maintenance",
+ message: "This is a test maintenance.",
+ from,
+ to,
+ workspaceId: 1,
+ });
+});
+
+test("Get Maintenance by ID", async () => {
+ const ctx = getTestContext();
+ const caller = appRouter.createCaller(ctx);
+
+ const createdMaintenance = await caller.maintenance.new({
+ title: "Test Maintenance",
+ message: "This is a test maintenance.",
+ startDate: from,
+ endDate: to,
+ pageId: 1,
+ });
+
+ const createdMaintenanceId = createdMaintenance.id;
+
+ const fetchedMaintenance = await caller.maintenance.getById({
+ id: createdMaintenanceId,
+ });
+
+ expect(fetchedMaintenance).toMatchObject({
+ id: createdMaintenance.id,
+ title: "Test Maintenance",
+ message: "This is a test maintenance.",
+ from,
+ to,
+ workspaceId: 1,
+ });
+});
+
+test("Update Maintenance", async () => {
+ const ctx = getTestContext();
+ const caller = appRouter.createCaller(ctx);
+
+ const createdMaintenance = await caller.maintenance.new({
+ title: "Test Maintenance",
+ message: "This is a test maintenance.",
+ startDate: from,
+ endDate: to,
+ pageId: 1,
+ });
+
+ const createdMaintenanceId = createdMaintenance.id;
+
+ await caller.maintenance.update({
+ id: createdMaintenanceId,
+ title: "Updated Test Maintenance",
+ message: "This is an updated test maintenance.",
+ startDate: from,
+ endDate: to,
+ });
+
+ const updatedMaintenance = await caller.maintenance.getById({
+ id: createdMaintenanceId,
+ });
+
+ expect(updatedMaintenance).toMatchObject({
+ id: createdMaintenanceId,
+ title: "Updated Test Maintenance",
+ message: "This is an updated test maintenance.",
+ from,
+ to,
+ workspaceId: 1,
+ });
+});
+
+test("Delete Maintenance", async () => {
+ const ctx = getTestContext();
+ const caller = appRouter.createCaller(ctx);
+
+ const createdMaintenance = await caller.maintenance.new({
+ title: "Test Maintenance",
+ message: "This is a test maintenance.",
+ startDate: from,
+ endDate: to,
+ pageId: 1,
+ });
+
+ const createdMaintenanceId = createdMaintenance.id;
+
+ const deletedMaintenance = await caller.maintenance.delete({
+ id: createdMaintenanceId,
+ });
+
+ expect(deletedMaintenance).toBeDefined();
+});
diff --git a/packages/api/src/router/monitor.test.ts b/packages/api/src/router/monitor.test.ts
new file mode 100644
index 000000000..b54f3c705
--- /dev/null
+++ b/packages/api/src/router/monitor.test.ts
@@ -0,0 +1,130 @@
+import { expect, test } from "bun:test";
+
+import { flyRegions } from "@openstatus/db/src/schema/constants";
+import { TRPCError } from "@trpc/server";
+import { appRouter } from "../root";
+import { createInnerTRPCContext } from "../trpc";
+
+function getTestContext(limits?: unknown) {
+ return createInnerTRPCContext({
+ req: undefined,
+ session: {
+ user: {
+ id: "1",
+ },
+ },
+ workspace: {
+ id: 1,
+ // @ts-expect-error
+ limits: limits || {
+ monitors: 10,
+ periodicity: ["1m"],
+ regions: ["fra"],
+ },
+ },
+ });
+}
+
+const monitorData = {
+ name: "Test Monitor",
+ url: "https://example.com",
+ jobType: "http" as const,
+ method: "GET" as const,
+ periodicity: "1m" as const,
+ regions: [flyRegions[0]],
+ statusAssertions: [],
+ headerAssertions: [],
+ textBodyAssertions: [],
+ notifications: [],
+ pages: [],
+ tags: [],
+};
+
+test("Create Monitor", async () => {
+ const ctx = getTestContext();
+ const caller = appRouter.createCaller(ctx);
+
+ const createdMonitor = await caller.monitor.create(monitorData);
+
+ expect(createdMonitor).toMatchObject({
+ id: expect.any(Number),
+ name: "Test Monitor",
+ url: "https://example.com",
+ workspaceId: 1,
+ });
+});
+
+test("Get Monitor by ID", async () => {
+ const ctx = getTestContext();
+ const caller = appRouter.createCaller(ctx);
+
+ const createdMonitor = await caller.monitor.create(monitorData);
+ const monitorId = createdMonitor.id;
+
+ const fetchedMonitor = await caller.monitor.get({
+ id: monitorId,
+ });
+
+ expect(fetchedMonitor).not.toBeNull();
+ expect(fetchedMonitor).toMatchObject({
+ id: monitorId,
+ name: "Test Monitor",
+ url: "https://example.com",
+ });
+});
+
+test("Update Monitor", async () => {
+ const ctx = getTestContext();
+ const caller = appRouter.createCaller(ctx);
+
+ const createdMonitor = await caller.monitor.create(monitorData);
+ const monitorId = createdMonitor.id;
+
+ await caller.monitor.update({
+ ...monitorData,
+ id: monitorId,
+ name: "Updated Test Monitor",
+ });
+
+ const updatedMonitor = await caller.monitor.get({
+ id: monitorId,
+ });
+
+ expect(updatedMonitor).not.toBeNull();
+ expect(updatedMonitor?.name).toBe("Updated Test Monitor");
+});
+
+test("Delete Monitor", async () => {
+ const ctx = getTestContext();
+ const caller = appRouter.createCaller(ctx);
+
+ const createdMonitor = await caller.monitor.create(monitorData);
+ const monitorId = createdMonitor.id;
+
+ await caller.monitor.delete({
+ id: monitorId,
+ });
+
+ const fetchedMonitor = await caller.monitor.get({
+ id: monitorId,
+ });
+ expect(fetchedMonitor).toBeNull();
+});
+
+test.todo("Monitor Limit Reached", async () => {
+ const ctx = getTestContext();
+ // @ts-expect-error
+ ctx.workspace.limits.monitors = 1;
+ const caller = appRouter.createCaller(ctx);
+
+ await caller.monitor.create(monitorData);
+
+ await caller.monitor.create(monitorData);
+
+ await expect(await caller.monitor.create(monitorData)).rejects.toThrow(
+ new TRPCError({
+ code: "FORBIDDEN",
+ message: "You reached your monitor limits.",
+ }),
+ );
+});
diff --git a/packages/api/src/router/workspace.test.ts b/packages/api/src/router/workspace.test.ts
index 9c750fcd9..ca7e76ce0 100644
--- a/packages/api/src/router/workspace.test.ts
+++ b/packages/api/src/router/workspace.test.ts
@@ -6,7 +6,6 @@ import { createInnerTRPCContext } from "../trpc";
test("Get Test Workspace", async () => {
const ctx = createInnerTRPCContext({
req: undefined,
- // @ts-expect-error some issues with types
session: {
user: {
id: "1",
@@ -38,10 +37,10 @@ test("Get Test Workspace", async () => {
test("by default we get the first workspace", async () => {
const ctx = createInnerTRPCContext({
req: undefined,
- // @ts-expect-error some issues with types
session: {
user: {
- id: "1",
+ // @ts-expect-error some issues with types
+ id: 1,
},
},
workspace: undefined,
@@ -67,7 +66,6 @@ test("by default we get the first workspace", async () => {
test("All workspaces", async () => {
const ctx = createInnerTRPCContext({
req: undefined,
- // @ts-expect-error some issues with types
session: {
user: {
id: "1",