mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-04-24 07:17:17 +00:00
feat: raise 404 in case if package is unknown for logs and patches
This commit is contained in:
parent
8635ee8953
commit
7aba67186f
@ -103,9 +103,9 @@ class LogsView(StatusViewGuard, BaseView):
|
||||
|
||||
try:
|
||||
_, status = self.service().package_get(package_base)
|
||||
logs = self.service().logs_get(package_base)
|
||||
except UnknownPackageError:
|
||||
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||
logs = self.service().logs_get(package_base)
|
||||
|
||||
response = {
|
||||
"package_base": package_base,
|
||||
|
@ -21,6 +21,7 @@ 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
|
||||
@ -75,7 +76,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": "Patch name is unknown", "schema": ErrorSchema},
|
||||
404: {"description": "Package base and/or patch name are unknown", "schema": ErrorSchema},
|
||||
500: {"description": "Internal server error", "schema": ErrorSchema},
|
||||
},
|
||||
security=[{"token": [GET_PERMISSION]}],
|
||||
@ -95,10 +96,13 @@ class PatchView(StatusViewGuard, BaseView):
|
||||
package_base = self.request.match_info["package"]
|
||||
variable = self.request.match_info["patch"]
|
||||
|
||||
patches = self.service().patches_get(package_base, variable)
|
||||
selected = next((patch for patch in patches if patch.key == variable), None)
|
||||
try:
|
||||
patches = self.service().patches_get(package_base, variable)
|
||||
except UnknownPackageError:
|
||||
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||
|
||||
selected = next((patch for patch in patches if patch.key == variable), None)
|
||||
if selected is None:
|
||||
raise HTTPNotFound
|
||||
raise HTTPNotFound(reason=f"Patch {variable} is unknown")
|
||||
|
||||
return json_response(selected.view())
|
||||
|
@ -19,8 +19,9 @@
|
||||
#
|
||||
import aiohttp_apispec # type: ignore[import-untyped]
|
||||
|
||||
from aiohttp.web import HTTPBadRequest, HTTPNoContent, Response, json_response
|
||||
from aiohttp.web import HTTPBadRequest, HTTPNoContent, HTTPNotFound, 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
|
||||
@ -49,6 +50,7 @@ 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]}],
|
||||
@ -66,7 +68,10 @@ class PatchesView(StatusViewGuard, BaseView):
|
||||
HTTPNotFound: if package base is unknown
|
||||
"""
|
||||
package_base = self.request.match_info["package"]
|
||||
patches = self.service().patches_get(package_base, None)
|
||||
try:
|
||||
patches = self.service().patches_get(package_base, None)
|
||||
except UnknownPackageError:
|
||||
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||
|
||||
response = [patch.view() for patch in patches]
|
||||
return json_response(response)
|
||||
|
@ -19,8 +19,9 @@
|
||||
#
|
||||
import aiohttp_apispec # type: ignore[import-untyped]
|
||||
|
||||
from aiohttp.web import Response, json_response
|
||||
from aiohttp.web import HTTPNotFound, Response, json_response
|
||||
|
||||
from ahriman.core.exceptions import UnknownPackageError
|
||||
from ahriman.models.user_access import UserAccess
|
||||
from ahriman.web.schemas import AuthSchema, ErrorSchema, LogSchema, PackageNameSchema, PaginationSchema
|
||||
from ahriman.web.views.base import BaseView
|
||||
@ -67,7 +68,10 @@ class LogsView(StatusViewGuard, BaseView):
|
||||
"""
|
||||
package_base = self.request.match_info["package"]
|
||||
limit, offset = self.page()
|
||||
logs = self.service().logs_get(package_base, limit, offset)
|
||||
try:
|
||||
logs = self.service().logs_get(package_base, limit, offset)
|
||||
except UnknownPackageError:
|
||||
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||
|
||||
response = [
|
||||
{
|
||||
|
@ -79,3 +79,16 @@ async def test_get_not_found(client: TestClient, package_ahriman: Package) -> No
|
||||
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
|
||||
"""
|
||||
await client.post(f"/api/v1/packages/{package_ahriman.base}",
|
||||
json={"status": BuildStatusEnum.Success.value, "package": package_ahriman.view()})
|
||||
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())
|
||||
|
@ -46,6 +46,17 @@ 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
|
||||
|
@ -96,3 +96,14 @@ async def test_get_bad_request(client: TestClient, package_ahriman: Package) ->
|
||||
response = await client.get(f"/api/v2/packages/{package_ahriman.base}/logs", params={"offset": "offset"})
|
||||
assert response.status == 400
|
||||
assert not response_schema.validate(await response.json())
|
||||
|
||||
|
||||
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(LogsView.get, code=404)
|
||||
|
||||
response = await client.get(f"/api/v2/packages/{package_ahriman.base}/logs")
|
||||
assert response.status == 404
|
||||
assert not response_schema.validate(await response.json())
|
||||
|
Loading…
Reference in New Issue
Block a user