Add GET /api/v1/config (Phase 2)
Returns the device's settings document: the stored JSONB, plus the settings that live in real columns, plus config_version last - mirroring configApplyJson, which applies the version only after the rest of the payload has been processed. No response model, deliberately. devices.config is one JSONB document rather than forty columns because the firmware grows tunables faster than a schema should change, and the device merges and clamps whatever arrives: unknown keys ignored, absent keys left alone, dangerous values clamped on the device itself. Partial documents are the normal case, not a degraded one. Validating this shape belongs on the write path, when the admin UI saves a config. Unknown keys are passed through rather than filtered, so the admin UI can push a new firmware tunable without waiting for the server to learn it. Column-backed keys win over any stale copy of themselves in the JSONB. The columns are the modelled source of truth, and a leftover key shadowing one would mean a change to the column never reached the camera. This also closes a loop left open at enrollment. Enrollment refuses to let a device overwrite a rename made in the admin UI, which raised the fair question of how the rename ever reaches the device. It reaches it here: configApplyJson merges device_name like any other key (app_config.cpp:307). The operator owns the name, the server pushes it down, and the device adopts it - so the two decisions are consistent rather than merely defensible. Named device_config.py so it does not read as poorbird.config. The route is /config as specified. Tests: 99 -> 109. Verified by letting the JSONB win over the columns: the precedence test fails, as intended. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
5fd3c094a4
commit
e83fc757d0
3 files changed
+244
-3
No files matched your search
@@ -0,0 +1,171 @@
|
||||
"""GET /api/v1/config.
|
||||
|
||||
The contract is a merge contract, not a schema contract: the device applies
|
||||
only the keys that are present and clamps what it does not like, so what
|
||||
matters here is which keys appear and which value wins.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import secrets
|
||||
|
||||
import pytest
|
||||
from conftest import needs_db
|
||||
|
||||
from poorbird.api.deps import hash_credential, new_credential
|
||||
from poorbird.models.devices import Device
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db():
|
||||
from poorbird.db import SessionLocal
|
||||
|
||||
with SessionLocal() as session:
|
||||
yield session
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def device(db):
|
||||
token = new_credential()
|
||||
d = Device(
|
||||
device_uid=secrets.token_hex(6),
|
||||
name="poorbird-cam-1",
|
||||
token_hash=hash_credential(token),
|
||||
enabled=True,
|
||||
)
|
||||
db.add(d)
|
||||
db.commit()
|
||||
db.refresh(d)
|
||||
|
||||
yield d, {"Authorization": f"Bearer {token}"}
|
||||
|
||||
db.delete(db.get(Device, d.id))
|
||||
db.commit()
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_config_version_is_always_present(client, device):
|
||||
"""It is the only value the device compares, so it can never be missing."""
|
||||
d, auth = device
|
||||
r = client.get("/api/v1/config", headers=auth)
|
||||
|
||||
assert r.status_code == 200
|
||||
assert r.json()["config_version"] == d.config_version
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_an_empty_config_still_returns_the_column_backed_keys(client, device, db):
|
||||
"""A device that has never been configured must still learn its name and
|
||||
its intervals, which live in columns rather than in the JSONB."""
|
||||
d, auth = device
|
||||
assert d.config == {}
|
||||
|
||||
body = client.get("/api/v1/config", headers=auth).json()
|
||||
assert body["device_name"] == "poorbird-cam-1"
|
||||
assert body["heartbeat_interval_s"] == d.heartbeat_interval_s
|
||||
assert body["status_interval_s"] == d.status_interval_s
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_the_stored_document_is_returned(client, device, db):
|
||||
d, auth = device
|
||||
d.config = {
|
||||
"frame_size": "SVGA",
|
||||
"jpeg_quality": 12,
|
||||
"night_behaviour": 2,
|
||||
"cam_day": {"ae_level": 1, "gainceiling": 2},
|
||||
}
|
||||
db.commit()
|
||||
|
||||
body = client.get("/api/v1/config", headers=auth).json()
|
||||
assert body["frame_size"] == "SVGA"
|
||||
assert body["jpeg_quality"] == 12
|
||||
assert body["cam_day"] == {"ae_level": 1, "gainceiling": 2}
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_a_column_wins_over_a_stale_copy_in_the_json(client, device, db):
|
||||
"""The column is the modelled source of truth. A leftover key in the JSONB
|
||||
must not shadow it, or changing the column would not reach the device."""
|
||||
d, auth = device
|
||||
d.config = {"heartbeat_interval_s": 30, "device_name": "stale-name"}
|
||||
d.heartbeat_interval_s = 600
|
||||
db.commit()
|
||||
|
||||
body = client.get("/api/v1/config", headers=auth).json()
|
||||
assert body["heartbeat_interval_s"] == 600
|
||||
assert body["device_name"] == "poorbird-cam-1"
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_an_operator_rename_reaches_the_device_through_config(client, device, db):
|
||||
"""Closes the loop left open at enrollment.
|
||||
|
||||
Enrollment refuses to let a device overwrite a rename made in the admin
|
||||
UI. This is the path by which that rename actually gets to the camera:
|
||||
configApplyJson merges device_name like any other key (app_config.cpp:307).
|
||||
"""
|
||||
d, auth = device
|
||||
d.name = "Feeder by the shed"
|
||||
db.commit()
|
||||
|
||||
assert client.get("/api/v1/config", headers=auth).json()["device_name"] == "Feeder by the shed"
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_keys_the_server_does_not_know_are_passed_through(client, device, db):
|
||||
"""The firmware gains tunables faster than the server learns them, and the
|
||||
device ignores what it does not recognise. Dropping them would make the
|
||||
admin UI unable to push a setting until the server caught up."""
|
||||
d, auth = device
|
||||
d.config = {"some_future_tunable": 7}
|
||||
db.commit()
|
||||
|
||||
assert client.get("/api/v1/config", headers=auth).json()["some_future_tunable"] == 7
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_the_read_has_no_side_effects(client, device, db):
|
||||
"""A GET must not record liveness; /heartbeat is what does that."""
|
||||
d, auth = device
|
||||
assert d.last_seen_at is None
|
||||
|
||||
client.get("/api/v1/config", headers=auth)
|
||||
|
||||
db.refresh(d)
|
||||
assert d.last_seen_at is None
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_config_requires_a_bearer_token(client):
|
||||
assert client.get("/api/v1/config").status_code == 401
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_a_disabled_device_is_refused(client, device, db):
|
||||
d, auth = device
|
||||
d.enabled = False
|
||||
db.commit()
|
||||
assert client.get("/api/v1/config", headers=auth).status_code == 403
|
||||
|
||||
|
||||
@needs_db
|
||||
def test_one_device_cannot_read_anothers_config(client, device, db):
|
||||
"""The token identifies the device; there is no device_id parameter to
|
||||
tamper with, and this pins that there never is one."""
|
||||
d, auth = device
|
||||
other_token = new_credential()
|
||||
other = Device(
|
||||
device_uid=secrets.token_hex(6),
|
||||
name="other-cam",
|
||||
token_hash=hash_credential(other_token),
|
||||
enabled=True,
|
||||
)
|
||||
db.add(other)
|
||||
db.commit()
|
||||
|
||||
body = client.get("/api/v1/config", headers={"Authorization": f"Bearer {other_token}"}).json()
|
||||
assert body["device_name"] == "other-cam"
|
||||
|
||||
db.delete(db.get(Device, other.id))
|
||||
db.commit()
|
||||
Reference in new issue
Block a user