2019-02-10 17:08:51 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
|
|
|
|
from axes.decorators import axes_dispatch, axes_form_invalid
|
2021-01-06 21:42:25 +00:00
|
|
|
from tests.base import AxesTestCase
|
2019-02-10 17:08:51 +00:00
|
|
|
|
|
|
|
|
|
2019-02-22 23:22:11 +00:00
|
|
|
class DecoratorTestCase(AxesTestCase):
|
2019-09-28 16:27:50 +00:00
|
|
|
SUCCESS_RESPONSE = HttpResponse(status=200, content="Dispatched")
|
2023-04-24 06:44:21 +00:00
|
|
|
LOCKOUT_RESPONSE = HttpResponse(status=429, content="Locked out")
|
2019-02-10 17:08:51 +00:00
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.request = MagicMock()
|
|
|
|
|
self.cls = MagicMock(return_value=self.request)
|
|
|
|
|
self.func = MagicMock(return_value=self.SUCCESS_RESPONSE)
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
@patch("axes.handlers.proxy.AxesProxyHandler.is_allowed", return_value=False)
|
|
|
|
|
@patch("axes.decorators.get_lockout_response", return_value=LOCKOUT_RESPONSE)
|
2019-02-10 17:08:51 +00:00
|
|
|
def test_axes_dispatch_locks_out(self, _, __):
|
|
|
|
|
response = axes_dispatch(self.func)(self.request)
|
|
|
|
|
self.assertEqual(response.content, self.LOCKOUT_RESPONSE.content)
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
@patch("axes.handlers.proxy.AxesProxyHandler.is_allowed", return_value=True)
|
|
|
|
|
@patch("axes.decorators.get_lockout_response", return_value=LOCKOUT_RESPONSE)
|
2019-02-10 17:08:51 +00:00
|
|
|
def test_axes_dispatch_dispatches(self, _, __):
|
|
|
|
|
response = axes_dispatch(self.func)(self.request)
|
|
|
|
|
self.assertEqual(response.content, self.SUCCESS_RESPONSE.content)
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
@patch("axes.handlers.proxy.AxesProxyHandler.is_allowed", return_value=False)
|
|
|
|
|
@patch("axes.decorators.get_lockout_response", return_value=LOCKOUT_RESPONSE)
|
2019-02-10 17:08:51 +00:00
|
|
|
def test_axes_form_invalid_locks_out(self, _, __):
|
|
|
|
|
response = axes_form_invalid(self.func)(self.cls)
|
|
|
|
|
self.assertEqual(response.content, self.LOCKOUT_RESPONSE.content)
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
@patch("axes.handlers.proxy.AxesProxyHandler.is_allowed", return_value=True)
|
|
|
|
|
@patch("axes.decorators.get_lockout_response", return_value=LOCKOUT_RESPONSE)
|
2019-02-10 17:08:51 +00:00
|
|
|
def test_axes_form_invalid_dispatches(self, _, __):
|
|
|
|
|
response = axes_form_invalid(self.func)(self.cls)
|
|
|
|
|
self.assertEqual(response.content, self.SUCCESS_RESPONSE.content)
|