2018-10-12 11:07:47 -06:00
|
|
|
"""Define tests for the SimpliSafe config flow."""
|
2022-01-30 14:12:01 -07:00
|
|
|
from unittest.mock import patch
|
2021-01-01 22:31:56 +01:00
|
|
|
|
2021-10-19 14:09:48 -06:00
|
|
|
import pytest
|
|
|
|
from simplipy.errors import InvalidCredentialsError, SimplipyError
|
2020-02-24 22:02:20 -07:00
|
|
|
|
2018-10-12 11:07:47 -06:00
|
|
|
from homeassistant import data_entry_flow
|
2020-03-12 23:00:00 -06:00
|
|
|
from homeassistant.components.simplisafe import DOMAIN
|
2021-04-25 12:27:40 +03:00
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
2022-01-30 14:12:01 -07:00
|
|
|
from homeassistant.const import CONF_CODE
|
2018-10-12 11:07:47 -06:00
|
|
|
|
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
async def test_duplicate_error(hass, config_entry, config_code, setup_simplisafe):
|
|
|
|
"""Test that errors are shown when duplicates are added."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2018-10-12 11:07:47 -06:00
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=config_code
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
2021-10-19 14:09:48 -06:00
|
|
|
|
2020-02-24 13:03:08 -07:00
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"exc,error_string",
|
|
|
|
[(InvalidCredentialsError, "invalid_auth"), (SimplipyError, "unknown")],
|
|
|
|
)
|
|
|
|
async def test_errors(hass, config_code, exc, error_string):
|
|
|
|
"""Test that exceptions show the appropriate error."""
|
2019-07-31 12:25:30 -07:00
|
|
|
with patch(
|
2022-01-30 14:12:01 -07:00
|
|
|
"homeassistant.components.simplisafe.API.async_from_auth",
|
|
|
|
side_effect=exc,
|
2019-07-31 12:25:30 -07:00
|
|
|
):
|
2020-03-12 23:00:00 -06:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-10-19 14:09:48 -06:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2022-01-30 14:12:01 -07:00
|
|
|
result["flow_id"], user_input=config_code
|
2021-10-19 14:09:48 -06:00
|
|
|
)
|
2022-01-30 14:12:01 -07:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["errors"] == {"base": error_string}
|
2018-10-12 11:07:47 -06:00
|
|
|
|
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
async def test_options_flow(hass, config_entry):
|
2020-03-12 23:00:00 -06:00
|
|
|
"""Test config flow options."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
|
|
|
):
|
2022-01-30 14:12:01 -07:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
2020-03-12 23:00:00 -06:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "init"
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_CODE: "4321"}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2022-01-30 14:12:01 -07:00
|
|
|
assert config_entry.options == {CONF_CODE: "4321"}
|
2020-03-12 23:00:00 -06:00
|
|
|
|
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
async def test_step_reauth_old_format(
|
|
|
|
hass, config, config_code, config_entry, setup_simplisafe
|
|
|
|
):
|
2021-10-19 14:09:48 -06:00
|
|
|
"""Test the re-auth step with "old" config entries (those with user IDs)."""
|
2020-07-23 20:02:29 -06:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-01-30 14:12:01 -07:00
|
|
|
DOMAIN, context={"source": SOURCE_REAUTH}, data=config
|
2020-07-23 20:02:29 -06:00
|
|
|
)
|
2021-10-19 14:09:48 -06:00
|
|
|
assert result["step_id"] == "user"
|
2020-07-23 20:02:29 -06:00
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=config_code
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "reauth_successful"
|
2020-07-23 20:02:29 -06:00
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|
2021-10-19 14:09:48 -06:00
|
|
|
[config_entry] = hass.config_entries.async_entries(DOMAIN)
|
2022-01-30 14:12:01 -07:00
|
|
|
assert config_entry.data == config
|
2020-07-23 20:02:29 -06:00
|
|
|
|
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
async def test_step_reauth_new_format(
|
|
|
|
hass, config, config_code, config_entry, setup_simplisafe
|
|
|
|
):
|
2021-10-19 14:09:48 -06:00
|
|
|
"""Test the re-auth step with "new" config entries (those with user IDs)."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-01-30 14:12:01 -07:00
|
|
|
DOMAIN, context={"source": SOURCE_REAUTH}, data=config
|
2021-10-19 14:09:48 -06:00
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
2018-10-12 11:07:47 -06:00
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=config_code
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "reauth_successful"
|
2020-07-23 20:02:29 -06:00
|
|
|
|
2021-10-19 14:09:48 -06:00
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
[config_entry] = hass.config_entries.async_entries(DOMAIN)
|
2022-01-30 14:12:01 -07:00
|
|
|
assert config_entry.data == config
|
2020-07-23 20:02:29 -06:00
|
|
|
|
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
async def test_step_reauth_wrong_account(
|
|
|
|
hass, api, config, config_code, config_entry, setup_simplisafe
|
|
|
|
):
|
2021-10-28 14:55:14 -06:00
|
|
|
"""Test the re-auth step returning a different account from this one."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-01-30 14:12:01 -07:00
|
|
|
DOMAIN, context={"source": SOURCE_REAUTH}, data=config
|
2021-10-28 14:55:14 -06:00
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
# Simulate the next auth call returning a different user ID than the one we've
|
|
|
|
# identified as this entry's unique ID:
|
|
|
|
api.user_id = "67890"
|
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=config_code
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "wrong_account"
|
2021-10-28 14:55:14 -06:00
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
[config_entry] = hass.config_entries.async_entries(DOMAIN)
|
|
|
|
assert config_entry.unique_id == "12345"
|
|
|
|
|
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
async def test_step_user(hass, config, config_code, setup_simplisafe):
|
2021-10-19 14:09:48 -06:00
|
|
|
"""Test the user step."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "user"
|
2020-07-23 20:02:29 -06:00
|
|
|
|
2022-01-30 14:12:01 -07:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=config_code
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2020-07-23 20:02:29 -06:00
|
|
|
|
2021-10-19 14:09:48 -06:00
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
[config_entry] = hass.config_entries.async_entries(DOMAIN)
|
2022-01-30 14:12:01 -07:00
|
|
|
assert config_entry.data == config
|