Skip to content

Commit

Permalink
fix: use internal exception
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Dec 11, 2024
1 parent a316e11 commit 6efe305
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
21 changes: 5 additions & 16 deletions src/uiprotect/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import asyncio
import contextlib
import hashlib
import http
import logging
import re
import sys
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
16 changes: 6 additions & 10 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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()
Expand Down

0 comments on commit 6efe305

Please sign in to comment.