Improve fitbit authentication error handling (#106885)

pull/106891/head
Allen Porter 2024-01-02 08:51:15 -08:00 committed by GitHub
parent 54ba00095b
commit 2e4c4729c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -69,6 +69,8 @@ class FitbitOAuth2Implementation(AuthImplementation):
)
if err.status == HTTPStatus.UNAUTHORIZED:
raise FitbitAuthException(f"Unauthorized error: {err}") from err
if err.status == HTTPStatus.BAD_REQUEST:
raise FitbitAuthException(f"Bad Request error: {err}") from err
raise FitbitApiException(f"Server error response: {err}") from err
except aiohttp.ClientError as err:
raise FitbitApiException(f"Client connection error: {err}") from err

View File

@ -106,7 +106,13 @@ async def test_token_refresh_success(
)
@pytest.mark.parametrize("token_expiration_time", [12345])
@pytest.mark.parametrize(
("token_expiration_time", "server_status"),
[
(12345, HTTPStatus.UNAUTHORIZED),
(12345, HTTPStatus.BAD_REQUEST),
],
)
@pytest.mark.parametrize("closing", [True, False])
async def test_token_requires_reauth(
hass: HomeAssistant,
@ -114,13 +120,14 @@ async def test_token_requires_reauth(
config_entry: MockConfigEntry,
aioclient_mock: AiohttpClientMocker,
setup_credentials: None,
server_status: HTTPStatus,
closing: bool,
) -> None:
"""Test where token is expired and the refresh attempt requires reauth."""
aioclient_mock.post(
OAUTH2_TOKEN,
status=HTTPStatus.UNAUTHORIZED,
status=server_status,
closing=closing,
)

View File

@ -599,21 +599,25 @@ async def test_settings_scope_config_entry(
@pytest.mark.parametrize(
("scopes"),
[(["heartrate"])],
("scopes", "server_status"),
[
(["heartrate"], HTTPStatus.INTERNAL_SERVER_ERROR),
(["heartrate"], HTTPStatus.BAD_REQUEST),
],
)
async def test_sensor_update_failed(
hass: HomeAssistant,
setup_credentials: None,
integration_setup: Callable[[], Awaitable[bool]],
requests_mock: Mocker,
server_status: HTTPStatus,
) -> None:
"""Test a failed sensor update when talking to the API."""
requests_mock.register_uri(
"GET",
TIMESERIES_API_URL_FORMAT.format(resource="activities/heart"),
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
status_code=server_status,
)
assert await integration_setup()