pdf-quiz-generator/docker-compose.test.yml
Daniel e72cdd6716 feat: a versioned API, refresh tokens, and an end-to-end stack that found four bugs
**The API.** Every route now lives under `/api/v1`, with `/api/...` rewritten
onto it — one route, two spellings, so they cannot drift and the OpenAPI
document describes each endpoint once. Errors carry an `error` object with a
stable code, one human sentence and, for a validation failure, the fields that
were wrong; `detail` is untouched so nothing that reads it breaks. The whole
surface — 320 routes, their parameters and their status codes — is checked in
as `backend/tests/api-contract.json`, and a test fails on any difference,
naming the routes that moved. `docs/api.md` is the contract in prose.

**Refresh tokens**, so an app can stay signed in without keeping a password.
Rows rather than signatures: listable, withdrawable, stored as hashes, rotated
on every use. A spent token coming back ends the whole session, because a theft
and a replay look identical from the server and the safe reading is the unsafe
one. A browser is not given one — it has nowhere to put it and a person to ask.

**An end-to-end stack**: `docker-compose.test.yml` with its own Postgres and
Redis, `e2e/seed.py` for the smallest world the tests name, and Playwright with
five projects — desktop, iPhone, Pixel, iPad and a browserless API project.
Devices because every bug reported this week was a phone bug found by a person
looking at a screenshot; a desktop-only suite would have passed through all of
them. Forty tests, five clean runs.

It found four things in its first hour:

- **A fresh deploy could not start.** `create_all()` ran before
  `CREATE EXTENSION vector`, so any database that had never had pgvector
  installed died on the first table with a vector column. Invisible here
  because this one has had the extension for a year.
- **A figure in a published article was a 404 for everyone but an admin.**
  Media in the library is nobody's to read by default, and nothing made an
  exception for a drawing an article actually shows — so every illustration
  added this week was an empty box for every real user.
- **Every rate limit was one bucket for the whole site.** The backend saw
  nginx's address for every request, so ten bad passwords from anybody locked
  out everybody, and no log line could say who. nginx now takes the real
  address from the proxy and overwrites the header on the way in; uvicorn runs
  with --proxy-headers.
- **The reading page's breakpoints disagreed** — 1150px in the component,
  820px in the stylesheet. Between them the menu button claimed the contents
  drawer and then toggled a class on a rail that was still in the layout: the
  contents did not open and the site menu did not either. The button was dead
  on every tablet.

And two smaller ones: the login limiter counted successful sign-ins, so eleven
people behind one hospital NAT locked each other out — it is cleared by a
correct password now; and `/uploads/{path}` served GET and HEAD from one route
with one operation id, which makes every OpenAPI client generator refuse the
document.

The first admin's password is generated and printed once at first start when
`DEFAULT_ADMIN_PASSWORD` is blank, rather than the account not existing:
`docker compose logs backend | grep -A3 "FIRST ADMIN"`.

CI (`.forgejo/workflows/tests.yml`) runs the backend suite, the contract, the
frontend suite and the build on every push to dev, main or master, and the
end-to-end stack on those branches and on pull requests into them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TqXevQJhxFrM7jJg82cgZN
2026-09-13 01:23:38 +02:00

108 lines
3.8 KiB
YAML

# The stack the end-to-end tests run against.
#
# Its own Postgres and Redis, on their own volumes, on their own network, with
# their own ports. Nothing here touches the running site: the point of an
# end-to-end test is to do the destructive things a real user can do — sit a
# session, delete an article, sign out everywhere — and none of that may
# happen to somebody's actual work.
#
# docker compose -f docker-compose.test.yml up -d --build
# docker compose -f docker-compose.test.yml run --rm seed
# cd e2e && npx playwright test
# docker compose -f docker-compose.test.yml down -v # -v: take the data with it
#
# The database is thrown away with the stack. That is deliberate — a test suite
# that depends on data surviving between runs is a test suite that passes on
# your machine.
name: pedshub-test
services:
postgres:
image: pgvector/pgvector:pg16
environment:
POSTGRES_DB: pedquiz_test
POSTGRES_USER: pedquiz_test
POSTGRES_PASSWORD: pedquiz_test
# No host port. Nothing outside this stack has any business reaching it,
# and binding 5432 would collide with the real one on a developer's box.
volumes:
- test_postgres:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U pedquiz_test"]
interval: 3s
timeout: 3s
retries: 20
redis:
image: redis:7-alpine
command: redis-server --save "" --appendonly no
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 3s
timeout: 3s
retries: 20
backend:
build: ./backend
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4 --proxy-headers --forwarded-allow-ips=*
environment: &backend_env
DATABASE_URL: postgresql://pedquiz_test:pedquiz_test@postgres:5432/pedquiz_test
REDIS_URL: redis://redis:6379/0
# Fixed, so a token minted by the seed script is still valid in the
# browser. Test-only by construction: it is in a file in the repository.
SECRET_KEY: e2e-only-not-a-secret-e2e-only-not-a-secret
ALGORITHM: HS256
ACCESS_TOKEN_EXPIRE_MINUTES: "1440"
APP_URL: http://localhost:8095
# Nothing may leave the machine during a test. No model, no mail, no
# object store: a suite that quietly calls a paid API is a suite nobody
# can run twice.
LITELLM_API_BASE: http://127.0.0.1:9/blackhole
LITELLM_API_KEY: unused
LITELLM_MODEL: none
SMTP_HOST: ""
ANONYMIZED_TELEMETRY: "False"
LOG_LEVEL: WARNING
# Registration open and no captcha, so the sign-up journey is testable.
CAP_SECRET_KEY: ""
# The whole suite arrives from one address, so the guess limiter would
# stop the run rather than an attacker. Raised here and nowhere else.
LOGIN_MAX_ATTEMPTS: "10000"
REFRESH_MAX_PER_HOUR: "10000"
volumes:
- test_uploads:/app/uploads
- test_chroma:/app/chroma_data
depends_on:
postgres: { condition: service_healthy }
redis: { condition: service_started }
healthcheck:
test: ["CMD-SHELL", "python -c \"import urllib.request;urllib.request.urlopen('http://localhost:8000/api/health')\""]
interval: 3s
timeout: 5s
retries: 30
# The same nginx image the site runs, so the tests exercise the real routing
# and the real built bundle rather than a dev server.
frontend:
build: ./frontend
ports:
- "127.0.0.1:8095:80"
depends_on:
backend: { condition: service_healthy }
# One-shot. Applies migrations and writes the fixture the tests expect.
seed:
build: ./backend
environment: *backend_env
volumes:
- test_uploads:/app/uploads
- ./e2e/seed.py:/app/seed.py:ro
depends_on:
postgres: { condition: service_healthy }
entrypoint: ["python", "/app/seed.py"]
profiles: ["tools"]
volumes:
test_postgres:
test_uploads:
test_chroma: