Files
bobbanandClaude Sonnet 5.5 9e483f2163 Mirror Uptime Kuma's maintenance schedule as maintenance windows
Uptime Kuma shares its maintenance windows only over its dashboard Socket.IO
connection, which takes a login. With a login username and password on the
integration, "Sync schedule" on the Maintenance page (and a 15-minute
background pass when switched on) reads each window's running and next time
slot and the monitors it covers, and makes a maintenance window on every
server those monitors match. Windows that haven't started are listed as
Scheduled and silence nothing until they begin; ones ended or cancelled by
hand stay ended, and ones removed in Kuma are removed here.

maintenance_windows gets a source column (migration 0017) and active windows
now require their start to have passed. Adds socket.io-client, and tests
against a stand-in Uptime Kuma speaking the same protocol.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 23:43:47 +02:00

94 lines
3.7 KiB
TypeScript

/**
* The Uptime Kuma integration's new fields, saved through the real integrations route, reach the code that uses them:
* the login password is stored encrypted (not in the visible config), and the "keep in step" tick-box arrives as a real boolean.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import http from "node:http";
import type { AddressInfo } from "node:net";
const dir = mkdtempSync(join(tmpdir(), "hm-kuma-fields-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, integrations } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { integrationsRouter } = await import("../src/routes/integrations.js");
const { loadIntegrationConfig } = await import("../src/integrations/loadIntegration.js");
await runMigrations();
describe("Uptime Kuma maintenance fields", () => {
let app: http.Server;
let base = "";
before(async () => {
await db.insert(users).values({ oidcSub: "tester", name: "Tester", role: "admin" });
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: "tester" } };
next();
});
a.use("/api/integrations", integrationsRouter);
app = await new Promise<http.Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/integrations`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
test("the login password is sealed away and the tick-box is a boolean when loaded", async () => {
const res = await fetch(base, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
type: "uptimekuma",
name: "Kuma",
config: { url: "https://kuma.example.lan", password: "api-key", loginUsername: "hm-reader", loginPassword: "very-secret", syncMaintenance: true },
}),
});
assert.equal(res.status, 201, await res.clone().text());
const created = (await res.json()).integration;
const [row] = await db.select().from(integrations);
assert.ok(!(row.config ?? "").includes("very-secret"), "the password is not in the plain config column");
assert.match(row.config ?? "", /hm-reader/);
const loaded = await loadIntegrationConfig(created.id);
const config = loaded!.config as Record<string, unknown>;
assert.equal(config.loginPassword, "very-secret");
assert.equal(config.loginUsername, "hm-reader");
assert.equal(config.syncMaintenance, true);
});
test("editing without touching the login password keeps it", async () => {
const [row] = await db.select().from(integrations);
const res = await fetch(`${base}/${row.id}`, {
method: "PATCH",
headers: { "content-type": "application/json" },
body: JSON.stringify({ config: { syncMaintenance: false } }),
});
assert.equal(res.status, 200, await res.clone().text());
const config = (await loadIntegrationConfig(row.id))!.config as Record<string, unknown>;
assert.equal(config.loginPassword, "very-secret");
assert.equal(config.syncMaintenance, false);
});
});