mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-02-04 08:13:38 +00:00
middleware: tests: Add tests for common error middleware
Signed-off-by: James Valleroy <jvalleroy@mailbox.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
parent
557a3b2588
commit
4b90e7618f
@ -8,12 +8,14 @@ from unittest.mock import MagicMock, Mock, call, patch
|
||||
import pytest
|
||||
from django.contrib.auth.models import AnonymousUser, Group, User
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db.utils import OperationalError
|
||||
from django.http import HttpResponse
|
||||
from django.test.client import RequestFactory
|
||||
from stronghold.decorators import public
|
||||
|
||||
from plinth import app as app_module
|
||||
from plinth.middleware import AdminRequiredMiddleware, SetupMiddleware
|
||||
from plinth.middleware import (AdminRequiredMiddleware, CommonErrorMiddleware,
|
||||
SetupMiddleware)
|
||||
|
||||
setup_helper = None
|
||||
|
||||
@ -255,3 +257,44 @@ class TestAdminMiddleware:
|
||||
|
||||
response = middleware.process_view(web_request, **kwargs)
|
||||
assert response is None
|
||||
|
||||
|
||||
class TestCommonErrorMiddleware:
|
||||
"""Test cases for common error middleware."""
|
||||
|
||||
@staticmethod
|
||||
@pytest.fixture(name='middleware')
|
||||
def fixture_middleware(load_cfg):
|
||||
"""Fixture for returning middleware."""
|
||||
return CommonErrorMiddleware(True)
|
||||
|
||||
@staticmethod
|
||||
@pytest.fixture(name='web_request')
|
||||
def fixture_web_request():
|
||||
"""Fixture for returning web request."""
|
||||
web_request = RequestFactory().get('/plinth/mockapp')
|
||||
web_request.user = Mock()
|
||||
return web_request
|
||||
|
||||
@staticmethod
|
||||
@pytest.fixture(name='operational_error')
|
||||
def fixture_operational_error():
|
||||
"""Fixture for returning an OperationalError."""
|
||||
return OperationalError()
|
||||
|
||||
@staticmethod
|
||||
@pytest.fixture(name='other_error')
|
||||
def fixture_other_error():
|
||||
"""Fixture for returning a different type of error."""
|
||||
return IndexError()
|
||||
|
||||
@staticmethod
|
||||
def test_operational_error(middleware, web_request, operational_error):
|
||||
response = middleware.process_exception(web_request, operational_error)
|
||||
assert response.template_name == 'error.html'
|
||||
assert 'message' in response.context_data
|
||||
|
||||
@staticmethod
|
||||
def test_other_error(middleware, web_request, other_error):
|
||||
response = middleware.process_exception(web_request, other_error)
|
||||
assert response is None
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user