From 7dbd0e52741fd5470ae43c4f4cdbccb95f75e13d Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 1 Sep 2021 22:38:00 +0200 Subject: [PATCH] Fix zeroconf mock and use it in CI group 1's tests (#55526) * Fix zeroconf mock and use it in CI group 1's tests * Mock HaAsyncServiceBrowser --- .../components/bosch_shc/test_config_flow.py | 26 +++++++++---------- .../devolo_home_control/test_init.py | 4 +-- tests/components/esphome/test_config_flow.py | 18 ++++++++----- .../components/homekit_controller/conftest.py | 7 ----- tests/conftest.py | 6 +++-- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/tests/components/bosch_shc/test_config_flow.py b/tests/components/bosch_shc/test_config_flow.py index 0e760b899c1..6d8ef9bd32e 100644 --- a/tests/components/bosch_shc/test_config_flow.py +++ b/tests/components/bosch_shc/test_config_flow.py @@ -28,7 +28,7 @@ DISCOVERY_INFO = { } -async def test_form_user(hass): +async def test_form_user(hass, mock_zeroconf): """Test we get the form.""" await setup.async_setup_component(hass, "persistent_notification", {}) result = await hass.config_entries.flow.async_init( @@ -92,7 +92,7 @@ async def test_form_user(hass): assert len(mock_setup_entry.mock_calls) == 1 -async def test_form_get_info_connection_error(hass): +async def test_form_get_info_connection_error(hass, mock_zeroconf): """Test we handle connection error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -136,7 +136,7 @@ async def test_form_get_info_exception(hass): assert result2["errors"] == {"base": "unknown"} -async def test_form_pairing_error(hass): +async def test_form_pairing_error(hass, mock_zeroconf): """Test we handle pairing error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -178,7 +178,7 @@ async def test_form_pairing_error(hass): assert result3["errors"] == {"base": "pairing_failed"} -async def test_form_user_invalid_auth(hass): +async def test_form_user_invalid_auth(hass, mock_zeroconf): """Test we handle invalid auth.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -227,7 +227,7 @@ async def test_form_user_invalid_auth(hass): assert result3["errors"] == {"base": "invalid_auth"} -async def test_form_validate_connection_error(hass): +async def test_form_validate_connection_error(hass, mock_zeroconf): """Test we handle connection error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -276,7 +276,7 @@ async def test_form_validate_connection_error(hass): assert result3["errors"] == {"base": "cannot_connect"} -async def test_form_validate_session_error(hass): +async def test_form_validate_session_error(hass, mock_zeroconf): """Test we handle session error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -325,7 +325,7 @@ async def test_form_validate_session_error(hass): assert result3["errors"] == {"base": "session_error"} -async def test_form_validate_exception(hass): +async def test_form_validate_exception(hass, mock_zeroconf): """Test we handle exception.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -374,7 +374,7 @@ async def test_form_validate_exception(hass): assert result3["errors"] == {"base": "unknown"} -async def test_form_already_configured(hass): +async def test_form_already_configured(hass, mock_zeroconf): """Test we get the form.""" await setup.async_setup_component(hass, "persistent_notification", {}) entry = MockConfigEntry( @@ -410,7 +410,7 @@ async def test_form_already_configured(hass): assert entry.data["host"] == "1.1.1.1" -async def test_zeroconf(hass): +async def test_zeroconf(hass, mock_zeroconf): """Test we get the form.""" await setup.async_setup_component(hass, "persistent_notification", {}) @@ -479,7 +479,7 @@ async def test_zeroconf(hass): assert len(mock_setup_entry.mock_calls) == 1 -async def test_zeroconf_already_configured(hass): +async def test_zeroconf_already_configured(hass, mock_zeroconf): """Test we get the form.""" await setup.async_setup_component(hass, "persistent_notification", {}) entry = MockConfigEntry( @@ -512,7 +512,7 @@ async def test_zeroconf_already_configured(hass): assert entry.data["host"] == "1.1.1.1" -async def test_zeroconf_cannot_connect(hass): +async def test_zeroconf_cannot_connect(hass, mock_zeroconf): """Test we get the form.""" with patch( "boschshcpy.session.SHCSession.mdns_info", side_effect=SHCConnectionError @@ -526,7 +526,7 @@ async def test_zeroconf_cannot_connect(hass): assert result["reason"] == "cannot_connect" -async def test_zeroconf_not_bosch_shc(hass): +async def test_zeroconf_not_bosch_shc(hass, mock_zeroconf): """Test we filter out non-bosch_shc devices.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -537,7 +537,7 @@ async def test_zeroconf_not_bosch_shc(hass): assert result["reason"] == "not_bosch_shc" -async def test_reauth(hass): +async def test_reauth(hass, mock_zeroconf): """Test we get the form.""" await setup.async_setup_component(hass, "persistent_notification", {}) mock_config = MockConfigEntry( diff --git a/tests/components/devolo_home_control/test_init.py b/tests/components/devolo_home_control/test_init.py index 657836f9d16..311da47aac0 100644 --- a/tests/components/devolo_home_control/test_init.py +++ b/tests/components/devolo_home_control/test_init.py @@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant from tests.components.devolo_home_control import configure_integration -async def test_setup_entry(hass: HomeAssistant): +async def test_setup_entry(hass: HomeAssistant, mock_zeroconf): """Test setup entry.""" entry = configure_integration(hass) with patch("homeassistant.components.devolo_home_control.HomeControl"): @@ -34,7 +34,7 @@ async def test_setup_entry_maintenance(hass: HomeAssistant): assert entry.state is ConfigEntryState.SETUP_RETRY -async def test_setup_gateway_offline(hass: HomeAssistant): +async def test_setup_gateway_offline(hass: HomeAssistant, mock_zeroconf): """Test setup entry fails on gateway offline.""" entry = configure_integration(hass) with patch( diff --git a/tests/components/esphome/test_config_flow.py b/tests/components/esphome/test_config_flow.py index a5de14d946d..27f0c853615 100644 --- a/tests/components/esphome/test_config_flow.py +++ b/tests/components/esphome/test_config_flow.py @@ -55,7 +55,7 @@ def mock_setup_entry(): yield -async def test_user_connection_works(hass, mock_client): +async def test_user_connection_works(hass, mock_client, mock_zeroconf): """Test we can finish a config flow.""" result = await hass.config_entries.flow.async_init( "esphome", @@ -86,7 +86,9 @@ async def test_user_connection_works(hass, mock_client): assert mock_client.password == "" -async def test_user_resolve_error(hass, mock_api_connection_error, mock_client): +async def test_user_resolve_error( + hass, mock_api_connection_error, mock_client, mock_zeroconf +): """Test user step with IP resolve error.""" class MockResolveError(mock_api_connection_error): @@ -116,7 +118,9 @@ async def test_user_resolve_error(hass, mock_api_connection_error, mock_client): assert len(mock_client.disconnect.mock_calls) == 1 -async def test_user_connection_error(hass, mock_api_connection_error, mock_client): +async def test_user_connection_error( + hass, mock_api_connection_error, mock_client, mock_zeroconf +): """Test user step with connection error.""" mock_client.device_info.side_effect = mock_api_connection_error @@ -135,7 +139,7 @@ async def test_user_connection_error(hass, mock_api_connection_error, mock_clien assert len(mock_client.disconnect.mock_calls) == 1 -async def test_user_with_password(hass, mock_client): +async def test_user_with_password(hass, mock_client, mock_zeroconf): """Test user step with password.""" mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test")) @@ -161,7 +165,9 @@ async def test_user_with_password(hass, mock_client): assert mock_client.password == "password1" -async def test_user_invalid_password(hass, mock_api_connection_error, mock_client): +async def test_user_invalid_password( + hass, mock_api_connection_error, mock_client, mock_zeroconf +): """Test user step with invalid password.""" mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test")) @@ -185,7 +191,7 @@ async def test_user_invalid_password(hass, mock_api_connection_error, mock_clien assert result["errors"] == {"base": "invalid_auth"} -async def test_discovery_initiation(hass, mock_client): +async def test_discovery_initiation(hass, mock_client, mock_zeroconf): """Test discovery importing works.""" mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266")) diff --git a/tests/components/homekit_controller/conftest.py b/tests/components/homekit_controller/conftest.py index 266fa177fb2..4e095b1d2d9 100644 --- a/tests/components/homekit_controller/conftest.py +++ b/tests/components/homekit_controller/conftest.py @@ -11,13 +11,6 @@ import homeassistant.util.dt as dt_util from tests.components.light.conftest import mock_light_profiles # noqa: F401 -@pytest.fixture(autouse=True) -def mock_zeroconf(): - """Mock zeroconf.""" - with mock.patch("homeassistant.components.zeroconf.models.HaZeroconf") as mock_zc: - yield mock_zc.return_value - - @pytest.fixture def utcnow(request): """Freeze time at a known point.""" diff --git a/tests/conftest.py b/tests/conftest.py index ce8e244f420..36cba8d8e47 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -480,8 +480,10 @@ async def mqtt_mock(hass, mqtt_client_mock, mqtt_config): @pytest.fixture def mock_zeroconf(): """Mock zeroconf.""" - with patch("homeassistant.components.zeroconf.models.HaZeroconf") as mock_zc: - yield mock_zc.return_value + with patch("homeassistant.components.zeroconf.HaZeroconf", autospec=True), patch( + "homeassistant.components.zeroconf.HaAsyncServiceBrowser", autospec=True + ): + yield @pytest.fixture