django-axes/axes/tests/test_middleware.py
Aleksi Häkli fa7f35dda5
Add tests for the new components
Use mocks and test new backends, handlers and middleware
on an API call level, aiming for a 100% coverage on behaviour.

Also add tests for old decorators which are not covered
after moving the default authentication checks from them
to the authentication backends, middleware and signal handlers.

Fixes #323

Signed-off-by: Aleksi Häkli <aleksi.hakli@iki.fi>
2019-02-10 19:22:13 +02:00

28 lines
1.1 KiB
Python

from unittest.mock import patch, MagicMock
from django.http import HttpResponse
from django.test import TestCase
from axes.exceptions import AxesSignalPermissionDenied
from axes.middleware import AxesMiddleware
class MiddlewareTestCase(TestCase):
SUCCESS_RESPONSE = HttpResponse(status=200, content='Dispatched')
LOCKOUT_RESPONSE = HttpResponse(status=403, content='Locked out')
def setUp(self):
self.request = MagicMock()
self.get_response = MagicMock()
@patch('axes.middleware.get_lockout_response', return_value=LOCKOUT_RESPONSE)
def test_process_exception_axes(self, _):
exception = AxesSignalPermissionDenied()
response = AxesMiddleware(self.get_response).process_exception(self.request, exception)
self.assertEqual(response, self.LOCKOUT_RESPONSE)
@patch('axes.middleware.get_lockout_response', return_value=LOCKOUT_RESPONSE)
def test_process_exception_other(self, _):
exception = Exception()
response = AxesMiddleware(self.get_response).process_exception(self.request, exception)
self.assertEqual(response, None)