/** * 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((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; 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; assert.equal(config.loginPassword, "very-secret"); assert.equal(config.syncMaintenance, false); }); });