mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Test generic config flow error recovery (#185487)
This commit is contained in:
@@ -6,6 +6,7 @@ import errno
|
||||
from http import HTTPStatus
|
||||
import os.path
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
|
||||
|
||||
import httpx2
|
||||
@@ -28,7 +29,11 @@ from homeassistant.components.stream import (
|
||||
CONF_RTSP_TRANSPORT,
|
||||
CONF_USE_WALLCLOCK_AS_TIMESTAMPS,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigFlowResult
|
||||
from homeassistant.config_entries import (
|
||||
ConfigEntriesFlowManager,
|
||||
ConfigFlowResult,
|
||||
OptionsFlowManager,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_AUTHENTICATION,
|
||||
CONF_PASSWORD,
|
||||
@@ -63,6 +68,19 @@ TESTDATA_ONLYSTREAM = TESTDATA.copy()
|
||||
TESTDATA_ONLYSTREAM.pop(CONF_STILL_IMAGE_URL)
|
||||
|
||||
|
||||
async def _async_configure_and_confirm(
|
||||
flow_manager: ConfigEntriesFlowManager | OptionsFlowManager,
|
||||
flow_id: str,
|
||||
user_input: dict[str, Any],
|
||||
) -> ConfigFlowResult:
|
||||
"""Submit the input and confirm the preview."""
|
||||
result = await flow_manager.async_configure(flow_id, user_input)
|
||||
assert result["step_id"] == "user_confirm"
|
||||
return await flow_manager.async_configure(
|
||||
result["flow_id"], user_input={CONF_CONFIRMED_OK: True}
|
||||
)
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_setup_entry")
|
||||
async def test_form(
|
||||
@@ -440,6 +458,8 @@ async def test_form_only_stream(
|
||||
assert image_obj.content == fakeimgbytes_jpg
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_setup_entry")
|
||||
async def test_form_still_and_stream_not_provided(
|
||||
hass: HomeAssistant, user_flow: ConfigFlowResult
|
||||
) -> None:
|
||||
@@ -455,9 +475,13 @@ async def test_form_still_and_stream_not_provided(
|
||||
},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"base": "no_still_image_or_stream_url"}
|
||||
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA_ONLYSTILL
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.parametrize(
|
||||
@@ -490,12 +514,13 @@ async def test_form_still_and_stream_not_provided(
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("mock_create_stream", "mock_setup_entry")
|
||||
async def test_form_image_http_exceptions(
|
||||
side_effect,
|
||||
expected_message,
|
||||
side_effect: Exception,
|
||||
expected_message: dict[str, str],
|
||||
hass: HomeAssistant,
|
||||
user_flow: ConfigFlowResult,
|
||||
mock_create_stream: MagicMock,
|
||||
fakeimgbytes_png: bytes,
|
||||
) -> None:
|
||||
"""Test we handle image http exceptions."""
|
||||
respx.get("http://127.0.0.1/testurl/1").side_effect = [side_effect]
|
||||
@@ -505,9 +530,14 @@ async def test_form_image_http_exceptions(
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == expected_message
|
||||
|
||||
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_form_image_http_302(
|
||||
@@ -535,10 +565,11 @@ async def test_form_image_http_302(
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("mock_create_stream", "mock_setup_entry")
|
||||
async def test_form_stream_invalidimage(
|
||||
hass: HomeAssistant,
|
||||
user_flow: ConfigFlowResult,
|
||||
mock_create_stream: MagicMock,
|
||||
fakeimgbytes_png: bytes,
|
||||
) -> None:
|
||||
"""Test we handle invalid image when a stream is specified."""
|
||||
respx.get("http://127.0.0.1/testurl/1").respond(stream=b"invalid")
|
||||
@@ -548,15 +579,21 @@ async def test_form_stream_invalidimage(
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"still_image_url": "invalid_still_image"}
|
||||
|
||||
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("mock_create_stream", "mock_setup_entry")
|
||||
async def test_form_stream_invalidimage2(
|
||||
hass: HomeAssistant,
|
||||
user_flow: ConfigFlowResult,
|
||||
mock_create_stream: MagicMock,
|
||||
fakeimgbytes_png: bytes,
|
||||
) -> None:
|
||||
"""Test we handle invalid image when a stream is specified."""
|
||||
respx.get("http://127.0.0.1/testurl/1").respond(content=None)
|
||||
@@ -566,15 +603,21 @@ async def test_form_stream_invalidimage2(
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"still_image_url": "unable_still_load_no_image"}
|
||||
|
||||
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("mock_create_stream", "mock_setup_entry")
|
||||
async def test_form_stream_invalidimage3(
|
||||
hass: HomeAssistant,
|
||||
user_flow: ConfigFlowResult,
|
||||
mock_create_stream: MagicMock,
|
||||
fakeimgbytes_png: bytes,
|
||||
) -> None:
|
||||
"""Test we handle invalid image when a stream is specified."""
|
||||
respx.get("http://127.0.0.1/testurl/1").respond(content=bytes([0xFF]))
|
||||
@@ -584,12 +627,17 @@ async def test_form_stream_invalidimage3(
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"still_image_url": "invalid_still_image"}
|
||||
|
||||
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png")
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_setup_entry")
|
||||
async def test_form_stream_timeout(
|
||||
hass: HomeAssistant,
|
||||
user_flow: ConfigFlowResult,
|
||||
@@ -608,12 +656,21 @@ async def test_form_stream_timeout(
|
||||
TESTDATA,
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"stream_source": "timeout"}
|
||||
|
||||
part_recv = mock_create_stream.return_value.add_provider.return_value.part_recv
|
||||
part_recv.return_value = True
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_form_stream_not_set_up(hass: HomeAssistant, user_flow) -> None:
|
||||
@pytest.mark.usefixtures("mock_create_stream", "mock_setup_entry")
|
||||
async def test_form_stream_not_set_up(
|
||||
hass: HomeAssistant, user_flow: ConfigFlowResult
|
||||
) -> None:
|
||||
"""Test we handle if stream has not been set up."""
|
||||
|
||||
with patch(
|
||||
@@ -627,9 +684,13 @@ async def test_form_stream_not_set_up(hass: HomeAssistant, user_flow) -> None:
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result1["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result1["errors"] == {"stream_source": "stream_not_set_up"}
|
||||
|
||||
result2 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA_ONLYSTREAM
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_form_stream_other_error(hass: HomeAssistant, user_flow) -> None:
|
||||
@@ -650,7 +711,7 @@ async def test_form_stream_other_error(hass: HomeAssistant, user_flow) -> None:
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png")
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_create_stream", "mock_setup_entry")
|
||||
async def test_form_stream_permission_error(
|
||||
hass: HomeAssistant, user_flow: ConfigFlowResult
|
||||
) -> None:
|
||||
@@ -664,12 +725,16 @@ async def test_form_stream_permission_error(
|
||||
TESTDATA,
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"stream_source": "stream_not_permitted"}
|
||||
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png")
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_create_stream", "mock_setup_entry")
|
||||
async def test_form_no_route_to_host(
|
||||
hass: HomeAssistant, user_flow: ConfigFlowResult
|
||||
) -> None:
|
||||
@@ -683,12 +748,16 @@ async def test_form_no_route_to_host(
|
||||
TESTDATA,
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"stream_source": "stream_no_route_to_host"}
|
||||
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png")
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_create_stream", "mock_setup_entry")
|
||||
async def test_form_stream_io_error(
|
||||
hass: HomeAssistant, user_flow: ConfigFlowResult
|
||||
) -> None:
|
||||
@@ -702,9 +771,13 @@ async def test_form_stream_io_error(
|
||||
TESTDATA,
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"stream_source": "stream_io_error"}
|
||||
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.flow, user_flow["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png")
|
||||
@@ -724,10 +797,9 @@ async def test_form_oserror(hass: HomeAssistant, user_flow: ConfigFlowResult) ->
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png")
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_create_stream")
|
||||
async def test_options_template_error(
|
||||
hass: HomeAssistant,
|
||||
mock_create_stream: MagicMock,
|
||||
config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the options flow with a template error."""
|
||||
@@ -792,9 +864,14 @@ async def test_options_template_error(
|
||||
user_input=data,
|
||||
)
|
||||
assert result7.get("type") is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result7["errors"] == {"stream_source": "malformed_url"}
|
||||
|
||||
data[CONF_STREAM_SOURCE] = "http://127.0.0.1/testurl/2"
|
||||
result8 = await _async_configure_and_confirm(
|
||||
hass.config_entries.options, result7["flow_id"], data
|
||||
)
|
||||
assert result8["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_slug(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""Test that the slug function generates an error in case of invalid template.
|
||||
@@ -845,7 +922,8 @@ async def test_options_only_stream(
|
||||
assert result3["data"][CONF_CONTENT_TYPE] == "image/jpeg"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_setup_entry")
|
||||
async def test_options_still_and_stream_not_provided(hass: HomeAssistant) -> None:
|
||||
"""Test we show a suitable error if neither still or stream URL are provided."""
|
||||
data = TESTDATA_ONLYSTILL.copy()
|
||||
@@ -869,12 +947,16 @@ async def test_options_still_and_stream_not_provided(hass: HomeAssistant) -> Non
|
||||
user_input=data,
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"base": "no_still_image_or_stream_url"}
|
||||
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.options, result2["flow_id"], TESTDATA_ONLYSTILL
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@respx.mock
|
||||
@pytest.mark.usefixtures("fakeimg_png")
|
||||
@pytest.mark.usefixtures("fakeimg_png", "mock_create_stream")
|
||||
async def test_options_permission_error(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
@@ -890,9 +972,13 @@ async def test_options_permission_error(
|
||||
TESTDATA,
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
|
||||
assert result2["errors"] == {"stream_source": "stream_not_permitted"}
|
||||
|
||||
result3 = await _async_configure_and_confirm(
|
||||
hass.config_entries.options, result2["flow_id"], TESTDATA
|
||||
)
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_migrate_existing_ids(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
|
||||
Reference in New Issue
Block a user