Backend: grants accept a list of permissions; PUT /access/grants edits or renews a set atomically; refusals carry a reason; the grant list includes refresh grants per relation and the administrator-only permissions; users keep their last groups and admin flag; the emergency operator cannot hold grants. Frontend: the Access screen shows the grantable permissions as a checkbox tree with dependencies, rows grouped by person or group, orbit and end date, expiry and no-effect states, and what each person can do right now. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
110 lines
6.3 KiB
JavaScript
110 lines
6.3 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import {
|
|
branchState, expiryState, groupGrants, hasNoEffect, neededBy, personAccess, reasonText, roleLabel, seenGroups, setMany, setPermission,
|
|
} from "../src/lib/access.ts";
|
|
|
|
const roles = [
|
|
{ name: "viewer", permissions: ["orbit.read", "snapshot.read"] },
|
|
{ name: "capturer", permissions: ["orbit.read", "snapshot.read", "capture.run"] },
|
|
{ name: "downloader", permissions: ["orbit.read", "snapshot.read", "snapshot.download"] },
|
|
];
|
|
const NOW = 1_000_000;
|
|
const grant = (id, principal, permission, extra = {}) => ({
|
|
id, principal_type: "user", principal, permission, scope_type: "global", scope_id: null, expires_at: null, created_by: "t", created_at: 1, ...extra,
|
|
});
|
|
|
|
test("ticking a permission ticks what it needs; unticking removes what depends on it", () => {
|
|
const download = setPermission(new Set(), "snapshot.download", true);
|
|
assert.deepEqual([...download].sort(), ["orbit.read", "snapshot.download", "snapshot.read"]);
|
|
const noList = setPermission(download, "snapshot.read", false);
|
|
assert.deepEqual([...noList], ["orbit.read"], "download needs list, so it goes too");
|
|
assert.deepEqual(neededBy(new Set(["orbit.read", "capture.run"]), "orbit.read"), ["capture.run"]);
|
|
assert.deepEqual(neededBy(new Set(["orbit.read"]), "orbit.read"), []);
|
|
});
|
|
|
|
test("a branch reports all, some or none and ticks as a whole", () => {
|
|
const ids = ["snapshot.read", "snapshot.download"];
|
|
assert.equal(branchState(new Set(), ids), "none");
|
|
assert.equal(branchState(new Set(["snapshot.read"]), ids), "some");
|
|
const all = setMany(new Set(), ids, true);
|
|
assert.equal(branchState(all, ids), "all");
|
|
assert.ok(all.has("orbit.read"));
|
|
assert.equal(branchState(setMany(all, ids, false), ids), "none");
|
|
});
|
|
|
|
test("names a set of permissions from the hub's roles, never inventing one", () => {
|
|
assert.equal(roleLabel(["snapshot.read", "orbit.read"], roles), "Viewer");
|
|
assert.equal(roleLabel(["orbit.read", "snapshot.read", "capture.run"], roles), "Capturer");
|
|
assert.equal(roleLabel(["orbit.read", "snapshot.read", "capture.run", "snapshot.download"], roles), "Capturer + download");
|
|
assert.equal(roleLabel(["capture.run"], roles), "Custom");
|
|
assert.equal(roleLabel([], roles), "Custom");
|
|
assert.equal(roleLabel(["orbit.read"], []), "Custom", "no roles known: always custom");
|
|
});
|
|
|
|
test("one row per person, orbit and end date, with permissions in tree order", () => {
|
|
const rows = groupGrants([
|
|
grant("1", "u", "capture.run"), grant("2", "u", "orbit.read"), grant("3", "u", "snapshot.read"),
|
|
grant("4", "u", "orbit.read", { scope_type: "orbit", scope_id: "o1" }),
|
|
grant("5", "u", "snapshot.read", { expires_at: 5 }),
|
|
grant("6", "dev", "orbit.read", { principal_type: "group" }),
|
|
]);
|
|
assert.equal(rows.length, 4);
|
|
assert.deepEqual(rows[0].permissions, ["orbit.read", "snapshot.read", "capture.run"]);
|
|
assert.deepEqual(rows[0].ids, ["1", "2", "3"]);
|
|
});
|
|
|
|
test("expiry is none, soon (within two weeks) or expired", () => {
|
|
assert.equal(expiryState(null, NOW), "none");
|
|
assert.equal(expiryState(NOW, NOW), "expired", "expiry is exclusive, as on the hub");
|
|
assert.equal(expiryState(NOW + 86_400, NOW), "soon");
|
|
assert.equal(expiryState(NOW + 30 * 86_400, NOW), "none");
|
|
});
|
|
|
|
test("a grant without orbit.read has no effect, unless another grant of theirs supplies it", () => {
|
|
const all = [grant("1", "u", "capture.run", { scope_type: "orbit", scope_id: "o1" })];
|
|
const [row] = groupGrants(all);
|
|
assert.equal(hasNoEffect(row, all, [], NOW), true);
|
|
const withGlobal = [...all, grant("2", "u", "orbit.read")];
|
|
assert.equal(hasNoEffect(row, withGlobal, [], NOW), false, "a global orbit.read covers it");
|
|
const otherOrbit = [...all, grant("3", "u", "orbit.read", { scope_type: "orbit", scope_id: "o2" })];
|
|
assert.equal(hasNoEffect(row, otherOrbit, [], NOW), true, "another orbit does not");
|
|
const viaGroup = [...all, grant("4", "qa", "orbit.read", { principal_type: "group" })];
|
|
assert.equal(hasNoEffect(row, viaGroup, ["qa"], NOW), false, "a group of theirs supplies it");
|
|
assert.equal(hasNoEffect(row, viaGroup, [], NOW), true, "a group they are not in does not");
|
|
const expired = [grant("5", "u", "capture.run", { expires_at: NOW - 1 })];
|
|
assert.equal(hasNoEffect(groupGrants(expired)[0], expired, [], NOW), false, "an expired grant is shown as expired instead");
|
|
});
|
|
|
|
test("a person is an administrator, waiting, or described by everything that reaches them", () => {
|
|
const user = (extra) => ({ id: "u", issuer: "i", display_name: "U", active: true, created_at: 1, last_login_at: 1, is_emergency: false, admin: false, groups: [], ...extra });
|
|
assert.equal(personAccess(user({ admin: true }), [], [], roles, NOW).kind, "admin");
|
|
assert.equal(personAccess(user(), [], [], roles, NOW).kind, "waiting");
|
|
const viaGroup = personAccess(user({ groups: ["dev"] }), [grant("1", "dev", "orbit.read", { principal_type: "group" }), grant("2", "dev", "snapshot.read", { principal_type: "group" })], [], roles, NOW);
|
|
assert.deepEqual([viaGroup.kind, viaGroup.label, viaGroup.via], ["granted", "Viewer", ["dev"]]);
|
|
const refreshOnly = personAccess(
|
|
user(), [],
|
|
[{ id: "r", principal_type: "user", principal: "u", relation_id: "x", version: 1, active: true, source_name: "prod", target_name: "stg", expires_at: null, created_at: 1 }],
|
|
roles, NOW,
|
|
);
|
|
assert.deepEqual([refreshOnly.kind, refreshOnly.refresh], ["granted", ["prod → stg"]]);
|
|
const ended = personAccess(
|
|
user(), [],
|
|
[{ id: "r", principal_type: "user", principal: "u", relation_id: "x", version: 1, active: false, source_name: "prod", target_name: "stg", expires_at: null, created_at: 1 }],
|
|
roles, NOW,
|
|
);
|
|
assert.equal(ended.kind, "waiting", "a grant on an ended relation gives nothing");
|
|
assert.equal(personAccess(user(), [grant("1", "u", "capture.run")], [], roles, NOW).noEffect, true);
|
|
});
|
|
|
|
test("seen groups are counted once per person and sorted by size", () => {
|
|
const u = (groups) => ({ groups });
|
|
assert.deepEqual(seenGroups([u(["dev", "qa"]), u(["dev", "dev"]), u(undefined)]), [{ name: "dev", people: 2 }, { name: "qa", people: 1 }]);
|
|
});
|
|
|
|
test("refusal codes become sentences and unknown ones fall back", () => {
|
|
assert.match(reasonText("already_granted", "x"), /Already granted/);
|
|
assert.equal(reasonText("something_new", "fallback"), "fallback");
|
|
assert.equal(reasonText(undefined, "fallback"), "fallback");
|
|
});
|