Files
meshcore-bot/tests/test_managed_keys.py
T
agessaman 966fa1726a feat(web_viewer): keep Home Assistant managed settings read-only
The add-on rewrites the keys it owns on every start, so editing them in the viewer would be silently undone. modules/managed_keys.py loads the list the add-on exports (MESHCORE_HA_MANAGED_KEYS) and guards writes in one place: ConfigIniStore.write_sections refuses to change or delete a managed key, and the viewer's other config.ini writers now go through that store instead of calling update_ini_values directly. The plugins page shows managed fields as disabled with "Managed by Home Assistant". With the variable unset, nothing changes.
2026-11-09 20:12:30 -08:00

208 lines
6.9 KiB
Python

"""Home Assistant managed-key loader and the config.ini write guard."""
from __future__ import annotations
import configparser
import json
import logging
from contextlib import contextmanager
from pathlib import Path
import pytest
from modules.ha_addon import MANAGED_KEYS_ENV
from modules.ini_writer import IniValueError
from modules.managed_keys import (
ManagedKeyError,
guard_managed_writes,
is_managed,
load_managed_keys,
)
from modules.settings_store import ConfigIniStore
LOGGER = "MeshCoreBot"
@contextmanager
def _watch(caplog: pytest.LogCaptureFixture):
"""Capture MeshCoreBot warnings even after the bot logger stops propagating."""
logger = logging.getLogger(LOGGER)
previous = logger.propagate
logger.propagate = True
try:
with caplog.at_level(logging.WARNING, logger=LOGGER):
yield
finally:
logger.propagate = previous
def _point(monkeypatch: pytest.MonkeyPatch, path: Path) -> None:
monkeypatch.setenv(MANAGED_KEYS_ENV, str(path))
def test_missing_env_is_empty(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None:
monkeypatch.delenv(MANAGED_KEYS_ENV, raising=False)
with _watch(caplog):
assert load_managed_keys() == frozenset()
assert not caplog.records
def test_blank_env_is_empty(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None:
monkeypatch.setenv(MANAGED_KEYS_ENV, " ")
with _watch(caplog):
assert load_managed_keys() == frozenset()
assert not caplog.records
def test_missing_file_is_empty(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
_point(monkeypatch, tmp_path / "missing.json")
with _watch(caplog):
assert load_managed_keys() == frozenset()
assert not caplog.records
def test_unreadable_file_is_empty(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
_point(monkeypatch, tmp_path)
with _watch(caplog):
assert load_managed_keys() == frozenset()
assert "cannot read" in caplog.text.lower()
@pytest.mark.parametrize(
"payload",
["{", "null", '{"Bot": "db_path"}', '[["Bot"]]', '[["Bot", 1]]', '[""]', '["Bot", "db_path"]'],
)
def test_malformed_file_is_empty(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
payload: str,
) -> None:
path = tmp_path / "managed.json"
path.write_text(payload, encoding="utf-8")
_point(monkeypatch, path)
with _watch(caplog):
assert load_managed_keys() == frozenset()
assert "malformed" in caplog.text.lower()
def test_normal_file_normalizes_keys(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
path = tmp_path / "managed.json"
path.write_text(
json.dumps(
[
[" Web_Viewer ", "Port"],
["Bot", "db_path"],
["Connection", "hostname"],
["Bot", "DB_Path"],
]
),
encoding="utf-8",
)
_point(monkeypatch, path)
with _watch(caplog):
loaded = load_managed_keys()
assert loaded == {
("Web_Viewer", "port"),
("Bot", "db_path"),
("Connection", "hostname"),
}
assert not caplog.records
assert is_managed("Web_Viewer", "PORT", loaded)
assert not is_managed("web_viewer", "port", loaded)
assert not is_managed("Bot", "bot_name", loaded)
def test_empty_list_is_empty(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
path = tmp_path / "managed.json"
path.write_text("[]\n", encoding="utf-8")
_point(monkeypatch, path)
with _watch(caplog):
assert load_managed_keys() == frozenset()
assert not caplog.records
def _config(text: str) -> configparser.ConfigParser:
parser = configparser.ConfigParser()
parser.read_string(text)
return parser
def test_guard_drops_unchanged_managed_key_and_keeps_the_rest(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
path = tmp_path / "managed.json"
path.write_text(json.dumps([["Bot", "DB_Path"]]), encoding="utf-8")
_point(monkeypatch, path)
config = _config("[Bot]\ndb_path = /data/db\n")
updates, deletes = guard_managed_writes(
{"Bot": {"DB_Path": "/data/db", "bot_name": "Zed"}},
None,
config,
)
assert updates == {"Bot": {"bot_name": "Zed"}}
assert deletes is None
def test_guard_rejects_a_changed_managed_key(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
path = tmp_path / "managed.json"
path.write_text(json.dumps([["Bot", "db_path"], ["Connection", "hostname"]]), encoding="utf-8")
_point(monkeypatch, path)
config = _config("[Bot]\ndb_path = /data/db\n")
with pytest.raises(ManagedKeyError, match=r"Managed by Home Assistant: \[Bot\] db_path") as caught:
guard_managed_writes(
{"Bot": {"db_path": "/other", "bot_name": "Zed"}},
{"Connection": ["hostname"]},
config,
)
assert caught.value.conflicts == [("Bot", "db_path"), ("Connection", "hostname")]
assert issubclass(ManagedKeyError, IniValueError)
def test_store_refuses_managed_write_and_leaves_disk_and_memory(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
config_path = tmp_path / "config.ini"
config_path.write_text("[Bot]\ndb_path = /data/db\nbot_name = A\n", encoding="utf-8")
managed = tmp_path / "managed.json"
managed.write_text(json.dumps([["Bot", "db_path"]]), encoding="utf-8")
_point(monkeypatch, managed)
config = configparser.ConfigParser()
config.read(config_path, encoding="utf-8")
store = ConfigIniStore(config, str(config_path))
with pytest.raises(ManagedKeyError):
store.write_values("Bot", {"db_path": "/other", "bot_name": "B"})
assert config.get("Bot", "db_path") == "/data/db"
assert config.get("Bot", "bot_name") == "A"
text = config_path.read_text(encoding="utf-8")
assert "db_path = /data/db" in text
assert "bot_name = B" not in text
def test_store_writes_unmanaged_keys_and_leaves_an_unchanged_managed_line(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
config_path = tmp_path / "config.ini"
original = "[Bot]\ndb_path = /data/db\nbot_name = A\n"
config_path.write_text(original, encoding="utf-8")
managed = tmp_path / "managed.json"
managed.write_text(json.dumps([["Bot", "db_path"]]), encoding="utf-8")
_point(monkeypatch, managed)
config = configparser.ConfigParser()
config.read(config_path, encoding="utf-8")
store = ConfigIniStore(config, str(config_path))
store.write_values("Bot", {"db_path": "/data/db", "bot_name": "B"})
assert config.get("Bot", "bot_name") == "B"
assert config.get("Bot", "db_path") == "/data/db"
text = config_path.read_text(encoding="utf-8")
assert "db_path = /data/db" in text
assert "bot_name = B" in text