Files
Chris TonjesandClaude Opus 5.5 5f2d885fdc Phase 0 milestone 0.8: CI pipeline on Gitea Actions
Workflow moved to .gitea/workflows/ci.yml (Gitea is the CI host). Every
gate now passes locally, step-for-step; green on main is pending an
act_runner on gitea.doesworks.net (runs #1-#5 are queued "Waiting").

Gate fixes found by actually running them:
- pip-audit: `--disable-pip` without `-r` was a usage error, so the old
  step could never pass. Now `pip-audit --strict .` audits the runtime
  dependency set from pyproject.
- pip-audit then flagged ecdsa (PYSEC-2026-1325, no fix), a hard dep of
  python-jose. Swapped to PyJWT (HS256 only; 3-line change in
  auth/security.py). Flag to OWC.
- PyJWT warned the test JWT secret was 11 bytes. JWT_SECRET now requires
  >= 32 bytes (RFC 7518 s3.2) at settings load; dev/compose/test defaults
  updated. Test-only value carries an inline gitleaks:allow.
- bandit B101: assert in retention._count_past replaced with a raise.
- gitleaks: gitleaks-action needs a GitHub token/license; replaced with
  the pinned 8.30.1 binary, sha256-verified, scanning full history.

Added:
- SBOM: CycloneDX 1.6 from a clean runtime-only venv (39 components),
  uploaded as a CI artifact. YPMS-POAM-042.
- cyclonedx-bom in dev extras; CI outputs gitignored.

158 tests passing, 95.83% coverage; ruff, mypy, bandit, pip-audit,
gitleaks clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 10:57:09 -04:00

149 lines
4.7 KiB
Python

"""Shared pytest fixtures.
Uses SQLite for speed (see ARCHITECTURE.md §4.8). Tests exercise the framework
against an in-memory Postgres-shaped SQLAlchemy Base — no Alembic migrations
in the test path; models create their tables from metadata.
Environment defaults are set at *module import* time (not via fixture) so
they are in place before pytest collects any test module that transitively
imports `ypms.main` (which instantiates the FastAPI app at import time).
"""
from __future__ import annotations
import os
from collections.abc import Callable, Iterator
from typing import Any
os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:")
# Test-only value, never deployed. 32+ bytes to satisfy the HS256 floor.
os.environ.setdefault("JWT_SECRET", "test-jwt-secret-at-least-32-bytes-long") # gitleaks:allow
os.environ.setdefault(
"COLUMN_ENCRYPTION_KEY", "test-col-enc-key-32bytes-______________"
)
os.environ.setdefault("ENV", "test")
import pytest # noqa: E402
from fastapi.testclient import TestClient # noqa: E402
from sqlalchemy import create_engine # noqa: E402
from sqlalchemy.orm import Session, sessionmaker # noqa: E402
from sqlalchemy.pool import StaticPool # noqa: E402
@pytest.fixture
def db() -> Iterator[Session]:
"""Fresh in-memory SQLite DB per test with all model tables created.
Import happens inside the fixture so the model modules register themselves
on Base.metadata before create_all runs.
"""
import ypms.models # noqa: F401
from ypms.db import Base
# StaticPool + check_same_thread=False share the same in-memory DB across
# threads. TestClient runs handlers in a worker thread, so without this
# the fixture-scoped session and the request-scoped session see different
# empty databases (SADeprecationWarning: SQLite objects created in a
# thread can only be used in that same thread).
engine = create_engine(
"sqlite:///:memory:",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
future=True,
)
Base.metadata.create_all(engine)
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True)
session = SessionLocal()
try:
yield session
finally:
session.close()
engine.dispose()
@pytest.fixture
def client() -> Iterator[TestClient]:
from ypms.main import create_app
app = create_app()
with TestClient(app) as c:
yield c
# ---------------- seeded RBAC + authenticated API client ----------------
Headers = dict[str, str]
@pytest.fixture
def roles(db: Session) -> dict[str, Any]:
"""All seed roles with the real permissions matrix loaded."""
from ypms.seed.permissions import seed_permissions
from ypms.seed.run import seed_roles
r = seed_roles(db)
seed_permissions(db, r)
db.flush()
return r
_HASH_CACHE: list[str] = []
def _unused_password_hash(hasher: Callable[[str], str]) -> str:
# PBKDF2 is deliberately slow; these users authenticate by minted token,
# so one hash shared across the session is enough.
if not _HASH_CACHE:
_HASH_CACHE.append(hasher("unused-in-these-tests"))
return _HASH_CACHE[0]
@pytest.fixture
def make_user(db: Session, roles: dict[str, Any]) -> Callable[..., tuple[Any, Headers]]:
"""Factory: make_user("name", "ROLE_A", ...) -> (User, auth headers)."""
from ypms.auth.security import create_access_token, hash_password
from ypms.users.models import ASSURANCE_STANDARD, User, UserRole
def _make(username: str, *role_codes: str) -> tuple[User, Headers]:
user = User(
username=username,
password_hash=_unused_password_hash(hash_password),
is_active=True,
assurance_level=ASSURANCE_STANDARD,
)
db.add(user)
db.flush()
for code in role_codes:
db.add(UserRole(user_id=user.id, role_id=roles[code].id))
db.flush()
db.refresh(user)
token = create_access_token(
sub=username, uid=user.id, roles=list(role_codes),
assurance_level=ASSURANCE_STANDARD, mfa_verified=False,
)
return user, {"Authorization": f"Bearer {token}"}
return _make
@pytest.fixture(scope="session")
def _shared_app() -> Any:
"""One app per session: building ~240 routes costs ~0.1s per create_app()."""
from ypms.main import create_app
return create_app()
@pytest.fixture
def api(db: Session, _shared_app: Any) -> Iterator[TestClient]:
"""TestClient on the shared app, wired to the per-test SQLite session."""
from ypms.db import get_db
def _override() -> Iterator[Session]:
yield db
_shared_app.dependency_overrides[get_db] = _override
with TestClient(_shared_app) as c:
yield c
_shared_app.dependency_overrides.clear()