Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ click-plugins==1.1.1
# via celery
click-repl==0.2.0
# via celery
commonground-api-common==2.10.7
commonground-api-common==2.11.0
# via open-api-framework
cryptography==44.0.1
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ click-repl==0.2.0
# celery
codecov==2.1.13
# via -r requirements/test-tools.in
commonground-api-common==2.10.7
commonground-api-common==2.11.0
# via
# -c requirements/base.txt
# -r requirements/base.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ codecov==2.1.13
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
commonground-api-common==2.10.7
commonground-api-common==2.11.0
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
Expand Down
2 changes: 1 addition & 1 deletion src/objecttypes/conf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"DEFAULT_VERSION": "v2", # NOT to be confused with API_VERSION - it's the major version part
"ALLOWED_VERSIONS": ("v2",),
"VERSION_PARAM": "version",
"EXCEPTION_HANDLER": "objecttypes.utils.views.exception_handler",
"EXCEPTION_HANDLER": "vng_api_common.exception_handling.exception_handler",
# test
"TEST_REQUEST_DEFAULT_FORMAT": "json",
}
Expand Down
34 changes: 15 additions & 19 deletions src/objecttypes/tests/v2/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from rest_framework import status
from rest_framework.test import APITestCase
from vng_api_common.tests import get_validation_errors

from objecttypes.core.constants import ObjectVersionStatus
from objecttypes.core.tests.factories import ObjectTypeFactory, ObjectVersionFactory
Expand All @@ -19,8 +20,8 @@ def test_patch_objecttype_with_uuid_fail(self):

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

data = response.json()
self.assertEqual(data["uuid"], ["This field can't be changed"])
error = get_validation_errors(response, "uuid")
self.assertEqual(error["reason"], "This field can't be changed")

def test_delete_objecttype_with_versions_fail(self):
object_type = ObjectTypeFactory.create()
Expand All @@ -31,12 +32,10 @@ def test_delete_objecttype_with_versions_fail(self):

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

data = response.json()
error = get_validation_errors(response, "nonFieldErrors")
self.assertEqual(
data["non_field_errors"],
[
"All related versions should be destroyed before destroying the objecttype"
],
error["reason"],
"All related versions should be destroyed before destroying the objecttype",
)


Expand All @@ -54,8 +53,10 @@ def test_create_version_with_incorrect_schema_fail(self):

response = self.client.post(url, data)

error = get_validation_errors(response, "jsonSchema")

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertTrue("jsonSchema" in response.json())
self.assertIsNotNone(error)

def test_create_version_with_incorrect_objecttype_fail(self):
url = reverse("objectversion-list", args=[uuid.uuid4()])
Expand All @@ -72,9 +73,8 @@ def test_create_version_with_incorrect_objecttype_fail(self):
response = self.client.post(url, data)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.json()["non_field_errors"], ["Objecttype url is invalid"]
)
error = get_validation_errors(response, "nonFieldErrors")
self.assertEqual(error["reason"], "Objecttype url is invalid")

def test_update_published_version_fail(self):
object_type = ObjectTypeFactory.create()
Expand All @@ -96,10 +96,8 @@ def test_update_published_version_fail(self):

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

data = response.json()
self.assertEqual(
data["non_field_errors"], ["Only draft versions can be changed"]
)
error = get_validation_errors(response, "nonFieldErrors")
self.assertEqual(error["reason"], "Only draft versions can be changed")

def test_delete_puclished_version_fail(self):
object_type = ObjectTypeFactory.create()
Expand All @@ -114,7 +112,5 @@ def test_delete_puclished_version_fail(self):

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

data = response.json()
self.assertEqual(
data["non_field_errors"], ["Only draft versions can be destroyed"]
)
error = get_validation_errors(response, "nonFieldErrors")
self.assertEqual(error["reason"], "Only draft versions can be destroyed")
1 change: 1 addition & 0 deletions src/objecttypes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
name="home",
),
path("oidc/", include("mozilla_django_oidc.urls")),
path("ref/", include("vng_api_common.urls")),
path("api/", include("objecttypes.api.urls")),
]

Expand Down
3 changes: 1 addition & 2 deletions src/objecttypes/utils/tests/test_exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from rest_framework.test import APITestCase
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.transport import Transport

from ..views import exception_handler
from vng_api_common.exception_handling import exception_handler


class InMemoryTransport(Transport):
Expand Down
47 changes: 0 additions & 47 deletions src/objecttypes/utils/views.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,13 @@
from django.utils.translation import gettext_lazy as _
from django.views.generic import RedirectView

import sentry_sdk
import structlog
from drf_spectacular.views import (
SpectacularJSONAPIView as _SpectacularJSONAPIView,
SpectacularYAMLAPIView as _SpectacularYAMLAPIView,
)
from open_api_framework.conf.utils import config
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import exception_handler as drf_exception_handler

logger = structlog.stdlib.get_logger(__name__)

DEFAULT_CODE = "invalid"
DEFAULT_DETAIL = _("Invalid input.")


def exception_handler(exc, context):
"""
Transform 5xx errors into DSO-compliant shape.
"""
response = drf_exception_handler(exc, context)
if not response:
if config("DEBUG", default=False):
return None

data = {
"code": "error",
"title": "Internal Server Error",
"status": status.HTTP_500_INTERNAL_SERVER_ERROR,
"detail": _("A server error has occurred."),
}
event = "api.uncaught_exception"

# make sure the exception still ends up in Sentry
sentry_sdk.capture_exception(exc)

response = Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR, data=data)
logger.exception(event, exc_info=exc)

return response

# exception logger event
logger.exception(
"api.handled_exception",
title=getattr(exc, "default_detail", DEFAULT_DETAIL).strip("'"),
code=getattr(exc, "default_code", DEFAULT_CODE),
status=getattr(response, "status_code", status.HTTP_400_BAD_REQUEST),
data=getattr(response, "data", {}),
exc_info=False,
)

return response


class AllowAllOriginsMixin:
def dispatch(self, request, *args, **kwargs):
Expand Down
Loading