[web_server] Move the AP only warning into web_server and emit the script tag once

This commit is contained in:
J. Nick Koston
2026-08-19 13:07:19 -05:00
parent deb641d413
commit 427b83dc0a
4 changed files with 118 additions and 80 deletions
+71 -13
View File
@@ -1,31 +1,89 @@
"""Tests for the web_server component index page builder."""
"""Tests for the web_server component index page builder and validation."""
from esphome.components.web_server import build_index_html
from esphome.const import CONF_CSS_URL, CONF_JS_URL
import logging
import pytest
from esphome.components.web_server import (
_final_validate_ap_only_hosted_ui,
build_index_html,
)
from esphome.const import (
CONF_AP,
CONF_CSS_URL,
CONF_JS_URL,
CONF_LOCAL,
CONF_NETWORKS,
CONF_SSID,
CONF_WIFI,
)
import esphome.final_validate as fv
JS_URL = "https://oi.esphome.io/v2/www.js"
def test_build_index_html_has_offline_hint() -> None:
"""The hosted script tag shows a hint when the browser cannot download it."""
html = build_index_html(
{CONF_JS_URL: "https://oi.esphome.io/v2/www.js", CONF_CSS_URL: ""}
)
assert '<script src="https://oi.esphome.io/v2/www.js" onerror="' in html
html = build_index_html({CONF_JS_URL: JS_URL, CONF_CSS_URL: ""})
assert f'<script src="{JS_URL}" onerror="' in html
assert "document.body.innerText='Could not download the web interface." in html
assert "local: true" in html
def test_build_index_html_hint_disabled() -> None:
"""The plain script tag is kept when the hint is not wanted (captive_portal)."""
html = build_index_html(
{CONF_JS_URL: "https://oi.esphome.io/v2/www.js", CONF_CSS_URL: ""},
offline_hint=False,
)
assert '<script src="https://oi.esphome.io/v2/www.js"></script>' in html
html = build_index_html({CONF_JS_URL: JS_URL, CONF_CSS_URL: ""}, offline_hint=False)
assert f'<script src="{JS_URL}"></script>' in html
assert "onerror" not in html
def test_build_index_html_without_js_url() -> None:
"""No hosted script and no hint when js_url is empty."""
html = build_index_html({CONF_JS_URL: "", CONF_CSS_URL: ""})
assert "onerror" not in html
assert "<script" not in html
@pytest.mark.parametrize(
("web_server_config", "full_config", "expect_warning"),
[
# AP only with a hosted page: warn.
({CONF_JS_URL: JS_URL}, {CONF_WIFI: {CONF_AP: {}}}, True),
# local: true serves the page from the device.
({CONF_JS_URL: JS_URL, CONF_LOCAL: True}, {CONF_WIFI: {CONF_AP: {}}}, False),
# No hosted script at all.
({CONF_JS_URL: ""}, {CONF_WIFI: {CONF_AP: {}}}, False),
# captive_portal serves its own local page.
(
{CONF_JS_URL: JS_URL},
{CONF_WIFI: {CONF_AP: {}}, "captive_portal": {}},
False,
),
# STA with AP fallback: the AP is rarely used, stay quiet.
(
{CONF_JS_URL: JS_URL},
{CONF_WIFI: {CONF_AP: {}, CONF_NETWORKS: [{CONF_SSID: "x"}]}},
False,
),
# No AP, or no wifi at all (ethernet).
(
{CONF_JS_URL: JS_URL},
{CONF_WIFI: {CONF_NETWORKS: [{CONF_SSID: "x"}]}},
False,
),
({CONF_JS_URL: JS_URL}, {}, False),
],
)
def test_final_validate_ap_only_hosted_ui_warning(
web_server_config: dict,
full_config: dict,
expect_warning: bool,
caplog: pytest.LogCaptureFixture,
) -> None:
"""AP only configs with a hosted page get a hint to use local: true."""
token = fv.full_config.set({"web_server": web_server_config, **full_config})
try:
with caplog.at_level(logging.WARNING):
_final_validate_ap_only_hosted_ui(web_server_config)
finally:
fv.full_config.reset(token)
assert ("AP only" in caplog.text) is expect_warning
-33
View File
@@ -1,13 +1,10 @@
"""Tests for WiFi component public helpers."""
import logging
import pytest
from esphome.components.esp32 import const
from esphome.components.wifi import (
check_placeholder_credentials,
final_validate,
has_native_wifi,
variant_has_wifi,
)
@@ -20,7 +17,6 @@ from esphome.const import (
Platform,
)
from esphome.core import EsphomeError, Lambda
import esphome.final_validate as fv
@pytest.mark.parametrize(
@@ -201,32 +197,3 @@ def test_check_placeholder_credentials_skips_template_ssid() -> None:
"""A templated (Lambda) SSID is not a string and is skipped."""
config = _wifi_config(networks=[{CONF_SSID: Lambda('return "x";')}])
assert check_placeholder_credentials(config) is None
@pytest.mark.parametrize(
("wifi_config", "full_config", "expect_warning"),
[
# AP only with a hosted web_server page: warn.
({CONF_AP: {}}, {"web_server": {}}, True),
# local: true serves the page from the device.
({CONF_AP: {}}, {"web_server": {"local": True}}, False),
# captive_portal serves its own local page.
({CONF_AP: {}}, {"web_server": {}, "captive_portal": {}}, False),
# STA with AP fallback: the AP is rarely used, stay quiet.
({CONF_AP: {}, CONF_NETWORKS: [{CONF_SSID: "x"}]}, {"web_server": {}}, False),
],
)
def test_final_validate_ap_only_hosted_web_server_warning(
wifi_config: dict,
full_config: dict,
expect_warning: bool,
caplog: pytest.LogCaptureFixture,
) -> None:
"""AP only configs with a hosted web_server page get a hint to use local: true."""
token = fv.full_config.set({"wifi": wifi_config, **full_config})
try:
with caplog.at_level(logging.WARNING):
final_validate(wifi_config)
finally:
fv.full_config.reset(token)
assert ("AP only" in caplog.text) is expect_warning