Add GET /firmware/{version}.bin, and fix test teardown (Phase 2)

The last device endpoint. With it, the whole firmware/API.md contract is
implemented apart from /stream, which is Phase 6.

The device checks three things before it erases a partition
(ota_mgr.cpp:110-128): status exactly 200, a Content-Length header at all -
it refuses chunked, because it wants to size-check first - and that length
equal to the size the envelope advertised. FileResponse gives an exact
length from the file, and the camera's :8080 Caddy block has no encode
directive, so nothing recompresses it on the way out.

A row whose file is missing, or whose file does not match its recorded size,
is 503 with a loud log rather than a quiet 404. The device would abort on the
Content-Length check anyway; failing here turns a confusing remote abort into
one clear server line.

A test walks the whole OTA handshake: heartbeat names a target, and the
relative URL it advertises is fetched the way ota_mgr builds it, base plus
url, and the bytes hash to the advertised sha256.

The admin-facing POST /firmware is not included. It needs user auth and an
admin role, neither of which exists yet (SEC-007).

Also fixes a real defect in the tests. Several put their cleanup at the end
of the test body, so a failing assertion skipped it. A deliberate mutation
made two tests fail, which left two is_target firmware rows behind, and
those rows then picked themselves as the OTA target for unrelated tests -
one broken thing looked like three. Teardown now lives in fixtures and runs
whatever happens. Re-running the same mutation now fails exactly the two
tests it should and leaves the database clean.

Tests: 148 -> 158.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Thomas KarnerandClaude Opus 5 committed 2026-09-21 13:19:26 +02:00
1 parent 6223d960c4
commit 269a6eadcb
4 files changed
+376 -28

No files matched your search

+5 -2
View File
@@ -8,7 +8,7 @@ from fastapi import FastAPI
from .. import __version__
from ..config import get_settings
from ..logging_setup import configure_logging
from .routes import commands, conditions, device_config, enroll, health, ingest
from .routes import commands, conditions, device_config, enroll, firmware, health, ingest
log = logging.getLogger(__name__)
@@ -50,8 +50,11 @@ def create_app() -> FastAPI:
app.include_router(device_config.router, prefix=API_PREFIX)
app.include_router(commands.router, prefix=API_PREFIX)
app.include_router(ingest.router, prefix=API_PREFIX)
app.include_router(firmware.router, prefix=API_PREFIX)
# Still to come: /firmware/{version}.bin.
# The device contract (firmware/API.md) is complete apart from /stream,
# which is Phase 6. Still to come: the admin-facing POST /firmware, which
# needs user auth, and the Phase 3 UI endpoints.
# Phase 3 mounts the UI endpoints.
return app