Clean up Guardian config flow tests (#64598)

* Clean up Guardian config flow tests

* Docstring
This commit is contained in:
Aaron Bach 2022-01-20 20:32:07 -07:00 committed by GitHub
parent ff535053a9
commit aef8d9ee4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 30 deletions

View File

@ -1,18 +1,54 @@
"""Define fixtures for Elexa Guardian tests.""" """Define fixtures for Elexa Guardian tests."""
import json
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
from homeassistant.components.guardian.const import DOMAIN
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
from homeassistant.setup import async_setup_component
@pytest.fixture() from tests.common import MockConfigEntry, load_fixture
def ping_client():
"""Define a patched client that returns a successful ping response."""
with patch( @pytest.fixture(name="config_entry")
"homeassistant.components.guardian.async_setup_entry", return_value=True def config_entry_fixture(hass, config, unique_id):
), patch("aioguardian.client.Client.connect"), patch( """Define a config entry fixture."""
entry = MockConfigEntry(domain=DOMAIN, unique_id=unique_id, data=config)
entry.add_to_hass(hass)
return entry
@pytest.fixture(name="config")
def config_fixture(hass):
"""Define a config entry data fixture."""
return {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PORT: 7777,
}
@pytest.fixture(name="data_ping", scope="session")
def data_ping_fixture():
"""Define data from a successful ping response."""
return json.loads(load_fixture("ping_data.json", "guardian"))
@pytest.fixture(name="setup_guardian")
async def setup_guardian_fixture(hass, config, data_ping):
"""Define a fixture to set up Guardian."""
with patch("aioguardian.client.Client.connect"), patch(
"aioguardian.commands.system.SystemCommands.ping", "aioguardian.commands.system.SystemCommands.ping",
return_value={"command": 0, "status": "ok", "data": {"uid": "ABCDEF123456"}}, return_value=data_ping,
), patch( ), patch("aioguardian.client.Client.disconnect"), patch(
"aioguardian.client.Client.disconnect" "homeassistant.components.guardian.PLATFORMS", []
): ):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
yield yield
@pytest.fixture(name="unique_id")
def unique_id_fixture(hass):
"""Define a config entry unique ID fixture."""
return "guardian_3456"

View File

@ -0,0 +1,7 @@
{
"command": 0,
"status": "ok",
"data": {
"uid": "ABCDEF123456"
}
}

View File

@ -13,35 +13,24 @@ from homeassistant.components.guardian.config_flow import (
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, SOURCE_ZEROCONF from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
from tests.common import MockConfigEntry
async def test_duplicate_error(hass, config, config_entry, setup_guardian):
async def test_duplicate_error(hass, ping_client):
"""Test that errors are shown when duplicate entries are added.""" """Test that errors are shown when duplicate entries are added."""
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
MockConfigEntry(domain=DOMAIN, unique_id="guardian_3456", data=conf).add_to_hass(
hass
)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf DOMAIN, context={"source": SOURCE_USER}, data=config
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
async def test_connect_error(hass): async def test_connect_error(hass, config):
"""Test that the config entry errors out if the device cannot connect.""" """Test that the config entry errors out if the device cannot connect."""
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
with patch( with patch(
"aioguardian.client.Client.connect", "aioguardian.client.Client.connect",
side_effect=GuardianError, side_effect=GuardianError,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf DOMAIN, context={"source": SOURCE_USER}, data=config
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {CONF_IP_ADDRESS: "cannot_connect"} assert result["errors"] == {CONF_IP_ADDRESS: "cannot_connect"}
@ -59,10 +48,8 @@ async def test_get_pin_from_uid():
assert pin == "3456" assert pin == "3456"
async def test_step_user(hass, ping_client): async def test_step_user(hass, config, setup_guardian):
"""Test the user step.""" """Test the user step."""
conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PORT: 7777}
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
) )
@ -70,7 +57,7 @@ async def test_step_user(hass, ping_client):
assert result["step_id"] == "user" assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf DOMAIN, context={"source": SOURCE_USER}, data=config
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "ABCDEF123456" assert result["title"] == "ABCDEF123456"
@ -81,7 +68,7 @@ async def test_step_user(hass, ping_client):
} }
async def test_step_zeroconf(hass, ping_client): async def test_step_zeroconf(hass, setup_guardian):
"""Test the zeroconf step.""" """Test the zeroconf step."""
zeroconf_data = zeroconf.ZeroconfServiceInfo( zeroconf_data = zeroconf.ZeroconfServiceInfo(
host="192.168.1.100", host="192.168.1.100",
@ -134,7 +121,7 @@ async def test_step_zeroconf_already_in_progress(hass):
assert result["reason"] == "already_in_progress" assert result["reason"] == "already_in_progress"
async def test_step_dhcp(hass, ping_client): async def test_step_dhcp(hass, setup_guardian):
"""Test the dhcp step.""" """Test the dhcp step."""
dhcp_data = dhcp.DhcpServiceInfo( dhcp_data = dhcp.DhcpServiceInfo(
ip="192.168.1.100", ip="192.168.1.100",