Add auth foundation and app DB management

This commit is contained in:
2026-04-20 15:16:47 +02:00
parent 044b47c573
commit e1aad408ab
30 changed files with 1834 additions and 20 deletions
+39 -3
View File
@@ -7,11 +7,18 @@ from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.auth_db import reset_auth_db_caches
import app.db as app_db
from app.config import get_settings
from app.main import create_app
def _make_app_alembic_config(database_url: str) -> Config:
config = Config("alembic_app.ini")
config.set_main_option("sqlalchemy.url", database_url)
return config
def _make_alembic_config(database_url: str) -> Config:
config = Config("alembic_location.ini")
config.set_main_option("sqlalchemy.url", database_url)
@@ -26,17 +33,25 @@ def _make_poo_alembic_config(database_url: str) -> Config:
@pytest.fixture
def test_database_urls(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
app_database_path = tmp_path / "app_test.db"
location_database_path = tmp_path / "location_test.db"
poo_database_path = tmp_path / "poo_placeholder.db"
app_database_url = f"sqlite:///{app_database_path}"
location_database_url = f"sqlite:///{location_database_path}"
poo_database_url = f"sqlite:///{poo_database_path}"
monkeypatch.setenv("APP_DATABASE_URL", app_database_url)
monkeypatch.setenv("LOCATION_DATABASE_URL", location_database_url)
monkeypatch.setenv("POO_DATABASE_URL", poo_database_url)
monkeypatch.setenv("AUTH_BOOTSTRAP_USERNAME", "admin")
monkeypatch.setenv("AUTH_BOOTSTRAP_PASSWORD", "test-password")
get_settings.cache_clear()
reset_auth_db_caches()
try:
yield {
"app_path": app_database_path,
"app_url": app_database_url,
"location_path": location_database_path,
"location_url": location_database_url,
"poo_path": poo_database_path,
@@ -44,6 +59,7 @@ def test_database_urls(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
}
finally:
get_settings.cache_clear()
reset_auth_db_caches()
@pytest.fixture
@@ -59,7 +75,17 @@ def ready_poo_database(test_database_urls):
@pytest.fixture
def app(ready_location_database, ready_poo_database):
def auth_database(test_database_urls, monkeypatch: pytest.MonkeyPatch):
database_url = test_database_urls["app_url"]
command.upgrade(_make_app_alembic_config(database_url), "head")
reset_auth_db_caches()
yield test_database_urls
reset_auth_db_caches()
@pytest.fixture
def app(ready_location_database, ready_poo_database, auth_database):
yield create_app()
@@ -70,7 +96,12 @@ def client(app):
@pytest.fixture
def location_client(ready_location_database, ready_poo_database, monkeypatch: pytest.MonkeyPatch):
def location_client(
ready_location_database,
ready_poo_database,
auth_database,
monkeypatch: pytest.MonkeyPatch,
):
database_url = ready_location_database["location_url"]
engine = create_engine(database_url, connect_args={"check_same_thread": False})
@@ -87,7 +118,12 @@ def location_client(ready_location_database, ready_poo_database, monkeypatch: py
@pytest.fixture
def poo_client(ready_location_database, ready_poo_database, monkeypatch: pytest.MonkeyPatch):
def poo_client(
ready_location_database,
ready_poo_database,
auth_database,
monkeypatch: pytest.MonkeyPatch,
):
database_url = ready_poo_database["poo_url"]
engine = create_engine(database_url, connect_args={"check_same_thread": False})