lawsynth/services/api/tests/authorization_test.py
caelum0x bd58228c14 feat: complete manifest services layer — decomposed modules + Rust gateway
Build the manifest's full decomposed services architecture as real, tested code:
- scheduler: 16 modules (queue/pool/policy/quota/placement/recovery + metrics/
  events/backoff/health/fairness/shutdown + honest nats seam) — 130 tests
- worker: 16 modules (execute/lease/resources/limits + heartbeat/events/telemetry/
  sandbox/recovery/cleanup/upload/artifacts + honest plugins seam) — 52 tests
- api: 20 modules (auth/authz/middleware + per-resource handlers + telemetry/
  facades), app.py now a thin composition root — 98 tests
- gateway: new std-only Rust reverse-proxy crate (proxy/routing/auth/cors/
  rate_limit/retry/timeouts/health/metrics/events/tracing + honest tls seam) — 110 tests

Every one of the 3,161 manifest files now exists. Extractions are behavior-
preserving (all prior tests green); new modules add real capability; nats/tls/
plugins/sandbox are honest documented seams, not fakes.

Rust workspace 352->605 tests; API 35->98. clippy -D warnings + fmt clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-17 19:55:26 +03:00

50 lines
1.4 KiB
Python

"""Unit tests for the scope-authorization adapter."""
from __future__ import annotations
import pytest
from lawsynth_api.authorization import (
ADMIN,
READ,
SCOPES,
WRITE,
require_scope,
require_scope_or_problem,
)
from lawsynth_api.middleware import RequestProblem
from lawsynth_server.auth import Principal
from lawsynth_server.errors import AuthorizationError
def _principal(*scopes: str) -> Principal:
return Principal(subject="token:test", organization_id="acme", scopes=frozenset(scopes))
def test_scope_vocabulary_is_the_expected_set():
assert SCOPES == frozenset({READ, WRITE, ADMIN})
def test_require_scope_allows_matching_scope():
require_scope(_principal(READ), READ) # does not raise
def test_require_scope_allows_admin_for_any_scope():
require_scope(_principal(ADMIN), WRITE) # admin is a superset
def test_require_scope_rejects_missing_scope_with_domain_error():
with pytest.raises(AuthorizationError):
require_scope(_principal(READ), WRITE)
def test_require_scope_or_problem_maps_failure_to_403():
with pytest.raises(RequestProblem) as excinfo:
require_scope_or_problem(_principal(READ), WRITE)
assert excinfo.value.status == 403
assert excinfo.value.code == "forbidden"
def test_require_scope_or_problem_passes_when_authorized():
require_scope_or_problem(_principal(READ, WRITE), WRITE) # does not raise