From 95cb14da9d27d2dd15e192d625df4402c73b5854 Mon Sep 17 00:00:00 2001 From: Alexa Amundson <118287761+blackboxprogramming@users.noreply.github.com> Date: Sun, 16 Nov 2025 23:24:00 -0600 Subject: [PATCH] Add backend test helper and fix compliance event metadata --- README.md | 22 +++++++++++++++++++++ backend/app/models/compliance_event.py | 2 +- backend/app/routers/compliance_ops.py | 5 +++-- scripts/run_backend_tests.sh | 27 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100755 scripts/run_backend_tests.sh diff --git a/README.md b/README.md index 24d90a4..d6976d0 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,28 @@ the validation and deploy jobs keep pointing at the same file. - **Manual check** – run `python scripts/railway/validate_env_template.py` locally to get the same assurance before pushing. +## Testing + +Backend tests run against an isolated SQLite database by default. A helper +script bootstraps a virtual environment, installs dependencies, and executes +pytest with sensible defaults for local development. + +```bash +# From the repo root +bash scripts/run_backend_tests.sh +``` + +The script exports `ENVIRONMENT=testing`, points `TEST_DATABASE_URL` to a local +`test.db` SQLite file (override it to point at Postgres if needed), and sets +`ALLOWED_ORIGINS` so CORS validation passes during the suite. To rerun tests +inside the prepared environment manually: + +```bash +cd backend +source .venv-tests/bin/activate +pytest -v --maxfail=1 +``` + ## Architecture ### Single-Page Application diff --git a/backend/app/models/compliance_event.py b/backend/app/models/compliance_event.py index 0cce803..55e0d80 100644 --- a/backend/app/models/compliance_event.py +++ b/backend/app/models/compliance_event.py @@ -13,7 +13,7 @@ class ComplianceEvent(Base): actor = Column(String(255)) action = Column(String(255)) resource = Column(String(255)) - metadata = Column(JSON, default=dict) + event_metadata = Column("metadata", JSON, default=dict) severity = Column(String(50), default="info") timestamp = Column(DateTime(timezone=True), server_default=func.now()) diff --git a/backend/app/routers/compliance_ops.py b/backend/app/routers/compliance_ops.py index 7c97fe8..8ec2e0b 100644 --- a/backend/app/routers/compliance_ops.py +++ b/backend/app/routers/compliance_ops.py @@ -3,7 +3,7 @@ from typing import List from fastapi import APIRouter, Depends from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select -from pydantic import BaseModel +from pydantic import BaseModel, Field from app.auth import get_current_active_user from app.database import get_db @@ -19,10 +19,11 @@ class ComplianceEventResponse(BaseModel): action: str resource: str severity: str - metadata: dict | None = None + metadata: dict | None = Field(default=None, alias="event_metadata") timestamp: str | None = None class Config: + populate_by_name = True from_attributes = True diff --git a/scripts/run_backend_tests.sh b/scripts/run_backend_tests.sh new file mode 100755 index 0000000..c8abad7 --- /dev/null +++ b/scripts/run_backend_tests.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Automated helper to install backend test dependencies and execute pytest +# using a lightweight SQLite database. This is intended for local development +# and CI smoke checks. + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +BACKEND_DIR="$ROOT_DIR/backend" +VENV_DIR="$BACKEND_DIR/.venv-tests" + +cd "$BACKEND_DIR" + +if [[ ! -d "$VENV_DIR" ]]; then + python -m venv "$VENV_DIR" +fi + +source "$VENV_DIR/bin/activate" + +pip install --upgrade pip +pip install -r requirements.txt + +export TEST_DATABASE_URL="${TEST_DATABASE_URL:-sqlite+aiosqlite:///./test.db}" +export ENVIRONMENT="${ENVIRONMENT:-testing}" +export ALLOWED_ORIGINS="${ALLOWED_ORIGINS:-http://localhost:3000,http://localhost:8000}" + +pytest -v --maxfail=1