Files
open_cv/tests/api/test_auth_profiles_health.py
T
2026-09-29 00:17:58 +03:00

87 lines
3.8 KiB
Python

"""Интеграционные тесты: аутентификация, единый формат ошибок, профили,
health/ready — docs/08 §8.3, IT-08, IT-09, IT-17."""
from __future__ import annotations
import numpy as np
from tests.api.helpers import make_blob_ref, post_features, post_verify
class TestAuth:
def test_it08_missing_key_401(self, client, facade_a):
response = client.post(
"/api/v1/features",
files={"image": ("image.jpg", facade_a, "image/jpeg")},
)
assert response.status_code == 401
assert response.json()["error"]["code"] == "UNAUTHORIZED"
def test_it08_wrong_key_403(self, client, facade_a):
response = client.post(
"/api/v1/features",
headers={"X-API-Key": "wrong-key"},
files={"image": ("image.jpg", facade_a, "image/jpeg")},
)
assert response.status_code == 403
assert response.json()["error"]["code"] == "FORBIDDEN"
def test_it08_health_without_key(self, client):
"""IT-08: /health без ключа — 200 (FR-004)."""
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_ready_without_key(self, client):
response = client.get("/ready")
assert response.status_code == 200
assert response.json() == {"status": "ready"}
def test_profiles_require_key(self, client):
assert client.get("/api/v1/profiles").status_code == 401
assert client.get("/api/v1/profiles", headers={"X-API-Key": "bad"}).status_code == 403
class TestErrorFormat:
def test_it09_unified_error_shape(self, client, facade_a, blob_a):
"""IT-09: все 4xx соответствуют единому формату ошибки."""
cases = [
client.post("/api/v1/verify", headers={"X-API-Key": "nope"}, files=[]),
post_features(client, b"not an image"),
post_verify(client, facade_a, []),
post_verify(client, facade_a, [make_blob_ref(blob_a[:100])]),
]
for response in cases:
assert 400 <= response.status_code < 500, response.status_code
body = response.json()
error = body["error"]
assert set(error) >= {"code", "message", "details", "trace_id"}
assert isinstance(error["code"], str) and error["code"]
assert len(error["trace_id"]) == 12
class TestProfiles:
def test_it17_catalog(self, client):
"""IT-17: каталог содержит facade, monument, repetitive (FR-009)."""
response = client.get("/api/v1/profiles", headers={"X-API-Key": "test-key"})
assert response.status_code == 200
body = response.json()
names = {p["name"] for p in body["profiles"]}
assert {"facade", "monument", "repetitive"} <= names
monument = next(p for p in body["profiles"] if p["name"] == "monument")
assert monument["comparison"]["geometry_model"] == "fundamental"
assert monument["extraction"]["clahe_clip_limit"] == 3.0
repetitive = next(p for p in body["profiles"] if p["name"] == "repetitive")
assert repetitive["comparison"]["lowe_ratio"] == 0.70
assert repetitive["comparison"]["match_inliers_min"] == 60
# базовые значения для наследования незаполненных полей
assert body["defaults"]["comparison"]["inliers_match"] == 50
def test_facade_profile_is_default(self, client):
response = client.get("/api/v1/profiles", headers={"X-API-Key": "test-key"})
facade = next(p for p in response.json()["profiles"] if p["name"] == "facade")
assert facade["extraction"] == {}
assert facade["comparison"] == {}