2018-10-12 17:07:47 +00:00
|
|
|
"""Define tests for the SimpliSafe config flow."""
|
2020-07-24 02:02:29 +00:00
|
|
|
from simplipy.errors import (
|
|
|
|
InvalidCredentialsError,
|
|
|
|
PendingAuthorizationError,
|
|
|
|
SimplipyError,
|
|
|
|
)
|
2020-02-25 05:02:20 +00:00
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
from homeassistant import data_entry_flow
|
2020-03-13 05:00:00 +00:00
|
|
|
from homeassistant.components.simplisafe import DOMAIN
|
2020-10-15 20:28:09 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_USER
|
2020-03-13 05:00:00 +00:00
|
|
|
from homeassistant.const import CONF_CODE, CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2020-09-17 05:20:00 +00:00
|
|
|
from tests.async_mock import AsyncMock, MagicMock, PropertyMock, patch
|
2020-03-13 05:00:00 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def mock_api():
|
|
|
|
"""Mock SimpliSafe API class."""
|
|
|
|
api = MagicMock()
|
2019-07-31 19:25:30 +00:00
|
|
|
type(api).refresh_token = PropertyMock(return_value="12345abc")
|
2018-10-12 17:07:47 +00:00
|
|
|
return api
|
|
|
|
|
|
|
|
|
|
|
|
async def test_duplicate_error(hass):
|
|
|
|
"""Test that errors are shown when duplicates are added."""
|
2020-07-24 02:02:29 +00:00
|
|
|
conf = {
|
|
|
|
CONF_USERNAME: "user@email.com",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_CODE: "1234",
|
|
|
|
}
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2020-07-24 02:02:29 +00:00
|
|
|
MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
unique_id="user@email.com",
|
|
|
|
data={CONF_USERNAME: "user@email.com", CONF_TOKEN: "12345", CONF_CODE: "1234"},
|
|
|
|
).add_to_hass(hass)
|
2020-02-24 20:03:08 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
async def test_invalid_credentials(hass):
|
|
|
|
"""Test that invalid credentials throws an error."""
|
2019-07-31 19:25:30 +00:00
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"simplipy.API.login_via_credentials",
|
2020-09-17 05:20:00 +00:00
|
|
|
new=AsyncMock(side_effect=InvalidCredentialsError),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2020-03-13 05:00:00 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
2020-10-18 18:55:01 +00:00
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
|
2020-03-13 05:00:00 +00:00
|
|
|
async def test_options_flow(hass):
|
|
|
|
"""Test config flow options."""
|
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
2020-08-27 11:56:20 +00:00
|
|
|
domain=DOMAIN,
|
|
|
|
unique_id="abcde12345",
|
|
|
|
data=conf,
|
|
|
|
options={CONF_CODE: "1234"},
|
2020-03-13 05:00:00 +00:00
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
|
|
|
):
|
2020-12-19 17:22:34 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
2020-03-13 05:00:00 +00:00
|
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
|
|
|
|
|
|
|
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
|
|
|
|
assert config_entry.options == {CONF_CODE: "4321"}
|
|
|
|
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
async def test_show_form(hass):
|
|
|
|
"""Test that the form is served with no input."""
|
2020-07-24 02:02:29 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-10-15 20:28:09 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2020-07-24 02:02:29 +00:00
|
|
|
)
|
2018-10-12 17:07:47 +00:00
|
|
|
|
2020-07-24 02:02:29 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "user"
|
2018-10-12 17:07:47 +00:00
|
|
|
|
|
|
|
|
2020-07-24 02:02:29 +00:00
|
|
|
async def test_step_reauth(hass):
|
|
|
|
"""Test that the reauth step works."""
|
|
|
|
MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
unique_id="user@email.com",
|
|
|
|
data={CONF_USERNAME: "user@email.com", CONF_TOKEN: "12345", CONF_CODE: "1234"},
|
|
|
|
).add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": "reauth"},
|
|
|
|
data={CONF_CODE: "1234", CONF_USERNAME: "user@email.com"},
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
2020-09-17 05:20:00 +00:00
|
|
|
), patch(
|
|
|
|
"simplipy.API.login_via_credentials", new=AsyncMock(return_value=mock_api())
|
|
|
|
):
|
2020-07-24 02:02:29 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
|
|
|
|
|
2018-10-12 17:07:47 +00:00
|
|
|
async def test_step_user(hass):
|
2020-07-24 02:02:29 +00:00
|
|
|
"""Test that the user step works (without MFA)."""
|
2018-10-12 17:07:47 +00:00
|
|
|
conf = {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_USERNAME: "user@email.com",
|
|
|
|
CONF_PASSWORD: "password",
|
2020-03-13 05:00:00 +00:00
|
|
|
CONF_CODE: "1234",
|
2018-10-12 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2020-03-13 05:00:00 +00:00
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
2020-09-17 05:20:00 +00:00
|
|
|
), patch(
|
|
|
|
"simplipy.API.login_via_credentials", new=AsyncMock(return_value=mock_api())
|
|
|
|
):
|
2020-07-24 02:02:29 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["title"] == "user@email.com"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_USERNAME: "user@email.com",
|
|
|
|
CONF_TOKEN: "12345abc",
|
|
|
|
CONF_CODE: "1234",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_step_user_mfa(hass):
|
|
|
|
"""Test that the user step works when MFA is in the middle."""
|
|
|
|
conf = {
|
|
|
|
CONF_USERNAME: "user@email.com",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_CODE: "1234",
|
|
|
|
}
|
|
|
|
|
|
|
|
with patch(
|
2020-09-17 05:20:00 +00:00
|
|
|
"simplipy.API.login_via_credentials",
|
|
|
|
new=AsyncMock(side_effect=PendingAuthorizationError),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2020-03-13 05:00:00 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
2020-07-24 02:02:29 +00:00
|
|
|
assert result["step_id"] == "mfa"
|
|
|
|
|
|
|
|
with patch(
|
2020-09-17 05:20:00 +00:00
|
|
|
"simplipy.API.login_via_credentials",
|
|
|
|
new=AsyncMock(side_effect=PendingAuthorizationError),
|
2020-07-24 02:02:29 +00:00
|
|
|
):
|
|
|
|
# Simulate the user pressing the MFA submit button without having clicked
|
|
|
|
# the link in the MFA email:
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
|
|
|
assert result["step_id"] == "mfa"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
|
2020-09-17 05:20:00 +00:00
|
|
|
), patch(
|
|
|
|
"simplipy.API.login_via_credentials", new=AsyncMock(return_value=mock_api())
|
|
|
|
):
|
2020-07-24 02:02:29 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2020-03-13 05:00:00 +00:00
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["title"] == "user@email.com"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_USERNAME: "user@email.com",
|
|
|
|
CONF_TOKEN: "12345abc",
|
|
|
|
CONF_CODE: "1234",
|
|
|
|
}
|
2020-07-24 02:02:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_unknown_error(hass):
|
|
|
|
"""Test that an unknown error raises the correct error."""
|
|
|
|
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
|
|
|
|
|
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"simplipy.API.login_via_credentials",
|
2020-09-17 05:20:00 +00:00
|
|
|
new=AsyncMock(side_effect=SimplipyError),
|
2020-07-24 02:02:29 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
|
|
|
assert result["errors"] == {"base": "unknown"}
|