Refactor: split serve/server.py (3220 lines) into focused modules
serve/server.py is now 69 lines — app factory, middleware, and router
registration only.
New modules:
deps.py (168 lines) — module-level globals + auth dependency functions
models.py (85 lines) — all Pydantic request/response models
tasks.py (136 lines) — background workers and job tracker
routers/ — one file per domain (10 routers, ~2750 lines total)
auth.py, me.py, admin.py, activities.py, uploads.py,
segments.py, strava.py, garmin.py, ideas.py, feed.py
cli.py updated to set globals on deps instead of server.
88 new regression tests in tests/serve/ cover auth guards and key
behaviours for every router; 294 total passing after the split.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
"""Shared fixtures for serve/ router tests.
|
||||
|
||||
The fixture patches data_dir on whichever module owns it — works against
|
||||
both the pre-split monolith (bincio.serve.server) and the post-split
|
||||
layout (bincio.serve.deps).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from bincio.serve.db import create_session, create_user, open_db
|
||||
|
||||
|
||||
def _set_data_dir(path: Path) -> None:
|
||||
try:
|
||||
import bincio.serve.deps as deps
|
||||
deps.data_dir = path
|
||||
deps._db = None
|
||||
except (ImportError, AttributeError):
|
||||
import bincio.serve.server as srv
|
||||
srv.data_dir = path
|
||||
srv._db = None
|
||||
|
||||
|
||||
def _get_data_dir() -> Path | None:
|
||||
try:
|
||||
import bincio.serve.deps as deps
|
||||
return deps.data_dir
|
||||
except (ImportError, AttributeError):
|
||||
import bincio.serve.server as srv
|
||||
return srv.data_dir
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def tmp_data(tmp_path: Path) -> Path:
|
||||
"""Return a tmp_path with a valid instance.db."""
|
||||
open_db(tmp_path) # creates schema
|
||||
return tmp_path
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def client(tmp_data: Path) -> TestClient:
|
||||
from bincio.serve.server import app
|
||||
_set_data_dir(tmp_data)
|
||||
return TestClient(app, raise_server_exceptions=False)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def admin_client(tmp_data: Path) -> TestClient:
|
||||
"""Client with an admin session cookie pre-set."""
|
||||
from bincio.serve.server import app
|
||||
_set_data_dir(tmp_data)
|
||||
|
||||
db = open_db(tmp_data)
|
||||
create_user(db, "admin", "Admin", "adminpass1", is_admin=True,
|
||||
wiki_access=True, activity_access=True)
|
||||
token = create_session(db, "admin")
|
||||
|
||||
c = TestClient(app, raise_server_exceptions=False)
|
||||
c.cookies.set("bincio_session", token)
|
||||
return c
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_client(tmp_data: Path) -> TestClient:
|
||||
"""Client with a regular (non-admin) session cookie pre-set."""
|
||||
from bincio.serve.server import app
|
||||
_set_data_dir(tmp_data)
|
||||
|
||||
db = open_db(tmp_data)
|
||||
create_user(db, "alice", "Alice", "alicepass1", is_admin=False,
|
||||
wiki_access=True, activity_access=True)
|
||||
(tmp_data / "alice" / "activities").mkdir(parents=True, exist_ok=True)
|
||||
token = create_session(db, "alice")
|
||||
|
||||
c = TestClient(app, raise_server_exceptions=False)
|
||||
c.cookies.set("bincio_session", token)
|
||||
return c
|
||||
Reference in New Issue
Block a user