1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 09:38:58 +01:00

Add HEOS Reauth Flow (#134465)

This commit is contained in:
Andrew Sayre
2025-01-03 12:11:10 -06:00
committed by GitHub
parent 94ad6ae814
commit dfcb977a1d
6 changed files with 290 additions and 63 deletions
+138
View File
@@ -10,6 +10,8 @@ from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
async def test_flow_aborts_already_setup(hass: HomeAssistant, config_entry) -> None:
"""Test flow aborts when entry already setup."""
@@ -329,3 +331,139 @@ async def test_options_flow_missing_one_param_recovers(
assert controller.sign_out.call_count == 0
assert result["data"] == user_input
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.parametrize(
("error", "expected_error_key"),
[
(
CommandFailedError("sign_in", "Invalid credentials", 6),
"invalid_auth",
),
(
CommandFailedError("sign_in", "User not logged in", 8),
"invalid_auth",
),
(CommandFailedError("sign_in", "user not found", 10), "invalid_auth"),
(CommandFailedError("sign_in", "System error", 12), "unknown"),
(HeosError(), "unknown"),
],
)
async def test_reauth_signs_in_aborts(
hass: HomeAssistant,
config_entry: MockConfigEntry,
controller,
error: HeosError,
expected_error_key: str,
) -> None:
"""Test reauth flow signs-in with entered credentials and aborts."""
config_entry.add_to_hass(hass)
result = await config_entry.start_reauth_flow(hass)
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Invalid credentials, system error, or unexpected error.
user_input = {CONF_USERNAME: "user", CONF_PASSWORD: "pass"}
controller.sign_in.side_effect = error
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": expected_error_key}
assert result["type"] is FlowResultType.FORM
# Valid credentials signs-in, updates options, and aborts
controller.sign_in.reset_mock()
controller.sign_in.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert config_entry.options[CONF_USERNAME] == user_input[CONF_USERNAME]
assert config_entry.options[CONF_PASSWORD] == user_input[CONF_PASSWORD]
assert result["reason"] == "reauth_successful"
assert result["type"] is FlowResultType.ABORT
async def test_reauth_signs_out(hass: HomeAssistant, config_entry, controller) -> None:
"""Test reauth flow signs-out when credentials cleared and aborts."""
config_entry.add_to_hass(hass)
result = await config_entry.start_reauth_flow(hass)
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Fail to sign-out, show error
user_input = {}
controller.sign_out.side_effect = HeosError()
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 0
assert controller.sign_out.call_count == 1
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": "unknown"}
assert result["type"] is FlowResultType.FORM
# Cleared credentials signs-out, updates options, and aborts
controller.sign_out.reset_mock()
controller.sign_out.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 0
assert controller.sign_out.call_count == 1
assert CONF_USERNAME not in config_entry.options
assert CONF_PASSWORD not in config_entry.options
assert result["reason"] == "reauth_successful"
assert result["type"] is FlowResultType.ABORT
@pytest.mark.parametrize(
("user_input", "expected_errors"),
[
({CONF_USERNAME: "user"}, {CONF_PASSWORD: "password_missing"}),
({CONF_PASSWORD: "pass"}, {CONF_USERNAME: "username_missing"}),
],
)
async def test_reauth_flow_missing_one_param_recovers(
hass: HomeAssistant,
config_entry,
controller,
user_input: dict[str, str],
expected_errors: dict[str, str],
) -> None:
"""Test reauth flow signs-in after recovering from only username or password being entered."""
config_entry.add_to_hass(hass)
# Start the options flow. Entry has not current options.
result = await config_entry.start_reauth_flow(hass)
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Enter only username or password
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == expected_errors
assert result["type"] is FlowResultType.FORM
# Enter valid credentials
user_input = {CONF_USERNAME: "user", CONF_PASSWORD: "pass"}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert config_entry.options[CONF_USERNAME] == user_input[CONF_USERNAME]
assert config_entry.options[CONF_PASSWORD] == user_input[CONF_PASSWORD]
assert result["reason"] == "reauth_successful"
assert result["type"] is FlowResultType.ABORT
+34 -1
View File
@@ -15,12 +15,14 @@ from homeassistant.components.heos import (
async_unload_entry,
)
from homeassistant.components.heos.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
async def test_async_setup_returns_true(
hass: HomeAssistant, config_entry, config
@@ -91,6 +93,37 @@ async def test_async_setup_entry_with_options_loads_platforms(
controller.disconnect.assert_not_called()
async def test_async_setup_entry_auth_failure_starts_reauth(
hass: HomeAssistant,
config_entry_options: MockConfigEntry,
controller: Mock,
) -> None:
"""Test load with auth failure starts reauth, loads platforms."""
config_entry_options.add_to_hass(hass)
# Simulates what happens when the controller can't sign-in during connection
async def connect_send_auth_failure() -> None:
controller.is_signed_in = False
controller.signed_in_username = None
controller.dispatcher.send(
const.SIGNAL_HEOS_EVENT, const.EVENT_USER_CREDENTIALS_INVALID
)
controller.connect.side_effect = connect_send_auth_failure
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
# Assert entry loaded and reauth flow started
assert controller.connect.call_count == 1
assert controller.get_favorites.call_count == 0
controller.disconnect.assert_not_called()
assert config_entry_options.state is ConfigEntryState.LOADED
assert any(
config_entry_options.async_get_active_flows(hass, sources=[SOURCE_REAUTH])
)
async def test_async_setup_entry_not_signed_in_loads_platforms(
hass: HomeAssistant,
config_entry,