2020-06-16 09:05:33 +00:00
|
|
|
"""Test the Blink config flow."""
|
2024-01-09 15:06:04 +00:00
|
|
|
from unittest.mock import patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
from blinkpy.auth import LoginError
|
|
|
|
from blinkpy.blinkpy import BlinkSetupError
|
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2020-06-16 09:05:33 +00:00
|
|
|
from homeassistant.components.blink import DOMAIN
|
2022-11-30 13:59:26 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-06-16 09:05:33 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form(hass: HomeAssistant) -> None:
|
2020-06-16 09:05:33 +00:00
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-06-16 09:05:33 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
with patch("homeassistant.components.blink.config_flow.Auth.startup"), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=False,
|
2020-06-16 09:05:33 +00:00
|
|
|
), patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.blink.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-06-16 09:05:33 +00:00
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"username": "blink@example.com", "password": "example"},
|
2020-06-16 09:05:33 +00:00
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-06-16 09:05:33 +00:00
|
|
|
|
|
|
|
assert result2["type"] == "create_entry"
|
|
|
|
assert result2["title"] == "blink"
|
|
|
|
assert result2["result"].unique_id == "blink@example.com"
|
|
|
|
assert result2["data"] == {
|
|
|
|
"username": "blink@example.com",
|
|
|
|
"password": "example",
|
2020-08-05 10:21:14 +00:00
|
|
|
"device_id": "Home Assistant",
|
|
|
|
"token": None,
|
|
|
|
"host": None,
|
|
|
|
"account_id": None,
|
|
|
|
"client_id": None,
|
|
|
|
"region_id": None,
|
2020-06-16 09:05:33 +00:00
|
|
|
}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_2fa(hass: HomeAssistant) -> None:
|
2020-06-16 09:05:33 +00:00
|
|
|
"""Test we get the 2fa form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-06-16 09:05:33 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
with patch("homeassistant.components.blink.config_flow.Auth.startup"), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=True,
|
2021-03-29 23:23:44 +00:00
|
|
|
):
|
2020-06-16 09:05:33 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"username": "blink@example.com", "password": "example"},
|
2020-06-16 09:05:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["step_id"] == "2fa"
|
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
with patch("homeassistant.components.blink.config_flow.Auth.startup"), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=False,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.send_auth_key",
|
|
|
|
return_value=True,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Blink.setup_urls",
|
|
|
|
return_value=True,
|
2020-06-16 09:05:33 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.async_setup_entry", return_value=True
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result2["flow_id"], {"pin": "1234"}
|
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-06-16 09:05:33 +00:00
|
|
|
|
|
|
|
assert result3["type"] == "create_entry"
|
|
|
|
assert result3["title"] == "blink"
|
|
|
|
assert result3["result"].unique_id == "blink@example.com"
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_2fa_connect_error(hass: HomeAssistant) -> None:
|
2020-08-05 10:21:14 +00:00
|
|
|
"""Test we report a connect error during 2fa setup."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("homeassistant.components.blink.config_flow.Auth.startup"), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=True,
|
2021-03-29 23:23:44 +00:00
|
|
|
):
|
2020-08-05 10:21:14 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"username": "blink@example.com", "password": "example"},
|
2020-08-05 10:21:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["step_id"] == "2fa"
|
|
|
|
|
|
|
|
with patch("homeassistant.components.blink.config_flow.Auth.startup"), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=False,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.send_auth_key",
|
|
|
|
return_value=True,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Blink.setup_urls",
|
|
|
|
side_effect=BlinkSetupError,
|
|
|
|
), patch(
|
2023-11-27 13:38:59 +00:00
|
|
|
"homeassistant.components.blink.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-08-05 10:21:14 +00:00
|
|
|
):
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result2["flow_id"], {"pin": "1234"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result3["type"] == "form"
|
|
|
|
assert result3["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_2fa_invalid_key(hass: HomeAssistant) -> None:
|
2020-08-05 10:21:14 +00:00
|
|
|
"""Test we report an error if key is invalid."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("homeassistant.components.blink.config_flow.Auth.startup"), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=True,
|
2021-03-29 23:23:44 +00:00
|
|
|
):
|
2020-08-05 10:21:14 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"username": "blink@example.com", "password": "example"},
|
2020-08-05 10:21:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["step_id"] == "2fa"
|
|
|
|
|
2023-02-02 17:35:24 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.startup",
|
|
|
|
), patch(
|
2020-08-05 10:21:14 +00:00
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=False,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.send_auth_key",
|
|
|
|
return_value=False,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Blink.setup_urls",
|
|
|
|
return_value=True,
|
|
|
|
), patch(
|
2023-11-27 13:38:59 +00:00
|
|
|
"homeassistant.components.blink.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-08-05 10:21:14 +00:00
|
|
|
):
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result2["flow_id"], {"pin": "1234"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result3["type"] == "form"
|
|
|
|
assert result3["errors"] == {"base": "invalid_access_token"}
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_2fa_unknown_error(hass: HomeAssistant) -> None:
|
2020-08-05 10:21:14 +00:00
|
|
|
"""Test we report an unknown error during 2fa setup."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-08-05 10:21:14 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("homeassistant.components.blink.config_flow.Auth.startup"), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=True,
|
2021-03-29 23:23:44 +00:00
|
|
|
):
|
2020-08-05 10:21:14 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"username": "blink@example.com", "password": "example"},
|
2020-08-05 10:21:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["step_id"] == "2fa"
|
|
|
|
|
|
|
|
with patch("homeassistant.components.blink.config_flow.Auth.startup"), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.check_key_required",
|
|
|
|
return_value=False,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Auth.send_auth_key",
|
|
|
|
return_value=True,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.blink.config_flow.Blink.setup_urls",
|
|
|
|
side_effect=KeyError,
|
|
|
|
), patch(
|
2023-11-27 13:38:59 +00:00
|
|
|
"homeassistant.components.blink.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-08-05 10:21:14 +00:00
|
|
|
):
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result2["flow_id"], {"pin": "1234"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result3["type"] == "form"
|
|
|
|
assert result3["errors"] == {"base": "unknown"}
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
|
2020-06-16 09:05:33 +00:00
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2020-08-05 10:21:14 +00:00
|
|
|
"homeassistant.components.blink.config_flow.Auth.startup",
|
|
|
|
side_effect=LoginError,
|
2020-06-16 09:05:33 +00:00
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], {"username": "blink@example.com", "password": "example"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_form_unknown_error(hass: HomeAssistant) -> None:
|
2020-06-16 09:05:33 +00:00
|
|
|
"""Test we handle unknown error at startup."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.blink.config_flow.Auth.startup",
|
|
|
|
side_effect=KeyError,
|
2020-06-16 09:05:33 +00:00
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], {"username": "blink@example.com", "password": "example"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "unknown"}
|
|
|
|
|
|
|
|
|
2023-02-08 17:08:43 +00:00
|
|
|
async def test_reauth_shows_user_step(hass: HomeAssistant) -> None:
|
2020-08-05 10:21:14 +00:00
|
|
|
"""Test reauth shows the user form."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-06-29 09:54:21 +00:00
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_REAUTH},
|
|
|
|
data={"username": "blink@example.com", "password": "invalid_password"},
|
2020-08-05 10:21:14 +00:00
|
|
|
)
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-08-05 10:21:14 +00:00
|
|
|
assert result["step_id"] == "user"
|