Files
loki/scripts/test/project-capabilities.ts
CatoandClaude Opus 5 ae92bde49e test(access): pin the project permission boundary, which nothing tested (#795)
Priority 5 of the 2026-09-17 review asks for the journey to be proved
with an editor and a reporter account. The live two-account walk still
needs two real sign-ins and is NOT what this does — see below. This is
the half that needs no accounts, and it turned out to be missing
entirely.

`getProjectAccess` is the one authorization decision every project
surface reads, and the rule inside it —

    canEdit: member.role === "editor",
    canManageMembers: false,

— lived in a function that cannot be imported without a DATABASE_URL. So
it had no test. The only way to find out that a viewer could edit, or
that an editor could add members, was to sign in as one and look. A
boundary whose only proof is a manual walk regains a hole the first time
someone adds a role.

The rule is now its own pure module and the query reads it. Behaviour is
unchanged — same two expressions, one place — and it is a Record over
ProjectRole, so adding a role without deciding its capabilities is a
type error rather than a silent `false` or, worse, a copy of another
role's answer.

Six checks, including two that hold for EVERY declared role rather than
the two that exist today: nobody but the owner can manage members, and
no collaborator role is silently as powerful as the owner. That is the
part a future role would break.

Proved by mutation: granting `viewer` canEdit fails with "a reporter
must not be able to dispatch work" — a viewer is the role a reporter
gets on the project they filed against, so that assertion is the
tenancy boundary itself.

WHAT THIS DOES NOT DO: it does not prove the end-to-end journey with two
real accounts. That needs credentials I do not have, and creating
accounts is not something to do unasked. Priority 5 stays open for the
live walk; this makes the rule it would be checking impossible to break
silently in the meantime.

tsc clean, lint 0 errors, test:unit 205/205.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-18 10:01:49 -07:00

87 lines
2.9 KiB
TypeScript

/**
* The project permission boundary, pinned.
*
* Priority 5 of the 2026-09-17 review asks for the journey to be proved with an
* editor and a reporter account. The live two-account walk still needs two real
* sign-ins; this is the half that does NOT — the rule itself, which until now
* lived inside a DATABASE_URL-bound query and so had no test at all. The only
* way to discover that a viewer could edit was to sign in as one and look.
*
* These are the claims a permission bug would have to break.
*/
import assert from "node:assert/strict";
import {
capabilitiesForRole,
OWNER_CAPABILITIES,
type ProjectCapabilities,
} from "@/lib/project-capabilities";
import { PROJECT_ROLE_VALUES } from "@/db/schema/project-memberships";
let passed = 0;
const check = (label: string, fn: () => void) => {
fn();
passed += 1;
console.log(` ✓ ${label}`);
};
check("the owner may do everything, including manage members", () => {
const owner = capabilitiesForRole("owner");
assert.equal(owner.canEdit, true);
assert.equal(owner.canManageMembers, true);
assert.deepEqual(owner, OWNER_CAPABILITIES);
});
check("an editor works the project but never changes who can reach it", () => {
const editor = capabilitiesForRole("editor");
assert.equal(editor.canEdit, true, "an editor is there to do the work");
assert.equal(
editor.canManageMembers,
false,
"membership is the owner's decision about their own tenant",
);
});
// A reporter is given `viewer` on the project they filed against: they can watch
// the fix, they cannot spend the owner's runner on another one.
check("a viewer reads and nothing more", () => {
const viewer = capabilitiesForRole("viewer");
assert.equal(viewer.canEdit, false, "a reporter must not be able to dispatch work");
assert.equal(viewer.canManageMembers, false);
});
check("NOBODY but the owner can manage members", () => {
for (const role of PROJECT_ROLE_VALUES) {
assert.equal(
capabilitiesForRole(role).canManageMembers,
false,
`${role} must not be able to change the member list`,
);
}
});
check("every declared role has a decided answer — none falls through", () => {
for (const role of PROJECT_ROLE_VALUES) {
const caps: ProjectCapabilities | undefined = capabilitiesForRole(role);
assert.ok(caps, `${role} has no capabilities entry`);
assert.equal(typeof caps.canEdit, "boolean", `${role}.canEdit must be decided`);
assert.equal(
typeof caps.canManageMembers,
"boolean",
`${role}.canManageMembers must be decided`,
);
}
});
check("no collaborator role is silently as powerful as the owner", () => {
for (const role of PROJECT_ROLE_VALUES) {
assert.notDeepEqual(
capabilitiesForRole(role),
OWNER_CAPABILITIES,
`${role} was granted the full owner capability set`,
);
}
});
console.log(`\nproject-capabilities: ${passed} checks passed`);