From 0ae1186554fe28cce16282b75a4fefc3c5434cf0 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 13 Oct 2021 08:34:57 -0700 Subject: [PATCH] Use gather ipv wait to remove credentials to catch exceptions (#57596) --- homeassistant/auth/__init__.py | 2 +- tests/auth/test_init.py | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/homeassistant/auth/__init__.py b/homeassistant/auth/__init__.py index f47228ee506..846e37a5d67 100644 --- a/homeassistant/auth/__init__.py +++ b/homeassistant/auth/__init__.py @@ -286,7 +286,7 @@ class AuthManager: ] if tasks: - await asyncio.wait(tasks) + await asyncio.gather(*tasks) await self._store.async_remove_user(user) diff --git a/tests/auth/test_init.py b/tests/auth/test_init.py index fa8c86536ca..ae2b1b53c12 100644 --- a/tests/auth/test_init.py +++ b/tests/auth/test_init.py @@ -17,7 +17,13 @@ from homeassistant.auth.const import MFA_SESSION_EXPIRATION from homeassistant.core import callback from homeassistant.util import dt as dt_util -from tests.common import CLIENT_ID, MockUser, ensure_auth_manager_loaded, flush_store +from tests.common import ( + CLIENT_ID, + MockUser, + async_capture_events, + ensure_auth_manager_loaded, + flush_store, +) @pytest.fixture @@ -931,14 +937,7 @@ async def test_enable_mfa_for_user(hass, hass_storage): async def test_async_remove_user(hass): """Test removing a user.""" - events = [] - - @callback - def user_removed(event): - events.append(event) - - hass.bus.async_listen("user_removed", user_removed) - + events = async_capture_events(hass, "user_removed") manager = await auth.auth_manager_from_config( hass, [ @@ -983,6 +982,18 @@ async def test_async_remove_user(hass): assert events[0].data["user_id"] == user.id +async def test_async_remove_user_fail_if_remove_credential_fails( + hass, hass_admin_user, hass_admin_credential +): + """Test removing a user.""" + await hass.auth.async_link_user(hass_admin_user, hass_admin_credential) + + with patch.object( + hass.auth, "async_remove_credentials", side_effect=ValueError + ), pytest.raises(ValueError): + await hass.auth.async_remove_user(hass_admin_user) + + async def test_new_users(mock_hass): """Test newly created users.""" manager = await auth.auth_manager_from_config(