From 4181a9baf030cb15ec363f1d1624a84c403dc0cb Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 26 Jan 2023 14:29:06 +0100 Subject: [PATCH] Improve Axis config flow tests (#86644) * Improve Axis config flow tests * Don't use prepare_config_entry --- tests/components/axis/test_config_flow.py | 90 ++++++++++++----------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index d66fb3881cb126..c1d1ad8bf9dd3a 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -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) @@ -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