mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Remove dead code and increase test coverage for google config flow (#128690)
This commit is contained in:
parent
22491afa58
commit
5816342bed
@ -175,7 +175,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
except aiohttp.ClientError as err:
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
if not async_entry_has_scopes(hass, entry):
|
||||
if not async_entry_has_scopes(entry):
|
||||
raise ConfigEntryAuthFailed(
|
||||
"Required scopes are not available, reauth required"
|
||||
)
|
||||
@ -198,7 +198,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
hass.config_entries.async_update_entry(entry, unique_id=primary_calendar.id)
|
||||
|
||||
# Only expose the add event service if we have the correct permissions
|
||||
if get_feature_access(hass, entry) is FeatureAccess.read_write:
|
||||
if get_feature_access(entry) is FeatureAccess.read_write:
|
||||
await async_setup_add_event_service(hass, calendar_service)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
@ -208,9 +208,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def async_entry_has_scopes(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
def async_entry_has_scopes(entry: ConfigEntry) -> bool:
|
||||
"""Verify that the config entry desired scope is present in the oauth token."""
|
||||
access = get_feature_access(hass, entry)
|
||||
access = get_feature_access(entry)
|
||||
token_scopes = entry.data.get("token", {}).get("scope", [])
|
||||
return access.scope in token_scopes
|
||||
|
||||
@ -224,7 +224,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
"""Reload config entry if the access options change."""
|
||||
if not async_entry_has_scopes(hass, entry):
|
||||
if not async_entry_has_scopes(entry):
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
|
||||
|
||||
|
@ -26,13 +26,7 @@ from homeassistant.helpers.event import (
|
||||
)
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import (
|
||||
CONF_CALENDAR_ACCESS,
|
||||
DATA_CONFIG,
|
||||
DEFAULT_FEATURE_ACCESS,
|
||||
DOMAIN,
|
||||
FeatureAccess,
|
||||
)
|
||||
from .const import CONF_CALENDAR_ACCESS, DEFAULT_FEATURE_ACCESS, FeatureAccess
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -161,27 +155,11 @@ class DeviceFlow:
|
||||
self._listener()
|
||||
|
||||
|
||||
def get_feature_access(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry | None = None
|
||||
) -> FeatureAccess:
|
||||
def get_feature_access(config_entry: ConfigEntry) -> FeatureAccess:
|
||||
"""Return the desired calendar feature access."""
|
||||
if (
|
||||
config_entry
|
||||
and config_entry.options
|
||||
and CONF_CALENDAR_ACCESS in config_entry.options
|
||||
):
|
||||
if config_entry.options and CONF_CALENDAR_ACCESS in config_entry.options:
|
||||
return FeatureAccess[config_entry.options[CONF_CALENDAR_ACCESS]]
|
||||
|
||||
# This may be called during config entry setup without integration setup running when there
|
||||
# is no google entry in configuration.yaml
|
||||
return cast(
|
||||
FeatureAccess,
|
||||
(
|
||||
hass.data.get(DOMAIN, {})
|
||||
.get(DATA_CONFIG, {})
|
||||
.get(CONF_CALENDAR_ACCESS, DEFAULT_FEATURE_ACCESS)
|
||||
),
|
||||
)
|
||||
return DEFAULT_FEATURE_ACCESS
|
||||
|
||||
|
||||
async def async_create_device_flow(
|
||||
|
@ -132,7 +132,7 @@ def _get_entity_descriptions(
|
||||
)
|
||||
read_only = not (
|
||||
calendar_item.access_role.is_writer
|
||||
and get_feature_access(hass, config_entry) is FeatureAccess.read_write
|
||||
and get_feature_access(config_entry) is FeatureAccess.read_write
|
||||
)
|
||||
# Prefer calendar sync down of resources when possible. However,
|
||||
# sync does not work for search. Also free-busy calendars denormalize
|
||||
@ -304,7 +304,7 @@ async def async_setup_entry(
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
if (
|
||||
any(calendar_item.access_role.is_writer for calendar_item in result.items)
|
||||
and get_feature_access(hass, config_entry) is FeatureAccess.read_write
|
||||
and get_feature_access(config_entry) is FeatureAccess.read_write
|
||||
):
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_CREATE_EVENT,
|
||||
|
@ -24,7 +24,6 @@ from .api import (
|
||||
InvalidCredential,
|
||||
OAuthError,
|
||||
async_create_device_flow,
|
||||
get_feature_access,
|
||||
)
|
||||
from .const import (
|
||||
CONF_CALENDAR_ACCESS,
|
||||
@ -117,7 +116,7 @@ class OAuth2FlowHandler(
|
||||
self.flow_impl,
|
||||
)
|
||||
return self.async_abort(reason="oauth_error")
|
||||
calendar_access = get_feature_access(self.hass)
|
||||
calendar_access = DEFAULT_FEATURE_ACCESS
|
||||
if self._reauth_config_entry and self._reauth_config_entry.options:
|
||||
calendar_access = FeatureAccess[
|
||||
self._reauth_config_entry.options[CONF_CALENDAR_ACCESS]
|
||||
@ -214,7 +213,7 @@ class OAuth2FlowHandler(
|
||||
title=primary_calendar.id,
|
||||
data=data,
|
||||
options={
|
||||
CONF_CALENDAR_ACCESS: get_feature_access(self.hass).name,
|
||||
CONF_CALENDAR_ACCESS: DEFAULT_FEATURE_ACCESS.name,
|
||||
},
|
||||
)
|
||||
|
||||
|
@ -26,9 +26,11 @@ from homeassistant.components.application_credentials import (
|
||||
async_import_client_credential,
|
||||
)
|
||||
from homeassistant.components.google.const import (
|
||||
CONF_CALENDAR_ACCESS,
|
||||
CONF_CREDENTIAL_TYPE,
|
||||
DOMAIN,
|
||||
CredentialType,
|
||||
FeatureAccess,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
@ -474,10 +476,27 @@ async def test_wrong_configuration(
|
||||
assert result.get("reason") == "oauth_error"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("options"),
|
||||
[
|
||||
({}),
|
||||
(
|
||||
{
|
||||
CONF_CALENDAR_ACCESS: FeatureAccess.read_write.name,
|
||||
}
|
||||
),
|
||||
(
|
||||
{
|
||||
CONF_CALENDAR_ACCESS: FeatureAccess.read_only.name,
|
||||
}
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_reauth_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_code_flow: Mock,
|
||||
mock_exchange: Mock,
|
||||
options: dict[str, Any] | None,
|
||||
) -> None:
|
||||
"""Test reauth of an existing config entry."""
|
||||
config_entry = MockConfigEntry(
|
||||
@ -486,6 +505,7 @@ async def test_reauth_flow(
|
||||
"auth_implementation": DOMAIN,
|
||||
"token": {"access_token": "OLD_ACCESS_TOKEN"},
|
||||
},
|
||||
options=options,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
await async_import_client_credential(
|
||||
@ -540,6 +560,8 @@ async def test_reauth_flow(
|
||||
},
|
||||
"credential_type": "device_auth",
|
||||
}
|
||||
# Options are preserved during reauth
|
||||
assert entries[0].options == options
|
||||
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user