Files
TREK/server/tests/unit/nest/managed.test.ts
T
Maurice bfb3bcd63c feat(config): mark an install as centrally administered
Some installs are run by somebody other than the person who administers them.
The operator owns the credentials, the configuration and the upgrade schedule,
and the admin of the instance is a user of the result rather than the one who
set it up. Nothing in the app knows that today, so it keeps offering settings
whose values are not the admin's to change, and the honest answer to half of
them would be a 403 the UI never anticipated.

TREK_MANAGED says it once. It is a single boolean and the surfaces that care
read it themselves, so adding one never comes back to the config layer.

Runtime-toggled, in the same class as DEMO_MODE, rather than a registerAs
token: roughly sixty tests mutate env mid-lifetime, and a value frozen per
built app breaks all of them. `=== true` on the way in, so an unset, empty or
misspelled value means off — a managed install sets it deliberately, and a typo
must not quietly take settings away from an admin who never asked for it.

The guard keys on a decorator, not on a path. MfaPolicyGuard documents where
the two literal path lists it replaced ended up: they stopped being maintained,
and every endpoint added afterwards silently fell on the wrong side. A marker
on the handler cannot drift away from the handler. It carries a mandatory
reason, which is for the next reader of the source rather than for the client,
so the marks stay auditable.

It registers third, after the auth and MFA guards, so a stranger still gets the
401 those two would have given them instead of a 403 that would confirm the
route exists. And it checks the mode before it touches metadata, because it
runs on every request of every install and almost all of them are self-hosted.

One 403 body, shared by REST and MCP. DEMO_MODE grew two texts for the same
condition and neither can be changed now without hunting for the other.

The flag reaches the client through app-config so the UI can stop offering what
the server would refuse anyway, but it is deliberately not persisted the way
demo_mode is: two installs can share a browser profile, and a stale "this one
is managed" would take settings away on an install that never set it.

No route carries the marker yet. The per-domain marks follow. Unset, which is
every self-hosted install, nothing about this changes anything.
2026-08-15 11:45:11 +02:00

87 lines
3.1 KiB
TypeScript

/**
* TREK_MANAGED — the switch that says the configuration belongs to whoever runs
* this install rather than to its admin.
*
* Runtime-toggled, exactly like DEMO_MODE: read live on every access, never
* snapshotted into a field or a registerAs token, because roughly sixty tests
* mutate process.env mid-lifetime and a frozen value breaks all of them.
* MANAGED-004 is the case that pins it.
*/
import { describe, it, expect, afterEach } from 'vitest';
import { isManagedBlocked, MANAGED_FORBIDDEN, MANAGED_FORBIDDEN_ERROR, ManagedForbidden } from '../../../src/nest/common/managed';
import { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
import { deriveManaged } from '../../../src/app-config/derive';
const env = new RuntimeEnvService();
afterEach(() => {
delete process.env.TREK_MANAGED;
});
describe('deriveManaged', () => {
it('MANAGED-001: off when the variable is unset', () => {
expect(deriveManaged({}).enabled).toBe(false);
});
it('MANAGED-002: on for the documented truthy spellings', () => {
for (const value of ['true', 'TRUE', '1', 'yes', 'on']) {
expect(deriveManaged({ TREK_MANAGED: value }).enabled).toBe(true);
}
});
it('MANAGED-003: off for an empty, unparseable or falsy value', () => {
// A typo must not take settings away from an admin who never asked for it,
// so anything that is not recognisably true stays off.
for (const value of ['', 'false', '0', 'no', 'off', 'maybe', 'TREK_MANAGED']) {
expect(deriveManaged({ TREK_MANAGED: value }).enabled).toBe(false);
}
});
});
describe('RuntimeEnvService.isManaged', () => {
it('MANAGED-004: reads the env live, so a toggle takes effect without a restart', () => {
expect(env.isManaged()).toBe(false);
process.env.TREK_MANAGED = 'true';
expect(env.isManaged()).toBe(true);
delete process.env.TREK_MANAGED;
expect(env.isManaged()).toBe(false);
});
});
describe('isManagedBlocked', () => {
it('MANAGED-005: false on a self-hosted install', () => {
expect(isManagedBlocked(env)).toBe(false);
});
it('MANAGED-006: true once the instance is centrally administered', () => {
process.env.TREK_MANAGED = 'true';
expect(isManagedBlocked(env)).toBe(true);
});
});
describe('the shared 403', () => {
it('MANAGED-007: one body, and it does not name a reason', () => {
// Pinned because DEMO_MODE grew two texts for one condition (REST says
// 'Self-host TREK for full functionality.', MCP says something else) and
// neither can now be changed without hunting for the other.
expect(MANAGED_FORBIDDEN_ERROR).toEqual({
error: 'This is configured by the operator of this instance.',
code: 'MANAGED_FORBIDDEN',
});
});
});
describe('ManagedForbidden', () => {
it('MANAGED-008: writes the reason onto the handler for the next reader', () => {
class Controller {
@ManagedForbidden('the operator holds this credential')
handler() {}
}
const meta = Reflect.getMetadata(MANAGED_FORBIDDEN, Controller.prototype.handler);
expect(meta).toEqual({ reason: 'the operator holds this credential' });
});
});