2020-02-25 23:37:41 +00:00
|
|
|
"""Test the Sense config flow."""
|
2022-02-21 22:05:12 +00:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2022-02-21 22:05:12 +00:00
|
|
|
import pytest
|
|
|
|
from sense_energy import (
|
2022-03-22 22:14:01 +00:00
|
|
|
SenseAPIException,
|
2022-02-21 22:05:12 +00:00
|
|
|
SenseAPITimeoutException,
|
|
|
|
SenseAuthenticationException,
|
|
|
|
SenseMFARequiredException,
|
|
|
|
)
|
2020-02-25 23:37:41 +00:00
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries
|
2020-02-25 23:37:41 +00:00
|
|
|
from homeassistant.components.sense.const import DOMAIN
|
2022-02-21 22:05:12 +00:00
|
|
|
from homeassistant.const import CONF_CODE
|
2023-02-08 16:12:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-02-25 23:37:41 +00:00
|
|
|
|
2022-02-21 22:05:12 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2020-02-25 23:37:41 +00:00
|
|
|
|
2022-02-21 22:05:12 +00:00
|
|
|
MOCK_CONFIG = {
|
|
|
|
"timeout": 6,
|
|
|
|
"email": "test-email",
|
|
|
|
"password": "test-password",
|
|
|
|
"access_token": "ABC",
|
|
|
|
"user_id": "123",
|
|
|
|
"monitor_id": "456",
|
2022-12-01 09:53:48 +00:00
|
|
|
"device_id": "789",
|
|
|
|
"refresh_token": "XYZ",
|
2022-02-21 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="mock_sense")
|
|
|
|
def mock_sense():
|
|
|
|
"""Mock Sense object for authenticatation."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.sense.config_flow.ASyncSenseable"
|
|
|
|
) as mock_sense:
|
|
|
|
mock_sense.return_value.authenticate = AsyncMock(return_value=True)
|
|
|
|
mock_sense.return_value.validate_mfa = AsyncMock(return_value=True)
|
|
|
|
mock_sense.return_value.sense_access_token = "ABC"
|
|
|
|
mock_sense.return_value.sense_user_id = "123"
|
|
|
|
mock_sense.return_value.sense_monitor_id = "456"
|
2022-12-01 09:53:48 +00:00
|
|
|
mock_sense.return_value.device_id = "789"
|
|
|
|
mock_sense.return_value.refresh_token = "XYZ"
|
2022-02-21 22:05:12 +00:00
|
|
|
yield mock_sense
|
|
|
|
|
|
|
|
|
2023-02-15 17:07:40 +00:00
|
|
|
async def test_form(hass: HomeAssistant, mock_sense) -> None:
|
2020-02-25 23:37:41 +00:00
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-02-25 23:37:41 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2022-02-21 22:05:12 +00:00
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.sense.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-02-25 23:37:41 +00:00
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-02-25 23:37:41 +00:00
|
|
|
|
|
|
|
assert result2["type"] == "create_entry"
|
|
|
|
assert result2["title"] == "test-email"
|
2022-02-21 22:05:12 +00:00
|
|
|
assert result2["data"] == MOCK_CONFIG
|
2020-02-25 23:37:41 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 16:12:54 +00:00
|
|
|
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
|
2020-02-25 23:37:41 +00:00
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"sense_energy.ASyncSenseable.authenticate",
|
|
|
|
side_effect=SenseAuthenticationException,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
|
|
|
2023-02-15 17:07:40 +00:00
|
|
|
async def test_form_mfa_required(hass: HomeAssistant, mock_sense) -> None:
|
2022-02-21 22:05:12 +00:00
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_sense.return_value.authenticate.side_effect = SenseMFARequiredException
|
|
|
|
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["step_id"] == "validation"
|
|
|
|
|
|
|
|
mock_sense.return_value.validate_mfa.side_effect = None
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_CODE: "012345"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result3["type"] == "create_entry"
|
|
|
|
assert result3["title"] == "test-email"
|
|
|
|
assert result3["data"] == MOCK_CONFIG
|
|
|
|
|
|
|
|
|
2023-02-15 17:07:40 +00:00
|
|
|
async def test_form_mfa_required_wrong(hass: HomeAssistant, mock_sense) -> None:
|
2022-02-21 22:05:12 +00:00
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_sense.return_value.authenticate.side_effect = SenseMFARequiredException
|
|
|
|
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["step_id"] == "validation"
|
|
|
|
|
|
|
|
mock_sense.return_value.validate_mfa.side_effect = SenseAuthenticationException
|
|
|
|
# Try with the WRONG verification code give us the form back again
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_CODE: "000000"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result3["type"] == "form"
|
|
|
|
assert result3["errors"] == {"base": "invalid_auth"}
|
|
|
|
assert result3["step_id"] == "validation"
|
|
|
|
|
|
|
|
|
2023-02-15 17:07:40 +00:00
|
|
|
async def test_form_mfa_required_timeout(hass: HomeAssistant, mock_sense) -> None:
|
2022-02-21 22:05:12 +00:00
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_sense.return_value.authenticate.side_effect = SenseMFARequiredException
|
|
|
|
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["step_id"] == "validation"
|
|
|
|
|
|
|
|
mock_sense.return_value.validate_mfa.side_effect = SenseAPITimeoutException
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_CODE: "000000"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result3["type"] == "form"
|
|
|
|
assert result3["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2023-02-15 17:07:40 +00:00
|
|
|
async def test_form_mfa_required_exception(hass: HomeAssistant, mock_sense) -> None:
|
2022-02-21 22:05:12 +00:00
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_sense.return_value.authenticate.side_effect = SenseMFARequiredException
|
|
|
|
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["step_id"] == "validation"
|
|
|
|
|
|
|
|
mock_sense.return_value.validate_mfa.side_effect = Exception
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_CODE: "000000"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result3["type"] == "form"
|
|
|
|
assert result3["errors"] == {"base": "unknown"}
|
|
|
|
|
|
|
|
|
2023-02-08 16:12:54 +00:00
|
|
|
async def test_form_timeout(hass: HomeAssistant) -> None:
|
2020-02-25 23:37:41 +00:00
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"sense_energy.ASyncSenseable.authenticate",
|
|
|
|
side_effect=SenseAPITimeoutException,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
2021-08-23 05:58:42 +00:00
|
|
|
|
|
|
|
|
2023-02-08 16:12:54 +00:00
|
|
|
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
2022-03-22 22:14:01 +00:00
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"sense_energy.ASyncSenseable.authenticate",
|
|
|
|
side_effect=SenseAPIException,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2023-02-08 16:12:54 +00:00
|
|
|
async def test_form_unknown_exception(hass: HomeAssistant) -> None:
|
2021-08-23 05:58:42 +00:00
|
|
|
"""Test we handle unknown error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"sense_energy.ASyncSenseable.authenticate",
|
|
|
|
side_effect=Exception,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"timeout": "6", "email": "test-email", "password": "test-password"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "unknown"}
|
2022-02-21 22:05:12 +00:00
|
|
|
|
|
|
|
|
2023-02-15 17:07:40 +00:00
|
|
|
async def test_reauth_no_form(hass: HomeAssistant, mock_sense) -> None:
|
2022-02-21 22:05:12 +00:00
|
|
|
"""Test reauth where no form needed."""
|
|
|
|
|
|
|
|
# set up initially
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=MOCK_CONFIG,
|
|
|
|
unique_id="test-email",
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
with patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_reload",
|
|
|
|
return_value=True,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=MOCK_CONFIG
|
|
|
|
)
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
|
|
|
|
|
2023-02-15 17:07:40 +00:00
|
|
|
async def test_reauth_password(hass: HomeAssistant, mock_sense) -> None:
|
2022-02-21 22:05:12 +00:00
|
|
|
"""Test reauth form."""
|
|
|
|
|
|
|
|
# set up initially
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=MOCK_CONFIG,
|
|
|
|
unique_id="test-email",
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_sense.return_value.authenticate.side_effect = SenseAuthenticationException
|
|
|
|
|
|
|
|
# Reauth success without user input
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
|
|
|
|
mock_sense.return_value.authenticate.side_effect = None
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.sense.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"password": "test-password"},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result2["type"] == "abort"
|
|
|
|
assert result2["reason"] == "reauth_successful"
|