mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 09:38:58 +01:00
Improve KNX tests and avoid dns lookups (#166508)
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
"""Test the KNX config flow."""
|
||||
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import MagicMock, Mock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
from xknx.exceptions import XKNXException
|
||||
from xknx.exceptions.exception import CommunicationError, InvalidSecureConfiguration
|
||||
from xknx.io import DEFAULT_MCAST_GRP, DEFAULT_MCAST_PORT
|
||||
from xknx.io.gateway_scanner import GatewayDescriptor
|
||||
@@ -60,6 +61,12 @@ FIXTURE_UPLOAD_UUID = "0123456789abcdef0123456789abcdef"
|
||||
GATEWAY_INDIVIDUAL_ADDRESS = IndividualAddress("1.0.0")
|
||||
|
||||
|
||||
async def _mock_validate_ip_for_invalid_local(ip_address: str) -> str:
|
||||
if ip_address in {"no_local_ip", "asdf"}:
|
||||
raise XKNXException
|
||||
return ip_address
|
||||
|
||||
|
||||
@pytest.fixture(name="knx_setup")
|
||||
def fixture_knx_setup():
|
||||
"""Mock KNX entry setup."""
|
||||
@@ -238,15 +245,19 @@ async def test_routing_setup_advanced(
|
||||
assert result["errors"] == {"base": "no_router_discovered"}
|
||||
|
||||
# invalid user input
|
||||
result_invalid_input = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_KNX_MCAST_GRP: "10.1.2.3", # no valid multicast group
|
||||
CONF_KNX_MCAST_PORT: 3675,
|
||||
CONF_KNX_INDIVIDUAL_ADDRESS: "not_a_valid_address",
|
||||
CONF_KNX_LOCAL_IP: "no_local_ip",
|
||||
},
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.components.knx.config_flow.xknx_validate_ip",
|
||||
new=AsyncMock(side_effect=_mock_validate_ip_for_invalid_local),
|
||||
):
|
||||
result_invalid_input = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_KNX_MCAST_GRP: "10.1.2.3", # no valid multicast group
|
||||
CONF_KNX_MCAST_PORT: 3675,
|
||||
CONF_KNX_INDIVIDUAL_ADDRESS: "not_a_valid_address",
|
||||
CONF_KNX_LOCAL_IP: "no_local_ip",
|
||||
},
|
||||
)
|
||||
assert result_invalid_input["type"] is FlowResultType.FORM
|
||||
assert result_invalid_input["step_id"] == "routing"
|
||||
assert result_invalid_input["errors"] == {
|
||||
@@ -751,15 +762,19 @@ async def test_tunneling_setup_for_local_ip(
|
||||
"base": "no_tunnel_discovered",
|
||||
}
|
||||
# invalid local ip address
|
||||
result_invalid_local = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_KNX_TUNNELING_TYPE: CONF_KNX_TUNNELING,
|
||||
CONF_HOST: "192.168.0.2",
|
||||
CONF_PORT: 3675,
|
||||
CONF_KNX_LOCAL_IP: "asdf",
|
||||
},
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.components.knx.config_flow.xknx_validate_ip",
|
||||
new=AsyncMock(side_effect=_mock_validate_ip_for_invalid_local),
|
||||
):
|
||||
result_invalid_local = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_KNX_TUNNELING_TYPE: CONF_KNX_TUNNELING,
|
||||
CONF_HOST: "192.168.0.2",
|
||||
CONF_PORT: 3675,
|
||||
CONF_KNX_LOCAL_IP: "asdf",
|
||||
},
|
||||
)
|
||||
assert result_invalid_local["type"] is FlowResultType.FORM
|
||||
assert result_invalid_local["step_id"] == "manual_tunnel"
|
||||
assert result_invalid_local["errors"] == {
|
||||
|
||||
Reference in New Issue
Block a user