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:
1 parent
6223d960c4
commit
269a6eadcb
4 files changed
+376
-28
No files matched your search
@@ -49,6 +49,39 @@ def device(db):
|
||||
db.commit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def firmware(db):
|
||||
"""Create firmware rows that are removed even when the test fails.
|
||||
|
||||
Teardown has to live in a fixture, not at the end of the test body: a
|
||||
failing assertion skips the rest of the function, and a leftover
|
||||
is_target row then picks itself as the OTA target for every later test.
|
||||
"""
|
||||
created: list[Firmware] = []
|
||||
|
||||
def make(version: str, *, is_target: bool = True) -> Firmware:
|
||||
row = Firmware(
|
||||
version=version,
|
||||
file_path=f"/srv/poorbird/fw/{version}.bin",
|
||||
sha256="a" * 64,
|
||||
size_bytes=1148320,
|
||||
is_target=is_target,
|
||||
)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
created.append(row)
|
||||
return row
|
||||
|
||||
yield make
|
||||
|
||||
for row in created:
|
||||
existing = db.get(Firmware, row.id)
|
||||
if existing is not None:
|
||||
db.delete(existing)
|
||||
db.commit()
|
||||
|
||||
|
||||
def body(reason: str = "periodic", **over) -> dict:
|
||||
b = {
|
||||
"reason": reason,
|
||||
@@ -186,40 +219,21 @@ def test_one_devices_commands_do_not_leak_to_another(client, device, db):
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_firmware_key_is_absent_when_the_device_is_on_target(client, device, db):
|
||||
def test_firmware_key_is_absent_when_the_device_is_on_target(client, device, firmware):
|
||||
"""Absent, not null: present-and-different is itself the flash instruction."""
|
||||
d, auth = device
|
||||
fw = Firmware(
|
||||
version="0.1.0", # same as the device
|
||||
file_path="/srv/poorbird/fw/0.1.0.bin",
|
||||
sha256="a" * 64,
|
||||
size_bytes=1148320,
|
||||
is_target=True,
|
||||
)
|
||||
db.add(fw)
|
||||
db.commit()
|
||||
firmware("0.1.0") # same version the device reports
|
||||
|
||||
e = client.post("/api/v1/heartbeat", json=body(), headers=auth).json()
|
||||
assert "firmware" not in e
|
||||
|
||||
db.delete(db.get(Firmware, fw.id))
|
||||
db.commit()
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_a_different_target_is_offered_with_a_relative_url(client, device, db):
|
||||
def test_a_different_target_is_offered_with_a_relative_url(client, device, firmware):
|
||||
"""ota_mgr.cpp appends a non-http url to the API base, which already ends
|
||||
in /api/v1 -- so this must not carry the prefix itself."""
|
||||
d, auth = device
|
||||
fw = Firmware(
|
||||
version="0.2.0",
|
||||
file_path="/srv/poorbird/fw/0.2.0.bin",
|
||||
sha256="b" * 64,
|
||||
size_bytes=1148320,
|
||||
is_target=True,
|
||||
)
|
||||
db.add(fw)
|
||||
db.commit()
|
||||
firmware("0.2.0")
|
||||
|
||||
e = client.post("/api/v1/heartbeat", json=body(), headers=auth).json()
|
||||
assert e["firmware"]["version"] == "0.2.0"
|
||||
@@ -227,9 +241,6 @@ def test_a_different_target_is_offered_with_a_relative_url(client, device, db):
|
||||
assert not e["firmware"]["url"].startswith("/api/v1")
|
||||
assert e["firmware"]["size"] == 1148320
|
||||
|
||||
db.delete(db.get(Firmware, fw.id))
|
||||
db.commit()
|
||||
|
||||
|
||||
# --- telemetry recording ----------------------------------------------------
|
||||
|
||||
|
||||
Reference in new issue
Block a user