Improve Axis config flow tests (#86644)

* Improve Axis config flow tests

* Don't use prepare_config_entry
This commit is contained in:
Robert Svensson 2023-01-26 14:29:06 +01:00 committed by GitHub
parent 6e285c87c3
commit 4181a9baf0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,6 +36,15 @@ from .const import DEFAULT_HOST, MAC, MODEL, NAME
from tests.common import MockConfigEntry
@pytest.fixture(name="mock_config_entry")
async def mock_config_entry_fixture(hass, config_entry):
"""Mock config entry and setup entry."""
with patch("homeassistant.components.axis.async_setup_entry", return_value=True):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
yield config_entry
async def test_flow_manual_configuration(hass, setup_default_vapix_requests):
"""Test that config flow works."""
MockConfigEntry(domain=AXIS_DOMAIN, source=SOURCE_IGNORE).add_to_hass(hass)
@ -70,10 +79,10 @@ async def test_flow_manual_configuration(hass, setup_default_vapix_requests):
async def test_manual_configuration_update_configuration(
hass, setup_config_entry, mock_vapix_requests
hass, mock_config_entry, mock_vapix_requests
):
"""Test that config flow fails on already configured device."""
device = hass.data[AXIS_DOMAIN][setup_config_entry.entry_id]
assert mock_config_entry.data[CONF_HOST] == "1.2.3.4"
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, context={"source": SOURCE_USER}
@ -82,25 +91,21 @@ async def test_manual_configuration_update_configuration(
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == SOURCE_USER
with patch(
"homeassistant.components.axis.async_setup_entry", return_value=True
) as mock_setup_entry:
mock_vapix_requests("2.3.4.5")
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: "2.3.4.5",
CONF_USERNAME: "user",
CONF_PASSWORD: "pass",
CONF_PORT: 80,
},
)
await hass.async_block_till_done()
mock_vapix_requests("2.3.4.5")
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: "2.3.4.5",
CONF_USERNAME: "user",
CONF_PASSWORD: "pass",
CONF_PORT: 80,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert device.host == "2.3.4.5"
assert len(mock_setup_entry.mock_calls) == 1
assert mock_config_entry.data[CONF_HOST] == "2.3.4.5"
async def test_flow_fails_faulty_credentials(hass):
@ -202,15 +207,17 @@ async def test_flow_create_entry_multiple_existing_entries_of_same_model(
async def test_reauth_flow_update_configuration(
hass, setup_config_entry, mock_vapix_requests
hass, mock_config_entry, mock_vapix_requests
):
"""Test that config flow fails on already configured device."""
device = hass.data[AXIS_DOMAIN][setup_config_entry.entry_id]
assert mock_config_entry.data[CONF_HOST] == "1.2.3.4"
assert mock_config_entry.data[CONF_USERNAME] == "root"
assert mock_config_entry.data[CONF_PASSWORD] == "pass"
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN,
context={"source": SOURCE_REAUTH},
data=setup_config_entry.data,
data=mock_config_entry.data,
)
assert result["type"] == FlowResultType.FORM
@ -230,9 +237,9 @@ async def test_reauth_flow_update_configuration(
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert device.host == "2.3.4.5"
assert device.username == "user2"
assert device.password == "pass2"
assert mock_config_entry.data[CONF_HOST] == "2.3.4.5"
assert mock_config_entry.data[CONF_USERNAME] == "user2"
assert mock_config_entry.data[CONF_PASSWORD] == "pass2"
@pytest.mark.parametrize(
@ -376,10 +383,10 @@ async def test_discovery_flow(
],
)
async def test_discovered_device_already_configured(
hass, setup_config_entry, source: str, discovery_info: dict
hass, mock_config_entry, source: str, discovery_info: dict
):
"""Test that discovery doesn't setup already configured devices."""
assert setup_config_entry.data[CONF_HOST] == DEFAULT_HOST
assert mock_config_entry.data[CONF_HOST] == DEFAULT_HOST
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, data=discovery_info, context={"source": source}
@ -387,7 +394,7 @@ async def test_discovered_device_already_configured(
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert setup_config_entry.data[CONF_HOST] == DEFAULT_HOST
assert mock_config_entry.data[CONF_HOST] == DEFAULT_HOST
@pytest.mark.parametrize(
@ -432,14 +439,14 @@ async def test_discovered_device_already_configured(
)
async def test_discovery_flow_updated_configuration(
hass,
setup_config_entry,
mock_config_entry,
mock_vapix_requests,
source: str,
discovery_info: dict,
expected_port: int,
):
"""Test that discovery flow update configuration with new parameters."""
assert setup_config_entry.data == {
assert mock_config_entry.data == {
CONF_HOST: DEFAULT_HOST,
CONF_PORT: 80,
CONF_USERNAME: "root",
@ -448,18 +455,15 @@ async def test_discovery_flow_updated_configuration(
CONF_NAME: NAME,
}
with patch(
"homeassistant.components.axis.async_setup_entry", return_value=True
) as mock_setup_entry:
mock_vapix_requests("2.3.4.5")
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, data=discovery_info, context={"source": source}
)
await hass.async_block_till_done()
mock_vapix_requests("2.3.4.5")
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, data=discovery_info, context={"source": source}
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert setup_config_entry.data == {
assert mock_config_entry.data == {
CONF_HOST: "2.3.4.5",
CONF_PORT: expected_port,
CONF_USERNAME: "root",
@ -467,7 +471,6 @@ async def test_discovery_flow_updated_configuration(
CONF_MODEL: MODEL,
CONF_NAME: NAME,
}
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.parametrize(
@ -570,9 +573,8 @@ async def test_discovery_flow_ignore_link_local_address(
async def test_option_flow(hass, setup_config_entry):
"""Test config flow options."""
device = hass.data[AXIS_DOMAIN][setup_config_entry.entry_id]
assert device.option_stream_profile == DEFAULT_STREAM_PROFILE
assert device.option_video_source == DEFAULT_VIDEO_SOURCE
assert CONF_STREAM_PROFILE not in setup_config_entry.options
assert CONF_VIDEO_SOURCE not in setup_config_entry.options
result = await hass.config_entries.options.async_init(setup_config_entry.entry_id)
@ -599,5 +601,5 @@ async def test_option_flow(hass, setup_config_entry):
CONF_STREAM_PROFILE: "profile_1",
CONF_VIDEO_SOURCE: 1,
}
assert device.option_stream_profile == "profile_1"
assert device.option_video_source == 1
assert setup_config_entry.options[CONF_STREAM_PROFILE] == "profile_1"
assert setup_config_entry.options[CONF_VIDEO_SOURCE] == 1