Update config flow test scaffolding (#30694)

* Update config flow test scaffolding

* asyncpatch -> patch

* Update classname
This commit is contained in:
Paulus Schoutsen 2020-01-13 06:20:25 -08:00 committed by Pascal Vizeli
parent 040b283a14
commit f50714d7e9
2 changed files with 37 additions and 13 deletions

View file

@ -13,22 +13,49 @@ _LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema({"host": str, "username": str, "password": str})
class PlaceholderHub:
"""Placeholder class to make tests pass.
TODO Remove this placeholder class and replace with things from your PyPI package.
"""
def __init__(self, host):
"""Initialize."""
self.host = host
async def authenticate(self, username, password) -> bool:
"""Test if we can authenticate with the host."""
return True
async def validate_input(hass: core.HomeAssistant, data):
"""Validate the user input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
"""
# TODO validate the data can be used to set up a connection.
# If your PyPI package is not built with async, pass your methods
# to the executor:
# await hass.async_add_executor_job(
# your_validate_func, data["username"], data["password"]
# )
hub = PlaceholderHub(data["host"])
if not await hub.authenticate(data["username"], data["password"]):
raise InvalidAuth
# If you cannot connect:
# throw CannotConnect
# If the authentication is wrong:
# InvalidAuth
# Return some info we want to store in the config entry.
# Return info that you want to store in the config entry.
return {"title": "Name of the device"}
class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for NEW_NAME."""
VERSION = 1

View file

@ -1,12 +1,10 @@
"""Test the NEW_NAME config flow."""
from unittest.mock import patch
from asynctest import patch
from homeassistant import config_entries, setup
from homeassistant.components.NEW_DOMAIN.config_flow import CannotConnect, InvalidAuth
from homeassistant.components.NEW_DOMAIN.const import DOMAIN
from tests.common import mock_coro
async def test_form(hass):
"""Test we get the form."""
@ -18,13 +16,12 @@ async def test_form(hass):
assert result["errors"] == {}
with patch(
"homeassistant.components.NEW_DOMAIN.config_flow.validate_input",
return_value=mock_coro({"title": "Test Title"}),
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
return_value=True,
), patch(
"homeassistant.components.NEW_DOMAIN.async_setup", return_value=mock_coro(True)
"homeassistant.components.NEW_DOMAIN.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.NEW_DOMAIN.async_setup_entry",
return_value=mock_coro(True),
"homeassistant.components.NEW_DOMAIN.async_setup_entry", return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -36,7 +33,7 @@ async def test_form(hass):
)
assert result2["type"] == "create_entry"
assert result2["title"] == "Test Title"
assert result2["title"] == "Name of the device"
assert result2["data"] == {
"host": "1.1.1.1",
"username": "test-username",
@ -54,7 +51,7 @@ async def test_form_invalid_auth(hass):
)
with patch(
"homeassistant.components.NEW_DOMAIN.config_flow.validate_input",
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
side_effect=InvalidAuth,
):
result2 = await hass.config_entries.flow.async_configure(
@ -77,7 +74,7 @@ async def test_form_cannot_connect(hass):
)
with patch(
"homeassistant.components.NEW_DOMAIN.config_flow.validate_input",
"homeassistant.components.NEW_DOMAIN.config_flow.PlaceholderHub.authenticate",
side_effect=CannotConnect,
):
result2 = await hass.config_entries.flow.async_configure(