mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 04:20:17 +02:00
Fix custom error views
This commit is contained in:
parent
02ab064ec3
commit
51ae604efd
1
.gitignore
vendored
1
.gitignore
vendored
@ -73,3 +73,4 @@ celerybeat-schedule
|
|||||||
.flake8
|
.flake8
|
||||||
.pylintrc
|
.pylintrc
|
||||||
Makefile
|
Makefile
|
||||||
|
alliance_auth.sqlite3
|
||||||
|
48
allianceauth/tests/test_views.py
Normal file
48
allianceauth/tests/test_views.py
Normal 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)
|
@ -1,10 +1,8 @@
|
|||||||
import logging
|
import logging
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib import messages
|
from django.http import HttpResponse, HttpResponseRedirect
|
||||||
from django.http import HttpResponseRedirect
|
from django.shortcuts import render
|
||||||
from django.shortcuts import redirect, render
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.views.generic.base import View
|
from django.views.generic.base import View
|
||||||
|
|
||||||
@ -51,49 +49,50 @@ class ThemeRedirectView(View):
|
|||||||
return HttpResponseRedirect(request.GET.get("next", "/"))
|
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 = _(
|
def Generic400Redirect(request, *args, **kwargs):
|
||||||
"Internal Server Error"
|
title = _("Bad Request")
|
||||||
)
|
|
||||||
message = _(
|
message = _(
|
||||||
"Auth encountered an error processing your request, please try again. "
|
"Auth encountered an error processing your request, please try again. "
|
||||||
"If the error persists, please contact the administrators."
|
"If the error persists, please contact the administrators."
|
||||||
)
|
)
|
||||||
|
response = _build_error_response(request, title, message, 400)
|
||||||
return render(request, "allianceauth/error.html", context={"error_title": title, "error_message": message})
|
return response
|
||||||
|
|
||||||
|
|
||||||
def Generic404Redirect(request, exception): # TODO Real view
|
def Generic403Redirect(request, *args, **kwargs):
|
||||||
title = _(
|
title = _("Permission Denied")
|
||||||
"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"
|
|
||||||
)
|
|
||||||
message = _(
|
message = _(
|
||||||
"You do not have permission to access the requested page. "
|
"You do not have permission to access the requested page. "
|
||||||
"If you believe this is in error please contact the administrators."
|
"If you believe this is in error please contact the administrators."
|
||||||
)
|
)
|
||||||
|
response = _build_error_response(request, title, message, 403)
|
||||||
return render(request, "allianceauth/error.html", context={"error_title": title, "error_message": message})
|
return response
|
||||||
|
|
||||||
|
|
||||||
def Generic400Redirect(request, exception): # TODO Real view
|
def Generic404Redirect(request, *args, **kwargs):
|
||||||
title = _(
|
title = _("Page Not Found")
|
||||||
"Bad Request"
|
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 = _(
|
message = _(
|
||||||
"Auth encountered an error processing your request, please try again. "
|
"Auth encountered an error processing your request, please try again. "
|
||||||
"If the error persists, please contact the administrators."
|
"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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user