SPB Git

Repository Public

conftest.py 10.1 KB 287 lines python

1"""Montage de test : la vraie passerelle et le faux moteur tournent chacun dans un serveur uvicorn réel2(sockets locaux), pour tester le streaming, l'annulation et les en-têtes comme en production."""34from __future__ import annotations56import asyncio7import hashlib8import json9import os10import secrets11import socket12import sys13import threading14import time15from collections.abc import Iterator16from dataclasses import dataclass17from pathlib import Path18from typing import Any1920import cbor221import httpx22import pytest23import uvicorn24from cryptography.hazmat.primitives import hashes25from cryptography.hazmat.primitives.asymmetric import ec2627sys.path.insert(0, str(Path(__file__).parent))28import fake_engine  # noqa: E4022930from maclustr_llm.app import Services, build_services, create_app  # noqa: E40231from maclustr_llm.config import load_settings  # noqa: E4023233ROOT = Path(__file__).resolve().parents[1]34MODEL = "maclustr-qwen3"353637def free_port() -> int:38    with socket.socket() as s:39        s.bind(("127.0.0.1", 0))40        return int(s.getsockname()[1])414243class ServerThread:44    def __init__(self, app: Any, port: int) -> None:45        self.server = uvicorn.Server(uvicorn.Config(app, host="127.0.0.1", port=port, log_level="error", lifespan="on"))46        self.thread = threading.Thread(target=self.server.run, daemon=True)4748    def start(self) -> None:49        self.thread.start()50        for _ in range(200):51            if self.server.started:52                return53            time.sleep(0.02)54        raise RuntimeError("serveur de test non démarré")5556    def stop(self) -> None:57        self.server.should_exit = True58        self.thread.join(timeout=10)596061@dataclass62class Stack:63    base: str64    origin: str65    svc: Services66    data_dir: Path67    engine_port: int68    engine_server: ServerThread6970    def run(self, coro: Any) -> Any:71        """Exécute une coroutine des services (hors boucle du serveur) — base SQLite sérialisée, sans risque."""72        return asyncio.run(coro)7374    def make_key(self, **fields: Any) -> tuple[dict[str, Any], str]:75        fields.setdefault("name", "test")76        key, secret = self.run(self.svc.keys.create(fields))77        return key, secret7879    def client(self, key: str | None = None, **kw: Any) -> httpx.Client:80        headers = {"Authorization": f"Bearer {key}"} if key else {}81        return httpx.Client(base_url=self.base, headers=headers, timeout=30, **kw)828384@pytest.fixture()85def stack(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[Stack]:86    engine_port, port = free_port(), free_port()87    fake_engine.STATE.update({"active": 0, "max_active": 0, "requests": 0, "cancelled": 0, "bodies": []})88    engine = ServerThread(fake_engine.app, engine_port)89    engine.start()90    catalog = tmp_path / "models.json"91    catalog.write_text(92        json.dumps(93            {94                "models": [95                    {96                        "id": MODEL,97                        "display_name": "Test",98                        "engine_model": "/nonexistent",99                        "repo": "test/test",100                        "revision": "rev",101                        "quantization": "4-bit",102                        "license": "apache-2.0",103                        "context_window": 2048,104                        "max_output_tokens": 512,105                        "default_max_tokens": 128,106                        "supports_tools": True,107                        "supports_thinking": True,108                    },109                    {110                        "id": "other-model",111                        "display_name": "Autre",112                        "engine_model": "/nonexistent",113                        "repo": "t/o",114                        "revision": "rev",115                        "quantization": "4-bit",116                        "license": "apache-2.0",117                        "context_window": 2048,118                        "max_output_tokens": 512,119                        "default_max_tokens": 128,120                    },121                ],122                "engines": [123                    {124                        "id": "e1",125                        "url": f"http://127.0.0.1:{engine_port}",126                        "models": [MODEL, "other-model"],127                        "max_active": 2,128                        "token_budget": 0,129                    }130                ],131            }132        )133    )134    origin = f"http://localhost:{port}"135    env = {136        "DATA_DIR": str(tmp_path / "data"),137        "CATALOG": str(catalog),138        "WEB_DIR": str(ROOT / "web"),139        "PUBLIC_ORIGIN": origin,140        "RP_ID": "localhost",141        "HOST": "127.0.0.1",142        "PORT": str(port),143        "TRUST_PROXY": "0",144        "MAX_QUEUE": "2",145        "QUEUE_TIMEOUT_S": "3",146        "MAX_BODY_BYTES": "200000",147    }148    for k, v in env.items():149        monkeypatch.setenv(f"MLLM_{k}", v)150    for k in list(os.environ):151        if k.startswith("MLLM_") and k[5:] not in env:152            monkeypatch.delenv(k)153    settings = load_settings()154    svc = build_services(settings)155    svc.scheduler.pressure = lambda: "normal"  # les tests ne dépendent pas de la pression mémoire du poste156    gateway = ServerThread(create_app(settings, services=svc), port)157    gateway.start()158    for _ in range(100):  # attend que la sonde ait vu le moteur159        if svc.scheduler.has_engine(MODEL):160            break161        time.sleep(0.02)162    try:163        yield Stack(f"http://127.0.0.1:{port}", origin, svc, tmp_path / "data", engine_port, engine)164    finally:165        gateway.stop()166        engine.stop()167168169# --- authentificateur WebAuthn logiciel (plateforme, clé résidente, vérification utilisateur) ---------170def b64u(data: bytes) -> str:171    import base64172173    return base64.urlsafe_b64encode(data).rstrip(b"=").decode()174175176def b64u_dec(text: str) -> bytes:177    import base64178179    return base64.urlsafe_b64decode(text + "=" * (-len(text) % 4))180181182class SoftAuthenticator:183    """Implémente juste ce qu'il faut de WebAuthn (ES256, attestation « none ») pour exercer le serveur184    avec de vraies signatures."""185186    def __init__(self, origin: str, rp_id: str = "localhost") -> None:187        self.origin = origin188        self.rp_id = rp_id189        self.key = ec.generate_private_key(ec.SECP256R1())190        self.credential_id = secrets.token_bytes(32)191        self.sign_count = 0192        self.user_handle = b""193194    def _client_data(self, type_: str, challenge: str, origin: str | None = None) -> bytes:195        return json.dumps(196            {"type": type_, "challenge": challenge, "origin": origin or self.origin, "crossOrigin": False}197        ).encode()198199    def create(self, options: dict[str, Any], *, origin: str | None = None) -> dict[str, Any]:200        pk = options["publicKey"]201        self.user_handle = b64u_dec(pk["user"]["id"])202        nums = self.key.public_key().public_numbers()203        cose = cbor2.dumps({1: 2, 3: -7, -1: 1, -2: nums.x.to_bytes(32, "big"), -3: nums.y.to_bytes(32, "big")})204        auth_data = (205            hashlib.sha256(self.rp_id.encode()).digest()206            + bytes([0x5D])207            + (0).to_bytes(4, "big")208            + bytes(16)209            + len(self.credential_id).to_bytes(2, "big")210            + self.credential_id211            + cose212        )213        att = cbor2.dumps({"fmt": "none", "attStmt": {}, "authData": auth_data})214        return {215            "id": b64u(self.credential_id),216            "rawId": b64u(self.credential_id),217            "type": "public-key",218            "authenticatorAttachment": "platform",219            "clientExtensionResults": {},220            "response": {221                "clientDataJSON": b64u(self._client_data("webauthn.create", pk["challenge"], origin)),222                "attestationObject": b64u(att),223                "transports": ["internal", "hybrid"],224            },225        }226227    def get(self, options: dict[str, Any], *, origin: str | None = None, tamper: bool = False) -> dict[str, Any]:228        pk = options["publicKey"]229        self.sign_count += 1230        auth_data = hashlib.sha256(self.rp_id.encode()).digest() + bytes([0x1D]) + self.sign_count.to_bytes(4, "big")231        client_data = self._client_data("webauthn.get", pk["challenge"], origin)232        sig = self.key.sign(auth_data + hashlib.sha256(client_data).digest(), ec.ECDSA(hashes.SHA256()))233        if tamper:234            sig = sig[:-2] + bytes([sig[-2] ^ 0xFF, sig[-1]])235        return {236            "id": b64u(self.credential_id),237            "rawId": b64u(self.credential_id),238            "type": "public-key",239            "authenticatorAttachment": "platform",240            "clientExtensionResults": {},241            "response": {242                "clientDataJSON": b64u(client_data),243                "authenticatorData": b64u(auth_data),244                "signature": b64u(sig),245                "userHandle": b64u(self.user_handle),246            },247        }248249250@dataclass251class Admin:252    http: httpx.Client253    csrf: str254    device: SoftAuthenticator255256    def post(self, path: str, body: Any = None, **kw: Any) -> httpx.Response:257        return self.http.post(path, json=body, headers={"X-CSRF-Token": self.csrf, **kw.pop("headers", {})}, **kw)258259    def reauth(self) -> None:260        opts = self.post("/admin/api/reauth/options").json()261        r = self.post(262            "/admin/api/reauth/verify", {"challenge_id": opts["challenge_id"], "credential": self.device.get(opts)}263        )264        assert r.status_code == 200, r.text265266267def activate(stack: Stack, purpose: str = "setup") -> Admin:268    """Active l'administrateur par un vrai lien à usage unique et retourne un client authentifié."""269    url = stack.svc.auth.create_setup_link_sync(purpose)270    token = url.rsplit("/", 1)[1]271    http = httpx.Client(base_url=stack.base, timeout=30)272    device = SoftAuthenticator(stack.origin)273    opts = http.post("/api/setup/options", json={"token": token}).json()274    r = http.post(275        "/api/setup/complete",276        json={"challenge_id": opts["challenge_id"], "credential": device.create(opts), "name": "Mac de test"},277    )278    assert r.status_code == 200, r.text279    return Admin(http, r.json()["csrf_token"], device)280281282@pytest.fixture()283def admin(stack: Stack) -> Iterator[Admin]:284    a = activate(stack)285    yield a286    a.http.close()287
Files main
Menu

Theme