mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-10-17 20:39:13 +00:00
web_interface/blueprints/api_v3.py held 111 routes, 56 helpers and 181 functions in one module -- 9% of the core by line count and three times the next largest file. It becomes a package of nine route modules grouped by path segment, plus __init__.py for the shared imports, constants, Blueprint and helpers. Every route module decorates the SAME api_v3 Blueprint object, so endpoint names stay api_v3.<function>, the URL map is unchanged and app.py is untouched. Verified: 111 routes before, 111 after, byte-identical rules, endpoints and methods, and every endpoint still on the one blueprint. plugins 3,867 config 1,178 starlark 692 system 619 fonts 452 misc 398 wifi 361 display 326 backup 212 __init__ 1,787 (imports, constants, Blueprint, 56 helpers) Two things the URL-map check could not catch, both found by running the suite: 1. PROJECT_ROOT = Path(__file__).parent.parent.parent. Moving the code one directory deeper made that resolve to web_interface/ instead of the project root. Nothing failed at import; it surfaced as ~110 tests failing with 404s and "installation script not found", because every path built from it was one level too shallow. Now parents[3], and test_api_v3_url_map.py asserts PROJECT_ROOT/run.py exists so the next move cannot repeat it. 2. Module-attribute patching. Tests do monkeypatch.setattr(api_v3_module, "_BACKUP_EXPORT_DIR", ...) and a route module that binds such a name by value never sees the patch. The shared code therefore stays in __init__.py rather than moving to a _common submodule -- it has to live on the module the tests patch -- and the eleven names tests patch are read back through the package (_pkg.X) instead of bound by value. Those eleven were found by AST-scanning every setattr in the test tree, not by guessing; "time" is among them, used to drive a fake clock through the second-resolution credential-backup filenames. Test changes are confined to what genuinely moved: patch targets that now name the owning route module, imports of helpers, and six tests that scan the api_v3 source as a file and now read the package directory. Full suite: 4,278 passed, 68 skipped, 0 failed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014RRtqXDCnvnY6EQwhT5CV9
91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
"""
|
|
Drift guard: api_v3 must use the canonical secret helpers.
|
|
|
|
Historically web_interface/blueprints/api_v3.py carried THREE inline
|
|
nested-function copies of ``find_secret_fields``/``separate_secrets`` (in the
|
|
main-config save, plugin-config save, and plugin-config reset endpoints).
|
|
They lacked the canonical module's array-item secret support and drifted from
|
|
each other. They have been migrated onto
|
|
``src/web_interface/secret_helpers`` — this file now guards against copies
|
|
REAPPEARING, and keeps the canonical array-item behavior executable.
|
|
"""
|
|
|
|
import re
|
|
from pathlib import Path
|
|
|
|
from src.web_interface.secret_helpers import find_secret_fields, separate_secrets
|
|
|
|
API_V3_PKG = (Path(__file__).resolve().parents[2]
|
|
/ "web_interface" / "blueprints" / "api_v3")
|
|
|
|
|
|
def _api_v3_source() -> str:
|
|
"""Every module of the api_v3 package as one string.
|
|
|
|
It used to be a single file; the inline copies this guards against could
|
|
now reappear in any module of the package.
|
|
"""
|
|
return "\n".join(p.read_text(encoding="utf-8")
|
|
for p in sorted(API_V3_PKG.glob("*.py")))
|
|
|
|
# The migration is complete: any inline reimplementation is a regression.
|
|
EXPECTED_INLINE_COPIES = 0
|
|
|
|
|
|
class TestNoInlineCopies:
|
|
def _count(self, name: str) -> int:
|
|
source = _api_v3_source()
|
|
return len(re.findall(rf"^\s*def {name}\(", source, flags=re.MULTILINE))
|
|
|
|
def test_no_inline_find_secret_fields(self):
|
|
count = self._count("find_secret_fields")
|
|
assert count == EXPECTED_INLINE_COPIES, (
|
|
f"api_v3.py has {count} inline find_secret_fields definitions, "
|
|
f"expected {EXPECTED_INLINE_COPIES}. Import it from "
|
|
f"src/web_interface/secret_helpers instead of re-implementing it."
|
|
)
|
|
|
|
def test_no_inline_separate_secrets(self):
|
|
count = self._count("separate_secrets")
|
|
assert count == EXPECTED_INLINE_COPIES, (
|
|
f"api_v3.py has {count} inline separate_secrets definitions, "
|
|
f"expected {EXPECTED_INLINE_COPIES}. Import it from "
|
|
f"src/web_interface/secret_helpers instead of re-implementing it."
|
|
)
|
|
|
|
def test_canonical_import_present(self):
|
|
# Tripwire: the endpoints still need the helpers, so removing the
|
|
# import means either dead secret handling or a new local copy.
|
|
source = _api_v3_source()
|
|
assert re.search(
|
|
r"from src\.web_interface\.secret_helpers import .*find_secret_fields",
|
|
source,
|
|
), "api_v3.py no longer imports the canonical secret helpers"
|
|
|
|
|
|
class TestCanonicalArrayItemBehavior:
|
|
"""Executable documentation of the array-item secret contract the
|
|
endpoints now inherit from the canonical module."""
|
|
|
|
SCHEMA = {
|
|
"accounts": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "object",
|
|
"properties": {
|
|
"name": {"type": "string"},
|
|
"token": {"type": "string", "x-secret": True},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
def test_canonical_module_routes_array_item_secrets(self):
|
|
paths = find_secret_fields(self.SCHEMA)
|
|
assert "accounts[].token" in paths
|
|
|
|
config = {"accounts": [{"name": "a", "token": "s3cret"}]}
|
|
regular, secrets = separate_secrets(config, paths)
|
|
assert regular == {"accounts": [{"name": "a"}]}
|
|
assert secrets == {"accounts": [{"token": "s3cret"}]}
|