/** * Saved views as favourites, the dashboard layout, and the share links that open a read-only wall without signing in. * * 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 type { AddressInfo } from "node:net"; import type { Server } from "node:http"; const dir = mkdtempSync(join(tmpdir(), "hm-share-")); 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, servers, integrations, auditLog, shareLinks, maintenanceWindows, secrets } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { meRouter, parseDashboardLayout } = await import("../src/routes/me.js"); const { shareLinksRouter } = await import("../src/routes/shareLinks.js"); const { publicWallRouter } = await import("../src/routes/publicWall.js"); const { eq } = await import("drizzle-orm"); await runMigrations(); describe("parseDashboardLayout", () => { test("anything that isn't a layout is the default layout", () => { const empty = { hidden: [], order: {} }; assert.deepEqual(parseDashboardLayout(null), empty); assert.deepEqual(parseDashboardLayout("not json"), empty); assert.deepEqual(parseDashboardLayout(JSON.stringify({ hidden: "Gitea" })), empty); assert.deepEqual(parseDashboardLayout(JSON.stringify({ hidden: ["Gitea"], order: { DNS: 1 } })), { hidden: ["Gitea"], order: { DNS: 1 } }); }); }); describe("favourites, dashboard layout and share links", () => { let app: Server; let base = ""; let who = "admin"; before(async () => { await db.insert(users).values([ { oidcSub: "admin", name: "Ada", role: "admin" }, { oidcSub: "viewer", name: "Vic", role: "viewer" }, ]); const hours = (n: number) => new Date(Date.now() - n * 3600_000).toISOString(); await db.insert(servers).values([ { name: "elsa", apiTokenHash: "a", apiTokenPrefix: "a", lastSeenAt: hours(5) }, // stopped reporting { name: "anna", apiTokenHash: "b", apiTokenPrefix: "b", lastSeenAt: new Date().toISOString() }, ]); // Something that alerts straight away (a process that has only just started doesn't alert about silent servers for a while). await db.insert(secrets).values({ name: "Old VPN token", type: "api_token", expiryDate: "2020-01-01", warnDays: 30 }); // Switched off, so collecting the alerts doesn't try to reach it. await db.insert(integrations).values({ type: "gitea", name: "Gitea", baseUrl: "https://git.lab", config: "{}", enabled: false }); const a = express(); a.use(express.json()); a.use((req, _res, next) => { (req as unknown as { session: unknown }).session = { user: { sub: who } }; next(); }); a.use("/api/me", meRouter); a.use("/api/share-links", shareLinksRouter); // The public route has no sign-in: a request there carries no session at all. const pub = express(); pub.use("/api/public/wall", publicWallRouter); a.use(pub); 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`; }); after(() => { app.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); const send = (method: string, path: string, body?: unknown) => fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) }); test("a page with its filters can be pinned, and anything that isn't a path in this app can't", async () => { const good = ["/servers", "/servers?status=offline", "/servers?q=elsa+running&sort=name&dir=desc&tag=back%C3%A5k"]; const res = await send("PUT", "/me/favorites", { paths: good }); assert.equal(res.status, 200); assert.deepEqual((await res.json()).favorites, good); for (const bad of ["//evil.example", "https://evil.example/x", "/servers?q=