Test config flow error recovery (batch 2) (#185463)

This commit is contained in:
Erwin Douna
2026-10-09 17:06:47 +02:00
committed by GitHub
parent 7a0f9170fc
commit ae5dec1b9b
23 changed files with 877 additions and 139 deletions
+14 -2
View File
@@ -118,8 +118,9 @@ async def test_manual_configuration_duplicate_fails(
(config_flow.CannotConnect, "cannot_connect"),
],
)
@pytest.mark.usefixtures("mock_default_requests")
async def test_flow_fails_on_api(
hass: HomeAssistant, exc: Exception, error: str
hass: HomeAssistant, exc: type[Exception], error: str
) -> None:
"""Test that config flow fails on faulty credentials."""
result = await hass.config_entries.flow.async_init(
@@ -144,9 +145,20 @@ async def test_flow_fails_on_api(
},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": error}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_PROTOCOL: "http",
CONF_HOST: "1.2.3.4",
CONF_USERNAME: "user",
CONF_PASSWORD: "pass",
CONF_PORT: 80,
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_default_requests")
@pytest.mark.parametrize("param_properties_status_code", [404])
@@ -3,13 +3,14 @@
from unittest.mock import AsyncMock
import aiohttp
import pytest
from homeassistant import config_entries
from homeassistant.components.azure_devops.const import CONF_ORG, CONF_PROJECT, DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import FIXTURE_REAUTH_INPUT, FIXTURE_USER_INPUT, UNIQUE_ID
from . import DEVOPS_PROJECT, FIXTURE_REAUTH_INPUT, FIXTURE_USER_INPUT, UNIQUE_ID
from tests.common import MockConfigEntry
@@ -24,6 +25,7 @@ async def test_show_user_form(hass: HomeAssistant) -> None:
assert result["step_id"] == "user"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_authorization_error(
hass: HomeAssistant,
mock_devops_client: AsyncMock,
@@ -48,9 +50,16 @@ async def test_authorization_error(
assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "user"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "invalid_auth"}
mock_devops_client.authorize.return_value = True
mock_devops_client.authorized = True
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
FIXTURE_USER_INPUT,
)
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_reauth_authorization_error(
hass: HomeAssistant,
@@ -74,10 +83,19 @@ async def test_reauth_authorization_error(
assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "reauth_confirm"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "invalid_auth"}
mock_devops_client.authorize.return_value = True
mock_devops_client.authorized = True
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
FIXTURE_REAUTH_INPUT,
)
assert result3["type"] is FlowResultType.ABORT
assert result3["reason"] == "reauth_successful"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_connection_error(
hass: HomeAssistant,
mock_devops_client: AsyncMock,
@@ -102,9 +120,16 @@ async def test_connection_error(
assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "user"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "cannot_connect"}
mock_devops_client.authorize.side_effect = None
mock_devops_client.authorized = True
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
FIXTURE_USER_INPUT,
)
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_reauth_connection_error(
hass: HomeAssistant,
@@ -129,10 +154,19 @@ async def test_reauth_connection_error(
assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "reauth_confirm"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "cannot_connect"}
mock_devops_client.authorize.side_effect = None
mock_devops_client.authorized = True
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
FIXTURE_REAUTH_INPUT,
)
assert result3["type"] is FlowResultType.ABORT
assert result3["reason"] == "reauth_successful"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_project_error(
hass: HomeAssistant,
mock_devops_client: AsyncMock,
@@ -158,9 +192,15 @@ async def test_project_error(
assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "user"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "project_error"}
mock_devops_client.get_project.return_value = DEVOPS_PROJECT
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
FIXTURE_USER_INPUT,
)
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_reauth_project_error(
hass: HomeAssistant,
@@ -187,9 +227,16 @@ async def test_reauth_project_error(
assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "reauth_confirm"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "project_error"}
mock_devops_client.get_project.return_value = DEVOPS_PROJECT
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
FIXTURE_REAUTH_INPUT,
)
assert result3["type"] is FlowResultType.ABORT
assert result3["reason"] == "reauth_successful"
async def test_reauth_flow(
hass: HomeAssistant,
@@ -160,9 +160,15 @@ async def test_connection_error_sas(
SAS_CONFIG.copy(),
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": error_message}
mock_get_eventhub_properties.side_effect = None
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
SAS_CONFIG.copy(),
)
assert result3["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.parametrize(
("side_effect", "error_message"),
@@ -197,9 +203,15 @@ async def test_connection_error_cs(
CS_CONFIG.copy(),
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": error_message}
mock_from_connection_string.return_value.get_eventhub_properties.side_effect = None
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CS_CONFIG.copy(),
)
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_options_flow(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test options flow."""
@@ -250,7 +250,6 @@ async def test_config_flow_errors(
)
assert result.get("type") is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result.get("errors") == {expected_field: expected_error}
if error_type == "restricted_bucket":
@@ -269,6 +268,16 @@ async def test_config_flow_errors(
"error_message": "test (bad_request)",
}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
**USER_INPUT,
CONF_KEY_ID: b2_fixture.key_id,
CONF_APPLICATION_KEY: b2_fixture.application_key,
},
)
assert result.get("type") is FlowResultType.CREATE_ENTRY
@pytest.mark.parametrize(
("flow_type", "scenario"),
@@ -320,6 +329,16 @@ async def test_advanced_flows(
assert result.get("type") is FlowResultType.FORM
assert result.get("errors") == {"base": "invalid_credentials"}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_KEY_ID: b2_fixture.key_id,
CONF_APPLICATION_KEY: b2_fixture.application_key,
},
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "reauth_successful"
elif flow_type == "reconfigure":
source = SOURCE_RECONFIGURE
step_name = "reconfigure"
@@ -359,8 +378,19 @@ async def test_advanced_flows(
if scenario == "validation_error":
assert result.get("type") is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result.get("errors") == {"base": "invalid_credentials"}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_KEY_ID: b2_fixture.key_id,
CONF_APPLICATION_KEY: b2_fixture.application_key,
"bucket": "testBucket",
"prefix": "new_prefix/",
},
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "reconfigure_successful"
else:
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "reconfigure_successful"
+30 -2
View File
@@ -66,9 +66,23 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {CONF_IP_ADDRESS: "cannot_connect"}
with (
_patch_device_config_flow(),
patch(
"homeassistant.components.baf.async_setup_entry",
return_value=True,
),
):
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_IP_ADDRESS: "127.0.0.1"},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_form_unknown_exception(hass: HomeAssistant) -> None:
"""Test we handle unknown exceptions."""
@@ -83,9 +97,23 @@ async def test_form_unknown_exception(hass: HomeAssistant) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "unknown"}
with (
_patch_device_config_flow(),
patch(
"homeassistant.components.baf.async_setup_entry",
return_value=True,
),
):
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_IP_ADDRESS: "127.0.0.1"},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_zeroconf_discovery(hass: HomeAssistant) -> None:
"""Test we can setup from zeroconf discovery."""
+55 -3
View File
@@ -68,9 +68,26 @@ async def test_form_cannot_connect(hass: HomeAssistant, client: MagicMock) -> No
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "cannot_connect"}
with (
patch(
"homeassistant.components.balboa.config_flow.SpaClient.__aenter__",
return_value=client,
),
patch(
"homeassistant.components.balboa.async_setup_entry",
return_value=True,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_form_spa_not_configured(hass: HomeAssistant, client: MagicMock) -> None:
"""Test we handle spa not configured error."""
@@ -88,9 +105,27 @@ async def test_form_spa_not_configured(hass: HomeAssistant, client: MagicMock) -
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "cannot_connect"}
client.async_configuration_loaded.return_value = True
with (
patch(
"homeassistant.components.balboa.config_flow.SpaClient.__aenter__",
return_value=client,
),
patch(
"homeassistant.components.balboa.async_setup_entry",
return_value=True,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_unknown_error(hass: HomeAssistant, client: MagicMock) -> None:
"""Test we handle unknown error."""
@@ -109,9 +144,26 @@ async def test_unknown_error(hass: HomeAssistant, client: MagicMock) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "unknown"}
with (
patch(
"homeassistant.components.balboa.config_flow.SpaClient.__aenter__",
return_value=client,
),
patch(
"homeassistant.components.balboa.async_setup_entry",
return_value=True,
),
):
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_already_configured(hass: HomeAssistant, client: MagicMock) -> None:
"""Test when provided credentials are already configured."""
@@ -37,11 +37,17 @@ async def test_config_flow_timeout_error(
data=TEST_DATA_USER,
)
assert result_user["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result_user["errors"] == {"base": "timeout_error"}
assert mock_mozart_client.get_beolink_self.call_count == 1
mock_mozart_client.get_beolink_self.side_effect = None
result_user = await hass.config_entries.flow.async_configure(
flow_id=result_user["flow_id"],
user_input=TEST_DATA_USER,
)
assert result_user["type"] is FlowResultType.CREATE_ENTRY
async def test_config_flow_client_connector_error(
hass: HomeAssistant, mock_mozart_client: AsyncMock
@@ -57,12 +63,19 @@ async def test_config_flow_client_connector_error(
data=TEST_DATA_USER,
)
assert result_user["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result_user["errors"] == {"base": "client_connector_error"}
assert mock_mozart_client.get_beolink_self.call_count == 1
mock_mozart_client.get_beolink_self.side_effect = None
result_user = await hass.config_entries.flow.async_configure(
flow_id=result_user["flow_id"],
user_input=TEST_DATA_USER,
)
assert result_user["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_mozart_client")
async def test_config_flow_invalid_ip(hass: HomeAssistant) -> None:
"""Test we handle invalid_ip."""
@@ -72,9 +85,14 @@ async def test_config_flow_invalid_ip(hass: HomeAssistant) -> None:
data=TEST_DATA_USER_INVALID,
)
assert result_user["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result_user["errors"] == {"base": "invalid_ip"}
result_user = await hass.config_entries.flow.async_configure(
flow_id=result_user["flow_id"],
user_input=TEST_DATA_USER,
)
assert result_user["type"] is FlowResultType.CREATE_ENTRY
async def test_config_flow_api_exception(
hass: HomeAssistant, mock_mozart_client: AsyncMock
@@ -88,11 +106,17 @@ async def test_config_flow_api_exception(
data=TEST_DATA_USER,
)
assert result_user["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result_user["errors"] == {"base": "api_exception"}
assert mock_mozart_client.get_beolink_self.call_count == 1
mock_mozart_client.get_beolink_self.side_effect = None
result_user = await hass.config_entries.flow.async_configure(
flow_id=result_user["flow_id"],
user_input=TEST_DATA_USER,
)
assert result_user["type"] is FlowResultType.CREATE_ENTRY
async def test_config_flow(hass: HomeAssistant, mock_mozart_client: AsyncMock) -> None:
"""Test config flow."""
+19 -3
View File
@@ -470,7 +470,6 @@ async def test_multi_numeric_state_observation(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
assert result["step_id"] == current_step
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "above_below"}
# This should work
@@ -486,6 +485,7 @@ async def test_multi_numeric_state_observation(hass: HomeAssistant) -> None:
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert config_entry.version == 1
assert config_entry.options == {
@@ -868,7 +868,6 @@ async def test_reconfiguring_observations(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
assert result["step_id"] == current_step
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "equal_probabilities"}
# This should work
@@ -884,6 +883,8 @@ async def test_reconfiguring_observations(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
assert "errors" not in result
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
# Confirm the changes to the state config
assert hass.config_entries.async_get_entry(config_entry.entry_id).options == {
@@ -1159,6 +1160,7 @@ async def test_invalid_configs(hass: HomeAssistant) -> None:
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
result = await hass.config_entries.subentries.async_init(
(config_entry.entry_id, "observation"),
@@ -1191,6 +1193,7 @@ async def test_invalid_configs(hass: HomeAssistant) -> None:
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
# Try with a ObservationTypes.TEMPLATE observation
result = await hass.config_entries.subentries.async_init(
(config_entry.entry_id, "observation"),
@@ -1213,5 +1216,18 @@ async def test_invalid_configs(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
assert result["step_id"] == current_step
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "equal_probabilities"}
result = await hass.config_entries.subentries.async_configure(
result["flow_id"],
{
CONF_VALUE_TEMPLATE: (
"{{ is_state('device_tracker.paulus', 'not_home') }}"
),
CONF_P_GIVEN_T: 50,
CONF_P_GIVEN_F: 10,
CONF_NAME: "Paulus not home",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
+21 -3
View File
@@ -370,7 +370,7 @@ async def test_aborted_flow_removes_listener(
hass: HomeAssistant,
mock_shared_listener: FakeListener,
) -> None:
"""Test listener is stopped after an aborted config flow."""
"""Test listener is stopped after a failed attempt and the flow recovers."""
with patch_config_flow_connectivity(
USER_HOST,
deliver_mac=False,
@@ -388,11 +388,20 @@ async def test_aborted_flow_removes_listener(
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "timeout_connect"}
mock_shared_listener.stop.assert_awaited_once()
assert not async_get_listener_registry(hass).has_listener(DEFAULT_PORT)
with patch_config_flow_connectivity(USER_HOST):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_HOST: USER_HOST,
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_invalid_mac_from_other_host_is_ignored(
hass: HomeAssistant, mock_shared_listener: FakeListener
@@ -484,9 +493,18 @@ async def test_invalid_mac_does_not_fail_other_flow(
assert first_result["type"] is FlowResultType.CREATE_ENTRY
assert first_result["result"].unique_id == TEST_DEVICE_MAC
assert second_result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert second_result["errors"] == {"base": "invalid_mac"}
with patch_config_flow_connectivity(ZEROCONF_HOST, mac_address=SECOND_DEVICE_MAC):
second_result = await hass.config_entries.flow.async_configure(
second_result["flow_id"],
{
CONF_HOST: ZEROCONF_HOST,
},
)
assert second_result["type"] is FlowResultType.CREATE_ENTRY
async def test_concurrent_flow_same_host_aborts(hass: HomeAssistant) -> None:
"""Test concurrent flows for the same host abort with already_in_progress."""
+78 -11
View File
@@ -1,7 +1,15 @@
"""Test Home Assistant config flow for BleBox devices."""
from contextlib import AbstractContextManager
from ipaddress import ip_address
from unittest.mock import DEFAULT, AsyncMock, PropertyMock, create_autospec, patch
from unittest.mock import (
DEFAULT,
AsyncMock,
MagicMock,
PropertyMock,
create_autospec,
patch,
)
import blebox_uniapi
import blebox_uniapi.box
@@ -120,8 +128,11 @@ def product_class_mock_fixture():
return patch(path, DEFAULT, blebox_uniapi.box.Box, True, True)
@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_with_connection_failure(
hass: HomeAssistant, product_class_mock
hass: HomeAssistant,
product_class_mock: AbstractContextManager[MagicMock],
valid_feature_mock: MagicMock,
) -> None:
"""Test that config flow works."""
with product_class_mock as products_class:
@@ -138,11 +149,24 @@ async def test_flow_with_connection_failure(
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "cannot_connect"}
products_class.async_from_host = AsyncMock(
return_value=valid_feature_mock.product
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_flow_with_api_failure(hass: HomeAssistant, product_class_mock) -> None:
@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_with_api_failure(
hass: HomeAssistant,
product_class_mock: AbstractContextManager[MagicMock],
valid_feature_mock: MagicMock,
) -> None:
"""Test that config flow works."""
with product_class_mock as products_class:
products_class.async_from_host = AsyncMock(
@@ -158,12 +182,23 @@ async def test_flow_with_api_failure(hass: HomeAssistant, product_class_mock) ->
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "cannot_connect"}
products_class.async_from_host = AsyncMock(
return_value=valid_feature_mock.product
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_with_unknown_failure(
hass: HomeAssistant, product_class_mock
hass: HomeAssistant,
product_class_mock: AbstractContextManager[MagicMock],
valid_feature_mock: MagicMock,
) -> None:
"""Test that config flow works."""
with product_class_mock as products_class:
@@ -177,12 +212,23 @@ async def test_flow_with_unknown_failure(
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "unknown"}
products_class.async_from_host = AsyncMock(
return_value=valid_feature_mock.product
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_with_unsupported_version(
hass: HomeAssistant, product_class_mock
hass: HomeAssistant,
product_class_mock: AbstractContextManager[MagicMock],
valid_feature_mock: MagicMock,
) -> None:
"""Test that config flow works."""
with product_class_mock as products_class:
@@ -199,11 +245,24 @@ async def test_flow_with_unsupported_version(
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "unsupported_version"}
products_class.async_from_host = AsyncMock(
return_value=valid_feature_mock.product
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_flow_with_auth_failure(hass: HomeAssistant, product_class_mock) -> None:
@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_with_auth_failure(
hass: HomeAssistant,
product_class_mock: AbstractContextManager[MagicMock],
valid_feature_mock: MagicMock,
) -> None:
"""Test that config flow works."""
with product_class_mock as products_class:
products_class.async_from_host = AsyncMock(
@@ -219,9 +278,17 @@ async def test_flow_with_auth_failure(hass: HomeAssistant, product_class_mock) -
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "invalid_auth"}
products_class.async_from_host = AsyncMock(
return_value=valid_feature_mock.product
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={config_flow.CONF_HOST: "172.2.3.4", config_flow.CONF_PORT: 80},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_async_setup(hass: HomeAssistant) -> None:
"""Test async_setup (for coverage)."""
+89 -7
View File
@@ -92,9 +92,22 @@ async def test_form_2fa_connect_error(hass: HomeAssistant) -> None:
)
assert result3["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "cannot_connect"}
with (
patch(
"homeassistant.components.blink.config_flow.Blink.send_2fa_code",
return_value=True,
),
patch("homeassistant.components.blink.async_setup_entry", return_value=True),
):
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"], {"pin": "1234"}
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
async def test_form_2fa_invalid_key(hass: HomeAssistant) -> None:
"""Test we report an error if key is invalid."""
@@ -133,9 +146,22 @@ async def test_form_2fa_invalid_key(hass: HomeAssistant) -> None:
)
assert result3["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "invalid_access_token"}
with (
patch(
"homeassistant.components.blink.config_flow.Blink.send_2fa_code",
return_value=True,
),
patch("homeassistant.components.blink.async_setup_entry", return_value=True),
):
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"], {"pin": "1234"}
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
async def test_form_2fa_unknown_error(hass: HomeAssistant) -> None:
"""Test we report an unknown error during 2fa setup."""
@@ -172,9 +198,22 @@ async def test_form_2fa_unknown_error(hass: HomeAssistant) -> None:
)
assert result3["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "unknown"}
with (
patch(
"homeassistant.components.blink.config_flow.Blink.send_2fa_code",
return_value=True,
),
patch("homeassistant.components.blink.async_setup_entry", return_value=True),
):
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"], {"pin": "1234"}
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
async def test_form_2fa_wrong_pin(hass: HomeAssistant) -> None:
"""Test we report invalid auth when send_2fa_code returns False."""
@@ -211,9 +250,22 @@ async def test_form_2fa_wrong_pin(hass: HomeAssistant) -> None:
)
assert result3["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "invalid_auth"}
with (
patch(
"homeassistant.components.blink.config_flow.Blink.send_2fa_code",
return_value=True,
),
patch("homeassistant.components.blink.async_setup_entry", return_value=True),
):
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"], {"pin": "1234"}
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
"""Test we handle invalid auth."""
@@ -230,9 +282,19 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "invalid_auth"}
with (
patch("homeassistant.components.blink.config_flow.Blink.start"),
patch("homeassistant.components.blink.async_setup_entry", return_value=True),
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"username": "blink@example.com", "password": "example"}
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_form_start_returns_false(hass: HomeAssistant) -> None:
"""Test we handle auth failure when blink.start() returns False without raising.
@@ -253,9 +315,19 @@ async def test_form_start_returns_false(hass: HomeAssistant) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "invalid_auth"}
with (
patch("homeassistant.components.blink.config_flow.Blink.start"),
patch("homeassistant.components.blink.async_setup_entry", return_value=True),
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"username": "blink@example.com", "password": "example"}
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_form_unknown_error(hass: HomeAssistant) -> None:
"""Test we handle unknown error at startup."""
@@ -272,9 +344,19 @@ async def test_form_unknown_error(hass: HomeAssistant) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "unknown"}
with (
patch("homeassistant.components.blink.config_flow.Blink.start"),
patch("homeassistant.components.blink.async_setup_entry", return_value=True),
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"username": "blink@example.com", "password": "example"}
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_reauth_shows_user_step(hass: HomeAssistant) -> None:
"""Test reauth shows the user form."""
+55 -6
View File
@@ -173,9 +173,22 @@ async def test_user_form_invalid_auth(hass: HomeAssistant) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "invalid_auth"}
with (
patch_bond_version(return_value={"bondid": "ZXXX12345"}),
patch_bond_bridge(),
patch_bond_device_ids(),
_patch_async_setup_entry(),
):
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_user_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
@@ -194,9 +207,22 @@ async def test_user_form_cannot_connect(hass: HomeAssistant) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "cannot_connect"}
with (
patch_bond_version(return_value={"bondid": "ZXXX12345"}),
patch_bond_bridge(),
patch_bond_device_ids(),
_patch_async_setup_entry(),
):
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_user_form_old_firmware(hass: HomeAssistant) -> None:
"""Test we handle unsupported old firmware."""
@@ -215,13 +241,25 @@ async def test_user_form_old_firmware(hass: HomeAssistant) -> None:
)
assert result2["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "old_firmware"}
with (
patch_bond_version(return_value={"bondid": "ZXXX12345"}),
patch_bond_bridge(),
patch_bond_device_ids(),
_patch_async_setup_entry(),
):
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
async def test_user_form_unexpected_client_error(hass: HomeAssistant) -> None:
"""Test we handle unexpected client error gracefully."""
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
await _help_test_form_unexpected_error(
hass,
source=config_entries.SOURCE_USER,
@@ -232,7 +270,6 @@ async def test_user_form_unexpected_client_error(hass: HomeAssistant) -> None:
async def test_user_form_unexpected_error(hass: HomeAssistant) -> None:
"""Test we handle unexpected error gracefully."""
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
await _help_test_form_unexpected_error(
hass,
source=config_entries.SOURCE_USER,
@@ -779,7 +816,6 @@ async def test_zeroconf_already_configured_keeps_valid_host(
async def test_zeroconf_form_unexpected_error(hass: HomeAssistant) -> None:
"""Test we handle unexpected error gracefully."""
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
await _help_test_form_unexpected_error(
hass,
source=config_entries.SOURCE_ZEROCONF,
@@ -822,6 +858,19 @@ async def _help_test_form_unexpected_error(
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "unknown"}
with (
patch_bond_version(return_value={"bondid": "ZXXX12345"}),
patch_bond_bridge(),
patch_bond_device_ids(),
_patch_async_setup_entry(),
):
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
def _patch_async_setup_entry():
return patch(
+129 -7
View File
@@ -1,5 +1,7 @@
"""Test the Bosch SHC config flow."""
from collections.abc import Generator
from contextlib import contextmanager
from ipaddress import ip_address
from unittest.mock import PropertyMock, mock_open, patch
@@ -36,6 +38,51 @@ DISCOVERY_INFO = ZeroconfServiceInfo(
)
@contextmanager
def _patch_mdns_info() -> Generator[None]:
"""Patch a successful SHC information lookup."""
with (
patch(
"boschshcpy.session.SHCSession.mdns_info",
return_value=SHCInformation,
),
patch(
"boschshcpy.information.SHCInformation.name",
new_callable=PropertyMock,
return_value="shc012345",
),
patch(
"boschshcpy.information.SHCInformation.unique_id",
new_callable=PropertyMock,
return_value="test-mac",
),
):
yield
@contextmanager
def _patch_successful_pairing() -> Generator[None]:
"""Patch a successful registration, authentication and entry setup."""
with (
patch(
"boschshcpy.register_client.SHCRegisterClient.register",
return_value={
"token": "abc:123",
"cert": b"content_cert",
"key": b"content_key",
},
),
patch("os.mkdir"),
patch("homeassistant.components.bosch_shc.config_flow.open"),
patch("boschshcpy.session.SHCSession.authenticate"),
patch(
"homeassistant.components.bosch_shc.async_setup_entry",
return_value=True,
),
):
yield
@pytest.mark.usefixtures("mock_zeroconf")
async def test_form_user(hass: HomeAssistant) -> None:
"""Test we get the form."""
@@ -130,10 +177,28 @@ async def test_form_get_info_connection_error(hass: HomeAssistant) -> None:
assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "user"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "cannot_connect"}
with _patch_mdns_info():
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": "1.1.1.1"},
)
assert result3["type"] is FlowResultType.FORM
assert result3["step_id"] == "credentials"
with _patch_successful_pairing():
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{"password": "test"},
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_zeroconf")
async def test_form_get_info_exception(hass: HomeAssistant) -> None:
"""Test we handle exceptions."""
result = await hass.config_entries.flow.async_init(
@@ -153,9 +218,26 @@ async def test_form_get_info_exception(hass: HomeAssistant) -> None:
assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "user"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2["errors"] == {"base": "unknown"}
with _patch_mdns_info():
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": "1.1.1.1"},
)
assert result3["type"] is FlowResultType.FORM
assert result3["step_id"] == "credentials"
with _patch_successful_pairing():
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{"password": "test"},
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_zeroconf")
async def test_form_pairing_error(hass: HomeAssistant) -> None:
@@ -201,9 +283,17 @@ async def test_form_pairing_error(hass: HomeAssistant) -> None:
assert result3["type"] is FlowResultType.FORM
assert result3["step_id"] == "credentials"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "pairing_failed"}
with _patch_successful_pairing():
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{"password": "test"},
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_zeroconf")
async def test_form_user_invalid_auth(hass: HomeAssistant) -> None:
@@ -261,9 +351,17 @@ async def test_form_user_invalid_auth(hass: HomeAssistant) -> None:
assert result3["type"] is FlowResultType.FORM
assert result3["step_id"] == "credentials"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "invalid_auth"}
with _patch_successful_pairing():
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{"password": "test"},
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_zeroconf")
async def test_form_validate_connection_error(hass: HomeAssistant) -> None:
@@ -321,9 +419,17 @@ async def test_form_validate_connection_error(hass: HomeAssistant) -> None:
assert result3["type"] is FlowResultType.FORM
assert result3["step_id"] == "credentials"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "cannot_connect"}
with _patch_successful_pairing():
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{"password": "test"},
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_zeroconf")
async def test_form_validate_session_error(hass: HomeAssistant) -> None:
@@ -381,9 +487,17 @@ async def test_form_validate_session_error(hass: HomeAssistant) -> None:
assert result3["type"] is FlowResultType.FORM
assert result3["step_id"] == "credentials"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "session_error"}
with _patch_successful_pairing():
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{"password": "test"},
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_zeroconf")
async def test_form_validate_exception(hass: HomeAssistant) -> None:
@@ -441,9 +555,17 @@ async def test_form_validate_exception(hass: HomeAssistant) -> None:
assert result3["type"] is FlowResultType.FORM
assert result3["step_id"] == "credentials"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result3["errors"] == {"base": "unknown"}
with _patch_successful_pairing():
result4 = await hass.config_entries.flow.async_configure(
result3["flow_id"],
{"password": "test"},
)
await hass.async_block_till_done()
assert result4["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_zeroconf")
async def test_form_already_configured(hass: HomeAssistant) -> None:
+57 -7
View File
@@ -205,9 +205,29 @@ async def test_user_invalid_host(hass: HomeAssistant) -> None:
result["flow_id"], user_input={CONF_HOST: "invalid/host"}
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {CONF_HOST: "invalid_host"}
with (
patch("pybravia.BraviaClient.connect"),
patch("pybravia.BraviaClient.pair"),
patch("pybravia.BraviaClient.set_wol_mode"),
patch(
"pybravia.BraviaClient.get_system_info",
return_value=BRAVIA_SYSTEM_INFO,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_HOST: "bravia-host"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_USE_PSK: False, CONF_USE_SSL: False}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PIN: "1234"}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.parametrize(
("side_effect", "error_message"),
@@ -217,13 +237,15 @@ async def test_user_invalid_host(hass: HomeAssistant) -> None:
(BraviaConnectionError, "cannot_connect"),
],
)
async def test_pin_form_error(hass: HomeAssistant, side_effect, error_message) -> None:
async def test_pin_form_error(
hass: HomeAssistant, side_effect: type[Exception], error_message: str
) -> None:
"""Test that PIN form errors are correct."""
with (
patch(
"pybravia.BraviaClient.connect",
side_effect=side_effect,
),
) as mock_connect,
patch("pybravia.BraviaClient.pair"),
):
result = await hass.config_entries.flow.async_init(
@@ -242,9 +264,22 @@ async def test_pin_form_error(hass: HomeAssistant, side_effect, error_message) -
result["flow_id"], user_input={CONF_PIN: "1234"}
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": error_message}
mock_connect.side_effect = None
with (
patch("pybravia.BraviaClient.set_wol_mode"),
patch(
"pybravia.BraviaClient.get_system_info",
return_value=BRAVIA_SYSTEM_INFO,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PIN: "1234"}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.parametrize(
("side_effect", "error_message"),
@@ -254,12 +289,14 @@ async def test_pin_form_error(hass: HomeAssistant, side_effect, error_message) -
(BraviaConnectionError, "cannot_connect"),
],
)
async def test_psk_form_error(hass: HomeAssistant, side_effect, error_message) -> None:
async def test_psk_form_error(
hass: HomeAssistant, side_effect: type[Exception], error_message: str
) -> None:
"""Test that PSK form errors are correct."""
with patch(
"pybravia.BraviaClient.connect",
side_effect=side_effect,
):
) as mock_connect:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
@@ -276,9 +313,22 @@ async def test_psk_form_error(hass: HomeAssistant, side_effect, error_message) -
result["flow_id"], user_input={CONF_PIN: "mypsk"}
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": error_message}
mock_connect.side_effect = None
with (
patch("pybravia.BraviaClient.set_wol_mode"),
patch(
"pybravia.BraviaClient.get_system_info",
return_value=BRAVIA_SYSTEM_INFO,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PIN: "mypsk"}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_no_ip_control(hass: HomeAssistant) -> None:
"""Test that error are shown when IP Control is disabled on the TV."""
+1 -4
View File
@@ -56,6 +56,7 @@ async def test_form(
(IndexError(), "unknown"),
],
)
@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_user_init_data_unknown_error_and_recover(
hass: HomeAssistant, mock_bring_client: AsyncMock, raise_error, text_error
) -> None:
@@ -71,14 +72,10 @@ async def test_flow_user_init_data_unknown_error_and_recover(
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"]["base"] == text_error
# Recover
mock_bring_client.login.side_effect = None
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=MOCK_DATA_STEP,
+14 -3
View File
@@ -89,12 +89,16 @@ async def test_form_duplicate_login(hass: HomeAssistant) -> None:
(Exception, "unknown"),
],
)
async def test_form_error(hass: HomeAssistant, side_effect, error_message) -> None:
async def test_form_error(
hass: HomeAssistant,
side_effect: Exception | type[Exception],
error_message: str,
) -> None:
"""Test we handle cannot connect."""
with patch(
"homeassistant.components.brunt.config_flow.BruntClientAsync.async_login",
side_effect=side_effect,
):
) as mock_login:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
@@ -108,9 +112,16 @@ async def test_form_error(hass: HomeAssistant, side_effect, error_message) -> No
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": error_message}
mock_login.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=CONFIG,
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.parametrize(
("side_effect", "result_type", "password", "step_id", "reason"),
@@ -123,9 +123,17 @@ async def test_form_cannot_connect_bad_file(
},
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "cannot_connect"}
mock_evolution_client_factory.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_FILENAME: "some-serial",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reconfigure(
+72 -56
View File
@@ -247,32 +247,7 @@ async def test_circuit_discovery_empty_result_falls_back_to_default(
)
async def test_connection_error(
hass: HomeAssistant,
mock_bsblan: MagicMock,
) -> None:
"""Test we show user form on BSBLan connection error."""
mock_bsblan.device.side_effect = BSBLANConnectionError
result = await _init_user_flow(hass)
_assert_form_result(result, "user")
result = await _configure_flow(
hass,
result["flow_id"],
{
CONF_HOST: "127.0.0.1",
CONF_PORT: 80,
CONF_PASSKEY: "1234",
CONF_USERNAME: "admin",
CONF_PASSWORD: "admin1234",
},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
_assert_form_result(result, "user", {"base": "cannot_connect"})
@pytest.mark.usefixtures("mock_setup_entry")
async def test_authentication_error(
hass: HomeAssistant,
mock_bsblan: MagicMock,
@@ -298,7 +273,6 @@ async def test_authentication_error(
)
assert result.get("type") is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result.get("errors") == {"base": "invalid_auth"}
assert result.get("step_id") == "user"
@@ -331,7 +305,18 @@ async def test_authentication_error(
# Password should never be pre-filled for security reasons
assert password_field.default is probatio.UNDEFINED
mock_bsblan.device.side_effect = None
result = await _configure_flow(
hass,
result["flow_id"],
{**user_input, CONF_PASSWORD: "admin1234"},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.usefixtures("mock_setup_entry")
async def test_authentication_error_vs_connection_error(
hass: HomeAssistant,
mock_bsblan: MagicMock,
@@ -371,9 +356,23 @@ async def test_authentication_error_vs_connection_error(
},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
_assert_form_result(result, "user", {"base": "invalid_auth"})
mock_bsblan.device.side_effect = None
result = await _configure_flow(
hass,
result["flow_id"],
{
CONF_HOST: "127.0.0.1",
CONF_PORT: 80,
CONF_USERNAME: "admin",
CONF_PASSWORD: "admin1234",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_user_device_exists_abort(
hass: HomeAssistant,
@@ -542,31 +541,6 @@ async def test_zeroconf_discovery_no_mac_no_auth_required(
assert len(mock_bsblan.device.mock_calls) == 1
async def test_zeroconf_discovery_connection_error(
hass: HomeAssistant,
mock_bsblan: MagicMock,
zeroconf_discovery_info: ZeroconfServiceInfo,
) -> None:
"""Test connection error during zeroconf discovery shows the correct form."""
mock_bsblan.device.side_effect = BSBLANConnectionError
result = await _init_zeroconf_flow(hass, zeroconf_discovery_info)
_assert_form_result(result, "discovery_confirm")
result = await _configure_flow(
hass,
result["flow_id"],
{
CONF_PASSKEY: "1234",
CONF_USERNAME: "admin",
CONF_PASSWORD: "admin1234",
},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
_assert_form_result(result, "discovery_confirm", {"base": "cannot_connect"})
async def test_zeroconf_discovery_updates_host_port_on_existing_entry(
hass: HomeAssistant,
mock_bsblan: MagicMock,
@@ -826,6 +800,7 @@ async def test_reauth_flow_success(
assert mock_config_entry.data[CONF_PORT] == 80
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reauth_flow_auth_error(
hass: HomeAssistant,
mock_bsblan: MagicMock,
@@ -859,7 +834,6 @@ async def test_reauth_flow_auth_error(
},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
_assert_form_result(result, "reauth_confirm", {"base": "invalid_auth"})
# Verify that user input is preserved in the form after error
@@ -877,7 +851,22 @@ async def test_reauth_flow_auth_error(
assert passkey_field.default() == "wrong_passkey"
assert username_field.default() == "wrong_admin"
mock_bsblan.device.side_effect = None
result = await _configure_flow(
hass,
result["flow_id"],
{
CONF_PASSKEY: "1234",
CONF_USERNAME: "admin",
CONF_PASSWORD: "admin1234",
},
)
_assert_abort_result(result, "reauth_successful")
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reauth_flow_connection_error(
hass: HomeAssistant,
mock_bsblan: MagicMock,
@@ -911,9 +900,22 @@ async def test_reauth_flow_connection_error(
},
)
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
_assert_form_result(result, "reauth_confirm", {"base": "cannot_connect"})
mock_bsblan.device.side_effect = None
result = await _configure_flow(
hass,
result["flow_id"],
{
CONF_PASSKEY: "1234",
CONF_USERNAME: "admin",
CONF_PASSWORD: "admin1234",
},
)
_assert_abort_result(result, "reauth_successful")
async def test_reauth_flow_preserves_existing_values(
hass: HomeAssistant,
@@ -1147,6 +1149,7 @@ async def test_reauth_flow_partial_clear_credentials(
assert entry.data[CONF_PORT] == 80
@pytest.mark.usefixtures("mock_setup_entry")
async def test_zeroconf_discovery_auth_error_during_confirm(
hass: HomeAssistant,
mock_bsblan: MagicMock,
@@ -1181,9 +1184,22 @@ async def test_zeroconf_discovery_auth_error_during_confirm(
)
# Should show the discovery_confirm form again with auth error
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
_assert_form_result(result, "discovery_confirm", {"base": "invalid_auth"})
mock_bsblan.device.side_effect = None
result = await _configure_flow(
hass,
result["flow_id"],
{
CONF_PASSKEY: "1234",
CONF_USERNAME: "admin",
CONF_PASSWORD: "admin1234",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_reconfigure_flow_success(
hass: HomeAssistant,
+14 -1
View File
@@ -82,6 +82,7 @@ async def test_form(
(AuthorizationError(reason="Other"), "cannot_connect"),
],
)
@pytest.mark.usefixtures("mock_setup_entry")
async def test_caldav_client_error(
hass: HomeAssistant,
side_effect: Exception,
@@ -106,9 +107,21 @@ async def test_caldav_client_error(
await hass.async_block_till_done()
assert result2.get("type") is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result2.get("errors") == {"base": expected_error}
dav_client.return_value.get_principal.side_effect = None
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_URL: TEST_URL,
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
},
)
await hass.async_block_till_done()
assert result3.get("type") is FlowResultType.CREATE_ENTRY
async def test_reauth_success(
hass: HomeAssistant,
+13 -3
View File
@@ -1,6 +1,6 @@
"""Test the Canary config flow."""
from unittest.mock import patch
from unittest.mock import MagicMock, patch
from requests import ConnectTimeout, HTTPError
@@ -42,7 +42,7 @@ async def test_user_form(hass: HomeAssistant, canary_config_flow) -> None:
async def test_user_form_cannot_connect(
hass: HomeAssistant, canary_config_flow
hass: HomeAssistant, canary_config_flow: MagicMock
) -> None:
"""Test we handle errors that should trigger the cannot connect error."""
canary_config_flow.side_effect = HTTPError()
@@ -67,9 +67,19 @@ async def test_user_form_cannot_connect(
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "cannot_connect"}
canary_config_flow.side_effect = None
with _patch_async_setup_entry():
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_user_form_unexpected_exception(
hass: HomeAssistant, canary_config_flow
@@ -180,9 +180,22 @@ async def test_user_step_error(
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": expected_error}
with (
patch("homeassistant.components.casper_glow.config_flow.CasperGlow.handshake"),
patch(
"homeassistant.components.casper_glow.async_setup_entry",
return_value=True,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ADDRESS: CASPER_GLOW_DISCOVERY_INFO.address},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_already_configured(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
@@ -128,9 +128,16 @@ async def test_abort_on_socket_failed(hass: HomeAssistant) -> None:
result["flow_id"], user_input={CONF_HOST: HOST}
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {CONF_HOST: "connection_reset"}
with patch(
"homeassistant.components.cert_expiry.config_flow.get_cert_expiry_timestamp"
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_HOST: HOST}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_reconfigure_successful(hass: HomeAssistant) -> None:
"""Test reconfiguration of an existing entry updates its data and unique_id."""
@@ -89,9 +89,27 @@ async def test_user_form_cannot_connect(
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "cannot_connect"}
instance.list_zones.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_ZONE,
)
with patch_async_setup_entry():
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_RECORDS,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_user_form_invalid_auth(
hass: HomeAssistant, cfupdate_flow: MagicMock
@@ -110,9 +128,27 @@ async def test_user_form_invalid_auth(
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "invalid_auth"}
instance.list_zones.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_ZONE,
)
with patch_async_setup_entry():
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_RECORDS,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_user_form_unexpected_exception(
hass: HomeAssistant, cfupdate_flow: MagicMock
@@ -131,9 +167,27 @@ async def test_user_form_unexpected_exception(
)
assert result["type"] is FlowResultType.FORM
# pylint: disable-next=home-assistant-tests-config-flow-error-recovery
assert result["errors"] == {"base": "unknown"}
instance.list_zones.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_ZONE,
)
with patch_async_setup_entry():
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_RECORDS,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_user_form_single_instance_allowed(hass: HomeAssistant) -> None:
"""Test that configuring more than one instance is rejected."""