mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 02:24:51 -05:00
Aladdin Connect refactor OAuth exceptions (#175269)
This commit is contained in:
@@ -5,7 +5,12 @@ from genie_partner_sdk.client import AladdinConnectClient
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.exceptions import (
|
||||
ConfigEntryAuthFailed,
|
||||
ConfigEntryNotReady,
|
||||
OAuth2TokenRequestError,
|
||||
OAuth2TokenRequestReauthError,
|
||||
)
|
||||
from homeassistant.helpers import (
|
||||
aiohttp_client,
|
||||
config_entry_oauth2_flow,
|
||||
@@ -42,11 +47,9 @@ async def async_setup_entry(
|
||||
|
||||
try:
|
||||
await session.async_ensure_token_valid()
|
||||
except aiohttp.ClientResponseError as err:
|
||||
if 400 <= err.status < 500:
|
||||
raise ConfigEntryAuthFailed(err) from err
|
||||
raise ConfigEntryNotReady from err
|
||||
except aiohttp.ClientError as err:
|
||||
except OAuth2TokenRequestReauthError as err:
|
||||
raise ConfigEntryAuthFailed(err) from err
|
||||
except (OAuth2TokenRequestError, aiohttp.ClientError) as err:
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
client = AladdinConnectClient(
|
||||
|
||||
@@ -11,6 +11,10 @@ import pytest
|
||||
from homeassistant.components.aladdin_connect import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import (
|
||||
OAuth2TokenRequestError,
|
||||
OAuth2TokenRequestReauthError,
|
||||
)
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.config_entry_oauth2_flow import (
|
||||
ImplementationUnavailableError,
|
||||
@@ -59,25 +63,37 @@ async def test_unload_entry(
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("status", "expected_state"),
|
||||
("exc", "expected_state"),
|
||||
[
|
||||
(http.HTTPStatus.UNAUTHORIZED, ConfigEntryState.SETUP_ERROR),
|
||||
(http.HTTPStatus.INTERNAL_SERVER_ERROR, ConfigEntryState.SETUP_RETRY),
|
||||
(
|
||||
OAuth2TokenRequestReauthError(
|
||||
request_info=RequestInfo("", "POST", {}, ""),
|
||||
status=http.HTTPStatus.UNAUTHORIZED,
|
||||
domain=DOMAIN,
|
||||
),
|
||||
ConfigEntryState.SETUP_ERROR,
|
||||
),
|
||||
(
|
||||
OAuth2TokenRequestError(
|
||||
request_info=RequestInfo("", "POST", {}, ""),
|
||||
status=http.HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
domain=DOMAIN,
|
||||
),
|
||||
ConfigEntryState.SETUP_RETRY,
|
||||
),
|
||||
],
|
||||
ids=["auth_failure", "server_error"],
|
||||
)
|
||||
async def test_setup_entry_token_error(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
status: http.HTTPStatus,
|
||||
exc: OAuth2TokenRequestError,
|
||||
expected_state: ConfigEntryState,
|
||||
) -> None:
|
||||
"""Test setup entry fails when token validation fails."""
|
||||
with patch(
|
||||
"homeassistant.helpers.config_entry_oauth2_flow.OAuth2Session.async_ensure_token_valid",
|
||||
side_effect=ClientResponseError(
|
||||
RequestInfo("", "POST", {}, ""), None, status=status
|
||||
),
|
||||
side_effect=exc,
|
||||
):
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user