From 86a95013b63a0d1d0543ccaab2643608d073262a Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 30 Sep 2024 16:38:34 +0200 Subject: [PATCH] Use start_reauth_flow helper in ezviz and netatmo tests (#127100) * Use start_reauth_flow helper in netatmo tests * Use start_reauth_flow helper in ezviz tests --- tests/common.py | 38 ++++++++++++-------- tests/components/ezviz/test_config_flow.py | 25 ++++++------- tests/components/netatmo/test_config_flow.py | 6 ++-- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/tests/common.py b/tests/common.py index 47ed259583b..d53c3821364 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1063,21 +1063,31 @@ class MockConfigEntry(config_entries.ConfigEntry): context: dict[str, Any] | None = None, data: dict[str, Any] | None = None, ) -> ConfigFlowResult: - """Start a reauthentication flow for a config entry. + """Start a reauthentication flow.""" + return await start_reauth_flow(hass, self, context, data) - This helper method should be aligned with `ConfigEntry._async_init_reauth`. - """ - return await hass.config_entries.flow.async_init( - self.domain, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": self.entry_id, - "title_placeholders": {"name": self.title}, - "unique_id": self.unique_id, - } - | (context or {}), - data=self.data | (data or {}), - ) + +async def start_reauth_flow( + hass: HomeAssistant, + entry: ConfigEntry, + context: dict[str, Any] | None = None, + data: dict[str, Any] | None = None, +) -> ConfigFlowResult: + """Start a reauthentication flow for a config entry. + + This helper method should be aligned with `ConfigEntry._async_init_reauth`. + """ + return await hass.config_entries.flow.async_init( + entry.domain, + context={ + "source": config_entries.SOURCE_REAUTH, + "entry_id": entry.entry_id, + "title_placeholders": {"name": entry.title}, + "unique_id": entry.unique_id, + } + | (context or {}), + data=entry.data | (data or {}), + ) def patch_yaml_files(files_dict, endswith=True): diff --git a/tests/components/ezviz/test_config_flow.py b/tests/components/ezviz/test_config_flow.py index f9459635f2c..63499996c89 100644 --- a/tests/components/ezviz/test_config_flow.py +++ b/tests/components/ezviz/test_config_flow.py @@ -20,11 +20,7 @@ from homeassistant.components.ezviz.const import ( DEFAULT_TIMEOUT, DOMAIN, ) -from homeassistant.config_entries import ( - SOURCE_INTEGRATION_DISCOVERY, - SOURCE_REAUTH, - SOURCE_USER, -) +from homeassistant.config_entries import SOURCE_INTEGRATION_DISCOVERY, SOURCE_USER from homeassistant.const import ( CONF_CUSTOMIZE, CONF_IP_ADDRESS, @@ -45,6 +41,8 @@ from . import ( patch_async_setup_entry, ) +from tests.common import MockConfigEntry, start_reauth_flow + @pytest.mark.usefixtures("ezviz_config_flow") async def test_user_form(hass: HomeAssistant) -> None: @@ -134,9 +132,8 @@ async def test_async_step_reauth(hass: HomeAssistant) -> None: assert len(mock_setup_entry.mock_calls) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH}, data=USER_INPUT_VALIDATE - ) + new_entry = hass.config_entries.async_entries(DOMAIN)[0] + result = await start_reauth_flow(hass, new_entry) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} @@ -182,9 +179,10 @@ async def test_step_discovery_abort_if_cloud_account_missing( async def test_step_reauth_abort_if_cloud_account_missing(hass: HomeAssistant) -> None: """Test reauth and confirm step, abort if cloud account was removed.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH}, data=USER_INPUT_VALIDATE - ) + entry = MockConfigEntry(domain=DOMAIN, data=USER_INPUT_VALIDATE) + entry.add_to_hass(hass) + + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "ezviz_cloud_account_missing" @@ -562,9 +560,8 @@ async def test_async_step_reauth_exception( assert len(mock_setup_entry.mock_calls) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH}, data=USER_INPUT_VALIDATE - ) + new_entry = hass.config_entries.async_entries(DOMAIN)[0] + result = await start_reauth_flow(hass, new_entry) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} diff --git a/tests/components/netatmo/test_config_flow.py b/tests/components/netatmo/test_config_flow.py index 29a065c3be3..436f75b12ec 100644 --- a/tests/components/netatmo/test_config_flow.py +++ b/tests/components/netatmo/test_config_flow.py @@ -23,7 +23,7 @@ from homeassistant.helpers import config_entry_oauth2_flow from .conftest import CLIENT_ID -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, start_reauth_flow from tests.test_util.aiohttp import AiohttpClientMocker from tests.typing import ClientSessionGenerator @@ -282,9 +282,7 @@ async def test_reauth( assert len(mock_setup.mock_calls) == 1 # Should show form - result = await hass.config_entries.flow.async_init( - "netatmo", context={"source": config_entries.SOURCE_REAUTH} - ) + result = await start_reauth_flow(hass, new_entry) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm"