Update config flow tests for litterrobot (#136658)

Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
Nathan Spencer 2025-01-28 03:21:46 -07:00 committed by GitHub
parent 5d55dcf392
commit b1a4ba7b7c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 77 deletions

View File

@ -23,12 +23,7 @@ rules:
comment: | comment: |
hub.py should be renamed to coordinator.py and updated accordingly hub.py should be renamed to coordinator.py and updated accordingly
Also should not need to return bool (never used) Also should not need to return bool (never used)
config-flow-test-coverage: config-flow-test-coverage: done
status: todo
comment: |
Fix stale title and docstring
Make sure every test ends in either ABORT or CREATE_ENTRY
so we also test that the flow is able to recover
config-flow: done config-flow: done
dependency-transparency: done dependency-transparency: done
docs-actions: docs-actions:

View File

@ -4,6 +4,7 @@ from unittest.mock import patch
from pylitterbot import Account from pylitterbot import Account
from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException
import pytest
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD from homeassistant.const import CONF_PASSWORD
@ -15,9 +16,8 @@ from .common import CONF_USERNAME, CONFIG, DOMAIN
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_form(hass: HomeAssistant, mock_account) -> None: async def test_full_flow(hass: HomeAssistant, mock_account) -> None:
"""Test we get the form.""" """Test full flow."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
@ -34,19 +34,18 @@ async def test_form(hass: HomeAssistant, mock_account) -> None:
return_value=True, return_value=True,
) as mock_setup_entry, ) as mock_setup_entry,
): ):
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG[DOMAIN] result["flow_id"], CONFIG[DOMAIN]
) )
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == CONFIG[DOMAIN][CONF_USERNAME] assert result["title"] == CONFIG[DOMAIN][CONF_USERNAME]
assert result2["data"] == CONFIG[DOMAIN] assert result["data"] == CONFIG[DOMAIN]
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
async def test_already_configured(hass: HomeAssistant) -> None: async def test_already_configured(hass: HomeAssistant) -> None:
"""Test we handle already configured.""" """Test already configured case."""
MockConfigEntry( MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
data=CONFIG[DOMAIN], data=CONFIG[DOMAIN],
@ -62,71 +61,32 @@ async def test_already_configured(hass: HomeAssistant) -> None:
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
async def test_form_invalid_auth(hass: HomeAssistant) -> None: @pytest.mark.parametrize(
"""Test we handle invalid auth.""" ("side_effect", "connect_errors"),
[
(Exception, {"base": "unknown"}),
(LitterRobotLoginException, {"base": "invalid_auth"}),
(LitterRobotException, {"base": "cannot_connect"}),
],
)
async def test_create_entry(
hass: HomeAssistant, mock_account, side_effect, connect_errors
) -> None:
"""Test creating an entry."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
with patch( with patch(
"homeassistant.components.litterrobot.config_flow.Account.connect", "homeassistant.components.litterrobot.config_flow.Account.connect",
side_effect=LitterRobotLoginException, side_effect=side_effect,
): ):
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG[DOMAIN] result["flow_id"], CONFIG[DOMAIN]
) )
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass: HomeAssistant) -> 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.litterrobot.config_flow.Account.connect",
side_effect=LitterRobotException,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG[DOMAIN]
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_unknown_error(hass: HomeAssistant) -> None:
"""Test we handle unknown error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.litterrobot.config_flow.Account.connect",
side_effect=Exception,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG[DOMAIN]
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "unknown"}
async def test_step_reauth(hass: HomeAssistant, mock_account: Account) -> None:
"""Test the reauth flow."""
entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG[DOMAIN],
)
entry.add_to_hass(hass)
result = await entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm" assert result["errors"] == connect_errors
with ( with (
patch( patch(
@ -136,19 +96,19 @@ async def test_step_reauth(hass: HomeAssistant, mock_account: Account) -> None:
patch( patch(
"homeassistant.components.litterrobot.async_setup_entry", "homeassistant.components.litterrobot.async_setup_entry",
return_value=True, return_value=True,
) as mock_setup_entry, ),
): ):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"], CONFIG[DOMAIN]
user_input={CONF_PASSWORD: CONFIG[DOMAIN][CONF_PASSWORD]},
) )
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful" assert result["type"] is FlowResultType.CREATE_ENTRY
assert len(mock_setup_entry.mock_calls) == 1 assert result["title"] == CONFIG[DOMAIN][CONF_USERNAME]
assert result["data"] == CONFIG[DOMAIN]
async def test_step_reauth_failed(hass: HomeAssistant, mock_account: Account) -> None: async def test_reauth(hass: HomeAssistant, mock_account: Account) -> None:
"""Test the reauth flow fails and recovers.""" """Test reauth flow (with fail and recover)."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
data=CONFIG[DOMAIN], data=CONFIG[DOMAIN],