godcrm/backend/routes/v3/system/__tests__/helpers.test.js
Holetron 15a57faa8b sync: land agent-tools principal identity, tool audit, mailbox provisioning, type ratchet
Snapshot of private main @ 10300fb6 through the extended publish excision
(SC-SIM module, internal infra/ops scripts, personal material, debug cruft)
plus the IP/e-mail scrub.

Notable:
- backend/services/agent-tools/principal.js — principal identity +
  owner-only naturalization (ADR-165 WP-4), with its two consumers.
- backend/services/audit/toolAudit.js — hash-chained tool audit.
- backend/services/mail/provisionMailbox.js + migration 089 — ADR-198
  mailbox provisioning.
- shared/column-types.js + migration 088 — ADR-0041R type ratchet.
- migration 090 — ADR-0066-B agent provenance.
- docs/MANIFESTO.md, docs/MCP.md — the two README links that were dangling.
- LICENSE now also lives in the source tree; package.json declares MIT.
- run-pes.js / deploy-package.cjs no longer carry credential fallbacks.
2026-09-03 17:16:42 +03:00

69 lines
2.6 KiB
JavaScript

/**
* ADR-189 — shared owner-role gate (`requireOwnerRole`) regression.
*
* `requireOwnerRole(req, res)` is the portable replacement for the
* `SELECT … spaces WHERE id = 11` + `owner_id === req.user.id` gate that
* audit.js, owner.js, agent-permissions.js and admin/chatNotifications.js
* each carried inline. Those gates 500'd `OWNER_SPACE_MISSING` on any
* from-empty community box (box 205 / devcrm) where space 11 never exists.
*
* DB-less by construction: the helper is a pure function of `req.user.role`
* and never touches the database — which is the whole point of the fix.
* Locks the branches that matter:
* 1. role='owner' → true, no response written
* 2. role='admin' → false + 403 (admin alone is NOT sufficient)
* 3. unauthenticated → false + 403
* 4. regression: owner passes with NO space 11 present — never 500
*
* NOTE (ADR-189): the gate admits ANY role='owner' principal — a deliberate,
* reviewed widening on master A. The `_secrets` vault (secrets.js) stays
* identity-based and is intentionally NOT converged onto this helper.
*/
import { describe, it, expect, vi } from 'vitest';
import { requireOwnerRole } from '../helpers.js';
function fakeRes() {
const res = { statusCode: null, body: null };
res.status = vi.fn((code) => {
res.statusCode = code;
return res;
});
res.json = vi.fn((payload) => {
res.body = payload;
return res;
});
return res;
}
describe('requireOwnerRole (ADR-189 owner-gate convergence)', () => {
it('admits role=owner and writes no response', () => {
const res = fakeRes();
const ok = requireOwnerRole({ user: { id: 1, role: 'owner' } }, res);
expect(ok).toBe(true);
expect(res.status).not.toHaveBeenCalled();
});
it('rejects role=admin with 403 (admin alone is not sufficient)', () => {
const res = fakeRes();
const ok = requireOwnerRole({ user: { id: 2, role: 'admin' } }, res);
expect(ok).toBe(false);
expect(res.statusCode).toBe(403);
});
it('rejects an unauthenticated request (no req.user) with 403', () => {
const res = fakeRes();
const ok = requireOwnerRole({}, res);
expect(ok).toBe(false);
expect(res.statusCode).toBe(403);
});
it('admits the owner when no space 11 exists (from-empty community box) — never 500', () => {
// The convergence removes the `spaces` read entirely, so a from-empty
// box with no space 11 can no longer 500 OWNER_SPACE_MISSING.
const res = fakeRes();
const ok = requireOwnerRole({ user: { id: 1, role: 'owner' } }, res);
expect(ok).toBe(true);
expect(res.statusCode).not.toBe(500);
});
});