2019-10-13 18:01:04 +00:00
|
|
|
"""Tests for the Abode config flow."""
|
2021-10-22 12:21:34 +00:00
|
|
|
from http import HTTPStatus
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2023-01-24 11:44:38 +00:00
|
|
|
from jaraco.abode.exceptions import (
|
|
|
|
AuthenticationException as AbodeAuthenticationException,
|
|
|
|
)
|
|
|
|
from jaraco.abode.helpers.errors import MFA_CODE_REQUIRED
|
2023-02-27 13:03:51 +00:00
|
|
|
import pytest
|
2022-01-10 14:54:09 +00:00
|
|
|
from requests.exceptions import ConnectTimeout
|
2019-10-13 18:01:04 +00:00
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.components.abode import config_flow
|
2022-01-10 14:54:09 +00:00
|
|
|
from homeassistant.components.abode.const import CONF_POLLING, DOMAIN
|
2021-06-30 20:28:57 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
2021-10-22 12:21:34 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2022-01-10 14:54:09 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-08 12:44:12 +00:00
|
|
|
|
2019-10-13 18:01:04 +00:00
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2023-02-27 13:03:51 +00:00
|
|
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
|
|
|
|
2019-10-13 18:01:04 +00:00
|
|
|
|
2022-01-10 14:54:09 +00:00
|
|
|
async def test_show_form(hass: HomeAssistant) -> None:
|
2019-10-13 18:01:04 +00:00
|
|
|
"""Test that the form is served with no input."""
|
|
|
|
flow = config_flow.AbodeFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
result = await flow.async_step_user(user_input=None)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2019-10-13 18:01:04 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
|
2022-01-10 14:54:09 +00:00
|
|
|
async def test_one_config_allowed(hass: HomeAssistant) -> None:
|
2019-10-13 18:01:04 +00:00
|
|
|
"""Test that only one Abode configuration is allowed."""
|
|
|
|
flow = config_flow.AbodeFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
MockConfigEntry(
|
2020-11-27 12:39:26 +00:00
|
|
|
domain=DOMAIN,
|
2019-10-13 18:01:04 +00:00
|
|
|
data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
|
|
|
|
).add_to_hass(hass)
|
|
|
|
|
|
|
|
step_user_result = await flow.async_step_user()
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert step_user_result["type"] == data_entry_flow.FlowResultType.ABORT
|
2019-10-13 18:01:04 +00:00
|
|
|
assert step_user_result["reason"] == "single_instance_allowed"
|
|
|
|
|
|
|
|
|
2022-01-10 14:54:09 +00:00
|
|
|
async def test_invalid_credentials(hass: HomeAssistant) -> None:
|
2019-10-13 18:01:04 +00:00
|
|
|
"""Test that invalid credentials throws an error."""
|
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
|
|
|
|
|
|
|
flow = config_flow.AbodeFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.abode.config_flow.Abode",
|
2021-10-22 12:21:34 +00:00
|
|
|
side_effect=AbodeAuthenticationException(
|
|
|
|
(HTTPStatus.BAD_REQUEST, "auth error")
|
|
|
|
),
|
2019-10-13 18:01:04 +00:00
|
|
|
):
|
|
|
|
result = await flow.async_step_user(user_input=conf)
|
2020-10-02 19:59:55 +00:00
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
2019-10-13 18:01:04 +00:00
|
|
|
|
|
|
|
|
2022-01-10 14:54:09 +00:00
|
|
|
async def test_connection_auth_error(hass: HomeAssistant) -> None:
|
2019-10-13 18:01:04 +00:00
|
|
|
"""Test other than invalid credentials throws an error."""
|
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
|
|
|
|
|
|
|
flow = config_flow.AbodeFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.abode.config_flow.Abode",
|
2020-04-08 21:20:03 +00:00
|
|
|
side_effect=AbodeAuthenticationException(
|
2021-10-22 12:21:34 +00:00
|
|
|
(HTTPStatus.INTERNAL_SERVER_ERROR, "connection error")
|
2020-04-08 21:20:03 +00:00
|
|
|
),
|
2019-10-13 18:01:04 +00:00
|
|
|
):
|
|
|
|
result = await flow.async_step_user(user_input=conf)
|
2020-10-02 19:59:55 +00:00
|
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
2019-10-13 18:01:04 +00:00
|
|
|
|
|
|
|
|
2022-01-10 14:54:09 +00:00
|
|
|
async def test_connection_error(hass: HomeAssistant) -> None:
|
|
|
|
"""Test login throws an error if connection times out."""
|
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
|
|
|
|
|
|
|
flow = config_flow.AbodeFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.abode.config_flow.Abode",
|
|
|
|
side_effect=ConnectTimeout,
|
|
|
|
):
|
|
|
|
result = await flow.async_step_user(user_input=conf)
|
|
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_step_user(hass: HomeAssistant) -> None:
|
2019-10-13 18:01:04 +00:00
|
|
|
"""Test that the user step works."""
|
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
|
|
|
|
2023-01-24 11:44:38 +00:00
|
|
|
with patch("homeassistant.components.abode.config_flow.Abode"):
|
2020-11-27 12:39:26 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2020-11-27 12:39:26 +00:00
|
|
|
assert result["title"] == "user@email.com"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_USERNAME: "user@email.com",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_POLLING: False,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-10 14:54:09 +00:00
|
|
|
async def test_step_mfa(hass: HomeAssistant) -> None:
|
2020-11-27 12:39:26 +00:00
|
|
|
"""Test that the MFA step works."""
|
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.abode.config_flow.Abode",
|
|
|
|
side_effect=AbodeAuthenticationException(MFA_CODE_REQUIRED),
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-11-27 12:39:26 +00:00
|
|
|
assert result["step_id"] == "mfa"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.abode.config_flow.Abode",
|
2021-10-22 12:21:34 +00:00
|
|
|
side_effect=AbodeAuthenticationException(
|
|
|
|
(HTTPStatus.BAD_REQUEST, "invalid mfa")
|
|
|
|
),
|
2020-11-27 12:39:26 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"mfa_code": "123456"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["errors"] == {"base": "invalid_mfa_code"}
|
|
|
|
|
2023-01-24 11:44:38 +00:00
|
|
|
with patch("homeassistant.components.abode.config_flow.Abode"):
|
2020-11-27 12:39:26 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"mfa_code": "123456"}
|
|
|
|
)
|
2019-10-13 18:01:04 +00:00
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2019-10-13 18:01:04 +00:00
|
|
|
assert result["title"] == "user@email.com"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_USERNAME: "user@email.com",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_POLLING: False,
|
|
|
|
}
|
2020-11-27 12:39:26 +00:00
|
|
|
|
|
|
|
|
2022-01-10 14:54:09 +00:00
|
|
|
async def test_step_reauth(hass: HomeAssistant) -> None:
|
2020-11-27 12:39:26 +00:00
|
|
|
"""Test the reauth flow."""
|
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
|
|
|
|
|
|
|
MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
unique_id="user@email.com",
|
|
|
|
data=conf,
|
|
|
|
).add_to_hass(hass)
|
|
|
|
|
2023-01-24 11:44:38 +00:00
|
|
|
with patch("homeassistant.components.abode.config_flow.Abode"):
|
2020-11-27 12:39:26 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": SOURCE_REAUTH},
|
2020-11-27 12:39:26 +00:00
|
|
|
data=conf,
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-11-27 12:39:26 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
|
|
|
with patch("homeassistant.config_entries.ConfigEntries.async_reload"):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input=conf,
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
2020-11-27 12:39:26 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|