Reuse fixtures in config flow tests for Whirlpool (#136489)

* Use fixtures in config flow tests for Whirlpool

* Keep old tests; new one will go to separate PR
This commit is contained in:
Abílio Costa 2025-01-25 17:30:52 +00:00 committed by GitHub
parent 821abc8c53
commit 42f7bd0a8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 128 additions and 168 deletions

View File

@ -39,7 +39,12 @@ def fixture_brand(request: pytest.FixtureRequest) -> tuple[str, Brand]:
@pytest.fixture(name="mock_auth_api")
def fixture_mock_auth_api():
"""Set up Auth fixture."""
with mock.patch("homeassistant.components.whirlpool.Auth") as mock_auth:
with (
mock.patch("homeassistant.components.whirlpool.Auth") as mock_auth,
mock.patch(
"homeassistant.components.whirlpool.config_flow.Auth", new=mock_auth
),
):
mock_auth.return_value.do_auth = AsyncMock()
mock_auth.return_value.is_access_token_valid.return_value = True
yield mock_auth
@ -48,9 +53,15 @@ def fixture_mock_auth_api():
@pytest.fixture(name="mock_appliances_manager_api")
def fixture_mock_appliances_manager_api():
"""Set up AppliancesManager fixture."""
with mock.patch(
"homeassistant.components.whirlpool.AppliancesManager"
) as mock_appliances_manager:
with (
mock.patch(
"homeassistant.components.whirlpool.AppliancesManager"
) as mock_appliances_manager,
mock.patch(
"homeassistant.components.whirlpool.config_flow.AppliancesManager",
new=mock_appliances_manager,
),
):
mock_appliances_manager.return_value.fetch_appliances = AsyncMock()
mock_appliances_manager.return_value.aircons = [
{"SAID": MOCK_SAID1, "NAME": "TestZone"},
@ -81,9 +92,15 @@ def fixture_mock_appliances_manager_laundry_api():
@pytest.fixture(name="mock_backend_selector_api")
def fixture_mock_backend_selector_api():
"""Set up BackendSelector fixture."""
with mock.patch(
"homeassistant.components.whirlpool.BackendSelector"
) as mock_backend_selector:
with (
mock.patch(
"homeassistant.components.whirlpool.BackendSelector"
) as mock_backend_selector,
mock.patch(
"homeassistant.components.whirlpool.config_flow.BackendSelector",
new=mock_backend_selector,
),
):
yield mock_backend_selector

View File

@ -1,9 +1,10 @@
"""Test the Whirlpool Sixth Sense config flow."""
from unittest.mock import patch
from unittest.mock import MagicMock, patch
import aiohttp
from aiohttp.client_exceptions import ClientConnectionError
import pytest
from homeassistant import config_entries
from homeassistant.components.whirlpool.const import CONF_BRAND, DOMAIN
@ -19,7 +20,10 @@ CONFIG_INPUT = {
}
async def test_form(hass: HomeAssistant, region, brand) -> None:
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
async def test_form(
hass: HomeAssistant, region, brand, mock_backend_selector_api: MagicMock
) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -28,28 +32,9 @@ async def test_form(hass: HomeAssistant, region, brand) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == config_entries.SOURCE_USER
with (
patch("homeassistant.components.whirlpool.config_flow.Auth.do_auth"),
patch(
"homeassistant.components.whirlpool.config_flow.Auth.is_access_token_valid",
return_value=True,
),
patch(
"homeassistant.components.whirlpool.config_flow.BackendSelector"
) as mock_backend_selector,
patch(
"homeassistant.components.whirlpool.async_setup_entry",
return_value=True,
) as mock_setup_entry,
patch(
"homeassistant.components.whirlpool.config_flow.AppliancesManager.aircons",
return_value=["test"],
),
patch(
"homeassistant.components.whirlpool.config_flow.AppliancesManager.fetch_appliances",
return_value=True,
),
):
with patch(
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
@ -65,92 +50,99 @@ async def test_form(hass: HomeAssistant, region, brand) -> None:
"brand": brand[0],
}
assert len(mock_setup_entry.mock_calls) == 1
mock_backend_selector.assert_called_once_with(brand[1], region[1])
mock_backend_selector_api.assert_called_once_with(brand[1], region[1])
async def test_form_invalid_auth(hass: HomeAssistant, region, brand) -> None:
async def test_form_invalid_auth(
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
) -> None:
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with (
patch("homeassistant.components.whirlpool.config_flow.Auth.do_auth"),
patch(
"homeassistant.components.whirlpool.config_flow.Auth.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]},
)
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]},
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass: HomeAssistant, region, brand) -> None:
async def test_form_cannot_connect(
hass: HomeAssistant,
region,
brand,
mock_auth_api: MagicMock,
) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.whirlpool.config_flow.Auth.do_auth",
side_effect=aiohttp.ClientConnectionError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
)
mock_auth_api.return_value.do_auth.side_effect = aiohttp.ClientConnectionError
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_auth_timeout(hass: HomeAssistant, region, brand) -> None:
async def test_form_auth_timeout(
hass: HomeAssistant,
region,
brand,
mock_auth_api: MagicMock,
) -> None:
"""Test we handle auth timeout error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.whirlpool.config_flow.Auth.do_auth",
side_effect=TimeoutError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
)
mock_auth_api.return_value.do_auth.side_effect = TimeoutError
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_generic_auth_exception(hass: HomeAssistant, region, brand) -> None:
async def test_form_generic_auth_exception(
hass: HomeAssistant,
region,
brand,
mock_auth_api: MagicMock,
) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.whirlpool.config_flow.Auth.do_auth",
side_effect=Exception,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
)
mock_auth_api.return_value.do_auth.side_effect = Exception
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "unknown"}
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
async def test_form_already_configured(hass: HomeAssistant, region, brand) -> None:
"""Test we handle cannot connect error."""
mock_entry = MockConfigEntry(
@ -167,36 +159,24 @@ 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
with (
patch("homeassistant.components.whirlpool.config_flow.Auth.do_auth"),
patch(
"homeassistant.components.whirlpool.config_flow.Auth.is_access_token_valid",
return_value=True,
),
patch(
"homeassistant.components.whirlpool.config_flow.AppliancesManager.aircons",
return_value=["test"],
),
patch(
"homeassistant.components.whirlpool.config_flow.AppliancesManager.fetch_appliances",
return_value=True,
),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
)
await hass.async_block_till_done()
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT
| {
"region": region[0],
"brand": brand[0],
},
)
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "already_configured"
async def test_no_appliances_flow(hass: HomeAssistant, region, brand) -> None:
@pytest.mark.usefixtures("mock_auth_api")
async def test_no_appliances_flow(
hass: HomeAssistant, region, brand, mock_appliances_manager_api: MagicMock
) -> None:
"""Test we get an error with no appliances."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -205,27 +185,19 @@ async def test_no_appliances_flow(hass: HomeAssistant, region, brand) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == config_entries.SOURCE_USER
with (
patch("homeassistant.components.whirlpool.config_flow.Auth.do_auth"),
patch(
"homeassistant.components.whirlpool.config_flow.Auth.is_access_token_valid",
return_value=True,
),
patch(
"homeassistant.components.whirlpool.config_flow.AppliancesManager.fetch_appliances",
return_value=True,
),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
)
await hass.async_block_till_done()
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]},
)
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "no_appliances"}
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
"""Test a successful reauth flow."""
mock_entry = MockConfigEntry(
@ -241,24 +213,8 @@ async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
with (
patch(
"homeassistant.components.whirlpool.async_setup_entry",
return_value=True,
),
patch("homeassistant.components.whirlpool.config_flow.Auth.do_auth"),
patch(
"homeassistant.components.whirlpool.config_flow.Auth.is_access_token_valid",
return_value=True,
),
patch(
"homeassistant.components.whirlpool.config_flow.AppliancesManager.aircons",
return_value=["test"],
),
patch(
"homeassistant.components.whirlpool.config_flow.AppliancesManager.fetch_appliances",
return_value=True,
),
with patch(
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -276,7 +232,10 @@ async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
}
async def test_reauth_flow_auth_error(hass: HomeAssistant, region, brand) -> None:
@pytest.mark.usefixtures("mock_appliances_manager_api")
async def test_reauth_flow_auth_error(
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
) -> None:
"""Test an authorization error reauth flow."""
mock_entry = MockConfigEntry(
@ -290,16 +249,10 @@ async def test_reauth_flow_auth_error(hass: HomeAssistant, region, brand) -> Non
assert result["step_id"] == "reauth_confirm"
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
with (
patch(
"homeassistant.components.whirlpool.async_setup_entry",
return_value=True,
),
patch("homeassistant.components.whirlpool.config_flow.Auth.do_auth"),
patch(
"homeassistant.components.whirlpool.config_flow.Auth.is_access_token_valid",
return_value=False,
),
mock_auth_api.return_value.is_access_token_valid.return_value = False
with patch(
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -311,8 +264,9 @@ async def test_reauth_flow_auth_error(hass: HomeAssistant, region, brand) -> Non
assert result2["errors"] == {"base": "invalid_auth"}
@pytest.mark.usefixtures("mock_appliances_manager_api")
async def test_reauth_flow_connnection_error(
hass: HomeAssistant, region, brand
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
) -> None:
"""Test a connection error reauth flow."""
@ -329,25 +283,14 @@ async def test_reauth_flow_connnection_error(
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
with (
patch(
"homeassistant.components.whirlpool.async_setup_entry",
return_value=True,
),
patch(
"homeassistant.components.whirlpool.config_flow.Auth.do_auth",
side_effect=ClientConnectionError,
),
patch(
"homeassistant.components.whirlpool.config_flow.Auth.is_access_token_valid",
return_value=False,
),
mock_auth_api.return_value.do_auth.side_effect = ClientConnectionError
with patch(
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
)
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}