Files
Termix/src/backend/tests/database/routes/host-locked-routes.test.ts
T
a89ccc6b07 v26.10.3 (#1692)
* feat: shell controller, TabStage, layout and theme plugins with repair view

* feat: move the classic layout into its own bundled plugin

* feat: move color themes into a bundled themes plugin

* fix: plugin tests and type checks work with a linked sdk

* fix: home tab, layout switch and missing layout fallbacks for looks

* chore: add classic layout and themes to the readme

* chore: drop example plugin links from the plugin guide

* feat: plugin filters by type, paged browse, hide plugins needing newer termix

* feat: move desktop, status, import, audit and branding features into plugins

* feat: host lock api so a plugin can load hosts from a config file (#1581)

* chore: add hosts config to the plugin table

* feat: login policy hook for plugins and optional https redirect (#1584)

* chore: add login security to the plugin table

* feat: api key socket tickets so a master can open remote sessions (#1654)

* feat: sync moves to a plugin, desktop gets multiple accounts (#1580, #1654)

* chore: add sync to the plugin table

* feat: move the sync screens and wizard out of core into the plugin (#1580)

* fix: add a neutral linked desktop hook for plugins (#1580)

* fix: show the linked account's name and admin flag in the app (#1580)

* feat: ssh transport providers, core ssh relay and plugin folders api

* feat: support FIDO2 security keys through ssh agent

* feat: add companion panels beside host tabs

* feat: mTLS client certificates for desktop server links

* feat: native web view for plugins in the desktop app

* fix: detect the desktop link window by Electron's default user agent

Fixes #1675

* fix: changelog for desktop link fix

* fix: keep text primary keys in the plugin data export

* fix: ship sharp binaries for Windows arm64 builds

* feat: client cert on the sync link types (sdk 1.0.18)

* chore: update translations

* feat: themed title bar for the desktop app (#1690)

* chore: changelog for the themed title bar

* fix: draw the title bar in the sidebar color

* feat: per host layout for the terminal panel bar

* chore: sdk 1.0.19 for the panel bar layout

* chore: bundle all 43 plugins for 26.10.3

* chore: update translations

---------

Co-authored-by: SirHephaistos <60616553+SirHephaistos@users.noreply.github.com>
Co-authored-by: Giovanny Aranda <garanda21@gmail.com>
2026-10-20 16:03:29 -05:00

131 lines
4.0 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import express, { type RequestHandler } from "express";
import request from "supertest";
const state = vi.hoisted(() => ({
updates: [] as Array<{ ids: number[]; update: Record<string, unknown> }>,
renamed: 0,
folderHosts: [] as Array<{ id: number }>,
}));
vi.mock("../../../utils/logger.js", () => ({
databaseLogger: { error: vi.fn(), warn: vi.fn(), info: vi.fn() },
sshLogger: { error: vi.fn(), warn: vi.fn(), info: vi.fn() },
pluginLogger: { warn: vi.fn() },
}));
vi.mock("../../../utils/database-save-trigger.js", () => ({
DatabaseSaveTrigger: { batched: (fn: RequestHandler) => fn },
}));
vi.mock("../../../hosts/defaults/overrides.js", () => ({
changeHostOverrides: async () => {},
}));
vi.mock("../../../hosts/defaults/recompute.js", () => ({
recompute: async () => ({ changedHostIds: [] }),
}));
vi.mock("../../../hosts/defaults/service.js", () => ({
folderCredentialIds: async () => [],
setFolderCredentialDefault: async () => {},
}));
vi.mock("../../../database/routes/host-plugin-settings.js", () => ({
setHostPluginEnabled: async () => true,
}));
vi.mock("../../../database/repositories/factory.js", () => ({
createCurrentHostRepository: () => ({
listBulkUpdateState: async (_userId: string, ids: number[]) =>
ids.map((id) => ({ id })),
updateManyForUser: async (
_userId: string,
ids: number[],
update: Record<string, unknown>,
) => {
state.updates.push({ ids, update });
},
}),
createCurrentHostFolderRepository: () => ({
listHostsInFolder: async () => state.folderHosts,
renameFolder: async () => {
state.renamed++;
return { updatedHosts: 1, updatedCredentials: 0 };
},
}),
createCurrentFolderAccessRepository: () => ({
renameFolder: async () => {},
}),
}));
const { registerHostBulkRoutes } =
await import("../../../database/routes/host-bulk-routes.js");
const { registerHostFolderRoutes } =
await import("../../../database/routes/host-folder-routes.js");
const { clearHostLocks, provideHostLocks } =
await import("../../../hosts/locks.js");
const pass: RequestHandler = (req, _res, next) => {
(req as unknown as { userId: string }).userId = "owner";
next();
};
function app() {
const router = express.Router();
registerHostBulkRoutes(router, pass, pass, pass);
registerHostFolderRoutes(router, {
authenticateJWT: pass,
requireViewPermission: pass,
requireEditPermission: pass,
requireDeletePermission: pass,
requireCredentialEditPermission: pass,
requireDataAccess: pass,
});
const server = express();
server.use(express.json());
server.use(router);
return server;
}
beforeEach(() => {
state.updates = [];
state.renamed = 0;
state.folderHosts = [];
provideHostLocks("cfg", "Config", {
isLocked: (hostId) => hostId === 2,
note: () => null,
allowManualCreate: () => true,
});
});
afterEach(() => clearHostLocks());
describe("routes on locked hosts", () => {
it("still lets a user pin a locked host", async () => {
await request(app())
.patch("/bulk-update")
.send({ hostIds: [1, 2], updates: { pin: true } })
.expect(200);
expect(state.updates).toEqual([{ ids: [1, 2], update: { pin: true } }]);
});
it("refuses a bulk change that touches a locked host", async () => {
const res = await request(app())
.patch("/bulk-update")
.send({ hostIds: [1, 2], updates: { folder: "Moved" } })
.expect(403);
expect(res.body).toMatchObject({ code: "HOST_LOCKED" });
expect(state.updates).toEqual([]);
});
it("refuses renaming a folder that holds a locked host", async () => {
state.folderHosts = [{ id: 1 }, { id: 2 }];
await request(app())
.put("/folders/rename")
.send({ oldName: "Prod", newName: "Live" })
.expect(403);
expect(state.renamed).toBe(0);
state.folderHosts = [{ id: 1 }];
await request(app())
.put("/folders/rename")
.send({ oldName: "Prod", newName: "Live" })
.expect(200);
expect(state.renamed).toBe(1);
});
});