dorfteich/apps/api/src/permissions/pond-permission-cache.test.ts
Claude Fable 5 0c6494f209
All checks were successful
CD / Build and push images (push) Successful in 2m54s
CI / Lint, typecheck, test (push) Successful in 2m25s
CI / Auth e2e pack (push) Successful in 2m58s
CI / Build container images (push) Has been skipped
CD / Deploy to Test (push) Successful in 9s
CD / Smoke tests against Test (push) Successful in 1m12s
CD / Promote to Int (push) Successful in 12s
Enforce permissions in API guards and retire interim access (#52)
Every route now declares its access rule explicitly and is enforced
through the shared resolution algorithm (permissions.md):

- PermissionGuard + decorators (@RequiresPondRole, @RequiresPagePermission,
  @RequiresAttachmentPermission, @AuthenticatedOnly) applied to every
  route; a route-enumeration test proves full coverage alongside
  @Public()/Site-Admin-guarded routes.
- 404/403 policy (documented in README conventions): denied reads answer
  404 (existence hiding), denied writes on readable things answer 403;
  trash views need write capability (ADR 0013).
- PermissionService resolves page/pond questions via the shared resolver,
  with an in-process pond-context cache (grants + label parents) that is
  invalidated on every grant/label-tree change and TTL-bounded as a
  multi-process safety net. Grant changes also fire pond_access_changed
  for collab revalidation (#39/#53).
- shared: pond-scope resolution (hasPondRole, canSeePond) next to the
  page resolver; grant wire schemas + GrantView.
- Owner Pond-Admin grants: migration backfill for all existing ponds,
  created transactionally with every new pond (shared + personal + seed).
- Grant CRUD under /ponds/:id/grants (pond_admin-gated) with structural
  and referential validation, last-admin protection, audit logs.
- InterimAccessService deleted; page lists, search, backlinks, phantom
  links, and trash listings are filtered per page through the resolver;
  collab tokens are now truly ro for readers.
- Fixture-matrix e2e (reader/editor/pond admin/foreign, label-deny,
  authenticated-subject, revoke-then-immediate-deny cache test).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PGdhRiwU1WRL4XxJfZYipY
2026-07-09 16:31:41 +02:00

37 lines
1.2 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { PondPermissionCache, PondPermissionContext } from './pond-permission-cache';
const CTX: PondPermissionContext = { grants: [], labelParents: {} };
describe('PondPermissionCache (issue #52)', () => {
beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());
it('serves a stored context and drops it on invalidate', () => {
const cache = new PondPermissionCache();
cache.set('pond-1', CTX);
expect(cache.get('pond-1')).toBe(CTX);
cache.invalidate('pond-1');
expect(cache.get('pond-1')).toBeUndefined();
});
it('invalidating one pond leaves others untouched', () => {
const cache = new PondPermissionCache();
cache.set('pond-1', CTX);
cache.set('pond-2', CTX);
cache.invalidate('pond-1');
expect(cache.get('pond-2')).toBe(CTX);
});
it('expires entries after the TTL (multi-process staleness bound)', () => {
const cache = new PondPermissionCache();
cache.set('pond-1', CTX);
vi.advanceTimersByTime(PondPermissionCache.TTL_MS - 1);
expect(cache.get('pond-1')).toBe(CTX);
vi.advanceTimersByTime(2);
expect(cache.get('pond-1')).toBeUndefined();
});
});