Skip to content
Snippets Groups Projects
Commit 6102891b authored by vlorentz's avatar vlorentz
Browse files

Badges: Return proper error instead of 500 status on invalid hashes

parent 8c949d58
No related branches found
No related tags found
No related merge requests found
......@@ -134,7 +134,7 @@ def swh_badge(
if parsed_object_type == ObjectType.RELEASE:
right_text = "release %s" % swh_object["name"]
left_text = "archived"
except (BadInputExc, ValidationError):
except (BadInputExc, ValidationError, ValueError):
right_text = f'invalid {object_type if object_type else "object"} id'
object_type = "error"
except NotFoundExc:
......
......@@ -5,6 +5,9 @@
from corsheaders.middleware import ACCESS_CONTROL_ALLOW_ORIGIN
from hypothesis import given
import pytest
from django.urls.exceptions import NoReverseMatch
from swh.model.hashutil import hash_to_bytes
from swh.model.swhids import ObjectType, QualifiedSWHID
......@@ -74,6 +77,7 @@ def test_badge_errors(
(ObjectType.RELEASE, invalid_sha1),
(ObjectType.REVISION, invalid_sha1),
(ObjectType.SNAPSHOT, invalid_sha1),
(ObjectType.SNAPSHOT, "123"),
):
url_args = {"object_type": object_type.name.lower(), "object_id": object_id}
url = reverse("swh-badge", url_args=url_args)
......@@ -90,6 +94,13 @@ def test_badge_errors(
)
_check_generated_badge(resp, "", "", error="invalid id")
for object_type, object_id in (
(ObjectType.SNAPSHOT, "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"),
):
url_args = {"object_type": object_type.name.lower(), "object_id": object_id}
with pytest.raises(NoReverseMatch):
reverse("swh-badge", url_args=url_args)
def test_badge_endpoints_have_cors_header(client, origin, release):
url = reverse(
......
......@@ -9,12 +9,17 @@ from swh.web.badges import swh_badge, swh_badge_swhid
urlpatterns = [
url(
r"^badge/(?P<object_type>[a-z]+)/(?P<object_id>.+)/$",
r"^badge/(?P<object_type>origin)/(?P<object_id>.+)/$",
swh_badge,
name="swh-badge",
),
url(
r"^badge/(?P<object_swhid>swh:[0-9]+:[a-z]+:[0-9a-f]+.*)/$",
r"^badge/(?P<object_type>[a-z]+)/(?P<object_id>[0-9a-f]+)/$",
swh_badge,
name="swh-badge",
),
url(
r"^badge/(?P<object_swhid>swh:[0-9]+:[a-z]+:[0-9a-f]+(;.*)?)/$",
swh_badge_swhid,
name="swh-badge-swhid",
),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment