49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib
|
|
from collections.abc import AsyncIterator
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest_asyncio
|
|
from alembic import command
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
cli = importlib.import_module("backup_tool.cli")
|
|
config = importlib.import_module("backup_tool.config")
|
|
Settings = config.Settings
|
|
|
|
|
|
def make_settings(tmp_path: Path) -> Any:
|
|
key = tmp_path / "master.key"
|
|
key.write_bytes(b"m2-test-master-key-material-32-bytes-minimum")
|
|
key.chmod(0o600)
|
|
data = tmp_path / "data"
|
|
repositories = tmp_path / "repositories"
|
|
sources = tmp_path / "sources"
|
|
restores = tmp_path / "restores"
|
|
for path in (data, repositories, sources, restores):
|
|
path.mkdir()
|
|
return Settings(
|
|
data_dir=data,
|
|
database_url=f"sqlite+aiosqlite:///{data / 'metadata.db'}",
|
|
repository_roots=(repositories,),
|
|
local_source_roots=(sources,),
|
|
restore_roots=(restores,),
|
|
master_key_file=key,
|
|
public_base_url="https://127.0.0.1:8000",
|
|
)
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def app_client(tmp_path: Path) -> AsyncIterator[tuple[AsyncClient, Any]]:
|
|
settings = make_settings(tmp_path)
|
|
command.upgrade(cli.build_alembic_config(settings), "head")
|
|
app_module = importlib.import_module("backup_tool.api.app")
|
|
app = app_module.create_app(settings)
|
|
async with AsyncClient(
|
|
transport=ASGITransport(app=app), base_url="https://127.0.0.1:8000"
|
|
) as client:
|
|
yield client, settings
|
|
await app.state.engine.dispose()
|