Prompt to reauth when the august password is changed or token expires (#40103)
* Prompt to reauth when the august password is changed or token expires * augment missing config flow coverage * augment test coverage * Adjust test * Update homeassistant/components/august/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * block until patch complete Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
540b925659
commit
5ea04d64f6
9 files changed
+379
-100
No files matched your search
@@ -43,12 +43,21 @@ def _mock_get_config():
|
||||
}
|
||||
|
||||
|
||||
def _mock_authenticator(auth_state):
|
||||
"""Mock an august authenticator."""
|
||||
authenticator = MagicMock()
|
||||
type(authenticator).state = PropertyMock(return_value=auth_state)
|
||||
return authenticator
|
||||
|
||||
|
||||
@patch("homeassistant.components.august.gateway.ApiAsync")
|
||||
@patch("homeassistant.components.august.gateway.AuthenticatorAsync.async_authenticate")
|
||||
async def _mock_setup_august(hass, api_instance, authenticate_mock, api_mock):
|
||||
"""Set up august integration."""
|
||||
authenticate_mock.side_effect = MagicMock(
|
||||
return_value=_mock_august_authentication("original_token", 1234)
|
||||
return_value=_mock_august_authentication(
|
||||
"original_token", 1234, AuthenticationState.AUTHENTICATED
|
||||
)
|
||||
)
|
||||
api_mock.return_value = api_instance
|
||||
assert await async_setup_component(hass, DOMAIN, _mock_get_config())
|
||||
@@ -185,11 +194,9 @@ async def _mock_setup_august_with_api_side_effects(hass, api_call_side_effects):
|
||||
return await _mock_setup_august(hass, api_instance)
|
||||
|
||||
|
||||
def _mock_august_authentication(token_text, token_timestamp):
|
||||
def _mock_august_authentication(token_text, token_timestamp, state):
|
||||
authentication = MagicMock(name="august.authentication")
|
||||
type(authentication).state = PropertyMock(
|
||||
return_value=AuthenticationState.AUTHENTICATED
|
||||
)
|
||||
type(authentication).state = PropertyMock(return_value=state)
|
||||
type(authentication).access_token = PropertyMock(return_value=token_text)
|
||||
type(authentication).access_token_expires = PropertyMock(
|
||||
return_value=token_timestamp
|
||||
|
||||
@@ -17,6 +17,7 @@ from homeassistant.components.august.exceptions import (
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_form(hass):
|
||||
@@ -84,6 +85,29 @@ async def test_form_invalid_auth(hass):
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_user_unexpected_exception(hass):
|
||||
"""Test we handle an unexpected exception."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.async_authenticate",
|
||||
side_effect=ValueError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_LOGIN_METHOD: "email",
|
||||
CONF_USERNAME: "my@email.tld",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass):
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -197,3 +221,49 @@ async def test_form_needs_validate(hass):
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_reauth(hass):
|
||||
"""Test reauthenticate."""
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_LOGIN_METHOD: "email",
|
||||
CONF_USERNAME: "my@email.tld",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_INSTALL_ID: None,
|
||||
CONF_TIMEOUT: 10,
|
||||
CONF_ACCESS_TOKEN_CACHE_FILE: ".my@email.tld.august.conf",
|
||||
},
|
||||
unique_id="my@email.tld",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "reauth"}, data=entry.data
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.async_authenticate",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.august.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.august.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_PASSWORD: "new-test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "abort"
|
||||
assert result2["reason"] == "reauth_successful"
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
@@ -1,4 +1,6 @@
|
||||
"""The gateway tests for the august platform."""
|
||||
from august.authenticator_common import AuthenticationState
|
||||
|
||||
from homeassistant.components.august.const import DOMAIN
|
||||
from homeassistant.components.august.gateway import AugustGateway
|
||||
|
||||
@@ -11,6 +13,7 @@ async def test_refresh_access_token(hass):
|
||||
await _patched_refresh_access_token(hass, "new_token", 5678)
|
||||
|
||||
|
||||
@patch("homeassistant.components.august.gateway.ApiAsync.async_get_operable_locks")
|
||||
@patch("homeassistant.components.august.gateway.AuthenticatorAsync.async_authenticate")
|
||||
@patch("homeassistant.components.august.gateway.AuthenticatorAsync.should_refresh")
|
||||
@patch(
|
||||
@@ -23,9 +26,12 @@ async def _patched_refresh_access_token(
|
||||
refresh_access_token_mock,
|
||||
should_refresh_mock,
|
||||
authenticate_mock,
|
||||
async_get_operable_locks_mock,
|
||||
):
|
||||
authenticate_mock.side_effect = MagicMock(
|
||||
return_value=_mock_august_authentication("original_token", 1234)
|
||||
return_value=_mock_august_authentication(
|
||||
"original_token", 1234, AuthenticationState.AUTHENTICATED
|
||||
)
|
||||
)
|
||||
august_gateway = AugustGateway(hass)
|
||||
mocked_config = _mock_get_config()
|
||||
@@ -38,7 +44,7 @@ async def _patched_refresh_access_token(
|
||||
|
||||
should_refresh_mock.return_value = True
|
||||
refresh_access_token_mock.return_value = _mock_august_authentication(
|
||||
new_token, new_token_expire_time
|
||||
new_token, new_token_expire_time, AuthenticationState.AUTHENTICATED
|
||||
)
|
||||
await august_gateway.async_refresh_access_token_if_needed()
|
||||
refresh_access_token_mock.assert_called()
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"""The tests for the august platform."""
|
||||
import asyncio
|
||||
|
||||
from aiohttp import ClientResponseError
|
||||
from august.authenticator_common import AuthenticationState
|
||||
from august.exceptions import AugustApiAIOHTTPError
|
||||
|
||||
from homeassistant import setup
|
||||
@@ -12,7 +14,10 @@ from homeassistant.components.august.const import (
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
|
||||
from homeassistant.config_entries import ENTRY_STATE_SETUP_RETRY
|
||||
from homeassistant.config_entries import (
|
||||
ENTRY_STATE_SETUP_ERROR,
|
||||
ENTRY_STATE_SETUP_RETRY,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
CONF_PASSWORD,
|
||||
@@ -30,6 +35,7 @@ from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.august.mocks import (
|
||||
_create_august_with_devices,
|
||||
_mock_august_authentication,
|
||||
_mock_doorsense_enabled_august_lock_detail,
|
||||
_mock_doorsense_missing_august_lock_detail,
|
||||
_mock_get_config,
|
||||
@@ -54,8 +60,8 @@ async def test_august_is_offline(hass):
|
||||
side_effect=asyncio.TimeoutError,
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert config_entry.state == ENTRY_STATE_SETUP_RETRY
|
||||
|
||||
|
||||
@@ -158,7 +164,7 @@ async def test_set_up_from_yaml(hass):
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup_august.mock_calls) == 1
|
||||
call = mock_setup_august.call_args
|
||||
args, kwargs = call
|
||||
args, _ = call
|
||||
imported_config_entry = args[1]
|
||||
# The import must use DEFAULT_AUGUST_CONFIG_FILE so they
|
||||
# do not loose their token when config is migrated
|
||||
@@ -170,3 +176,133 @@ async def test_set_up_from_yaml(hass):
|
||||
CONF_TIMEOUT: None,
|
||||
CONF_USERNAME: "mocked_username",
|
||||
}
|
||||
|
||||
|
||||
async def test_auth_fails(hass):
|
||||
"""Config entry state is ENTRY_STATE_SETUP_ERROR when auth fails."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=_mock_get_config()[DOMAIN],
|
||||
title="August august",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert hass.config_entries.flow.async_progress() == []
|
||||
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
with patch(
|
||||
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
||||
side_effect=ClientResponseError(None, None, status=401),
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
|
||||
assert flows[0]["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_bad_password(hass):
|
||||
"""Config entry state is ENTRY_STATE_SETUP_ERROR when the password has been changed."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=_mock_get_config()[DOMAIN],
|
||||
title="August august",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert hass.config_entries.flow.async_progress() == []
|
||||
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
with patch(
|
||||
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
||||
return_value=_mock_august_authentication(
|
||||
"original_token", 1234, AuthenticationState.BAD_PASSWORD
|
||||
),
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
|
||||
assert flows[0]["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_http_failure(hass):
|
||||
"""Config entry state is ENTRY_STATE_SETUP_RETRY when august is offline."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=_mock_get_config()[DOMAIN],
|
||||
title="August august",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert hass.config_entries.flow.async_progress() == []
|
||||
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
with patch(
|
||||
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
||||
side_effect=ClientResponseError(None, None, status=500),
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state == ENTRY_STATE_SETUP_RETRY
|
||||
|
||||
assert hass.config_entries.flow.async_progress() == []
|
||||
|
||||
|
||||
async def test_unknown_auth_state(hass):
|
||||
"""Config entry state is ENTRY_STATE_SETUP_ERROR when august is in an unknown auth state."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=_mock_get_config()[DOMAIN],
|
||||
title="August august",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert hass.config_entries.flow.async_progress() == []
|
||||
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
with patch(
|
||||
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
||||
return_value=_mock_august_authentication("original_token", 1234, None),
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
|
||||
assert flows[0]["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_requires_validation_state(hass):
|
||||
"""Config entry state is ENTRY_STATE_SETUP_ERROR when august requires validation."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=_mock_get_config()[DOMAIN],
|
||||
title="August august",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert hass.config_entries.flow.async_progress() == []
|
||||
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
with patch(
|
||||
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
||||
return_value=_mock_august_authentication(
|
||||
"original_token", 1234, AuthenticationState.REQUIRES_VALIDATION
|
||||
),
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
||||
|
||||
assert hass.config_entries.flow.async_progress() == []
|
||||
Reference in new issue
Block a user