From 2887309aae8117184f4b9f0dbc5e50a5e0c6c0f2 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 4 Jul 2026 08:56:12 -0600 Subject: [PATCH] Hotspot: address review comments. * POST /api/hotspot/settings checks is_docker_mode() directly instead of string-matching the error message (still 500, matching the sibling hotspot/bluetooth/throttle endpoints). * Escape \ ; , " in the WiFi QR code per the WPA QR format. * Skip the controller.yaml write when a settings update changes nothing. Co-Authored-By: Claude Fable 5 --- app/src/components/admin/ControllerPage.js | 4 +++- controller/api/admin.py | 7 ++++--- controller/lib/admin.py | 13 +++++++++---- controller/test/test_admin.py | 11 +++++++++++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/components/admin/ControllerPage.js b/app/src/components/admin/ControllerPage.js index 1df31229..9cf0cc27 100644 --- a/app/src/components/admin/ControllerPage.js +++ b/app/src/components/admin/ControllerPage.js @@ -1395,7 +1395,9 @@ function HotspotSettingsForm() { .map(device => ({key: device, value: device, text: device})); // Special string which allows a mobile device to connect to a specific Wi-Fi. - const qrCodeValue = `WIFI:S:${form.ssid};T:WPA;P:${form.password};;`; + // The WPA QR format requires backslash-escaping of \ ; , " in the SSID and password. + const escapeWifi = (s) => s.replace(/([\\;,"])/g, '\\$1'); + const qrCodeValue = `WIFI:S:${escapeWifi(form.ssid)};T:WPA;P:${escapeWifi(form.password)};;`; return
diff --git a/controller/api/admin.py b/controller/api/admin.py index 3d12d563..4e28bae9 100644 --- a/controller/api/admin.py +++ b/controller/api/admin.py @@ -83,11 +83,12 @@ async def hotspot_settings() -> HotspotSettingsResponse: @router.post("/api/hotspot/settings", response_model=HotspotSettingsResponse) async def hotspot_settings_update(request: HotspotSettingsRequest) -> HotspotSettingsResponse: """Update WiFi hotspot settings; they are saved in controller.yaml.""" + # 500 matches the other subsystem endpoints (hotspot/bluetooth/throttle) in Docker mode. + if is_docker_mode(): + raise HTTPException(status_code=500, detail="Not available in Docker mode") result = update_hotspot_settings(device=request.device, ssid=request.ssid, password=request.password) if not result.get("success"): - error = result.get("error", "Failed") - status_code = 500 if "Docker" in error else 400 - raise HTTPException(status_code=status_code, detail=error) + raise HTTPException(status_code=400, detail=result.get("error", "Failed")) return HotspotSettingsResponse(device=result["device"], ssid=result["ssid"], password=result["password"]) diff --git a/controller/lib/admin.py b/controller/lib/admin.py index fc8b366a..f4da130c 100644 --- a/controller/lib/admin.py +++ b/controller/lib/admin.py @@ -76,17 +76,22 @@ def update_hotspot_settings(device: str = None, ssid: str = None, password: str if password is not None and not 8 <= len(password) <= 63: return {"success": False, "error": "Hotspot password must be 8 to 63 characters"} + changed = False if device: config_lib.update_config('hotspot.device', device) + changed = True if ssid: config_lib.update_config('hotspot.ssid', ssid) + changed = True if password: config_lib.update_config('hotspot.password', password) + changed = True - try: - config_lib.save_config() - except RuntimeError as e: - return {"success": False, "error": str(e)} + if changed: + try: + config_lib.save_config() + except RuntimeError as e: + return {"success": False, "error": str(e)} logger.info("Hotspot settings updated (device=%s, ssid=%s)", get_hotspot_device(), get_hotspot_ssid()) return { diff --git a/controller/test/test_admin.py b/controller/test/test_admin.py index 9b4c2202..a6b69dec 100644 --- a/controller/test/test_admin.py +++ b/controller/test/test_admin.py @@ -404,6 +404,17 @@ class TestUpdateHotspotSettings: assert result["success"] is False assert "Docker" in result["error"] + def test_noop_does_not_save(self, tmp_path, reset_runtime_config): + """An empty update succeeds without writing controller.yaml.""" + missing_path = tmp_path / 'missing' / 'controller.yaml' + with mock.patch("controller.lib.admin.is_docker_mode", return_value=False): + # save_config() would raise RuntimeError here; success proves it was skipped. + with mock.patch("controller.lib.config.CONFIG_PATH_ON_DRIVE", missing_path): + result = update_hotspot_settings() + + assert result["success"] is True + assert not missing_path.exists() + def test_error_when_drive_not_mounted(self, tmp_path, reset_runtime_config): """Saving fails cleanly when the primary drive is not mounted.""" missing_path = tmp_path / 'missing' / 'controller.yaml'