Improve fitbit authentication error handling (#106885)

This commit is contained in:
Allen Porter 2024-01-02 08:51:15 -08:00 committed by Franck Nijhof
parent 596f855eab
commit 6f18a29241
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
3 changed files with 18 additions and 5 deletions

View file

@ -69,6 +69,8 @@ class FitbitOAuth2Implementation(AuthImplementation):
) )
if err.status == HTTPStatus.UNAUTHORIZED: if err.status == HTTPStatus.UNAUTHORIZED:
raise FitbitAuthException(f"Unauthorized error: {err}") from err 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 raise FitbitApiException(f"Server error response: {err}") from err
except aiohttp.ClientError as err: except aiohttp.ClientError as err:
raise FitbitApiException(f"Client connection error: {err}") from 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]) @pytest.mark.parametrize("closing", [True, False])
async def test_token_requires_reauth( async def test_token_requires_reauth(
hass: HomeAssistant, hass: HomeAssistant,
@ -114,13 +120,14 @@ async def test_token_requires_reauth(
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
setup_credentials: None, setup_credentials: None,
server_status: HTTPStatus,
closing: bool, closing: bool,
) -> None: ) -> None:
"""Test where token is expired and the refresh attempt requires reauth.""" """Test where token is expired and the refresh attempt requires reauth."""
aioclient_mock.post( aioclient_mock.post(
OAUTH2_TOKEN, OAUTH2_TOKEN,
status=HTTPStatus.UNAUTHORIZED, status=server_status,
closing=closing, closing=closing,
) )

View file

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