- Add /api/v1/health endpoint with component-level checks - Verify DB connectivity, fail2ban socket, scheduler, session cache - Add SQLite WAL cleanup on startup (orphan crash files) - Migration 8: import_log.timestamp → INTEGER UNIX epoch - Align import_log timestamps with history_archive (already UNIX int) - Add unit tests for DB cleanup and health router Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
"""Shared pytest fixtures for the BanGUI backend test suite.
|
|
|
|
All fixtures are async-compatible via pytest-asyncio. External dependencies
|
|
(fail2ban socket, HTTP APIs) are always mocked so tests never touch real
|
|
infrastructure.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import aiosqlite
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from app.config import Settings
|
|
from app.db import init_db
|
|
from app.main import create_app
|
|
from app.models.server import ServerStatus
|
|
|
|
|
|
@pytest.fixture
|
|
def test_settings(tmp_path: Path) -> Settings:
|
|
"""Return a ``Settings`` instance configured for testing.
|
|
|
|
Uses a temporary directory for the database so tests are isolated from
|
|
each other and from the development database.
|
|
|
|
Args:
|
|
tmp_path: Pytest-provided temporary directory (unique per test).
|
|
|
|
Returns:
|
|
A :class:`~app.config.Settings` instance with overridden paths.
|
|
"""
|
|
config_dir = tmp_path / "fail2ban"
|
|
(config_dir / "jail.d").mkdir(parents=True)
|
|
(config_dir / "filter.d").mkdir(parents=True)
|
|
(config_dir / "action.d").mkdir(parents=True)
|
|
|
|
return Settings(
|
|
database_path=str(tmp_path / "test_bangui.db"),
|
|
fail2ban_socket="/tmp/fake_fail2ban.sock",
|
|
fail2ban_config_dir=str(config_dir),
|
|
session_secret="test-secret-key-do-not-use-in-production",
|
|
session_duration_minutes=60,
|
|
timezone="UTC",
|
|
log_level="debug",
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
async def client(test_settings: Settings) -> AsyncClient: # type: ignore[misc]
|
|
"""Provide an ``AsyncClient`` wired to a test instance of the BanGUI app.
|
|
|
|
The client sends requests directly to the ASGI application (no network).
|
|
``app.state.db`` is initialised manually so router tests can use the
|
|
database without triggering the full ASGI lifespan.
|
|
|
|
Args:
|
|
test_settings: Injected test settings fixture.
|
|
|
|
Yields:
|
|
An :class:`httpx.AsyncClient` with ``base_url="http://test"``.
|
|
"""
|
|
from unittest.mock import MagicMock
|
|
|
|
app = create_app(settings=test_settings)
|
|
|
|
# Ensure fail2ban is reported as online for tests (mock socket is not
|
|
# actually connected so we need to set the cached status manually).
|
|
app.state.server_status = ServerStatus(online=True)
|
|
|
|
# Mock scheduler for health check tests (lifespan not run in ASGITransport tests)
|
|
mock_scheduler = MagicMock()
|
|
mock_scheduler.running = True
|
|
app.state.scheduler = mock_scheduler
|
|
|
|
# Bootstrap the database schema before making requests. ASGITransport
|
|
# does not run the application lifespan, so we create the test SQLite file
|
|
# directly rather than relying on startup logic.
|
|
db: aiosqlite.Connection = await aiosqlite.connect(test_settings.database_path)
|
|
db.row_factory = aiosqlite.Row
|
|
await init_db(db)
|
|
|
|
transport: ASGITransport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
|
yield ac
|
|
|
|
await db.close()
|