Skip to content

Open-source status page, incident response and uptime monitoring

🧪 Added tests for all the endpoints (#613)

Kushagra Sharma04adcf8f163a
diff --git a/apps/server/src/v1/incidents.test.ts b/apps/server/src/v1/incidents.test.ts
index c14d10580..82d79bdf7 100644
--- a/apps/server/src/v1/incidents.test.ts
+++ b/apps/server/src/v1/incidents.test.ts
@@ -3,16 +3,21 @@ import { expect, test } from "bun:test";
import { api } from ".";
test("GET one Incident", async () => {
- const res = await api.request("/incident/1", {
+ const res = await api.request("/incident/2", {
headers: {
"x-openstatus-key": "1",
},
});
expect(res.status).toBe(200);
expect(await res.json()).toMatchObject({
- id: 1,
+ id: 2,
startedAt: expect.any(String),
monitorId: 1,
+ acknowledgedAt: null,
+ resolvedAt: null,
+ resolvedBy: null,
+ acknowledgedBy: null,
+
});
});
@@ -29,7 +34,7 @@ test("Update an incident ", async () => {
});
expect(res.status).toBe(200);
- expect(await res.json()).toMatchObject({
+ expect((await res.json())).toMatchObject({
acknowledgedAt: "2023-11-08T21:03:13.000Z",
monitorId: 1,
id: 2,
@@ -39,3 +44,75 @@ test("Update an incident ", async () => {
acknowledgedBy: null,
});
});
+
+test("Update an incident not in db should return 404", async () => {
+ const res = await api.request("/incident/404", {//accessing invalid monitor
+ method: "PUT",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ acknowledgedAt: "2023-11-08T21:03:13.000Z",
+ }),
+ });
+ expect(res.status).toBe(200);
+ expect((await res.json())).toMatchObject({
+ code: 404,
+ message:"Not Found"
+ });
+})
+
+test("Update an incident without auth key should return 401", async () => {
+ const res = await api.request("/incident/2", {
+ method: "PUT",
+ headers: {//not passing correct key
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ acknowledgedAt: "2023-11-08T21:03:13.000Z",
+ }),
+ });
+ expect(res.status).toBe(401);
+});
+
+test("Update an incident with invalid data should return 403", async () => {
+ const res = await api.request("/incident/2", {
+ method: "PUT",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({//passing incorrect body
+ acknowledgedAt: "2023-11-0",
+ }),
+ });
+ expect(res.status).toBe(400);
+ expect((await res.json())).toMatchObject({
+ error:{
+ issues: expect.any(Array),
+ name: "ZodError"
+ },
+ success: false,
+ });
+});
+
+test("Get all Incidents", async () => {
+ const res = await api.request("/incident", {
+ method:"GET",
+ headers: {
+ "x-openstatus-key": "1",
+ },
+ });
+ expect(res.status).toBe(200);
+ expect((await res.json())[0]).toMatchObject({
+ acknowledgedAt: null,
+ monitorId: 1,
+ id: 1,
+ startedAt: expect.any(String),
+ resolvedAt: null,
+ resolvedBy: null,
+ acknowledgedBy: null,
+ });
+
+});
\ No newline at end of file
diff --git a/apps/server/src/v1/monitor.test.ts b/apps/server/src/v1/monitor.test.ts
index 5f91fd273..a9e616c75 100644
--- a/apps/server/src/v1/monitor.test.ts
+++ b/apps/server/src/v1/monitor.test.ts
@@ -1,5 +1,4 @@
import { expect, test } from "bun:test";
-
import { api } from ".";
test("GET one monitor", async () => {
@@ -31,6 +30,18 @@ test("GET all monitor", async () => {
},
});
expect(res.status).toBe(200);
+ expect((await res.json())[0]).toMatchObject({
+ id: 1,
+ periodicity: "1m",
+ url: "https://www.openstatus.dev",
+ regions: ["ams"],
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ method: "POST",
+ body: '{"hello":"world"}',
+ headers: [{ key: "key", value: "value" }],
+ active: true,
+ });
});
test("Create a monitor", async () => {
@@ -53,7 +64,6 @@ test("Create a monitor", async () => {
},
body: JSON.stringify(data),
});
- // console.log(await res.text());
expect(res.status).toBe(200);
expect(await res.json()).toMatchObject({
@@ -69,3 +79,204 @@ test("Create a monitor", async () => {
active: true,
});
});
+
+test("Create a monitor without auth key should return 401", async () => {
+ const data = {
+ periodicity: "10m",
+ url: "https://www.openstatus.dev",
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ regions: ["ams", "gru"],
+ method: "POST",
+ body: '{"hello":"world"}',
+ headers: [{ key: "key", value: "value" }],
+ active: true,
+ };
+ const res = await api.request("/monitor", {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: JSON.stringify(data),
+ });
+ expect(res.status).toBe(401);
+});
+
+test("Create a monitor with invalid data should return 403", async () => {
+ const data = {
+ periodicity: 32, //not valid value
+ url: "https://www.openstatus.dev",
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ regions: ["ams", "gru"],
+ method: "POST",
+ body: '{"hello":"world"}',
+ headers: [{ key: "key", value: "value" }],
+ active: true,
+ };
+ const res = await api.request("/monitor", {
+ method: "POST",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify(data),
+ });
+ expect(res.status).toBe(400);
+ expect(await res.json()).toMatchObject({
+ error:{
+ issues: expect.any(Array),
+ name: "ZodError"
+ },
+ success: false,
+ });
+});
+
+test("Update a Monitor ", async () => {
+ const data = {
+ periodicity: "10m",
+ url: "https://www.openstatus.dev",
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ regions: ["ams"],
+ method: "GET",
+ body: '{"hello":"world"}',
+ headers: [{ key: "key", value: "value" }],
+ active: true,
+ };
+
+ const res = await api.request("/monitor/1", {
+ method: "PUT",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify(data),
+ });
+ expect(res.status).toBe(200);
+
+ expect(await res.json()).toMatchObject({
+ id: 1,
+ periodicity: "10m",
+ url: "https://www.openstatus.dev",
+ regions: ["ams"],
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ method: "GET",
+ body: '{"hello":"world"}',
+ headers: [{ key: "key", value: "value" }],
+ active: true,
+ });
+});
+
+test("Update a monitor not in db should return 404", async () => {
+ const data = {
+ periodicity: "10m",
+ url: "https://www.openstatus.dev",
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ regions: ["ams"],
+ method: "GET",
+ body: '{"hello":"world"}',
+ headers: [{ key: "key", value: "value" }],
+ active: true,
+ };
+
+ const res = await api.request("/monitor/404", {//accessing wrong monitor, just returns 404
+ method: "PUT",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify(data),
+ });
+ expect(res.status).toBe(200);
+
+ expect(await res.json()).toMatchObject({
+ code: 404,
+ message: "Not Found"
+ });
+});
+
+test("Update a monitor without auth key should return 401", async () => {
+ const data = {
+ periodicity: "5m",
+ url: "https://www.openstatus.dev",
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ regions: ["ams"],
+ method: "GET",
+ body: '{"hello":"world"}',
+ headers: [{ key: "key", value: "value" }],
+ active: true,
+ };
+ const res = await api.request("/monitor/3", {
+ method: "PUT",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: JSON.stringify(data),
+ });
+ expect(res.status).toBe(401);
+})
+test("Update a monitor with invalid data should return 403", async () => {
+ const data = {
+ periodicity: 9,//not passing correct value returns 403
+ url: "https://www.openstatus.dev",
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ regions: ["ams"],
+ method: "GET",
+ body: '{"hello":"world"}',
+ headers: [{ key: "key", value: "value" }],
+ active: true,
+ };
+ const res = await api.request("/monitor/3", {
+ method: "PUT",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify(data),
+ });
+ expect(res.status).toBe(400);
+ expect(await res.json()).toMatchObject({
+ error:{
+ issues: expect.any(Array),
+ name: "ZodError"
+ },
+ success: false,
+ });
+})
+
+test("Delete one monitor", async () => {
+ const res = await api.request("/monitor/3", {
+ method: "DELETE",
+ headers: {
+ "x-openstatus-key": "1",
+ },
+ });
+ expect(res.status).toBe(200);
+
+ expect(await res.json()).toMatchObject({
+ message: "Deleted"
+ });
+});
+
+test.todo("Get monitor daily Summary")
+// test("Get monitor daily Summary", async () => {
+// const res = await api.request("/monitor/1/summary", {
+// headers: {
+// "x-openstatus-key": "1",
+// },
+// });
+// expect(res.status).toBe(200);
+// expect(await res.json()).toMatchObject({
+// ok: 4,
+// count: 13,
+// avgLatency: 1,
+// day: "31-01-2024"
+// });
+// });
+
+
diff --git a/apps/server/src/v1/monitor.ts b/apps/server/src/v1/monitor.ts
index 131b93e6f..f6aaf8734 100644
--- a/apps/server/src/v1/monitor.ts
+++ b/apps/server/src/v1/monitor.ts
@@ -539,7 +539,6 @@ monitorApi.openapi(getMonitorStats, async (c) => {
const cache = await redis.get<z.infer<typeof dailyStatsSchemaArray>>(
`${monitorId}-daily-stats`,
);
-
if (cache) {
console.log("fetching from cache");
return c.jsonT({
diff --git a/apps/server/src/v1/statusReport.test.ts b/apps/server/src/v1/statusReport.test.ts
index 302953b06..8084e3d24 100644
--- a/apps/server/src/v1/statusReport.test.ts
+++ b/apps/server/src/v1/statusReport.test.ts
@@ -11,8 +11,6 @@ test("GET one status report", async () => {
expect(res.status).toBe(200);
expect(await res.json()).toMatchObject({
id: 1,
- status: "investigating",
- title: "Test Status Report",
// TODO: discuss if we should return `updates` instead of `status_report_updates`
status_report_updates: expect.any(Array),
});
@@ -27,14 +25,152 @@ test("create one status report", async () => {
},
body: JSON.stringify({
status: "investigating",
- date: "2023-11-08T21:03:13.000Z",
title: "Test Status Report",
}),
});
expect(res.status).toBe(200);
expect(await res.json()).toMatchObject({
id: expect.any(Number),
+ status_report_updates: expect.any(Array)
+ });
+});
+
+test("Create one status report without auth key should return 401", async () => {
+ const res = await api.request("/status_report", {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ status: "investigating",
+ title: "Test Status Report",
+ }),
+ });
+ expect(res.status).toBe(401);//unauthenticated
+});
+
+test("Create one status report with invalid data should return 403", async () => {
+ const res = await api.request("/status_report", {
+ method: "POST",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({//passing incompelete body
+ title: "Test Status Report",
+ }),
+ });
+ expect(res.status).toBe(400);
+ expect(await res.json()).toMatchObject({
+ error:{
+ issues: expect.any(Array),
+ name: "ZodError"
+ },
+ success: false,
+ });
+});
+
+test("Get all status report", async () => {
+ const res = await api.request("/status_report", {
+ headers: {
+ "x-openstatus-key": "1",
+ },
+ });
+ expect(res.status).toBe(200);
+ expect((await res.json())[0]).toMatchObject({
+ id: expect.any(Number),
+ status_report_updates: expect.any(Array),
+ });
+});
+
+test("Delete a status report", async () => {
+ const res = await api.request("/status_report/1", {
+ method: "DELETE",
+ headers: {
+ "x-openstatus-key": "1",
+ },
+ });
+ expect(res.status).toBe(200);
+ expect(await res.json()).toMatchObject({
+ message:"Deleted"
+ });
+});
+
+test("create a status report update", async () => {
+ const res = await api.request("/status_report/2/update", {
+ method: "POST",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ status: "investigating",
+ date: "2023-11-08T21:03:13.000Z",
+ message: "Test Status Report",
+ }),
+ });
+ expect(res.status).toBe(200);
+ expect(await res.json()).toMatchObject({
status: "investigating",
- title: "Test Status Report",
+ id: "1",
+ date: "Wed Nov 08 2023 21:03:13 GMT+0000 (Coordinated Universal Time)",
+ message: "Test Status Report"
+ });
+});
+
+test("Create a status report update not in db should return 404", async () => {
+ const res = await api.request("/status_report/404/update", {
+ method: "POST",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ status: "investigating",
+ date: "2023-11-08T21:03:13.000Z",
+ message: "Test Status Report",
+ }),
+ });
+ expect(res.status).toBe(200);
+ expect(await res.json()).toMatchObject({
+ code: 404,
+ message: "Not Found"
+ });
+});
+
+test("Create a status report update without auth key should return 401", async () => {
+ const res = await api.request("/status_report/2/update", {
+ method: "POST",
+ headers: {//not having the key returns unauthorized
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ status: "investigating",
+ date: "2023-11-08T21:03:13.000Z",
+ message: "Test Status Report",
+ }),
+ });
+ expect(res.status).toBe(401);
+});
+
+test("Create a status report update with invalid data should return 403", async () => {
+ const res = await api.request("/status_report/2/update", {
+ method: "POST",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({//passing in incompelete body
+ date: "2023-11-08T21:03:13.000Z",
+ message: "Test Status Report",
+ }),
+ });
+ expect(res.status).toBe(400);
+ expect(await res.json()).toMatchObject({
+ error:{
+ issues: expect.any(Array),
+ name: "ZodError"
+ },
+ success: false,
});
});
diff --git a/apps/server/src/v1/statusReport.ts b/apps/server/src/v1/statusReport.ts
index eda299e76..2e87a3472 100644
--- a/apps/server/src/v1/statusReport.ts
+++ b/apps/server/src/v1/statusReport.ts
@@ -331,7 +331,7 @@ statusReportApi.openapi(postRouteUpdate, async (c) => {
)
.get();
- if (!_statusReport) return c.jsonT({ code: 401, message: "Not authorized" });
+ if (!_statusReport) return c.jsonT({ code: 404, message: "Not Found" });
const _statusReportUpdate = await db
.insert(statusReportUpdate)
diff --git a/apps/server/src/v1/statusReportUpdate.test.ts b/apps/server/src/v1/statusReportUpdate.test.ts
index 45116c461..c37c5bde0 100644
--- a/apps/server/src/v1/statusReportUpdate.test.ts
+++ b/apps/server/src/v1/statusReportUpdate.test.ts
@@ -35,3 +35,42 @@ test("create one status report update ", async () => {
message: "test",
});
});
+
+test("create one status report update without auth key should return 401", async () => {
+ const res = await api.request("/status_report_update", {
+ method: "POST",
+ headers: {//not passing in the key
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ status: "investigating",
+ date: "2023-11-08T21:03:13.000Z",
+ message: "test",
+ status_report_id: 1,
+ }),
+ });
+ expect(res.status).toBe(401);
+
+});
+
+test("create one status report update with invalid data should return 403", async () => {
+ const res = await api.request("/status_report_update", {
+ method: "POST",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({//incompelete body
+ status: "investigating",
+ date: "2023-11-08T21:03:13.000Z",
+ }),
+ });
+ expect(res.status).toBe(400);
+ expect(await res.json()).toMatchObject({
+ error:{
+ issues: expect.any(Array),
+ name: "ZodError"
+ },
+ success: false,
+ });
+});
diff --git a/packages/db/src/seed.mts b/packages/db/src/seed.mts
index f24d8cf66..d88bf8f6c 100644
--- a/packages/db/src/seed.mts
+++ b/packages/db/src/seed.mts
@@ -73,9 +73,22 @@ async function main() {
method: "GET",
regions: "gru",
},
+ {
+ id: 3,
+ workspaceId: 1,
+ active: true,
+ url: "https://www.openstatus.dev",
+ name: "OpenStatus",
+ description: "OpenStatus website",
+ method: "GET",
+ periodicity: "1m",
+ regions: "ams",
+ headers: '[{"key":"key", "value":"value"}]',
+ body: '{"hello":"world"}',
+ },
])
.run();
-
+
await db
.insert(page)
.values({