mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-04-24 15:27: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:
|
try:
|
||||||
_, status = self.service().package_get(package_base)
|
_, status = self.service().package_get(package_base)
|
||||||
|
logs = self.service().logs_get(package_base)
|
||||||
except UnknownPackageError:
|
except UnknownPackageError:
|
||||||
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||||
logs = self.service().logs_get(package_base)
|
|
||||||
|
|
||||||
response = {
|
response = {
|
||||||
"package_base": package_base,
|
"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 aiohttp.web import HTTPNoContent, HTTPNotFound, Response, json_response
|
||||||
|
|
||||||
|
from ahriman.core.exceptions import UnknownPackageError
|
||||||
from ahriman.models.user_access import UserAccess
|
from ahriman.models.user_access import UserAccess
|
||||||
from ahriman.web.schemas import AuthSchema, ErrorSchema, PatchNameSchema, PatchSchema
|
from ahriman.web.schemas import AuthSchema, ErrorSchema, PatchNameSchema, PatchSchema
|
||||||
from ahriman.web.views.base import BaseView
|
from ahriman.web.views.base import BaseView
|
||||||
@ -75,7 +76,7 @@ class PatchView(StatusViewGuard, BaseView):
|
|||||||
200: {"description": "Success response", "schema": PatchSchema},
|
200: {"description": "Success response", "schema": PatchSchema},
|
||||||
401: {"description": "Authorization required", "schema": ErrorSchema},
|
401: {"description": "Authorization required", "schema": ErrorSchema},
|
||||||
403: {"description": "Access is forbidden", "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},
|
500: {"description": "Internal server error", "schema": ErrorSchema},
|
||||||
},
|
},
|
||||||
security=[{"token": [GET_PERMISSION]}],
|
security=[{"token": [GET_PERMISSION]}],
|
||||||
@ -95,10 +96,13 @@ class PatchView(StatusViewGuard, BaseView):
|
|||||||
package_base = self.request.match_info["package"]
|
package_base = self.request.match_info["package"]
|
||||||
variable = self.request.match_info["patch"]
|
variable = self.request.match_info["patch"]
|
||||||
|
|
||||||
|
try:
|
||||||
patches = self.service().patches_get(package_base, variable)
|
patches = self.service().patches_get(package_base, variable)
|
||||||
selected = next((patch for patch in patches if patch.key == variable), None)
|
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:
|
if selected is None:
|
||||||
raise HTTPNotFound
|
raise HTTPNotFound(reason=f"Patch {variable} is unknown")
|
||||||
|
|
||||||
return json_response(selected.view())
|
return json_response(selected.view())
|
||||||
|
@ -19,8 +19,9 @@
|
|||||||
#
|
#
|
||||||
import aiohttp_apispec # type: ignore[import-untyped]
|
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.pkgbuild_patch import PkgbuildPatch
|
||||||
from ahriman.models.user_access import UserAccess
|
from ahriman.models.user_access import UserAccess
|
||||||
from ahriman.web.schemas import AuthSchema, ErrorSchema, PackageNameSchema, PatchSchema
|
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)},
|
200: {"description": "Success response", "schema": PatchSchema(many=True)},
|
||||||
401: {"description": "Authorization required", "schema": ErrorSchema},
|
401: {"description": "Authorization required", "schema": ErrorSchema},
|
||||||
403: {"description": "Access is forbidden", "schema": ErrorSchema},
|
403: {"description": "Access is forbidden", "schema": ErrorSchema},
|
||||||
|
404: {"description": "Package base is unknown", "schema": ErrorSchema},
|
||||||
500: {"description": "Internal server error", "schema": ErrorSchema},
|
500: {"description": "Internal server error", "schema": ErrorSchema},
|
||||||
},
|
},
|
||||||
security=[{"token": [GET_PERMISSION]}],
|
security=[{"token": [GET_PERMISSION]}],
|
||||||
@ -66,7 +68,10 @@ class PatchesView(StatusViewGuard, BaseView):
|
|||||||
HTTPNotFound: if package base is unknown
|
HTTPNotFound: if package base is unknown
|
||||||
"""
|
"""
|
||||||
package_base = self.request.match_info["package"]
|
package_base = self.request.match_info["package"]
|
||||||
|
try:
|
||||||
patches = self.service().patches_get(package_base, None)
|
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]
|
response = [patch.view() for patch in patches]
|
||||||
return json_response(response)
|
return json_response(response)
|
||||||
|
@ -19,8 +19,9 @@
|
|||||||
#
|
#
|
||||||
import aiohttp_apispec # type: ignore[import-untyped]
|
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.models.user_access import UserAccess
|
||||||
from ahriman.web.schemas import AuthSchema, ErrorSchema, LogSchema, PackageNameSchema, PaginationSchema
|
from ahriman.web.schemas import AuthSchema, ErrorSchema, LogSchema, PackageNameSchema, PaginationSchema
|
||||||
from ahriman.web.views.base import BaseView
|
from ahriman.web.views.base import BaseView
|
||||||
@ -67,7 +68,10 @@ class LogsView(StatusViewGuard, BaseView):
|
|||||||
"""
|
"""
|
||||||
package_base = self.request.match_info["package"]
|
package_base = self.request.match_info["package"]
|
||||||
limit, offset = self.page()
|
limit, offset = self.page()
|
||||||
|
try:
|
||||||
logs = self.service().logs_get(package_base, limit, offset)
|
logs = self.service().logs_get(package_base, limit, offset)
|
||||||
|
except UnknownPackageError:
|
||||||
|
raise HTTPNotFound(reason=f"Package {package_base} is unknown")
|
||||||
|
|
||||||
response = [
|
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")
|
response = await client.get(f"/api/v1/packages/{package_ahriman.base}/patches/random")
|
||||||
assert response.status == 404
|
assert response.status == 404
|
||||||
assert not response_schema.validate(await response.json())
|
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()]
|
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:
|
async def test_post(client: TestClient, package_ahriman: Package) -> None:
|
||||||
"""
|
"""
|
||||||
must create patch
|
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"})
|
response = await client.get(f"/api/v2/packages/{package_ahriman.base}/logs", params={"offset": "offset"})
|
||||||
assert response.status == 400
|
assert response.status == 400
|
||||||
assert not response_schema.validate(await response.json())
|
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