Add debug logging for failed OAuth token refreshes to help users diagnose (#44637)

This commit is contained in:
Allen Porter 2020-12-30 01:03:27 -08:00 committed by GitHub
parent ee194b9411
commit eb07282e9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View File

@ -191,6 +191,13 @@ class LocalOAuth2Implementation(AbstractOAuth2Implementation):
data["client_secret"] = self.client_secret
resp = await session.post(self.token_url, data=data)
if resp.status >= 400 and _LOGGER.isEnabledFor(logging.DEBUG):
body = await resp.text()
_LOGGER.debug(
"Token request failed with status=%s, body=%s",
resp.status,
body,
)
resp.raise_for_status()
return cast(dict, await resp.json())

View File

@ -3,6 +3,7 @@ import asyncio
import logging
import time
import aiohttp
import pytest
from homeassistant import config_entries, data_entry_flow, setup
@ -546,3 +547,32 @@ async def test_implementation_provider(hass, local_impl):
assert await config_entry_oauth2_flow.async_get_implementations(
hass, mock_domain_with_impl
) == {TEST_DOMAIN: local_impl, "cloud": provider_source[mock_domain_with_impl]}
async def test_oauth_session_refresh_failure(
hass, flow_handler, local_impl, aioclient_mock
):
"""Test the OAuth2 session helper when no refresh is needed."""
flow_handler.async_register_implementation(hass, local_impl)
aioclient_mock.post(TOKEN_URL, status=400)
config_entry = MockConfigEntry(
domain=TEST_DOMAIN,
data={
"auth_implementation": TEST_DOMAIN,
"token": {
"refresh_token": REFRESH_TOKEN,
"access_token": ACCESS_TOKEN_1,
# Already expired, requires a refresh
"expires_in": -500,
"expires_at": time.time() - 500,
"token_type": "bearer",
"random_other_data": "should_stay",
},
},
)
session = config_entry_oauth2_flow.OAuth2Session(hass, config_entry, local_impl)
with pytest.raises(aiohttp.client_exceptions.ClientResponseError):
await session.async_request("post", "https://example.com")