Skip to content

Commit

Permalink
Improve Axis config flow tests (home-assistant#86644)
Browse files Browse the repository at this point in the history
* Improve Axis config flow tests

* Don't use prepare_config_entry
  • Loading branch information
Kane610 authored Jan 26, 2023
1 parent 6e285c8 commit 4181a9b
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions tests/components/axis/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
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)
Expand Down Expand Up @@ -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}
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -376,18 +383,18 @@ 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}
)

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(
Expand Down Expand Up @@ -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",
Expand All @@ -448,26 +455,22 @@ 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",
CONF_PASSWORD: "pass",
CONF_MODEL: MODEL,
CONF_NAME: NAME,
}
assert len(mock_setup_entry.mock_calls) == 1


@pytest.mark.parametrize(
Expand Down Expand Up @@ -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)

Expand All @@ -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

0 comments on commit 4181a9b

Please sign in to comment.