From 6efe30591c13c8f898b0a437ca2eadab6e7e0eaf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 11 Dec 2024 22:35:57 +0100 Subject: [PATCH] fix: use internal exception --- src/uiprotect/api.py | 21 +++++---------------- tests/test_api.py | 16 ++++++---------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/src/uiprotect/api.py b/src/uiprotect/api.py index ac0c1aa2..d7532cb1 100644 --- a/src/uiprotect/api.py +++ b/src/uiprotect/api.py @@ -5,7 +5,6 @@ import asyncio import contextlib import hashlib -import http import logging import re import sys @@ -24,7 +23,7 @@ import aiohttp import orjson from aiofiles import os as aos -from aiohttp import ClientResponseError, CookieJar, client_exceptions +from aiohttp import CookieJar, client_exceptions from platformdirs import user_cache_dir, user_config_dir from yarl import URL @@ -701,12 +700,6 @@ def _on_websocket_state_change(self, state: WebsocketState) -> None: _LOGGER.debug("Websocket state changed: %s", state) -ALLOWED_FAILURE_CODES_USERS_KEYRINGS = { - http.HTTPStatus.FORBIDDEN.value, - http.HTTPStatus.NOT_FOUND.value, -} - - class ProtectApiClient(BaseApiClient): """ Main UFP API Client @@ -838,17 +831,13 @@ async def update(self) -> Bootstrap: if bootstrap.nvr.version >= NFC_FINGERPRINT_SUPPORT_VERSION: try: keyrings = await self.api_request_list("keyrings") - except ClientResponseError as err: - if err.status not in ALLOWED_FAILURE_CODES_USERS_KEYRINGS: - raise - _LOGGER.debug("No access to keyrings %s, skipping", err.status) + except NotAuthorized as err: + _LOGGER.debug("No access to keyrings %s, skipping", err) keyrings = [] try: ulp_users = await self.api_request_list("ulp-users") - except ClientResponseError as err: - if err.status not in ALLOWED_FAILURE_CODES_USERS_KEYRINGS: - raise - _LOGGER.debug("No access to ulp-users %s, skipping", err.status) + except NotAuthorized as err: + _LOGGER.debug("No access to ulp-users %s, skipping", err) ulp_users = [] bootstrap.keyrings = Keyrings.from_list( cast(list[Keyring], list_from_unifi_list(self, keyrings)) diff --git a/tests/test_api.py b/tests/test_api.py index 8ea3a38f..effd614b 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -7,10 +7,9 @@ from io import BytesIO from ipaddress import IPv4Address from typing import TYPE_CHECKING -from unittest.mock import AsyncMock, Mock, patch +from unittest.mock import AsyncMock, patch import pytest -from aiohttp import ClientResponseError from PIL import Image from tests.conftest import ( @@ -40,7 +39,7 @@ create_from_unifi_dict, ) from uiprotect.data.types import Version, VideoMode -from uiprotect.exceptions import BadRequest, NvrError +from uiprotect.exceptions import BadRequest, NotAuthorized, NvrError from uiprotect.utils import to_js_time from .common import assert_equal_dump @@ -379,11 +378,8 @@ async def test_force_update_with_nfc_fingerprint_version( assert len(protect_client.bootstrap.ulp_users) -@pytest.mark.parametrize("status_code", [404, 403]) @pytest.mark.asyncio() -async def test_force_update_no_user_keyring_access( - protect_client: ProtectApiClient, status_code: int -): +async def test_force_update_no_user_keyring_access(protect_client: ProtectApiClient): protect_client._bootstrap = None await protect_client.update() @@ -400,7 +396,7 @@ async def test_force_update_no_user_keyring_access( ) as get_bootstrap_mock, patch( "uiprotect.api.ProtectApiClient.api_request_list", - side_effect=ClientResponseError(Mock(), (), code=status_code), + side_effect=NotAuthorized, ) as api_request_list_mock, ): await protect_client.update() @@ -427,7 +423,7 @@ async def test_force_update_user_keyring_internal_error( assert protect_client.bootstrap protect_client._bootstrap = None with ( - pytest.raises(ClientResponseError), + pytest.raises(BadRequest), patch( "uiprotect.api.ProtectApiClient.get_bootstrap", return_value=AsyncMock( @@ -436,7 +432,7 @@ async def test_force_update_user_keyring_internal_error( ), patch( "uiprotect.api.ProtectApiClient.api_request_list", - side_effect=ClientResponseError(Mock(), (), code=500), + side_effect=BadRequest, ), ): await protect_client.update()