Files
stack/packages/cli/tests/access-boot.test.mjs
T
jason.woltje afef0ea887 launchpad: row 294 FED R6 build and plan, granted-user sign-in (unrun, #1800)
Console granted-user sign-in, a user-cli transport per session, the
per-request grant re-read, the owner-only gate and user page, the notice,
the hub label and peer links, accessContextFor peers, and the bus boot
access wiring (sage-3142). Tests G1-G8 and B1-B3 are written and not yet
run; runs wait for Sage's CLEAR. Choices C1-C9 in
agents/launchpad/work/queue-294/BUILD.md.
2026-10-23 01:37:03 -05:00

72 lines
3.2 KiB
JavaScript

// FED R6 (#1800, sage-3142): the bus host's boot config carries the runtime's `access`
// ({ file, context }) when <configDir>/access.json exists, with the cli's accessContextFor as the
// context. The broker then binds a granted user; with no file, or a file R1's loader refuses, no
// user binds and the owner's bus still boots. Scratch roots only. The boot config goes through a
// JSON round trip first, as it does over the host's IPC.
import test from "node:test";
import assert from "node:assert/strict";
import { chmodSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { startBroker } from "../../bus/src/runtime.mjs";
import { bootConfig, loadSystem } from "../src/config.mjs";
import { fixture, tmp } from "./helpers.mjs";
const SSO = { sso: { providers: [{ id: "authentik", subjects: [{ sub: "jason-sub" }] }] } };
const DOC = {
accessVersion: 1,
revision: 1,
users: [{ id: "alice", home: "factory", subjects: [{ provider: "authentik", sub: "alice-sub" }] }],
grants: [{ id: "g-1", user: "alice", business: "acme", projects: ["*"], instances: ["*"], level: "collaborator" }],
};
// A scratch instance whose system config names the sign-in provider and one peer, and the boot
// config bootConfig makes for it. `file` is the access file's text, or null for none.
async function boot(t, file) {
const f = fixture(tmp(t, "mosaic-access-boot-"), "acme");
const system = {
...loadSystem({ env: f.env }),
console: SSO,
federation: { self: "hub", peers: [{ id: "factory", origin: "https://factory.example.test", label: "Software factory" }] },
};
const path = join(dirname(f.config), "access.json");
if (file !== null) {
writeFileSync(path, file);
chmodSync(path, 0o600);
}
const warnings = [];
const { trackers, ...config } = JSON.parse(JSON.stringify(bootConfig({ system, businessId: "acme", env: f.env, warn: (w) => warnings.push(w) })));
const runtime = await startBroker(config);
t.after(() => runtime.close());
const bind = (user) => {
try {
return runtime.broker.bindUser({ business: "acme", user }) ? "bound" : "none";
} catch (e) {
return e.code;
}
};
return { config, path, bind, warnings };
}
test("R6 B1 a boot with access.json binds a granted user, whose home is a configured peer", async (t) => {
const s = await boot(t, JSON.stringify(DOC));
assert.equal(s.config.access.file, s.path);
assert.deepEqual(s.config.access.context.peers, ["factory"]);
assert.deepEqual(s.config.access.context.ssoSubjects, [{ provider: "authentik", sub: "jason-sub" }]);
assert.equal(s.bind("alice"), "bound");
assert.equal(s.bind("bob"), "access-revoked");
});
test("R6 B2 a boot without access.json has no access and binds no user", async (t) => {
const s = await boot(t, null);
assert.equal(Object.hasOwn(s.config, "access"), false);
assert.equal(s.bind("alice"), "access-revoked");
});
test("R6 B3 a boot with a file R1's loader refuses still boots and binds no user", async (t) => {
for (const text of ["{", JSON.stringify({ ...DOC, users: [{ ...DOC.users[0], home: "elsewhere" }] })]) {
const s = await boot(t, text);
assert.equal(s.config.access.file, s.path);
assert.equal(s.bind("alice"), "access-revoked", text);
}
});