2018-10-12 17:07:47 +00:00
|
|
|
"""Define tests for the SimpliSafe config flow."""
|
2022-08-04 17:43:02 +00:00
|
|
|
import logging
|
2022-01-30 21:12:01 +00:00
|
|
|
from unittest.mock import patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
import pytest
|
2022-07-24 20:09:02 +00:00
|
|
|
from simplipy.errors import InvalidCredentialsError, SimplipyError
|
2020-02-25 05:02:20 +00:00
|
|
|
|
2020-03-13 05:00:00 +00:00
|
|
|
from homeassistant.components.simplisafe import DOMAIN
|
2022-07-24 20:09:02 +00:00
|
|
|
from homeassistant.components.simplisafe.config_flow import CONF_AUTH_CODE
|
2021-04-25 09:27:40 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
2022-04-27 08:16:28 +00:00
|
|
|
from homeassistant.const import CONF_CODE, CONF_TOKEN, CONF_USERNAME
|
2023-02-08 16:12:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-08-07 20:01:32 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2022-08-04 17:43:02 +00:00
|
|
|
VALID_AUTH_CODE = "code12345123451234512345123451234512345123451"
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2023-02-15 14:23:34 +00:00
|
|
|
async def test_duplicate_error(
|
|
|
|
config_entry, hass: HomeAssistant, setup_simplisafe
|
|
|
|
) -> None:
|
2022-07-24 20:09:02 +00:00
|
|
|
"""Test that errors are shown when duplicates are added."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
2022-08-07 20:01:32 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2022-04-28 18:45:37 +00:00
|
|
|
|
2022-07-24 20:09:02 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2022-08-04 17:43:02 +00:00
|
|
|
result["flow_id"], user_input={CONF_AUTH_CODE: VALID_AUTH_CODE}
|
2022-07-24 20:09:02 +00:00
|
|
|
)
|
2022-09-21 07:17:20 +00:00
|
|
|
assert result["type"] == FlowResultType.ABORT
|
2022-07-24 20:09:02 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2022-04-27 08:16:28 +00:00
|
|
|
|
|
|
|
|
2023-02-08 16:12:54 +00:00
|
|
|
async def test_invalid_auth_code_length(hass: HomeAssistant) -> None:
|
2022-08-04 17:43:02 +00:00
|
|
|
"""Test that an invalid auth code length show the correct error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
2022-08-07 20:01:32 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2022-08-04 17:43:02 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_AUTH_CODE: "too_short_code"}
|
|
|
|
)
|
2022-08-07 20:01:32 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2022-08-04 17:43:02 +00:00
|
|
|
assert result["errors"] == {CONF_AUTH_CODE: "invalid_auth_code_length"}
|
|
|
|
|
|
|
|
|
2023-02-08 16:12:54 +00:00
|
|
|
async def test_invalid_credentials(hass: HomeAssistant) -> None:
|
2022-07-24 20:09:02 +00:00
|
|
|
"""Test that invalid credentials show the correct error."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.config_flow.API.async_from_auth",
|
|
|
|
side_effect=InvalidCredentialsError,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
2022-08-07 20:01:32 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2022-04-27 08:16:28 +00:00
|
|
|
|
2022-07-24 20:09:02 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2022-08-04 17:43:02 +00:00
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_AUTH_CODE: VALID_AUTH_CODE},
|
2022-07-24 20:09:02 +00:00
|
|
|
)
|
2022-08-07 20:01:32 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2022-08-04 17:43:02 +00:00
|
|
|
assert result["errors"] == {CONF_AUTH_CODE: "invalid_auth"}
|
2021-10-19 20:09:48 +00:00
|
|
|
|
2020-02-24 20:03:08 +00:00
|
|
|
|
2023-02-15 14:23:34 +00:00
|
|
|
async def test_options_flow(config_entry, hass: HomeAssistant) -> None:
|
2020-03-13 05:00:00 +00:00
|
|
|
"""Test config flow options."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
|
|
|
):
|
2022-01-30 21:12:01 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
2022-07-24 20:09:02 +00:00
|
|
|
|
2022-08-07 20:01:32 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2020-03-13 05:00:00 +00:00
|
|
|
assert result["step_id"] == "init"
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_CODE: "4321"}
|
|
|
|
)
|
|
|
|
|
2022-09-21 07:17:20 +00:00
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
2022-07-24 20:09:02 +00:00
|
|
|
assert config_entry.options == {CONF_CODE: "4321"}
|
2020-03-13 05:00:00 +00:00
|
|
|
|
2022-04-28 18:45:37 +00:00
|
|
|
|
2023-02-15 14:23:34 +00:00
|
|
|
async def test_step_reauth(config_entry, hass: HomeAssistant, setup_simplisafe) -> None:
|
2022-07-24 20:09:02 +00:00
|
|
|
"""Test the re-auth step."""
|
2020-07-24 02:02:29 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-07-24 20:09:02 +00:00
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_REAUTH},
|
|
|
|
data={CONF_USERNAME: "12345", CONF_TOKEN: "token123"},
|
2022-04-27 08:16:28 +00:00
|
|
|
)
|
2022-07-24 20:09:02 +00:00
|
|
|
assert result["step_id"] == "user"
|
2020-07-24 02:02:29 +00:00
|
|
|
|
2022-07-24 20:09:02 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
|
|
|
), patch("homeassistant.config_entries.ConfigEntries.async_reload"):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2022-08-04 17:43:02 +00:00
|
|
|
result["flow_id"], user_input={CONF_AUTH_CODE: VALID_AUTH_CODE}
|
2022-07-24 20:09:02 +00:00
|
|
|
)
|
2022-09-21 07:17:20 +00:00
|
|
|
assert result["type"] == FlowResultType.ABORT
|
2022-07-24 20:09:02 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
2022-04-28 18:45:37 +00:00
|
|
|
|
2022-07-24 20:09:02 +00:00
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|
2021-10-19 20:09:48 +00:00
|
|
|
[config_entry] = hass.config_entries.async_entries(DOMAIN)
|
2022-07-24 20:09:02 +00:00
|
|
|
assert config_entry.data == {CONF_USERNAME: "12345", CONF_TOKEN: "token123"}
|
2020-07-24 02:02:29 +00:00
|
|
|
|
2022-04-28 18:45:37 +00:00
|
|
|
|
2022-07-24 20:09:02 +00:00
|
|
|
@pytest.mark.parametrize("unique_id", ["some_other_id"])
|
2023-02-15 14:23:34 +00:00
|
|
|
async def test_step_reauth_wrong_account(
|
|
|
|
config_entry, hass: HomeAssistant, setup_simplisafe
|
|
|
|
) -> None:
|
2022-07-24 20:09:02 +00:00
|
|
|
"""Test the re-auth step where the wrong account is used during login."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_REAUTH},
|
|
|
|
data={CONF_USERNAME: "12345", CONF_TOKEN: "token123"},
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
2020-07-24 02:02:29 +00:00
|
|
|
|
2022-04-27 08:16:28 +00:00
|
|
|
with patch(
|
2022-07-24 20:09:02 +00:00
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
|
|
|
), patch("homeassistant.config_entries.ConfigEntries.async_reload"):
|
2022-04-27 08:16:28 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2022-08-04 17:43:02 +00:00
|
|
|
result["flow_id"], user_input={CONF_AUTH_CODE: VALID_AUTH_CODE}
|
2022-04-27 08:16:28 +00:00
|
|
|
)
|
2022-09-21 07:17:20 +00:00
|
|
|
assert result["type"] == FlowResultType.ABORT
|
2022-07-24 20:09:02 +00:00
|
|
|
assert result["reason"] == "wrong_account"
|
2022-04-27 08:16:28 +00:00
|
|
|
|
|
|
|
|
2022-08-04 17:43:02 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("auth_code", "log_statement"),
|
2022-08-04 17:43:02 +00:00
|
|
|
[
|
|
|
|
(
|
|
|
|
VALID_AUTH_CODE,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
f"={VALID_AUTH_CODE}",
|
|
|
|
'Stripping "=" from the start of the authorization code',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2023-02-15 14:23:34 +00:00
|
|
|
async def test_step_user(
|
|
|
|
auth_code,
|
|
|
|
caplog: pytest.LogCaptureFixture,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
log_statement,
|
|
|
|
setup_simplisafe,
|
|
|
|
) -> None:
|
2022-08-04 17:43:02 +00:00
|
|
|
"""Test successfully completion of the user step."""
|
|
|
|
caplog.set_level = logging.DEBUG
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-07-24 20:09:02 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2021-10-19 20:09:48 +00:00
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2022-07-24 20:09:02 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
|
|
|
), patch("homeassistant.config_entries.ConfigEntries.async_reload"):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2022-08-04 17:43:02 +00:00
|
|
|
result["flow_id"], user_input={CONF_AUTH_CODE: auth_code}
|
2022-07-24 20:09:02 +00:00
|
|
|
)
|
2022-09-21 07:17:20 +00:00
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
2020-07-24 02:02:29 +00:00
|
|
|
|
2022-08-04 17:43:02 +00:00
|
|
|
if log_statement:
|
|
|
|
assert any(m for m in caplog.messages if log_statement in m)
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
[config_entry] = hass.config_entries.async_entries(DOMAIN)
|
2022-07-24 20:09:02 +00:00
|
|
|
assert config_entry.data == {CONF_USERNAME: "12345", CONF_TOKEN: "token123"}
|
2022-04-27 08:16:28 +00:00
|
|
|
|
|
|
|
|
2023-02-15 14:23:34 +00:00
|
|
|
async def test_unknown_error(hass: HomeAssistant, setup_simplisafe) -> None:
|
2022-07-24 20:09:02 +00:00
|
|
|
"""Test that an unknown error shows ohe correct error."""
|
2022-04-27 08:16:28 +00:00
|
|
|
with patch(
|
2022-07-24 20:09:02 +00:00
|
|
|
"homeassistant.components.simplisafe.config_flow.API.async_from_auth",
|
|
|
|
side_effect=SimplipyError,
|
2022-04-27 08:16:28 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
2022-08-07 20:01:32 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2022-04-27 08:16:28 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2022-08-04 17:43:02 +00:00
|
|
|
result["flow_id"], user_input={CONF_AUTH_CODE: VALID_AUTH_CODE}
|
2022-04-27 08:16:28 +00:00
|
|
|
)
|
2022-08-07 20:01:32 +00:00
|
|
|
assert result["type"] == FlowResultType.FORM
|
2022-07-24 20:09:02 +00:00
|
|
|
assert result["errors"] == {"base": "unknown"}
|