Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 committed Aug 5, 2022
1 parent f3da446 commit 4c6cac7
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 71 deletions.
9 changes: 4 additions & 5 deletions homeassistant/components/system_bridge/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ async def _validate_input(
hass.async_create_task(websocket_client.listen())
response = await websocket_client.get_data(GetData(modules=["system"]))
_LOGGER.info("Got response: %s", response.json())
system = System(**response.data)
try:
system = System(**response.data)
except ValueError as err:
raise CannotConnect from err
except AuthenticationException as exception:
_LOGGER.warning(
"Authentication error when connecting to %s: %s", data[CONF_HOST], exception
Expand All @@ -80,10 +83,6 @@ async def _validate_input(

_LOGGER.debug("Got System data: %s", system.json())

if system.uuid is None:
error = "No UUID in result!"
raise CannotConnect(error)

return {"hostname": host, "uuid": system.uuid}


Expand Down
167 changes: 101 additions & 66 deletions tests/components/system_bridge/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@
import asyncio
from unittest.mock import patch

from systembridgeconnector.const import (
EVENT_DATA,
EVENT_MESSAGE,
EVENT_MODULE,
EVENT_SUBTYPE,
EVENT_TYPE,
SUBTYPE_BAD_API_KEY,
TYPE_DATA_UPDATE,
TYPE_ERROR,
)
from systembridgeconnector.const import TYPE_DATA_UPDATE
from systembridgeconnector.exceptions import (
AuthenticationException,
ConnectionClosedException,
ConnectionErrorException,
)
from systembridgeconnector.models.response import Response
from systembridgeconnector.models.system import LastUpdated, System

from homeassistant import config_entries, data_entry_flow
from homeassistant.components import zeroconf
Expand Down Expand Up @@ -48,8 +41,8 @@
addresses=["1.1.1.1"],
port=9170,
hostname="test-bridge.local.",
type="_system-bridge._udp.local.",
name="System Bridge - test-bridge._system-bridge._udp.local.",
type="_system-bridge._tcp.local.",
name="System Bridge - test-bridge._system-bridge._tcp.local.",
properties={
"address": "http://test-bridge:9170",
"fqdn": "test-bridge",
Expand All @@ -66,34 +59,56 @@
addresses=["1.1.1.1"],
port=9170,
hostname="test-bridge.local.",
type="_system-bridge._udp.local.",
name="System Bridge - test-bridge._system-bridge._udp.local.",
type="_system-bridge._tcp.local.",
name="System Bridge - test-bridge._system-bridge._tcp.local.",
properties={
"something": "bad",
},
)

FIXTURE_DATA_SYSTEM = {
EVENT_TYPE: TYPE_DATA_UPDATE,
EVENT_MESSAGE: "Data changed",
EVENT_MODULE: "system",
EVENT_DATA: {
"uuid": FIXTURE_UUID,
},
}

FIXTURE_DATA_SYSTEM_BAD = {
EVENT_TYPE: TYPE_DATA_UPDATE,
EVENT_MESSAGE: "Data changed",
EVENT_MODULE: "system",
EVENT_DATA: {},
}
FIXTURE_SYSTEM = System(
boot_time=1,
fqdn="",
hostname="1.1.1.1",
ip_address_4="1.1.1.1",
mac_address=FIXTURE_MAC_ADDRESS,
platform="",
platform_version="",
uptime=1,
uuid=FIXTURE_UUID,
version="",
version_latest="",
version_newer_available=False,
last_updated=LastUpdated(
boot_time=1,
fqdn=1,
hostname=1,
ip_address_4=1,
mac_address=1,
platform=1,
platform_version=1,
uptime=1,
uuid=1,
version=1,
version_latest=1,
version_newer_available=1,
),
)

FIXTURE_DATA_AUTH_ERROR = {
EVENT_TYPE: TYPE_ERROR,
EVENT_SUBTYPE: SUBTYPE_BAD_API_KEY,
EVENT_MESSAGE: "Invalid api-key",
}
FIXTURE_DATA_RESPONSE = Response(
id="1234",
type=TYPE_DATA_UPDATE,
message="Data received",
data=FIXTURE_SYSTEM.dict(),
)

FIXTURE_DATA_RESPONSE_BAD = Response(
id="1234",
type=TYPE_DATA_UPDATE,
message="Data received",
data={},
)


async def test_show_user_form(hass: HomeAssistant) -> None:
Expand All @@ -117,9 +132,11 @@ async def test_user_flow(hass: HomeAssistant) -> None:

with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch("systembridgeconnector.websocket_client.WebSocketClient.get_data"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
return_value=FIXTURE_DATA_SYSTEM,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
return_value=FIXTURE_DATA_RESPONSE,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen"
), patch(
"homeassistant.components.system_bridge.async_setup_entry",
return_value=True,
Expand Down Expand Up @@ -167,11 +184,13 @@ async def test_form_connection_closed_cannot_connect(hass: HomeAssistant) -> Non
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] is None

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
side_effect=ConnectionClosedException,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], FIXTURE_USER_INPUT
Expand All @@ -192,11 +211,13 @@ async def test_form_timeout_cannot_connect(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] is None

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
side_effect=asyncio.TimeoutError,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], FIXTURE_USER_INPUT
Expand All @@ -217,11 +238,13 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] is None

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
side_effect=AuthenticationException,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], FIXTURE_USER_INPUT
Expand All @@ -242,11 +265,13 @@ async def test_form_uuid_error(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] is None

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
return_value=FIXTURE_DATA_SYSTEM_BAD,
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
return_value=FIXTURE_DATA_RESPONSE_BAD,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], FIXTURE_USER_INPUT
Expand All @@ -267,11 +292,13 @@ async def test_form_unknown_error(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] is None

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
side_effect=Exception,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], FIXTURE_USER_INPUT
Expand All @@ -292,11 +319,13 @@ async def test_reauth_authorization_error(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "authenticate"

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
side_effect=AuthenticationException,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], FIXTURE_AUTH_INPUT
Expand Down Expand Up @@ -340,11 +369,13 @@ async def test_reauth_connection_closed_error(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "authenticate"

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
side_effect=ConnectionClosedException,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], FIXTURE_AUTH_INPUT
Expand All @@ -370,11 +401,13 @@ async def test_reauth_flow(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "authenticate"

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
return_value=FIXTURE_DATA_SYSTEM,
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
return_value=FIXTURE_DATA_RESPONSE,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.listen"
), patch(
"homeassistant.components.system_bridge.async_setup_entry",
return_value=True,
Expand Down Expand Up @@ -402,11 +435,13 @@ async def test_zeroconf_flow(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert not result["errors"]

with patch("systembridgeconnector.websocket_client.WebSocketClient.connect"), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data"
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect"
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
return_value=FIXTURE_DATA_RESPONSE,
), patch(
"systembridgeconnector.websocket_client.WebSocketClient.receive_message",
return_value=FIXTURE_DATA_SYSTEM,
"systembridgeconnector.websocket_client.WebSocketClient.listen"
), patch(
"homeassistant.components.system_bridge.async_setup_entry",
return_value=True,
Expand Down

0 comments on commit 4c6cac7

Please sign in to comment.