Fix custom error views

This commit is contained in:
Erik Kalkoken 2023-12-02 01:30:26 +00:00 committed by Ariel Rin
parent 02ab064ec3
commit 51ae604efd
3 changed files with 79 additions and 31 deletions

1
.gitignore vendored
View File

@ -73,3 +73,4 @@ celerybeat-schedule
.flake8
.pylintrc
Makefile
alliance_auth.sqlite3

View File

@ -0,0 +1,48 @@
from django.test import RequestFactory, TestCase
from allianceauth import views
from .auth_utils import AuthUtils
class TestCustomErrorHandlerViews(TestCase):
@classmethod
def setUpTestData(cls) -> None:
cls.user = AuthUtils.create_user("my_user")
cls.factory = RequestFactory()
def test_should_return_status_code_400(self):
# give
request = self.factory.get("/")
request.user = self.user
# when
response = views.Generic400Redirect(request)
# then
self.assertEqual(response.status_code, 400)
def test_should_return_status_code_403(self):
# give
request = self.factory.get("/")
request.user = self.user
# when
response = views.Generic403Redirect(request)
# then
self.assertEqual(response.status_code, 403)
def test_should_return_status_code_404(self):
# give
request = self.factory.get("/")
request.user = self.user
# when
response = views.Generic404Redirect(request)
# then
self.assertEqual(response.status_code, 404)
def test_should_return_status_code_500(self):
# give
request = self.factory.get("/")
request.user = self.user
# when
response = views.Generic500Redirect(request)
# then
self.assertEqual(response.status_code, 500)

View File

@ -1,10 +1,8 @@
import logging
import warnings
from django.conf import settings
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import redirect, render
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import View
@ -51,49 +49,50 @@ class ThemeRedirectView(View):
return HttpResponseRedirect(request.GET.get("next", "/"))
def Generic500Redirect(request): # TODO Real view
# TODO: error views should be renamed to a proper function name when possible
title = _(
"Internal Server Error"
)
def Generic400Redirect(request, *args, **kwargs):
title = _("Bad Request")
message = _(
"Auth encountered an error processing your request, please try again. "
"If the error persists, please contact the administrators."
)
return render(request, "allianceauth/error.html", context={"error_title": title, "error_message": message})
response = _build_error_response(request, title, message, 400)
return response
def Generic404Redirect(request, exception): # TODO Real view
title = _(
"Page Not Found"
)
message = _(
"Page does not exist. If you believe this is in error please contact the administrators. "
)
return render(request, "allianceauth/error.html", context={"error_title": title, "error_message": message})
def Generic403Redirect(request, exception): # TODO Real view
title = _(
"Permission Denied"
)
def Generic403Redirect(request, *args, **kwargs):
title = _("Permission Denied")
message = _(
"You do not have permission to access the requested page. "
"If you believe this is in error please contact the administrators."
)
return render(request, "allianceauth/error.html", context={"error_title": title, "error_message": message})
response = _build_error_response(request, title, message, 403)
return response
def Generic400Redirect(request, exception): # TODO Real view
title = _(
"Bad Request"
def Generic404Redirect(request, *args, **kwargs):
title = _("Page Not Found")
message = _(
"Page does not exist. "
"If you believe this is in error please contact the administrators. "
)
response = _build_error_response(request, title, message, 404)
return response
def Generic500Redirect(request, *args, **kwargs):
title = _("Internal Server Error")
message = _(
"Auth encountered an error processing your request, please try again. "
"If the error persists, please contact the administrators."
)
response = _build_error_response(request, title, message, 500)
return response
return render(request, "allianceauth/error.html", context={"error_title": title, "error_message": message})
def _build_error_response(request, title, message, status_code) -> HttpResponse:
context = {"error_title": title, "error_message": message}
response = render(request, "allianceauth/error.html", context)
response.status_code = status_code
return response