mirror of
https://github.com/home-assistant/core.git
synced 2026-07-04 13:15:29 +01:00
5472a537ed
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
293 lines
8.6 KiB
Python
293 lines
8.6 KiB
Python
"""Test the huum config flow."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from huum.exceptions import Forbidden
|
|
import pytest
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.huum.const import DOMAIN
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
TEST_USERNAME = "huum@sauna.org"
|
|
TEST_PASSWORD = "ukuuku"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_huum_client")
|
|
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
|
"""Test we get the form."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == TEST_USERNAME
|
|
assert result["data"] == {
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_huum_client", "mock_setup_entry")
|
|
async def test_signup_flow_already_set_up(
|
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
|
) -> None:
|
|
"""Test that we handle already existing entities with same id."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
assert result["type"] is FlowResultType.ABORT
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
(
|
|
"raises",
|
|
"error_base",
|
|
),
|
|
[
|
|
(Exception, "unknown"),
|
|
(Forbidden, "invalid_auth"),
|
|
],
|
|
)
|
|
@pytest.mark.usefixtures("mock_setup_entry")
|
|
async def test_huum_errors(
|
|
hass: HomeAssistant, mock_huum_client: AsyncMock, raises: Exception, error_base: str
|
|
) -> None:
|
|
"""Test we handle cannot connect error."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
mock_huum_client.status.side_effect = raises
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": error_base}
|
|
|
|
mock_huum_client.status.side_effect = None
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_huum_client")
|
|
async def test_reauth_flow(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test reauthentication flow succeeds with valid credentials."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
result = await mock_config_entry.start_reauth_flow(hass)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "reauth_confirm"
|
|
assert result["errors"] == {}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_PASSWORD: "new_password"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "reauth_successful"
|
|
assert mock_config_entry.data[CONF_USERNAME] == TEST_USERNAME
|
|
assert mock_config_entry.data[CONF_PASSWORD] == "new_password"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
(
|
|
"raises",
|
|
"error_base",
|
|
),
|
|
[
|
|
(Exception, "unknown"),
|
|
(Forbidden, "invalid_auth"),
|
|
],
|
|
)
|
|
async def test_reauth_errors(
|
|
hass: HomeAssistant,
|
|
mock_huum_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
raises: Exception,
|
|
error_base: str,
|
|
) -> None:
|
|
"""Test reauthentication flow handles errors and recovers."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
result = await mock_config_entry.start_reauth_flow(hass)
|
|
|
|
mock_huum_client.status.side_effect = raises
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_PASSWORD: "wrong_password"},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": error_base}
|
|
|
|
# Recover with valid credentials
|
|
mock_huum_client.status.side_effect = None
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_PASSWORD: "new_password"},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "reauth_successful"
|
|
assert mock_config_entry.data[CONF_USERNAME] == TEST_USERNAME
|
|
assert mock_config_entry.data[CONF_PASSWORD] == "new_password"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_huum_client", "mock_setup_entry")
|
|
async def test_reconfigure_flow(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test reconfiguration flow."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
result = await mock_config_entry.start_reconfigure_flow(hass)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "reconfigure"
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_USERNAME: "new@sauna.org",
|
|
CONF_PASSWORD: "new_password",
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "reconfigure_successful"
|
|
assert mock_config_entry.title == "new@sauna.org"
|
|
assert mock_config_entry.data[CONF_USERNAME] == "new@sauna.org"
|
|
assert mock_config_entry.data[CONF_PASSWORD] == "new_password"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_huum_client", "mock_setup_entry")
|
|
async def test_reconfigure_flow_already_configured(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test reconfiguration flow aborts when username already configured."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
other_entry = MockConfigEntry(
|
|
domain=DOMAIN,
|
|
data={
|
|
CONF_USERNAME: "other@sauna.org",
|
|
CONF_PASSWORD: "other_password",
|
|
},
|
|
entry_id="OTHER_ENTRY",
|
|
)
|
|
other_entry.add_to_hass(hass)
|
|
|
|
result = await mock_config_entry.start_reconfigure_flow(hass)
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_USERNAME: "other@sauna.org",
|
|
CONF_PASSWORD: "new_password",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
(
|
|
"raises",
|
|
"error_base",
|
|
),
|
|
[
|
|
(Exception, "unknown"),
|
|
(Forbidden, "invalid_auth"),
|
|
],
|
|
)
|
|
@pytest.mark.usefixtures("mock_setup_entry")
|
|
async def test_reconfigure_errors(
|
|
hass: HomeAssistant,
|
|
mock_huum_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
raises: Exception,
|
|
error_base: str,
|
|
) -> None:
|
|
"""Test reconfiguration flow handles errors and recovers."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
result = await mock_config_entry.start_reconfigure_flow(hass)
|
|
|
|
mock_huum_client.status.side_effect = raises
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: "wrong_password",
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": error_base}
|
|
|
|
# Recover with valid credentials
|
|
mock_huum_client.status.side_effect = None
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: "new_password",
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "reconfigure_successful"
|
|
assert mock_config_entry.data[CONF_USERNAME] == TEST_USERNAME
|
|
assert mock_config_entry.data[CONF_PASSWORD] == "new_password"
|