mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-25 15:06:20 +01:00
Slow tests: - TestPerformance inserted 10 000 records three times per backend. One test per backend now times insert, save and load on the same records, with the same assertions. - test_measurement_realtime_stream slept one second per update, 20 s per backend. The updates use synthetic timestamps one second apart in the recent past, without a pause, and send the same duplicates, out-of-order and burst writes. - test_readers_never_see_a_partial_document: the readers spun on the cache and kept the writer waiting for the GIL (~50 s). They now yield with time.sleep(0) after each lookup (~0.5 s); a non-atomic save still fails the test 10 of 10 times. - test_devicessettingsbattery.py compiled the same plain profile before each of its 235 tests. A module-scoped fixture compiles it once; a compiled profile is frozen and may be shared (20.8 s -> 9.4 s). - The --finalize case of test_geneticoptimize.py runs 250 instead of 400 generations. For this input and seed the best fitness is final in generation 213, and the solutions after 250 and 400 generations are identical field by field. The test checks the keys and the consistency of the result, not the schedule, so it detects the same as before (127 s -> 80 s on GitHub runners). test_genetic0optimize.py keeps 400. Tests that depended on another test of the same process, found by running them in another order and under pytest-xdist: - TestPredictionContainer::test_len relied on a class flag that a TestPredictionProvider fixture set and never reset. Both fixtures now set the flags with monkeypatch. - The EnergyCharts status code test got a cached response from an earlier test; it now calls _request_forecast(force_update=True) and asserts that the request was made. - test_ingress_safe_links did not wait for EOSdash, which EOS starts after its own startup. - test_forward_stream_truncates_very_long_line found the log queue left behind by test_server_start_eosdash (no worker, closed event loop). It installs its own queue now, and test_server_start_eosdash no longer leaves its queue behind. Dropping lines from a full queue was only covered through that left-over queue and gets a test of its own. Refs #1553 Co-authored-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com> Assisted-by: Claude Code (claude-opus-5-5) Assisted-by: Claude Code (claude-fable-5-2)
205 lines
7.4 KiB
Python
205 lines
7.4 KiB
Python
import re
|
|
import time
|
|
from http import HTTPStatus
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
|
|
from akkudoktoreos.server.dash.context import EOSDASH_ROOT, ROOT_PATH, request_url_for
|
|
|
|
# -----------------------------------------------------
|
|
# URL filtering logic
|
|
# -----------------------------------------------------
|
|
|
|
ALLOWED_PREFIXES = [
|
|
"/api/hassio_ingress/",
|
|
"http://", "https://", # external URLs
|
|
"mailto:", "tel:", # contact URLs
|
|
"#", # anchor links
|
|
]
|
|
|
|
|
|
def is_allowed_prefix(url: str) -> bool:
|
|
return any(url.startswith(p) for p in ALLOWED_PREFIXES)
|
|
|
|
|
|
ABSOLUTE_URL = re.compile(r"^/[^/].*")
|
|
RELATIVE_PARENT = re.compile(r"^\.\./")
|
|
WS_REGEX = re.compile(r'new\s+WebSocket\s*\(\s*[\'"]([^\'"]*)[\'"]')
|
|
|
|
|
|
# -----------------------------------------------------
|
|
# Core HTML parser
|
|
# -----------------------------------------------------
|
|
|
|
def scan_html_for_link_issues(html: str):
|
|
soup = BeautifulSoup(html, "html.parser")
|
|
|
|
found_absolute: list[str] = []
|
|
found_relative_up: list[str] = []
|
|
all_urls = []
|
|
|
|
def add_issue(lst, tag, attr, value) -> None:
|
|
lst.append(f"<{tag.name} {attr}='{value}'>")
|
|
|
|
for tag in soup.find_all(True):
|
|
for attr in ("href", "src", "action"):
|
|
if attr not in tag.attrs:
|
|
continue
|
|
|
|
value = tag[attr]
|
|
if not isinstance(value, str):
|
|
continue
|
|
|
|
all_urls.append(value)
|
|
|
|
# (1) absolute URL
|
|
if ABSOLUTE_URL.match(value) and not is_allowed_prefix(value):
|
|
add_issue(found_absolute, tag, attr, value)
|
|
|
|
# (2) relative going up
|
|
if RELATIVE_PARENT.match(value):
|
|
add_issue(found_relative_up, tag, attr, value)
|
|
|
|
# (3) mixed usage check: both absolute + relative appear
|
|
used_absolute = any(u.startswith("/") for u in all_urls if not is_allowed_prefix(u))
|
|
used_relative = any(not u.startswith("/") for u in all_urls if not is_allowed_prefix(u))
|
|
|
|
mixed_usage = used_absolute and used_relative
|
|
|
|
# (4) detect absolute WebSocket URLs in JS
|
|
ws_bad = []
|
|
for m in WS_REGEX.findall(html):
|
|
if m.startswith("/") and not is_allowed_prefix(m):
|
|
ws_bad.append(m)
|
|
|
|
return found_absolute, found_relative_up, mixed_usage, ws_bad
|
|
|
|
|
|
def collect_testable_routes(app):
|
|
urls = []
|
|
for r in app.routes:
|
|
if not hasattr(r, "path"):
|
|
continue
|
|
path = r.path
|
|
|
|
# skip API-style or binary endpoints:
|
|
if path.startswith("/api"):
|
|
continue
|
|
if path.endswith(".js") or path.endswith(".css"):
|
|
continue
|
|
|
|
urls.append(path)
|
|
return sorted(set(urls))
|
|
|
|
|
|
class TestEOSDash:
|
|
|
|
def _assert_server_alive(self, base_url: str, timeout: int):
|
|
"""Poll the /eosdash/health endpoint until it's alive or timeout reached."""
|
|
startup = False
|
|
error = ""
|
|
result = None
|
|
|
|
for _ in range(int(timeout / 3)):
|
|
try:
|
|
result = requests.get(f"{base_url}/eosdash/health", timeout=2)
|
|
if result.status_code == HTTPStatus.OK:
|
|
startup = True
|
|
break
|
|
error = f"{result.status_code}, {str(result.content)}"
|
|
except Exception as ex:
|
|
error = str(ex)
|
|
time.sleep(3)
|
|
|
|
assert startup, f"Connection to {base_url}/eosdash/health failed: {error}"
|
|
assert result is not None
|
|
assert result.json()["status"] == "alive"
|
|
|
|
def test_eosdash_started(self, server_setup_for_class, is_system_test):
|
|
"""Test the EOSdash server is started by EOS server."""
|
|
eosdash_server = server_setup_for_class["eosdash_server"]
|
|
timeout = server_setup_for_class["timeout"]
|
|
self._assert_server_alive(eosdash_server, timeout)
|
|
|
|
def test_eosdash_proxied_by_eos(self, server_setup_for_class, is_system_test):
|
|
"""Test the EOSdash server proxied by EOS server."""
|
|
server = server_setup_for_class["server"]
|
|
timeout = server_setup_for_class["timeout"]
|
|
self._assert_server_alive(server, timeout)
|
|
|
|
def test_ingress_safe_links(self, server_setup_for_class, monkeypatch, tmp_path):
|
|
base = server_setup_for_class["eosdash_server"]
|
|
# The fixture waits for EOS only; EOS starts EOSdash after its own startup.
|
|
self._assert_server_alive(base, server_setup_for_class["timeout"])
|
|
|
|
with patch("akkudoktoreos.server.dash.context.ROOT_PATH", "/api/hassio_ingress/TOKEN/"):
|
|
eos_dir = tmp_path
|
|
monkeypatch.setenv("EOS_DIR", str(eos_dir))
|
|
monkeypatch.setenv("EOS_CONFIG_DIR", str(eos_dir))
|
|
|
|
# Import with environment vars set to prevent creation of EOS.config.json in wrong dir.
|
|
from akkudoktoreos.server.eosdash import app
|
|
|
|
for path in collect_testable_routes(app):
|
|
url = f"{base}{path}"
|
|
resp = requests.get(url)
|
|
resp.raise_for_status()
|
|
|
|
abs_issues, rel_up_issues, mixed_usage, ws_issues = scan_html_for_link_issues(resp.text)
|
|
|
|
#assert not abs_issues, (
|
|
# f"Forbidden absolute paths detected on {path}:\n" +
|
|
# "\n".join(abs_issues)
|
|
#)
|
|
|
|
assert not rel_up_issues, (
|
|
f"Relative paths navigating up (`../`) detected on {path}:\n" +
|
|
"\n".join(rel_up_issues)
|
|
)
|
|
|
|
assert not mixed_usage, f"Mixed absolute/relative linking detected on page {path}"
|
|
|
|
assert not ws_issues, f"Forbidden WebSocket paths detected on {path}:\n" + "\n".join(ws_issues)
|
|
|
|
@pytest.mark.parametrize(
|
|
"root_path,path,expected",
|
|
[
|
|
("/", "/eosdash/footer", "/eosdash/footer"),
|
|
("/", "eosdash/footer", "/eosdash/footer"),
|
|
("/", "footer", "/eosdash/footer"),
|
|
("/", "eosdash/assets/logo.png", "/eosdash/assets/logo.png"),
|
|
("/api/hassio_ingress/TOKEN/", "/api/hassio_ingress/TOKEN/eosdash/footer", "/api/hassio_ingress/TOKEN/eosdash/footer"),
|
|
("/api/hassio_ingress/TOKEN/", "/eosdash/footer", "/api/hassio_ingress/TOKEN/eosdash/footer"),
|
|
("/api/hassio_ingress/TOKEN/", "eosdash/footer", "/api/hassio_ingress/TOKEN/eosdash/footer"),
|
|
("/api/hassio_ingress/TOKEN/", "footer", "/api/hassio_ingress/TOKEN/eosdash/footer"),
|
|
("/api/hassio_ingress/TOKEN/", "assets/logo.png", "/api/hassio_ingress/TOKEN/eosdash/assets/logo.png"),
|
|
],
|
|
)
|
|
def test_request_url_for(self, root_path, path, expected):
|
|
"""Test that request_url_for produces absolute non-rewritable URLs.
|
|
|
|
Args:
|
|
root_path (str): Root path.
|
|
path (str): Path passed to request_url_for().
|
|
expected (str): Final produced path.
|
|
"""
|
|
|
|
result = request_url_for(path, root_path = root_path)
|
|
assert result == expected, (
|
|
f"URL rewriting mismatch. "
|
|
f"root_path={root_path}, path={path}, expected={expected}, got={result}"
|
|
)
|
|
|
|
# Test fallback to global var
|
|
with patch("akkudoktoreos.server.dash.context.ROOT_PATH", root_path):
|
|
result = request_url_for(path, root_path = None)
|
|
|
|
assert result == expected, (
|
|
f"URL rewriting mismatch. "
|
|
f"root_path={root_path}, path={path}, expected={expected}, got={result}"
|
|
)
|