Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/clockd/routers/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ async def update_camera(camera_id: str, camera: CameraConfig, request: Request)
if camera_id not in cameras:
raise HTTPException(status_code=404, detail=f"Camera '{camera_id}' not found")
cameras_dir = request.app.state.server_cfg.cameras_dir
# If camera_id in the path differs from the body, rename the file
# If camera_id in the path differs from the body, rename the file — but
# never overwrite a different existing camera in the process.
if camera.camera_id != camera_id:
if camera.camera_id in cameras:
raise HTTPException(
status_code=409, detail=f"Camera '{camera.camera_id}' already exists"
)
delete_camera_file(cameras_dir, camera_id)
del cameras[camera_id]
save_camera(cameras_dir, camera)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_api_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,28 @@ async def test_create_camera_under_limit(client):
resp = await client.post("/cameras", json=_camera_body("under_limit"))
assert resp.status_code == 201
assert resp.json()["camera_id"] == "under_limit"


@pytest.mark.asyncio
async def test_update_camera_rename_onto_existing_conflicts(client):
# A second camera exists; renaming test_cam onto its id must not clobber it.
app.state.server_cfg.max_cameras = 10
assert (await client.post("/cameras", json=_camera_body("other_cam"))).status_code == 201

resp = await client.put("/cameras/test_cam", json=_camera_body("other_cam"))
assert resp.status_code == 409
assert "already exists" in resp.json()["detail"]

# Both cameras survive unchanged — no silent data loss.
assert (await client.get("/cameras/test_cam")).status_code == 200
assert (await client.get("/cameras/other_cam")).status_code == 200


@pytest.mark.asyncio
async def test_update_camera_rename_to_free_id_succeeds(client):
resp = await client.put("/cameras/test_cam", json=_camera_body("renamed_cam"))
assert resp.status_code == 200
assert resp.json()["camera_id"] == "renamed_cam"
# Old id is gone, new id resolves.
assert (await client.get("/cameras/test_cam")).status_code == 404
assert (await client.get("/cameras/renamed_cam")).status_code == 200
Loading