Add test helper for starting reconfiguration flow (#127154)

This commit is contained in:
epenet 2024-10-01 12:18:07 +02:00 committed by GitHub
parent f02f0eae59
commit b95dfe2b00
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 70 additions and 8 deletions

View file

@ -1066,6 +1066,25 @@ class MockConfigEntry(config_entries.ConfigEntry):
"""Start a reauthentication flow."""
return await start_reauth_flow(hass, self, context, data)
async def start_reconfigure_flow(
self,
hass: HomeAssistant,
context: dict[str, Any] | None = None,
data: dict[str, Any] | None = None,
) -> ConfigFlowResult:
"""Start a reconfiguration flow."""
return await hass.config_entries.flow.async_init(
self.domain,
context={
"source": config_entries.SOURCE_RECONFIGURE,
"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,

View file

@ -17,7 +17,6 @@ from homeassistant.components.axis.const import (
)
from homeassistant.config_entries import (
SOURCE_DHCP,
SOURCE_RECONFIGURE,
SOURCE_SSDP,
SOURCE_USER,
SOURCE_ZEROCONF,
@ -240,13 +239,7 @@ async def test_reconfiguration_flow_update_configuration(
assert config_entry_setup.data[CONF_USERNAME] == "root"
assert config_entry_setup.data[CONF_PASSWORD] == "pass"
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN,
context={
"source": SOURCE_RECONFIGURE,
"entry_id": config_entry_setup.entry_id,
},
)
result = await config_entry_setup.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"

View file

@ -6440,3 +6440,53 @@ async def test_reauth_helper_alignment(
# Ensure context and init data are aligned
assert helper_flow_context == reauth_flow_context
assert helper_flow_init_data == reauth_flow_init_data
async def test_reconfigure_helper_alignment(
hass: HomeAssistant,
manager: config_entries.ConfigEntries,
) -> None:
"""Test `start_reconfigure_flow` helper alignment.
It should be aligned with `ConfigEntry._async_init_reconfigure`.
"""
entry = MockConfigEntry(
title="test_title",
domain="test",
entry_id="01J915Q6T9F6G5V0QJX6HBC94T",
data={"host": "any", "port": 123},
unique_id=None,
)
entry.add_to_hass(hass)
mock_integration(hass, MockModule("test"))
mock_platform(hass, "test.config_flow", None)
# Check context via auto-generated reconfigure
entry.async_start_reconfigure(hass)
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
reconfigure_flow_context = flows[0]["context"]
reconfigure_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_reconfigure_flow` helper
await entry.start_reconfigure_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 == reconfigure_flow_context
assert helper_flow_init_data == reconfigure_flow_init_data