diff --git a/codeforlife/tests/api.py b/codeforlife/tests/api.py index bacb3d51..a99cbae6 100644 --- a/codeforlife/tests/api.py +++ b/codeforlife/tests/api.py @@ -37,8 +37,17 @@ class APIClient(_APIClient, t.Generic[RequestUser]): _test_case: "APITestCase[RequestUser]" - def __init__(self, enforce_csrf_checks: bool = False, **defaults): - super().__init__(enforce_csrf_checks, **defaults) + def __init__( + self, + enforce_csrf_checks: bool = False, + raise_request_exception=False, + **defaults, + ): + super().__init__( + enforce_csrf_checks, + raise_request_exception=raise_request_exception, + **defaults, + ) self.request_factory = APIRequestFactory( self.get_request_user_class(), @@ -329,7 +338,7 @@ def generic( assert status_code_assertion( status_code ), f"Unexpected status code: {status_code}." + ( - "\nValidation errors:: " + "\nValidation errors: " + json.dumps( # pylint: disable-next=no-member response.json(), # type: ignore[attr-defined] diff --git a/codeforlife/urls/__init__.py b/codeforlife/urls/__init__.py new file mode 100644 index 00000000..e02834fe --- /dev/null +++ b/codeforlife/urls/__init__.py @@ -0,0 +1,7 @@ +""" +© Ocado Group +Created on 16/09/2024 at 15:23:05(+01:00). +""" + +from .handlers import handler400, handler403, handler404, handler500 +from .patterns import UrlPatterns, get_urlpatterns diff --git a/codeforlife/urls/handlers.py b/codeforlife/urls/handlers.py new file mode 100644 index 00000000..a575eed7 --- /dev/null +++ b/codeforlife/urls/handlers.py @@ -0,0 +1,23 @@ +""" +© Ocado Group +Created on 16/09/2024 at 15:19:54(+01:00). + +Custom error handlers which override django's default behavior to render a +template. + +https://docs.djangoproject.com/en/3.2/ref/urls/#module-django.conf.urls +""" + +from django.http import ( + HttpResponseBadRequest, + HttpResponseForbidden, + HttpResponseNotFound, + HttpResponseServerError, +) + +# pylint: disable=unnecessary-lambda-assignment +handler400 = lambda request, template: HttpResponseBadRequest() +handler403 = lambda request, template: HttpResponseForbidden() +handler404 = lambda request, template: HttpResponseNotFound() +handler500 = lambda request: HttpResponseServerError() +# pylint: enable=unnecessary-lambda-assignment diff --git a/codeforlife/urls.py b/codeforlife/urls/patterns.py similarity index 96% rename from codeforlife/urls.py rename to codeforlife/urls/patterns.py index 60350643..bfcdc94c 100644 --- a/codeforlife/urls.py +++ b/codeforlife/urls/patterns.py @@ -10,8 +10,8 @@ from django.urls import URLPattern, URLResolver, include, path, re_path from rest_framework import status -from .settings import SERVICE_IS_ROOT, SERVICE_NAME -from .views import CsrfCookieView, LogoutView +from ..settings import SERVICE_IS_ROOT, SERVICE_NAME +from ..views import CsrfCookieView, LogoutView UrlPatterns = t.List[t.Union[URLResolver, URLPattern]]