/** * Pinned pages follow the person: stored on their user, read with /api/me, replaced with PUT /api/me/favorites, and only ever their own. * * 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-favorites-")); 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 } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { meRouter, parseFavorites, MAX_FAVORITES } = await import("../src/routes/me.js"); await runMigrations(); describe("parseFavorites", () => { test("a stored list reads back; anything else is an empty list", () => { assert.deepEqual(parseFavorites('["/servers","/alerts"]'), ["/servers", "/alerts"]); assert.deepEqual(parseFavorites(null), []); assert.deepEqual(parseFavorites("not json"), []); assert.deepEqual(parseFavorites('{"a":1}'), []); assert.deepEqual(parseFavorites('["/ok", 5, null]'), ["/ok"]); }); }); describe("/api/me favourites", () => { let app: Server; let base = ""; let who = "anna"; before(async () => { await db.insert(users).values([ { oidcSub: "anna", name: "Anna", role: "viewer" }, { oidcSub: "bob", name: "Bob", role: "operator" }, ]); const a = express(); a.use(express.json()); a.use((req, _res, next) => { if (who) (req as unknown as { session: unknown }).session = { user: { sub: who } }; else (req as unknown as { session: unknown }).session = {}; next(); }); a.use("/api/me", meRouter); 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/me`; }); after(() => { app.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); const put = (paths: unknown) => fetch(`${base}/favorites`, { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ paths }) }); const me = async () => (await (await fetch(base)).json()).user; test("a new user has none", async () => { assert.deepEqual((await me()).favorites, []); }); test("a saved list comes back with the user, in order, with repeats removed", async () => { const res = await put(["/servers", "/alerts", "/servers", "/settings/notifications"]); assert.equal(res.status, 200); assert.deepEqual((await res.json()).favorites, ["/servers", "/alerts", "/settings/notifications"]); assert.deepEqual((await me()).favorites, ["/servers", "/alerts", "/settings/notifications"]); }); test("it is replaced, not added to, and can be emptied", async () => { await put(["/maintenance"]); assert.deepEqual((await me()).favorites, ["/maintenance"]); await put([]); assert.deepEqual((await me()).favorites, []); }); test("one person's list is not another's", async () => { who = "anna"; await put(["/secrets"]); who = "bob"; assert.deepEqual((await me()).favorites, []); await put(["/dns"]); who = "anna"; assert.deepEqual((await me()).favorites, ["/secrets"]); }); test("anything that isn't an in-app path is refused, as is a list that is too long", async () => { for (const bad of [["https://evil.example"], ["//evil.example"], ["servers"], ["/a b"], ["/