Use mock_config_flow test helper in config tests (#142461)

This commit is contained in:
Erik Montnemery 2025-04-07 12:27:15 +02:00 committed by GitHub
parent 04dfa45db0
commit 2ed70ef241
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,7 +12,7 @@ import voluptuous as vol
from homeassistant import config_entries as core_ce, data_entry_flow, loader
from homeassistant.components.config import config_entries
from homeassistant.config_entries import HANDLERS, ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResultType
@ -34,13 +34,6 @@ from tests.common import (
from tests.typing import ClientSessionGenerator, WebSocketGenerator
@pytest.fixture
def clear_handlers() -> Generator[None]:
"""Clear config entry handlers."""
with patch.dict(HANDLERS, clear=True):
yield
@pytest.fixture(autouse=True)
def mock_test_component(hass: HomeAssistant) -> None:
"""Ensure a component called 'test' exists."""
@ -74,7 +67,7 @@ def mock_flow() -> Generator[None]:
@pytest.mark.usefixtures("freezer")
@pytest.mark.usefixtures("clear_handlers", "mock_flow")
@pytest.mark.usefixtures("mock_flow")
async def test_get_entries(hass: HomeAssistant, client: TestClient) -> None:
"""Test get entries."""
mock_integration(hass, MockModule("comp1"))
@ -358,7 +351,7 @@ async def test_reload_entry_in_setup_retry(
entry.add_to_hass(hass)
hass.config.components.add("comp")
with patch.dict(HANDLERS, {"comp": ConfigFlow, "test": ConfigFlow}):
with mock_config_flow("comp", ConfigFlow), mock_config_flow("test", ConfigFlow):
resp = await client.post(
f"/api/config/config_entries/entry/{entry.entry_id}/reload"
)
@ -422,7 +415,7 @@ async def test_initialize_flow(hass: HomeAssistant, client: TestClient) -> None:
errors={"username": "Should be unique."},
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow",
json={"handler": "test", "show_advanced_options": True},
@ -471,7 +464,7 @@ async def test_initialize_flow_unmet_dependency(
async def async_step_user(self, user_input=None):
pass
with patch.dict(HANDLERS, {"test2": TestFlow}):
with mock_config_flow("test2", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow",
json={"handler": "test2", "show_advanced_options": True},
@ -502,7 +495,7 @@ async def test_initialize_flow_unauth(
errors={"username": "Should be unique."},
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -519,7 +512,7 @@ async def test_abort(hass: HomeAssistant, client: TestClient) -> None:
async def async_step_user(self, user_input=None):
return self.async_abort(reason="bla")
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -552,7 +545,7 @@ async def test_create_account(hass: HomeAssistant, client: TestClient) -> None:
title="Test Entry", data={"secret": "account_token"}
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -620,7 +613,7 @@ async def test_two_step_flow(hass: HomeAssistant, client: TestClient) -> None:
title=user_input["user_title"], data={"secret": "account_token"}
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -638,7 +631,7 @@ async def test_two_step_flow(hass: HomeAssistant, client: TestClient) -> None:
"preview": None,
}
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
f"/api/config/config_entries/flow/{flow_id}",
json={"user_title": "user-title"},
@ -707,7 +700,7 @@ async def test_continue_flow_unauth(
title=user_input["user_title"], data={"secret": "account_token"}
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -774,7 +767,7 @@ async def test_get_progress_index(
assert self._get_reconfigure_entry() is entry
return await self.async_step_account()
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
form_hassio = await hass.config_entries.flow.async_init(
"test", context={"source": core_ce.SOURCE_HASSIO}
)
@ -838,7 +831,7 @@ async def test_get_progress_flow(hass: HomeAssistant, client: TestClient) -> Non
errors={"username": "Should be unique."},
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -874,7 +867,7 @@ async def test_get_progress_flow_unauth(
errors={"username": "Should be unique."},
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -918,7 +911,7 @@ async def test_options_flow(hass: HomeAssistant, client: TestClient) -> None:
).add_to_hass(hass)
entry = hass.config_entries.async_entries()[0]
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
url = "/api/config/config_entries/options/flow"
resp = await client.post(url, json={"handler": entry.entry_id})
@ -980,7 +973,7 @@ async def test_options_flow_unauth(
hass_admin_user.groups = []
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await getattr(client, method)(endpoint, json={"handler": entry.entry_id})
assert resp.status == HTTPStatus.UNAUTHORIZED
@ -1017,7 +1010,7 @@ async def test_two_step_options_flow(hass: HomeAssistant, client: TestClient) ->
).add_to_hass(hass)
entry = hass.config_entries.async_entries()[0]
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
url = "/api/config/config_entries/options/flow"
resp = await client.post(url, json={"handler": entry.entry_id})
@ -1035,7 +1028,7 @@ async def test_two_step_options_flow(hass: HomeAssistant, client: TestClient) ->
"preview": None,
}
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
f"/api/config/config_entries/options/flow/{flow_id}",
json={"enabled": True},
@ -1092,7 +1085,7 @@ async def test_options_flow_with_invalid_data(
).add_to_hass(hass)
entry = hass.config_entries.async_entries()[0]
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
url = "/api/config/config_entries/options/flow"
resp = await client.post(url, json={"handler": entry.entry_id})
@ -1118,7 +1111,7 @@ async def test_options_flow_with_invalid_data(
"preview": None,
}
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
f"/api/config/config_entries/options/flow/{flow_id}",
json={"choices": ["valid", "invalid"]},
@ -1812,7 +1805,7 @@ async def test_ignore_flow(
ws_client = await hass_ws_client(hass)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
result = await hass.config_entries.flow.async_init(
"test", context={"source": core_ce.SOURCE_USER} | flow_context
)
@ -1861,7 +1854,7 @@ async def test_ignore_flow_nonexisting(
assert response["error"]["code"] == "not_found"
@pytest.mark.usefixtures("clear_handlers", "freezer")
@pytest.mark.usefixtures("freezer")
async def test_get_matching_entries_ws(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None:
@ -2313,7 +2306,6 @@ async def test_get_matching_entries_ws(
assert response["success"] is False
@pytest.mark.usefixtures("clear_handlers")
async def test_subscribe_entries_ws(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
@ -2532,7 +2524,6 @@ async def test_subscribe_entries_ws(
]
@pytest.mark.usefixtures("clear_handlers")
async def test_subscribe_entries_ws_filtered(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
@ -2792,7 +2783,7 @@ async def test_flow_with_multiple_schema_errors(
),
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -2834,7 +2825,7 @@ async def test_flow_with_multiple_schema_errors_base(
),
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow", json={"handler": "test"}
)
@ -2893,7 +2884,7 @@ async def test_supports_reconfigure(
data={"secret": "account_token"},
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow",
json={"handler": "test", "entry_id": "1"},
@ -2915,7 +2906,7 @@ async def test_supports_reconfigure(
"errors": None,
}
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
f"/api/config/config_entries/flow/{flow_id}",
json={},
@ -2953,7 +2944,7 @@ async def test_does_not_support_reconfigure(
title="Test Entry", data={"secret": "account_token"}
)
with patch.dict(HANDLERS, {"test": TestFlow}):
with mock_config_flow("test", TestFlow):
resp = await client.post(
"/api/config/config_entries/flow",
json={"handler": "test", "entry_id": "1"},