prepared skeleton for lab5
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
"""Идентификатор реплики и заголовки ответов без PostgreSQL, Redis и Docker."""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from fastapi import Request
|
||||
|
||||
from app.core.instance import resolve_instance_id
|
||||
from app.errors.exceptions import unavailable
|
||||
from app.main import create_app
|
||||
from conftest import close_app, make_client
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clean_db() -> None:
|
||||
"""Заменяет очистку БД из conftest: эти тесты не обращаются к хранилищам."""
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("configured", "expected"),
|
||||
[
|
||||
("api-explicit", "api-explicit"),
|
||||
(" api-explicit ", "api-explicit"),
|
||||
(None, "container-123"),
|
||||
("", "container-123"),
|
||||
(" ", "container-123"),
|
||||
],
|
||||
)
|
||||
def test_resolve_prefers_configured_id_then_hostname(monkeypatch, configured, expected):
|
||||
monkeypatch.setenv("HOSTNAME", " container-123 ")
|
||||
|
||||
assert resolve_instance_id(configured) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("hostname", [None, "", " "])
|
||||
def test_resolve_without_config_or_hostname_returns_uuid(monkeypatch, hostname):
|
||||
if hostname is None:
|
||||
monkeypatch.delenv("HOSTNAME", raising=False)
|
||||
else:
|
||||
monkeypatch.setenv("HOSTNAME", hostname)
|
||||
|
||||
assert UUID(resolve_instance_id(None)).version == 4
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def instance_client(make_settings):
|
||||
app = create_app(
|
||||
make_settings(
|
||||
instance_id="api-test",
|
||||
database_url="postgresql+asyncpg://unused@127.0.0.1:1/unused",
|
||||
redis_port=1,
|
||||
)
|
||||
)
|
||||
|
||||
@app.get("/__test__/request-id", include_in_schema=False)
|
||||
async def request_id(request: Request) -> dict[str, str]:
|
||||
return {"request_id": request.state.request_id}
|
||||
|
||||
@app.get("/__test__/explode", include_in_schema=False)
|
||||
async def explode() -> None:
|
||||
raise RuntimeError("test error")
|
||||
|
||||
@app.get("/__test__/unavailable", include_in_schema=False)
|
||||
async def dependency_failure() -> None:
|
||||
raise unavailable()
|
||||
|
||||
try:
|
||||
async with make_client(app, raise_app_exceptions=False) as client:
|
||||
yield client
|
||||
finally:
|
||||
await close_app(app)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("method", "path", "status"),
|
||||
[
|
||||
("GET", "/health/live", 200),
|
||||
("POST", "/api/v1/users", 400),
|
||||
("GET", "/api/v1/users/me", 401),
|
||||
("GET", "/no-such-path", 404),
|
||||
("DELETE", "/health/live", 405),
|
||||
("GET", "/__test__/explode", 500),
|
||||
("GET", "/__test__/unavailable", 503),
|
||||
],
|
||||
)
|
||||
async def test_instance_header_is_present_on_success_and_errors(
|
||||
instance_client, method, path, status
|
||||
):
|
||||
response = await instance_client.request(method, path)
|
||||
|
||||
assert response.status_code == status
|
||||
assert response.headers["X-Instance-Id"] == "api-test"
|
||||
assert response.headers["X-Request-Id"]
|
||||
if status >= 400:
|
||||
assert response.json()["error"]["request_id"] == response.headers["X-Request-Id"]
|
||||
|
||||
|
||||
async def test_instance_id_is_stable_while_request_ids_are_distinct(instance_client):
|
||||
first = await instance_client.get("/__test__/request-id")
|
||||
second = await instance_client.get("/__test__/request-id")
|
||||
|
||||
assert first.status_code == second.status_code == 200
|
||||
assert first.headers["X-Instance-Id"] == second.headers["X-Instance-Id"] == "api-test"
|
||||
assert first.headers["X-Request-Id"] != second.headers["X-Request-Id"]
|
||||
for response in (first, second):
|
||||
assert response.json()["request_id"] == response.headers["X-Request-Id"]
|
||||
Reference in New Issue
Block a user