Files
ihasmail/server/src/permissionSchema.test.ts
T
jcoffey-dev a00d07b430 Add Roles to Administration, with Stalwart's permissions in every language
A role is a named set of permissions given to accounts, groups and
tenants. It gets its own section under a new Access heading: every role
listed with the permissions it grants once its bases are followed, and a
panel to create, edit and delete one.

A role builds on others and has everything they grant; a denial anywhere in
the tree wins, which is how Stalwart resolves it (permissions.rs unions
enabled and disabled across the tree, then subtracts). The picker is
Stalwart's own list of permissions, under its headings, searchable and
filterable to what is granted or set here. Each permission is not set,
allowed or denied, and one that is inherited says which role it comes from.
Only permissions the viewer holds can be allowed, because Stalwart refuses
the rest, and a role carrying anything the viewer lacks opens read-only with
no delete, because Stalwart checks a grant but not a delete. Saving sends a
pointer for each permission and base role that changed.

The roles Stalwart hands out by default, read from x:Authentication, say so
before they are changed and cannot be deleted here; a role still in use is
kept by the server, and the refusal names what uses it.

The permission list is Stalwart's schema. A new route, GET
/api/admin/permissions, fetches /api/schema as the signed-in account and
returns only names and labels, behind the same two gates as the registry
methods and held in memory for an hour. Its labels are English only, so
every one of the 661 has a translation in each of the eight other
languages, in its own file keyed by permission name and loaded only when
Roles opens. A permission a later Stalwart adds shows its English label. A
test holds every language to the 0.16.22 snapshot: nothing missing, nothing
stale.

The mock answers x:Role/set with the grant check, loops and in-use
refusals, reads the defaults from x:Authentication, and serves the schema
gzipped as the real one is.

Fifty-two new strings and two plurals in all nine catalogues, and 661
permission labels with 59 headings in each of the eight translations.
2026-09-15 09:13:05 -07:00

29 lines
1.3 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { gzipSync } from "node:zlib";
import { extractPermissions, parseSchemaBody } from "./permissionSchema.js";
/** Stalwart's permission list, out of the registry schema it serves at /api/schema. */
test("the permission list is enums.Permission, names and labels, once each", () => {
const schema = { objects: {}, enums: { Permission: [
{ name: "sysAccountGet", label: "Accounts Management: Get accounts" },
{ name: "authenticate", label: "" },
{ name: "sysAccountGet", label: "a repeat" },
{ label: "no name" },
"not an object",
] } };
assert.deepEqual(extractPermissions(schema), [
{ name: "sysAccountGet", label: "Accounts Management: Get accounts" },
{ name: "authenticate", label: "authenticate" },
]);
assert.deepEqual(extractPermissions({ enums: {} }), []);
assert.deepEqual(extractPermissions(null), []);
});
test("the schema reads whether or not the transport already inflated it", () => {
const doc = { enums: { Permission: [{ name: "impersonate", label: "Act on behalf of another user" }] } };
const plain = new TextEncoder().encode(JSON.stringify(doc));
assert.deepEqual(parseSchemaBody(plain), doc);
assert.deepEqual(parseSchemaBody(new Uint8Array(gzipSync(plain))), doc);
});