lawsynth/services/api/tests/auth_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

48 lines
1.7 KiB
Python

"""Unit tests for the bearer-token authentication adapter."""
from __future__ import annotations
import pytest
from lawsynth_api.auth import ApiAuthenticator
from lawsynth_api.middleware import RequestProblem
from lawsynth_server.auth import Principal, TokenAuthenticator
TOKEN = "0123456789abcdef0123456789abcdef"
def _authenticator() -> ApiAuthenticator:
return ApiAuthenticator(TokenAuthenticator({TOKEN: ("acme", frozenset({"read", "write"}))}))
def test_authenticate_extracts_principal_from_header():
principal = _authenticator().authenticate({"Authorization": f"Bearer {TOKEN}"})
assert isinstance(principal, Principal)
assert principal.organization_id == "acme"
assert principal.scopes == frozenset({"read", "write"})
def test_authenticate_raises_domain_error_on_missing_header():
from lawsynth_server.errors import AuthenticationError
with pytest.raises(AuthenticationError):
_authenticator().authenticate({})
def test_authenticate_or_problem_maps_failure_to_401():
with pytest.raises(RequestProblem) as excinfo:
_authenticator().authenticate_or_problem({"Authorization": "Bearer wrong-token-000000000"})
assert excinfo.value.status == 401
assert excinfo.value.code == "authentication_required"
def test_or_problem_returns_principal_on_success():
principal = _authenticator().authenticate_or_problem({"Authorization": f"Bearer {TOKEN}"})
assert principal.organization_id == "acme"
def test_silent_returns_none_on_failure_and_principal_on_success():
authenticator = _authenticator()
assert authenticator.silent({}) is None
assert authenticator.silent({"Authorization": f"Bearer {TOKEN}"}).organization_id == "acme"