From 91598951c5ceffb67fdc5cccf6df1b548c0d5188 Mon Sep 17 00:00:00 2001 From: Evgenii Alekseev Date: Mon, 8 Jan 2024 14:30:57 +0200 Subject: [PATCH] fix: do not raise 404 in case of unknown package on patches endpoints Previous improvements raise 404 error in case if no packages were found for patches endpoints. However, in case of multirepo setup this feature doesn't work properly because package can be located in any other repository different from default --- src/ahriman/core/status/watcher.py | 3 ++- src/ahriman/web/views/v1/status/patch.py | 10 +++------- src/ahriman/web/views/v1/status/patches.py | 12 ++---------- tests/ahriman/core/status/test_watcher.py | 8 -------- .../web/views/v1/status/test_view_v1_status_patch.py | 11 ----------- .../views/v1/status/test_view_v1_status_patches.py | 11 ----------- 6 files changed, 7 insertions(+), 48 deletions(-) diff --git a/src/ahriman/core/status/watcher.py b/src/ahriman/core/status/watcher.py index dd304a9e..7b36dae8 100644 --- a/src/ahriman/core/status/watcher.py +++ b/src/ahriman/core/status/watcher.py @@ -190,7 +190,8 @@ class Watcher(LazyLogging): Returns: list[PkgbuildPatch]: list of patches which are stored for the package """ - self.package_get(package_base) + # patches are package base based, we don't know (and don't differentiate) to which package does them belong + # so here we skip checking if package exists or not variables = [variable] if variable is not None else None return self.database.patches_list(package_base, variables).get(package_base, []) diff --git a/src/ahriman/web/views/v1/status/patch.py b/src/ahriman/web/views/v1/status/patch.py index 76cc4281..bb72fc2e 100644 --- a/src/ahriman/web/views/v1/status/patch.py +++ b/src/ahriman/web/views/v1/status/patch.py @@ -21,7 +21,6 @@ import aiohttp_apispec # type: ignore[import-untyped] from aiohttp.web import HTTPNoContent, HTTPNotFound, Response, json_response -from ahriman.core.exceptions import UnknownPackageError from ahriman.models.user_access import UserAccess from ahriman.web.schemas import AuthSchema, ErrorSchema, PatchNameSchema, PatchSchema from ahriman.web.views.base import BaseView @@ -76,7 +75,7 @@ class PatchView(StatusViewGuard, BaseView): 200: {"description": "Success response", "schema": PatchSchema}, 401: {"description": "Authorization required", "schema": ErrorSchema}, 403: {"description": "Access is forbidden", "schema": ErrorSchema}, - 404: {"description": "Package base and/or patch name are unknown", "schema": ErrorSchema}, + 404: {"description": "Patch name is unknown", "schema": ErrorSchema}, 500: {"description": "Internal server error", "schema": ErrorSchema}, }, security=[{"token": [GET_PERMISSION]}], @@ -91,15 +90,12 @@ class PatchView(StatusViewGuard, BaseView): Response: 200 with package patch on success Raises: - HTTPNotFound: if package base is unknown + HTTPNotFound: if package patch is unknown """ package_base = self.request.match_info["package"] variable = self.request.match_info["patch"] - try: - patches = self.service().patches_get(package_base, variable) - except UnknownPackageError: - raise HTTPNotFound(reason=f"Package {package_base} is unknown") + patches = self.service().patches_get(package_base, variable) selected = next((patch for patch in patches if patch.key == variable), None) if selected is None: diff --git a/src/ahriman/web/views/v1/status/patches.py b/src/ahriman/web/views/v1/status/patches.py index e4582c1c..54dc3760 100644 --- a/src/ahriman/web/views/v1/status/patches.py +++ b/src/ahriman/web/views/v1/status/patches.py @@ -19,9 +19,8 @@ # import aiohttp_apispec # type: ignore[import-untyped] -from aiohttp.web import HTTPBadRequest, HTTPNoContent, HTTPNotFound, Response, json_response +from aiohttp.web import HTTPBadRequest, HTTPNoContent, Response, json_response -from ahriman.core.exceptions import UnknownPackageError from ahriman.models.pkgbuild_patch import PkgbuildPatch from ahriman.models.user_access import UserAccess from ahriman.web.schemas import AuthSchema, ErrorSchema, PackageNameSchema, PatchSchema @@ -50,7 +49,6 @@ class PatchesView(StatusViewGuard, BaseView): 200: {"description": "Success response", "schema": PatchSchema(many=True)}, 401: {"description": "Authorization required", "schema": ErrorSchema}, 403: {"description": "Access is forbidden", "schema": ErrorSchema}, - 404: {"description": "Package base is unknown", "schema": ErrorSchema}, 500: {"description": "Internal server error", "schema": ErrorSchema}, }, security=[{"token": [GET_PERMISSION]}], @@ -63,15 +61,9 @@ class PatchesView(StatusViewGuard, BaseView): Returns: Response: 200 with package patches on success - - Raises: - HTTPNotFound: if package base is unknown """ package_base = self.request.match_info["package"] - try: - patches = self.service().patches_get(package_base, None) - except UnknownPackageError: - raise HTTPNotFound(reason=f"Package {package_base} is unknown") + patches = self.service().patches_get(package_base, None) response = [patch.view() for patch in patches] return json_response(response) diff --git a/tests/ahriman/core/status/test_watcher.py b/tests/ahriman/core/status/test_watcher.py index 288b8609..9962e1dc 100644 --- a/tests/ahriman/core/status/test_watcher.py +++ b/tests/ahriman/core/status/test_watcher.py @@ -212,14 +212,6 @@ def test_patches_get(watcher: Watcher, package_ahriman: Package, mocker: MockerF ]) -def test_patches_get_failed(watcher: Watcher, package_ahriman: Package) -> None: - """ - must raise UnknownPackageError on patches in case of unknown package - """ - with pytest.raises(UnknownPackageError): - watcher.patches_get(package_ahriman.base, None) - - def test_patches_remove(watcher: Watcher, package_ahriman: Package, mocker: MockerFixture) -> None: """ must remove patches for the package diff --git a/tests/ahriman/web/views/v1/status/test_view_v1_status_patch.py b/tests/ahriman/web/views/v1/status/test_view_v1_status_patch.py index 31d77dbf..147b4b28 100644 --- a/tests/ahriman/web/views/v1/status/test_view_v1_status_patch.py +++ b/tests/ahriman/web/views/v1/status/test_view_v1_status_patch.py @@ -70,17 +70,6 @@ async def test_get(client: TestClient, package_ahriman: Package) -> None: assert patches == patch.view() -async def test_get_not_found(client: TestClient, package_ahriman: Package) -> None: - """ - must return not found for missing package - """ - response_schema = pytest.helpers.schema_response(PatchView.get, code=404) - - response = await client.get(f"/api/v1/packages/{package_ahriman.base}/patches/random") - assert response.status == 404 - assert not response_schema.validate(await response.json()) - - async def test_get_patch_not_found(client: TestClient, package_ahriman: Package) -> None: """ must return not found for missing patch diff --git a/tests/ahriman/web/views/v1/status/test_view_v1_status_patches.py b/tests/ahriman/web/views/v1/status/test_view_v1_status_patches.py index 7a57ab3f..b1279c71 100644 --- a/tests/ahriman/web/views/v1/status/test_view_v1_status_patches.py +++ b/tests/ahriman/web/views/v1/status/test_view_v1_status_patches.py @@ -46,17 +46,6 @@ async def test_get(client: TestClient, package_ahriman: Package) -> None: assert patches == [patch.view()] -async def test_get_not_found(client: TestClient, package_ahriman: Package) -> None: - """ - must return not found for missing package - """ - response_schema = pytest.helpers.schema_response(PatchesView.get, code=404) - - response = await client.get(f"/api/v1/packages/{package_ahriman.base}/patches") - assert response.status == 404 - assert not response_schema.validate(await response.json()) - - async def test_post(client: TestClient, package_ahriman: Package) -> None: """ must create patch