Improve Whirlpool config flow test completeness and naming (#143118)

This commit is contained in:
Abílio Costa 2025-04-17 13:40:57 +01:00 committed by GitHub
parent bbb8a1bacc
commit f9bb7e404e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,10 +5,12 @@ from unittest.mock import MagicMock, patch
import aiohttp
import pytest
from whirlpool.auth import AccountLockedError
from whirlpool.backendselector import Brand, Region
from homeassistant import config_entries
from homeassistant.components.whirlpool.const import CONF_BRAND, DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -20,6 +22,42 @@ CONFIG_INPUT = {
}
def assert_successful_user_flow(
mock_whirlpool_setup_entry: MagicMock,
result: ConfigFlowResult,
region: str,
brand: str,
) -> None:
"""Assert that the flow was successful."""
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "test-username"
assert result["data"] == {
CONF_USERNAME: CONFIG_INPUT[CONF_USERNAME],
CONF_PASSWORD: CONFIG_INPUT[CONF_PASSWORD],
CONF_REGION: region,
CONF_BRAND: brand,
}
assert result["result"].unique_id == CONFIG_INPUT[CONF_USERNAME]
assert len(mock_whirlpool_setup_entry.mock_calls) == 1
def assert_successful_reauth_flow(
mock_entry: MockConfigEntry,
result: ConfigFlowResult,
region: str,
brand: str,
) -> None:
"""Assert that the reauth flow was successful."""
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert mock_entry.data == {
CONF_USERNAME: CONFIG_INPUT[CONF_USERNAME],
CONF_PASSWORD: "new-password",
CONF_REGION: region[0],
CONF_BRAND: brand[0],
}
@pytest.fixture(name="mock_whirlpool_setup_entry")
def fixture_mock_whirlpool_setup_entry():
"""Set up async_setup_entry fixture."""
@ -30,14 +68,14 @@ def fixture_mock_whirlpool_setup_entry():
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
async def test_form(
async def test_user_flow(
hass: HomeAssistant,
region,
brand,
region: tuple[str, Region],
brand: tuple[str, Brand],
mock_backend_selector_api: MagicMock,
mock_whirlpool_setup_entry: MagicMock,
) -> None:
"""Test we get the form."""
"""Test successful flow initialized by the user."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
@ -45,38 +83,39 @@ async def test_form(
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == config_entries.SOURCE_USER
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
result = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]}
)
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "test-username"
assert result2["data"] == {
"username": "test-username",
"password": "test-password",
"region": region[0],
"brand": brand[0],
}
assert len(mock_whirlpool_setup_entry.mock_calls) == 1
assert_successful_user_flow(mock_whirlpool_setup_entry, result, region[0], brand[0])
mock_backend_selector_api.assert_called_once_with(brand[1], region[1])
async def test_form_invalid_auth(
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
async def test_user_flow_invalid_auth(
hass: HomeAssistant,
region: tuple[str, Region],
brand: tuple[str, Brand],
mock_auth_api: MagicMock,
mock_whirlpool_setup_entry: MagicMock,
) -> None:
"""Test we handle invalid auth."""
"""Test invalid authentication in the flow initialized by the user."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
mock_auth_api.return_value.is_access_token_valid.return_value = False
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
result = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]}
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_auth"}
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "invalid_auth"}
# Test that it succeeds if the authentication is valid
mock_auth_api.return_value.is_access_token_valid.return_value = True
result = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]}
)
assert_successful_user_flow(mock_whirlpool_setup_entry, result, region[0], brand[0])
@pytest.mark.usefixtures("mock_appliances_manager_api")
@ -89,16 +128,16 @@ async def test_form_invalid_auth(
(Exception, "unknown"),
],
)
async def test_form_auth_error(
async def test_user_flow_auth_error(
hass: HomeAssistant,
exception: Exception,
expected_error: str,
region,
brand,
region: tuple[str, Region],
brand: tuple[str, Brand],
mock_auth_api: MagicMock,
mock_whirlpool_setup_entry: MagicMock,
) -> None:
"""Test we handle cannot connect error."""
"""Test authentication exceptions in the flow initialized by the user."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
@ -108,8 +147,8 @@ async def test_form_auth_error(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
CONF_REGION: region[0],
CONF_BRAND: brand[0],
},
)
assert result["type"] is FlowResultType.FORM
@ -118,27 +157,20 @@ async def test_form_auth_error(
# Test that it succeeds after the error is cleared
mock_auth_api.return_value.do_auth.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
result["flow_id"], CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "test-username"
assert result["data"] == {
"username": "test-username",
"password": "test-password",
"region": region[0],
"brand": brand[0],
}
assert len(mock_whirlpool_setup_entry.mock_calls) == 1
assert_successful_user_flow(mock_whirlpool_setup_entry, result, region[0], brand[0])
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
async def test_form_already_configured(hass: HomeAssistant, region, brand) -> None:
async def test_already_configured(
hass: HomeAssistant, region: tuple[str, Region], brand: tuple[str, Brand]
) -> None:
"""Test that configuring the integration twice with the same data fails."""
mock_entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
data=CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]},
unique_id="test-username",
)
mock_entry.add_to_hass(hass)
@ -150,22 +182,20 @@ async def test_form_already_configured(hass: HomeAssistant, region, brand) -> No
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == config_entries.SOURCE_USER
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
result = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]}
)
assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "already_configured"
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
@pytest.mark.usefixtures("mock_auth_api")
async def test_no_appliances_flow(
hass: HomeAssistant, region, brand, mock_appliances_manager_api: MagicMock
hass: HomeAssistant,
region: tuple[str, Region],
brand: tuple[str, Brand],
mock_appliances_manager_api: MagicMock,
) -> None:
"""Test we get an error with no appliances."""
result = await hass.config_entries.flow.async_init(
@ -177,23 +207,24 @@ async def test_no_appliances_flow(
mock_appliances_manager_api.return_value.aircons = []
mock_appliances_manager_api.return_value.washer_dryers = []
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
result = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]}
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "no_appliances"}
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "no_appliances"}
@pytest.mark.usefixtures(
"mock_auth_api", "mock_appliances_manager_api", "mock_whirlpool_setup_entry"
)
async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
async def test_reauth_flow(
hass: HomeAssistant, region: tuple[str, Region], brand: tuple[str, Brand]
) -> None:
"""Test a successful reauth flow."""
mock_entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
data=CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]},
unique_id="test-username",
)
mock_entry.add_to_hass(hass)
@ -204,30 +235,25 @@ async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]}
)
assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "reauth_successful"
assert mock_entry.data == {
CONF_USERNAME: "test-username",
CONF_PASSWORD: "new-password",
"region": region[0],
"brand": brand[0],
}
assert_successful_reauth_flow(mock_entry, result, region, brand)
@pytest.mark.usefixtures("mock_appliances_manager_api", "mock_whirlpool_setup_entry")
async def test_reauth_flow_invalid_auth(
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
hass: HomeAssistant,
region: tuple[str, Region],
brand: tuple[str, Brand],
mock_auth_api: MagicMock,
) -> None:
"""Test an authorization error reauth flow."""
mock_entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
data=CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]},
unique_id="test-username",
)
mock_entry.add_to_hass(hass)
@ -238,13 +264,21 @@ async def test_reauth_flow_invalid_auth(
assert result["errors"] == {}
mock_auth_api.return_value.is_access_token_valid.return_value = False
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_auth"}
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "invalid_auth"}
# Test that it succeeds if the credentials are valid
mock_auth_api.return_value.is_access_token_valid.return_value = True
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]}
)
assert_successful_reauth_flow(mock_entry, result, region, brand)
@pytest.mark.usefixtures("mock_appliances_manager_api", "mock_whirlpool_setup_entry")
@ -261,15 +295,15 @@ async def test_reauth_flow_auth_error(
hass: HomeAssistant,
exception: Exception,
expected_error: str,
region,
brand,
region: tuple[str, Region],
brand: tuple[str, Brand],
mock_auth_api: MagicMock,
) -> None:
"""Test a connection error reauth flow."""
mock_entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
data=CONFIG_INPUT | {CONF_REGION: region[0], CONF_BRAND: brand[0]},
unique_id="test-username",
)
mock_entry.add_to_hass(hass)
@ -281,9 +315,16 @@ async def test_reauth_flow_auth_error(
assert result["errors"] == {}
mock_auth_api.return_value.do_auth.side_effect = exception
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]}
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": expected_error}
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": expected_error}
# Test that it succeeds if the exception is cleared
mock_auth_api.return_value.do_auth.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]}
)
assert_successful_reauth_flow(mock_entry, result, region, brand)