Add tests for Roku (#34380)
This commit is contained in:
1 parent
3b12fd22a4
commit
b3eba49a2f
13 files changed
+520
-105
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""Test the Roku config flow."""
|
||||
from socket import gaierror as SocketGIAError
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from asynctest import patch
|
||||
from requests.exceptions import RequestException
|
||||
from requests_mock import Mocker
|
||||
from roku import RokuException
|
||||
|
||||
from homeassistant.components.roku.const import DOMAIN
|
||||
@@ -27,91 +27,74 @@ from tests.components.roku import (
|
||||
SSDP_LOCATION,
|
||||
UPNP_FRIENDLY_NAME,
|
||||
UPNP_SERIAL,
|
||||
MockDeviceInfo,
|
||||
mock_connection,
|
||||
setup_integration,
|
||||
)
|
||||
|
||||
|
||||
async def async_configure_flow(
|
||||
hass: HomeAssistantType, flow_id: str, user_input: Optional[Dict] = None
|
||||
) -> Any:
|
||||
"""Set up mock Roku integration flow."""
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku.device_info",
|
||||
new=MockDeviceInfo,
|
||||
):
|
||||
return await hass.config_entries.flow.async_configure(
|
||||
flow_id=flow_id, user_input=user_input
|
||||
)
|
||||
|
||||
|
||||
async def async_init_flow(
|
||||
hass: HomeAssistantType,
|
||||
handler: str = DOMAIN,
|
||||
context: Optional[Dict] = None,
|
||||
data: Any = None,
|
||||
) -> Any:
|
||||
"""Set up mock Roku integration flow."""
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku.device_info",
|
||||
new=MockDeviceInfo,
|
||||
):
|
||||
return await hass.config_entries.flow.async_init(
|
||||
handler=handler, context=context, data=data
|
||||
)
|
||||
|
||||
|
||||
async def test_duplicate_error(hass: HomeAssistantType) -> None:
|
||||
async def test_duplicate_error(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
await setup_integration(hass, skip_entry_setup=True)
|
||||
await setup_integration(hass, requests_mock, skip_entry_setup=True)
|
||||
|
||||
result = await async_init_flow(
|
||||
hass, context={CONF_SOURCE: SOURCE_IMPORT}, data={CONF_HOST: HOST}
|
||||
mock_connection(requests_mock)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_IMPORT}, data=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
result = await async_init_flow(
|
||||
hass, context={CONF_SOURCE: SOURCE_USER}, data={CONF_HOST: HOST}
|
||||
user_input = {CONF_HOST: HOST}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}, data=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
result = await async_init_flow(
|
||||
hass,
|
||||
context={CONF_SOURCE: SOURCE_SSDP},
|
||||
data={
|
||||
ATTR_UPNP_FRIENDLY_NAME: UPNP_FRIENDLY_NAME,
|
||||
ATTR_SSDP_LOCATION: SSDP_LOCATION,
|
||||
ATTR_UPNP_SERIAL: UPNP_SERIAL,
|
||||
},
|
||||
discovery_info = {
|
||||
ATTR_UPNP_FRIENDLY_NAME: UPNP_FRIENDLY_NAME,
|
||||
ATTR_SSDP_LOCATION: SSDP_LOCATION,
|
||||
ATTR_UPNP_SERIAL: UPNP_SERIAL,
|
||||
}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistantType) -> None:
|
||||
async def test_form(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
"""Test the user step."""
|
||||
await async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
mock_connection(requests_mock)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
"homeassistant.components.roku.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.roku.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await async_configure_flow(hass, result["flow_id"], {CONF_HOST: HOST})
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == HOST
|
||||
assert result["data"] == {CONF_HOST: HOST}
|
||||
|
||||
assert result["data"]
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
@@ -144,12 +127,13 @@ async def test_form_cannot_connect_request(hass: HomeAssistantType) -> None:
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku._call",
|
||||
side_effect=RequestException,
|
||||
) as mock_validate_input:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input={CONF_HOST: HOST}
|
||||
flow_id=result["flow_id"], user_input=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
@@ -165,12 +149,13 @@ async def test_form_cannot_connect_socket(hass: HomeAssistantType) -> None:
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku._call",
|
||||
side_effect=SocketGIAError,
|
||||
) as mock_validate_input:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input={CONF_HOST: HOST}
|
||||
flow_id=result["flow_id"], user_input=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
@@ -186,11 +171,12 @@ async def test_form_unknown_error(hass: HomeAssistantType) -> None:
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku._call", side_effect=Exception,
|
||||
) as mock_validate_input:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input={CONF_HOST: HOST}
|
||||
flow_id=result["flow_id"], user_input=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
@@ -200,36 +186,42 @@ async def test_form_unknown_error(hass: HomeAssistantType) -> None:
|
||||
assert len(mock_validate_input.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import(hass: HomeAssistantType) -> None:
|
||||
async def test_import(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
"""Test the import step."""
|
||||
mock_connection(requests_mock)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
"homeassistant.components.roku.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.roku.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await async_init_flow(
|
||||
hass, context={CONF_SOURCE: SOURCE_IMPORT}, data={CONF_HOST: HOST}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_IMPORT}, data=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == HOST
|
||||
assert result["data"] == {CONF_HOST: HOST}
|
||||
|
||||
assert result["data"]
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_ssdp_discovery(hass: HomeAssistantType) -> None:
|
||||
async def test_ssdp_discovery(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
"""Test the ssdp discovery step."""
|
||||
mock_connection(requests_mock)
|
||||
|
||||
discovery_info = {
|
||||
ATTR_SSDP_LOCATION: SSDP_LOCATION,
|
||||
ATTR_UPNP_FRIENDLY_NAME: UPNP_FRIENDLY_NAME,
|
||||
ATTR_UPNP_SERIAL: UPNP_SERIAL,
|
||||
}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_SSDP},
|
||||
data={
|
||||
ATTR_SSDP_LOCATION: SSDP_LOCATION,
|
||||
ATTR_UPNP_FRIENDLY_NAME: UPNP_FRIENDLY_NAME,
|
||||
ATTR_UPNP_SERIAL: UPNP_SERIAL,
|
||||
},
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
@@ -241,14 +233,17 @@ async def test_ssdp_discovery(hass: HomeAssistantType) -> None:
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.roku.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await async_configure_flow(hass, result["flow_id"], {})
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input={}
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == UPNP_FRIENDLY_NAME
|
||||
assert result["data"] == {
|
||||
CONF_HOST: HOST,
|
||||
CONF_NAME: UPNP_FRIENDLY_NAME,
|
||||
}
|
||||
|
||||
assert result["data"]
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
assert result["data"][CONF_NAME] == UPNP_FRIENDLY_NAME
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
Reference in new issue
Block a user