2024-03-15 17:13:35 +00:00
|
|
|
"""Test the fyta config flow."""
|
2024-03-22 23:27:57 +00:00
|
|
|
|
2024-03-15 17:13:35 +00:00
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
|
|
|
from fyta_cli.fyta_exceptions import (
|
|
|
|
FytaAuthentificationError,
|
|
|
|
FytaConnectionError,
|
|
|
|
FytaPasswordError,
|
|
|
|
)
|
|
|
|
import pytest
|
|
|
|
|
2024-04-02 21:01:37 +00:00
|
|
|
from homeassistant import config_entries
|
2024-12-03 20:23:52 +00:00
|
|
|
from homeassistant.components.dhcp import DhcpServiceInfo
|
2024-04-29 15:09:07 +00:00
|
|
|
from homeassistant.components.fyta.const import CONF_EXPIRATION, DOMAIN
|
|
|
|
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_PASSWORD, CONF_USERNAME
|
2024-03-15 17:13:35 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-02 21:01:37 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2024-03-15 17:13:35 +00:00
|
|
|
|
2024-05-23 08:51:30 +00:00
|
|
|
from .const import ACCESS_TOKEN, EXPIRATION, PASSWORD, USERNAME
|
2024-03-15 17:13:35 +00:00
|
|
|
|
2024-05-23 08:51:30 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2024-03-15 17:13:35 +00:00
|
|
|
|
|
|
|
|
2024-12-03 20:23:52 +00:00
|
|
|
async def user_step(
|
|
|
|
hass: HomeAssistant, flow_id: str, mock_setup_entry: AsyncMock
|
|
|
|
) -> None:
|
|
|
|
"""Test user step (helper function)."""
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
flow_id, {CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == USERNAME
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_USERNAME: USERNAME,
|
|
|
|
CONF_PASSWORD: PASSWORD,
|
|
|
|
CONF_ACCESS_TOKEN: ACCESS_TOKEN,
|
|
|
|
CONF_EXPIRATION: EXPIRATION,
|
|
|
|
}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2024-03-15 17:13:35 +00:00
|
|
|
async def test_user_flow(
|
2024-05-23 08:51:30 +00:00
|
|
|
hass: HomeAssistant, mock_fyta_connector: AsyncMock, mock_setup_entry: AsyncMock
|
2024-03-15 17:13:35 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we get the form."""
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2024-04-02 21:01:37 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2024-03-15 17:13:35 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2024-12-03 20:23:52 +00:00
|
|
|
await user_step(hass, result["flow_id"], mock_setup_entry)
|
2024-03-15 17:13:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("exception", "error"),
|
|
|
|
[
|
|
|
|
(FytaConnectionError, {"base": "cannot_connect"}),
|
|
|
|
(FytaAuthentificationError, {"base": "invalid_auth"}),
|
|
|
|
(FytaPasswordError, {"base": "invalid_auth", CONF_PASSWORD: "password_error"}),
|
|
|
|
(Exception, {"base": "unknown"}),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_form_exceptions(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
exception: Exception,
|
|
|
|
error: dict[str, str],
|
2024-05-23 08:51:30 +00:00
|
|
|
mock_fyta_connector: AsyncMock,
|
2024-04-29 15:09:07 +00:00
|
|
|
mock_setup_entry: AsyncMock,
|
2024-03-15 17:13:35 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we can handle Form exceptions."""
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2024-05-23 08:51:30 +00:00
|
|
|
mock_fyta_connector.login.side_effect = exception
|
2024-03-15 17:13:35 +00:00
|
|
|
|
|
|
|
# tests with connection error
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], {CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:01:37 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2024-03-15 17:13:35 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == error
|
|
|
|
|
2024-05-23 08:51:30 +00:00
|
|
|
mock_fyta_connector.login.side_effect = None
|
2024-03-15 17:13:35 +00:00
|
|
|
|
|
|
|
# tests with all information provided
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], {CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:01:37 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2024-03-15 17:13:35 +00:00
|
|
|
assert result["title"] == USERNAME
|
|
|
|
assert result["data"][CONF_USERNAME] == USERNAME
|
|
|
|
assert result["data"][CONF_PASSWORD] == PASSWORD
|
2024-04-29 15:09:07 +00:00
|
|
|
assert result["data"][CONF_ACCESS_TOKEN] == ACCESS_TOKEN
|
2024-05-23 08:51:30 +00:00
|
|
|
assert result["data"][CONF_EXPIRATION] == EXPIRATION
|
2024-03-15 17:13:35 +00:00
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2024-05-23 08:51:30 +00:00
|
|
|
async def test_duplicate_entry(
|
|
|
|
hass: HomeAssistant, mock_fyta_connector: AsyncMock
|
|
|
|
) -> None:
|
2024-03-15 17:13:35 +00:00
|
|
|
"""Test duplicate setup handling."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title=USERNAME,
|
|
|
|
data={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:01:37 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2024-03-15 17:13:35 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:01:37 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2024-03-15 17:13:35 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2024-04-12 18:33:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("exception", "error"),
|
|
|
|
[
|
|
|
|
(FytaConnectionError, {"base": "cannot_connect"}),
|
|
|
|
(FytaAuthentificationError, {"base": "invalid_auth"}),
|
|
|
|
(FytaPasswordError, {"base": "invalid_auth", CONF_PASSWORD: "password_error"}),
|
|
|
|
(Exception, {"base": "unknown"}),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_reauth(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
exception: Exception,
|
|
|
|
error: dict[str, str],
|
2024-05-23 08:51:30 +00:00
|
|
|
mock_fyta_connector: AsyncMock,
|
2024-04-29 15:09:07 +00:00
|
|
|
mock_setup_entry: AsyncMock,
|
2024-04-12 18:33:24 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test reauth-flow works."""
|
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title=USERNAME,
|
2024-04-29 15:09:07 +00:00
|
|
|
data={
|
|
|
|
CONF_USERNAME: USERNAME,
|
|
|
|
CONF_PASSWORD: PASSWORD,
|
|
|
|
CONF_ACCESS_TOKEN: ACCESS_TOKEN,
|
2024-05-23 08:51:30 +00:00
|
|
|
CONF_EXPIRATION: EXPIRATION,
|
2024-04-29 15:09:07 +00:00
|
|
|
},
|
2024-04-12 18:33:24 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2024-08-28 13:47:57 +00:00
|
|
|
result = await entry.start_reauth_flow(hass)
|
2024-04-12 18:33:24 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
2024-05-23 08:51:30 +00:00
|
|
|
mock_fyta_connector.login.side_effect = exception
|
2024-04-12 18:33:24 +00:00
|
|
|
|
|
|
|
# tests with connection error
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2024-04-29 15:09:07 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
2024-04-12 18:33:24 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
assert result["errors"] == error
|
|
|
|
|
2024-05-23 08:51:30 +00:00
|
|
|
mock_fyta_connector.login.side_effect = None
|
2024-04-12 18:33:24 +00:00
|
|
|
|
|
|
|
# tests with all information provided
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_USERNAME: "other_username", CONF_PASSWORD: "other_password"},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert entry.data[CONF_USERNAME] == "other_username"
|
|
|
|
assert entry.data[CONF_PASSWORD] == "other_password"
|
2024-04-29 15:09:07 +00:00
|
|
|
assert entry.data[CONF_ACCESS_TOKEN] == ACCESS_TOKEN
|
2024-05-23 08:51:30 +00:00
|
|
|
assert entry.data[CONF_EXPIRATION] == EXPIRATION
|
2024-12-03 20:23:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_dhcp_discovery(
|
|
|
|
hass: HomeAssistant, mock_fyta_connector: AsyncMock, mock_setup_entry: AsyncMock
|
|
|
|
) -> None:
|
|
|
|
"""Test DHCP discovery flow."""
|
|
|
|
|
|
|
|
service_info = DhcpServiceInfo(
|
|
|
|
hostname="FYTA HUB",
|
|
|
|
ip="1.2.3.4",
|
|
|
|
macaddress="aabbccddeeff",
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=service_info,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
await user_step(hass, result["flow_id"], mock_setup_entry)
|