From 4bb768f39cab34aec8731c71de9855945f39744f Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:28:13 +0200 Subject: [PATCH] Add test for start_reauth_flow test helper (#127093) * Improve docstring in start_reauth_flow * Add test * Make private * Make fully private until actually needed --- tests/common.py | 5 ++- tests/test_config_entries.py | 59 ++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/tests/common.py b/tests/common.py index 9603e7e2f29..47ed259583b 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1063,7 +1063,10 @@ class MockConfigEntry(config_entries.ConfigEntry): context: dict[str, Any] | None = None, data: dict[str, Any] | None = None, ) -> ConfigFlowResult: - """Start a reauthentication flow.""" + """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( self.domain, context={ diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index e16e0a0ace5..e92095bad75 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -6381,3 +6381,62 @@ async def test_async_has_matching_flow_not_implemented( # The flow does not implement is_matching with pytest.raises(NotImplementedError): manager.flow.async_has_matching_flow(flow) + + +async def test_reauth_helper_alignment( + hass: HomeAssistant, + manager: config_entries.ConfigEntries, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test `start_reauth_flow` helper alignment. + + It should be aligned with `ConfigEntry._async_init_reauth`. + """ + entry = MockConfigEntry( + title="test_title", + domain="test", + entry_id="01J915Q6T9F6G5V0QJX6HBC94T", + data={"host": "any", "port": 123}, + unique_id=None, + ) + entry.add_to_hass(hass) + + mock_setup_entry = AsyncMock( + side_effect=ConfigEntryAuthFailed("The password is no longer valid") + ) + mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry)) + mock_platform(hass, "test.config_flow", None) + + # Check context via auto-generated reauth + await manager.async_setup(entry.entry_id) + await hass.async_block_till_done() + assert "could not authenticate: The password is no longer valid" in caplog.text + + assert entry.state is config_entries.ConfigEntryState.SETUP_ERROR + assert entry.reason == "The password is no longer valid" + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + + reauth_flow_context = flows[0]["context"] + reauth_flow_init_data = hass.config_entries.flow._progress[ + flows[0]["flow_id"] + ].init_data + + # Clear to make way for `start_reauth_flow` helper + manager.flow.async_abort(flows[0]["flow_id"]) + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 0 + + # Check context via `start_reauth_flow` helper + await entry.start_reauth_flow(hass) + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + helper_flow_context = flows[0]["context"] + helper_flow_init_data = hass.config_entries.flow._progress[ + flows[0]["flow_id"] + ].init_data + + # Ensure context and init data are aligned + assert helper_flow_context == reauth_flow_context + assert helper_flow_init_data == reauth_flow_init_data