Files
smeta-web/tests/test_stove_api_transport.py
T

76 lines
3.7 KiB
Python

"""Boundary tests: authentication precedes body parsing; success and errors share a contract."""
import os
import sys
from fastapi import FastAPI
from fastapi.testclient import TestClient
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if ROOT not in sys.path:
sys.path.insert(0, ROOT)
import stove_api
def _client():
app = FastAPI()
app.include_router(stove_api.secure)
app.include_router(stove_api.public)
return TestClient(app)
def test_invalid_key_wins_over_invalid_body(monkeypatch):
monkeypatch.setattr(stove_api.stove_keys, "authenticate", lambda secret, ip: {"status": "invalid_api_key"})
response = _client().post("/api/v1/stove-estimate", content="not-json",
headers={"Content-Type": "application/json", "X-API-Key": "wrong"})
assert response.status_code == 401
assert response.json()["code"] == "invalid_api_key"
assert response.json()["request_id"] == response.headers["X-Request-Id"]
def test_estimate_success_and_history(monkeypatch):
calls = []
key = {"id": 7, "limit_day": 1000, "ai_budget_rub_day": 300}
monkeypatch.setattr(stove_api.stove_keys, "authenticate", lambda *args: {"status": "ok", "key": key})
monkeypatch.setattr(stove_api.stove_history, "open_request", lambda **kw:
{"admitted": True, "limit": 1000, "remaining": 999})
monkeypatch.setattr(stove_api.stove_history, "finish_request", lambda *args, **kw: calls.append(kw))
monkeypatch.setattr(stove_api.stove_pricing, "load", lambda parsed:
{"valid_at": "III кв. 2026", "region": "Москва", "lines": []})
monkeypatch.setattr(stove_api.stove_cache, "response_get", lambda key: None)
monkeypatch.setattr(stove_api.stove_cache, "response_put", lambda *args, **kw: None)
async def fill(prices, **kw):
return prices
monkeypatch.setattr(stove_api.stove_match, "fill", fill)
response = _client().post("/api/v1/stove-estimate", json={"stove": {
"kind": "bath", "bricks": {"ceramic": 1, "fireclay": 0}, "region": "Москва"}},
headers={"X-API-Key": "test"})
assert response.status_code == 200, response.text
body = response.json()
assert body["total"] is None
assert body["missing_data"] == ["расценка на кладку банной печи: в базах нет", "ведомость материалов"]
assert body["request_id"] == response.headers["X-Request-Id"]
assert response.headers["Infosmeta-RateLimit-Remaining"] == "999"
assert calls and calls[0]["http_status"] == 200
def test_invalid_request_reports_field_path(monkeypatch):
key = {"id": 7, "limit_day": 1000, "ai_budget_rub_day": 300}
monkeypatch.setattr(stove_api.stove_keys, "authenticate", lambda *args: {"status": "ok", "key": key})
monkeypatch.setattr(stove_api.stove_history, "open_request", lambda **kw:
{"admitted": True, "limit": 1000, "remaining": 999})
monkeypatch.setattr(stove_api.stove_history, "finish_request", lambda *args, **kw: None)
response = _client().post("/api/v1/stove-estimate", json={"stove": {"kind": "bath"}},
headers={"X-API-Key": "test"})
assert response.status_code == 400
assert response.json()["code"] == "invalid_request"
assert "stove.bricks" in response.json()["detail"]
def test_protocol_and_schema_are_public():
client = _client()
assert client.get("/api/v1/stove-estimate/protocol").status_code == 200
schema = client.get("/api/v1/stove-estimate/openapi.json").json()
assert "stove" in schema["paths"]["/api/v1/stove-estimate"]["post"]["requestBody"]["content"]["application/json"]["schema"]["properties"]
assert client.get("/api/v1/stove-estimate").json()["code"] == "method_not_allowed"