Improve Axis config flow tests (#86644)
* Improve Axis config flow tests * Don't use prepare_config_entry
This commit is contained in:
parent
6e285c87c3
commit
4181a9baf0
1 changed files with 46 additions and 44 deletions
|
@ -36,6 +36,15 @@ from .const import DEFAULT_HOST, MAC, MODEL, NAME
|
||||||
from tests.common import MockConfigEntry
|
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):
|
async def test_flow_manual_configuration(hass, setup_default_vapix_requests):
|
||||||
"""Test that config flow works."""
|
"""Test that config flow works."""
|
||||||
MockConfigEntry(domain=AXIS_DOMAIN, source=SOURCE_IGNORE).add_to_hass(hass)
|
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(
|
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."""
|
"""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(
|
result = await hass.config_entries.flow.async_init(
|
||||||
AXIS_DOMAIN, context={"source": SOURCE_USER}
|
AXIS_DOMAIN, context={"source": SOURCE_USER}
|
||||||
|
@ -82,25 +91,21 @@ async def test_manual_configuration_update_configuration(
|
||||||
assert result["type"] == FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
assert result["step_id"] == SOURCE_USER
|
assert result["step_id"] == SOURCE_USER
|
||||||
|
|
||||||
with patch(
|
mock_vapix_requests("2.3.4.5")
|
||||||
"homeassistant.components.axis.async_setup_entry", return_value=True
|
result = await hass.config_entries.flow.async_configure(
|
||||||
) as mock_setup_entry:
|
result["flow_id"],
|
||||||
mock_vapix_requests("2.3.4.5")
|
user_input={
|
||||||
result = await hass.config_entries.flow.async_configure(
|
CONF_HOST: "2.3.4.5",
|
||||||
result["flow_id"],
|
CONF_USERNAME: "user",
|
||||||
user_input={
|
CONF_PASSWORD: "pass",
|
||||||
CONF_HOST: "2.3.4.5",
|
CONF_PORT: 80,
|
||||||
CONF_USERNAME: "user",
|
},
|
||||||
CONF_PASSWORD: "pass",
|
)
|
||||||
CONF_PORT: 80,
|
await hass.async_block_till_done()
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == FlowResultType.ABORT
|
assert result["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
assert device.host == "2.3.4.5"
|
assert mock_config_entry.data[CONF_HOST] == "2.3.4.5"
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_fails_faulty_credentials(hass):
|
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(
|
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."""
|
"""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(
|
result = await hass.config_entries.flow.async_init(
|
||||||
AXIS_DOMAIN,
|
AXIS_DOMAIN,
|
||||||
context={"source": SOURCE_REAUTH},
|
context={"source": SOURCE_REAUTH},
|
||||||
data=setup_config_entry.data,
|
data=mock_config_entry.data,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
|
@ -230,9 +237,9 @@ async def test_reauth_flow_update_configuration(
|
||||||
|
|
||||||
assert result["type"] == FlowResultType.ABORT
|
assert result["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
assert device.host == "2.3.4.5"
|
assert mock_config_entry.data[CONF_HOST] == "2.3.4.5"
|
||||||
assert device.username == "user2"
|
assert mock_config_entry.data[CONF_USERNAME] == "user2"
|
||||||
assert device.password == "pass2"
|
assert mock_config_entry.data[CONF_PASSWORD] == "pass2"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
@ -376,10 +383,10 @@ async def test_discovery_flow(
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_discovered_device_already_configured(
|
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."""
|
"""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(
|
result = await hass.config_entries.flow.async_init(
|
||||||
AXIS_DOMAIN, data=discovery_info, context={"source": source}
|
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["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
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(
|
@pytest.mark.parametrize(
|
||||||
|
@ -432,14 +439,14 @@ async def test_discovered_device_already_configured(
|
||||||
)
|
)
|
||||||
async def test_discovery_flow_updated_configuration(
|
async def test_discovery_flow_updated_configuration(
|
||||||
hass,
|
hass,
|
||||||
setup_config_entry,
|
mock_config_entry,
|
||||||
mock_vapix_requests,
|
mock_vapix_requests,
|
||||||
source: str,
|
source: str,
|
||||||
discovery_info: dict,
|
discovery_info: dict,
|
||||||
expected_port: int,
|
expected_port: int,
|
||||||
):
|
):
|
||||||
"""Test that discovery flow update configuration with new parameters."""
|
"""Test that discovery flow update configuration with new parameters."""
|
||||||
assert setup_config_entry.data == {
|
assert mock_config_entry.data == {
|
||||||
CONF_HOST: DEFAULT_HOST,
|
CONF_HOST: DEFAULT_HOST,
|
||||||
CONF_PORT: 80,
|
CONF_PORT: 80,
|
||||||
CONF_USERNAME: "root",
|
CONF_USERNAME: "root",
|
||||||
|
@ -448,18 +455,15 @@ async def test_discovery_flow_updated_configuration(
|
||||||
CONF_NAME: NAME,
|
CONF_NAME: NAME,
|
||||||
}
|
}
|
||||||
|
|
||||||
with patch(
|
mock_vapix_requests("2.3.4.5")
|
||||||
"homeassistant.components.axis.async_setup_entry", return_value=True
|
result = await hass.config_entries.flow.async_init(
|
||||||
) as mock_setup_entry:
|
AXIS_DOMAIN, data=discovery_info, context={"source": source}
|
||||||
mock_vapix_requests("2.3.4.5")
|
)
|
||||||
result = await hass.config_entries.flow.async_init(
|
await hass.async_block_till_done()
|
||||||
AXIS_DOMAIN, data=discovery_info, context={"source": source}
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == FlowResultType.ABORT
|
assert result["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
assert setup_config_entry.data == {
|
assert mock_config_entry.data == {
|
||||||
CONF_HOST: "2.3.4.5",
|
CONF_HOST: "2.3.4.5",
|
||||||
CONF_PORT: expected_port,
|
CONF_PORT: expected_port,
|
||||||
CONF_USERNAME: "root",
|
CONF_USERNAME: "root",
|
||||||
|
@ -467,7 +471,6 @@ async def test_discovery_flow_updated_configuration(
|
||||||
CONF_MODEL: MODEL,
|
CONF_MODEL: MODEL,
|
||||||
CONF_NAME: NAME,
|
CONF_NAME: NAME,
|
||||||
}
|
}
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@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):
|
async def test_option_flow(hass, setup_config_entry):
|
||||||
"""Test config flow options."""
|
"""Test config flow options."""
|
||||||
device = hass.data[AXIS_DOMAIN][setup_config_entry.entry_id]
|
assert CONF_STREAM_PROFILE not in setup_config_entry.options
|
||||||
assert device.option_stream_profile == DEFAULT_STREAM_PROFILE
|
assert CONF_VIDEO_SOURCE not in setup_config_entry.options
|
||||||
assert device.option_video_source == DEFAULT_VIDEO_SOURCE
|
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_init(setup_config_entry.entry_id)
|
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_STREAM_PROFILE: "profile_1",
|
||||||
CONF_VIDEO_SOURCE: 1,
|
CONF_VIDEO_SOURCE: 1,
|
||||||
}
|
}
|
||||||
assert device.option_stream_profile == "profile_1"
|
assert setup_config_entry.options[CONF_STREAM_PROFILE] == "profile_1"
|
||||||
assert device.option_video_source == 1
|
assert setup_config_entry.options[CONF_VIDEO_SOURCE] == 1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue