fix(webgui): harden config bool parsing and action error handling
This commit is contained in:
1 parent
3659d5b7da
commit
37f736c158
10 files changed
+1366
-233
No files matched your search
@@ -0,0 +1,137 @@
|
||||
"""web_gui_pkg.routes 关键逻辑单测(无需启动 HTTP 服务)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from gui.web_gui_pkg import routes
|
||||
|
||||
|
||||
class _DummyHandler:
|
||||
def __init__(self, body=None, csrf_token: str = "token"):
|
||||
self.headers = {"X-CSRF-Token": csrf_token}
|
||||
self._body = body if body is not None else {}
|
||||
self.calls = []
|
||||
|
||||
def _read_json(self):
|
||||
return self._body
|
||||
|
||||
def _send_json(self, obj, status: int = 200):
|
||||
self.calls.append((status, obj))
|
||||
return {"status": status, "obj": obj}
|
||||
|
||||
def send_error(self, status):
|
||||
self.calls.append((status, {"error": "send_error"}))
|
||||
return None
|
||||
|
||||
|
||||
class _DummyRunner:
|
||||
def get_stats(self, include_events=False): # noqa: ARG002
|
||||
return {"ok": True}
|
||||
|
||||
def is_running(self):
|
||||
return False
|
||||
|
||||
def get_reservations(self):
|
||||
return {}
|
||||
|
||||
def get_high_value_rooms(self):
|
||||
return {}
|
||||
|
||||
def force_stop(self):
|
||||
return None
|
||||
|
||||
def stop(self):
|
||||
return None
|
||||
|
||||
def send_back_key(self, _device_id): # noqa: ARG002
|
||||
return True
|
||||
|
||||
def start_main_run(self, _cfg): # noqa: ARG002
|
||||
return None
|
||||
|
||||
|
||||
def _post(path: str, body: dict, monkeypatch, csrf: str = "token"):
|
||||
handler = _DummyHandler(body=body, csrf_token=csrf)
|
||||
runner = _DummyRunner()
|
||||
u = SimpleNamespace(path=path, query="")
|
||||
routes.handle_post(handler, u, runner, "token")
|
||||
assert handler.calls, f"{path} should produce a response"
|
||||
return handler.calls[-1]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"raw,expected",
|
||||
[
|
||||
(True, True),
|
||||
(False, False),
|
||||
("true", True),
|
||||
("TRUE", True),
|
||||
("1", True),
|
||||
("yes", True),
|
||||
("on", True),
|
||||
("false", False),
|
||||
("FALSE", False),
|
||||
("0", False),
|
||||
("no", False),
|
||||
("off", False),
|
||||
("", False),
|
||||
(0, False),
|
||||
(1, True),
|
||||
],
|
||||
)
|
||||
def test_coerce_bool(raw, expected):
|
||||
assert routes._coerce_bool(raw) is expected
|
||||
|
||||
|
||||
def test_config_bool_string_false_written_as_false(monkeypatch):
|
||||
written = {}
|
||||
|
||||
monkeypatch.setattr(routes, "read_config", lambda: {"enable_streaming_screenshot": True})
|
||||
monkeypatch.setattr(routes, "write_config", lambda cfg: written.update(cfg))
|
||||
|
||||
status, obj = _post(
|
||||
"/api/config",
|
||||
{"config": {"enable_streaming_screenshot": "false"}},
|
||||
monkeypatch,
|
||||
)
|
||||
assert status == 200
|
||||
assert obj.get("ok") is True
|
||||
assert written.get("enable_streaming_screenshot") is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"ports_raw,expected_invalid",
|
||||
[
|
||||
(None, False),
|
||||
("", False),
|
||||
("5555", False),
|
||||
("5555,5556", False),
|
||||
("5555-5560", False),
|
||||
(["5555", 5556], False),
|
||||
("abc", True),
|
||||
("5555,abc", True),
|
||||
("1-2-3", True),
|
||||
(70000, True),
|
||||
(["x"], True),
|
||||
],
|
||||
)
|
||||
def test_is_invalid_ports_spec(ports_raw, expected_invalid):
|
||||
assert routes._is_invalid_ports_spec(ports_raw) is expected_invalid
|
||||
|
||||
|
||||
def test_wifi_scan_invalid_ports_returns_400(monkeypatch):
|
||||
monkeypatch.setattr(routes, "find_adb_executable", lambda: "adb")
|
||||
status, obj = _post("/api/wifi/scan", {"ports": "abc"}, monkeypatch)
|
||||
assert status == 400
|
||||
assert obj.get("error") == "invalid ports"
|
||||
|
||||
|
||||
def test_wifi_auto_reconnect_without_adb_returns_422(monkeypatch):
|
||||
monkeypatch.setattr(routes, "find_adb_executable", lambda: "")
|
||||
status, obj = _post("/api/wifi/auto-reconnect", {}, monkeypatch)
|
||||
assert status == 422
|
||||
assert obj.get("error") == "adb not found"
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
"""WebUI 关键安全/稳定性源码锚点测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def _read(rel: str) -> str:
|
||||
return (ROOT / rel).read_text(encoding="utf-8")
|
||||
|
||||
|
||||
def test_config_form_bool_literal_coercion_guard():
|
||||
src = _read("webui/js/config_form.js")
|
||||
assert 'if (type === "bool")' in src
|
||||
assert '["1", "true", "yes", "y", "on"]' in src
|
||||
assert '["0", "false", "no", "n", "off", ""]' in src
|
||||
|
||||
|
||||
def test_runtime_actions_reservation_fields_guarded():
|
||||
src = _read("webui/js/runtime_actions.js")
|
||||
assert "safePrizeValue" in src
|
||||
assert "safePosSeen" in src
|
||||
assert "safeRoomPos" in src
|
||||
assert "safeReservationsCount" in src
|
||||
assert "safeTotal" in src
|
||||
assert "escapeHtml(item.future_str || '-')" in src
|
||||
|
||||
|
||||
def test_runtime_device_actions_send_back_key_handles_error():
|
||||
src = _read("webui/js/runtime_device_actions.js")
|
||||
m = re.search(
|
||||
r"async function sendBackKey\(\)\s*\{(?P<body>.*?)\n\}",
|
||||
src,
|
||||
flags=re.S,
|
||||
)
|
||||
assert m, "sendBackKey function not found"
|
||||
body = m.group("body")
|
||||
assert "try {" in body
|
||||
assert "catch" in body
|
||||
|
||||
Reference in new issue
Block a user