v1.1.0: Alembic, pytest, домен и документация
- Миграции PostgreSQL через Alembic; DDL убран из lifespan приложения. - Тесты: health, status, ingress Grafana; моки Vault/Grafana/Forgejo. - Пакет onguard24/domain/ (сущности, шина событий), docs/DOMAIN.md. - Обновлены README, CHANGELOG, ARCHITECTURE. Made-with: Cursor
This commit is contained in:
61
tests/test_status.py
Normal file
61
tests/test_status.py
Normal file
@ -0,0 +1,61 @@
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
def test_status_without_integrations(client: TestClient) -> None:
|
||||
"""Без БД и без URL внешних сервисов — всё disabled."""
|
||||
r = client.get("/api/v1/status")
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert data["service"] == "onGuard24"
|
||||
assert data["database"] == "disabled"
|
||||
assert data["vault"] == "disabled"
|
||||
assert data["grafana"] == "disabled"
|
||||
assert data["forgejo"] == "disabled"
|
||||
|
||||
|
||||
def test_status_with_mocks(client: TestClient) -> None:
|
||||
"""Моки внешних вызовов — ok-ветки без сети."""
|
||||
with (
|
||||
patch("onguard24.status_snapshot.vault_ping", new_callable=AsyncMock) as vp,
|
||||
patch("onguard24.status_snapshot.grafana_api.ping", new_callable=AsyncMock) as gp,
|
||||
patch(
|
||||
"onguard24.status_snapshot.grafana_api.get_signed_in_user",
|
||||
new_callable=AsyncMock,
|
||||
) as gu,
|
||||
patch("onguard24.status_snapshot.forgejo_api.probe", new_callable=AsyncMock) as fp,
|
||||
):
|
||||
vp.return_value = (True, None)
|
||||
gp.return_value = (True, None)
|
||||
gu.return_value = ({"login": "tester", "email": "t@x"}, None)
|
||||
fp.return_value = {"status": "ok", "url": "https://x", "api": "authenticated", "login": "u"}
|
||||
|
||||
# Подмена полей settings (pydantic-settings иначе тянет env поверх конструктора)
|
||||
from types import SimpleNamespace
|
||||
|
||||
app = client.app
|
||||
real = app.state.settings
|
||||
app.state.settings = SimpleNamespace(
|
||||
database_url="",
|
||||
vault_addr="https://vault.example",
|
||||
vault_token="t",
|
||||
grafana_url="https://grafana.example",
|
||||
grafana_service_account_token="g",
|
||||
forgejo_url="https://git.example",
|
||||
forgejo_token="f",
|
||||
grafana_webhook_secret="",
|
||||
http_addr="0.0.0.0:8080",
|
||||
log_level="info",
|
||||
)
|
||||
try:
|
||||
r = client.get("/api/v1/status")
|
||||
finally:
|
||||
app.state.settings = real
|
||||
|
||||
assert r.status_code == 200
|
||||
d = r.json()
|
||||
assert d["vault"]["status"] == "ok"
|
||||
assert d["grafana"]["status"] == "ok"
|
||||
assert d["grafana"].get("service_account_login") == "tester"
|
||||
assert d["forgejo"]["status"] == "ok"
|
||||
Reference in New Issue
Block a user