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
This commit is contained in:
epenet 2024-09-30 14:28:13 +02:00 committed by GitHub
parent 730012edfd
commit 4bb768f39c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 1 deletions

View File

@ -1063,7 +1063,10 @@ class MockConfigEntry(config_entries.ConfigEntry):
context: dict[str, Any] | None = None, context: dict[str, Any] | None = None,
data: dict[str, Any] | None = None, data: dict[str, Any] | None = None,
) -> ConfigFlowResult: ) -> 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( return await hass.config_entries.flow.async_init(
self.domain, self.domain,
context={ context={

View File

@ -6381,3 +6381,62 @@ async def test_async_has_matching_flow_not_implemented(
# The flow does not implement is_matching # The flow does not implement is_matching
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
manager.flow.async_has_matching_flow(flow) 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