mirror of
https://github.com/jazzband/django-axes.git
synced 2026-03-16 22:30:23 +00:00
style: apply isort and black formatting, add test and docstring for get_lockout_tier
This commit is contained in:
parent
7298e9e33d
commit
e1bb4ce15b
3 changed files with 18 additions and 9 deletions
|
|
@ -109,7 +109,7 @@ def get_cool_off_iso8601(delta: timedelta) -> str:
|
|||
|
||||
|
||||
def get_lockout_tier(failures: int) -> Optional[LockoutTier]:
|
||||
"""Return the matching ``LockoutTier`` for *failures*, or ``None``."""
|
||||
"""Return the highest ``LockoutTier`` threshold met by *failures*."""
|
||||
tiers = settings.AXES_LOCKOUT_TIERS
|
||||
if not tiers:
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ The following ``settings.py`` options are available for customizing Axes behavio
|
|||
+------------------------------------------------------+----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||||
| AXES_COOLOFF_TIME | None | If set, defines a period of inactivity after which old failed login attempts will be cleared. Can be set to a Python timedelta object, an integer, a float, a callable, or a string path to a callable which takes the request as argument. If an integer or float, will be interpreted as a number of hours: ``AXES_COOLOFF_TIME = 2`` 2 hours, ``AXES_COOLOFF_TIME = 2.0`` 2 hours, 120 minutes, ``AXES_COOLOFF_TIME = 1.7`` 1.7 hours, 102 minutes, 6120 seconds |
|
||||
+------------------------------------------------------+----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||||
| AXES_LOCKOUT_TIERS | None | A list of ``LockoutTier(failures, cooloff)`` instances that define progressive lockout durations. When set, overrides ``AXES_FAILURE_LIMIT`` and ``AXES_COOLOFF_TIME``. The lowest tier threshold becomes the effective failure limit, and each subsequent tier applies a longer cool-off. Example: ``from datetime import timedelta``, ``from axes.conf import LockoutTier``, ``AXES_LOCKOUT_TIERS = [LockoutTier(failures=3, cooloff=timedelta(minutes=15)), LockoutTier(failures=6, cooloff=timedelta(hours=2)), LockoutTier(failures=10, cooloff=timedelta(days=1))]``. With this configuration: 3 failures → 15 min lockout, 6 failures → 2 h, 10+ failures → 24 h. |
|
||||
| AXES_LOCKOUT_TIERS | None | A list of ``LockoutTier(failures, cooloff)`` instances that define progressive lockout durations. When set, overrides ``AXES_FAILURE_LIMIT`` and ``AXES_COOLOFF_TIME``. The lowest tier threshold becomes the effective failure limit, and each subsequent tier applies a longer cool-off. Example: ``from datetime import timedelta``, ``from axes.conf import LockoutTier``, ``AXES_LOCKOUT_TIERS = [LockoutTier(failures=3, cooloff=timedelta(minutes=15)), LockoutTier(failures=6, cooloff=timedelta(hours=2)), LockoutTier(failures=10, cooloff=timedelta(days=1))]``. With this configuration: 3 failures → 15 min lockout, 6 failures → 2 h, 10+ failures → 24 h. .. note:: When tiers are active, ``AXES_RESET_COOL_OFF_ON_FAILURE_DURING_LOCKOUT`` still works and will reset the *current tier's* cool-off timer. |
|
||||
+------------------------------------------------------+----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||||
| AXES_ONLY_ADMIN_SITE | False | If ``True``, lock is only enabled for admin site. Admin site is determined by checking request path against the path of ``"admin:index"`` view. If admin urls are not registered in current urlconf, all requests will not be locked. |
|
||||
+------------------------------------------------------+----------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||||
|
|
|
|||
|
|
@ -1107,7 +1107,6 @@ class AxesCleanseParamsTestCase(AxesTestCase):
|
|||
self.assertEqual("sensitive", cleansed["other_sensitive_data"])
|
||||
|
||||
|
||||
|
||||
class AxesLockoutTiersTestCase(AxesTestCase):
|
||||
SAMPLE_TIERS = [
|
||||
LockoutTier(failures=3, cooloff=timedelta(minutes=15)),
|
||||
|
|
@ -1139,6 +1138,11 @@ class AxesLockoutTiersTestCase(AxesTestCase):
|
|||
tier = get_lockout_tier(6)
|
||||
self.assertEqual(tier, LockoutTier(failures=6, cooloff=timedelta(hours=2)))
|
||||
|
||||
@override_settings(AXES_LOCKOUT_TIERS=SAMPLE_TIERS)
|
||||
def test_get_lockout_tier_seven_failures(self):
|
||||
tier = get_lockout_tier(7)
|
||||
self.assertEqual(tier, LockoutTier(failures=6, cooloff=timedelta(hours=2)))
|
||||
|
||||
@override_settings(AXES_LOCKOUT_TIERS=SAMPLE_TIERS)
|
||||
def test_get_lockout_tier_highest_tier(self):
|
||||
tier = get_lockout_tier(10)
|
||||
|
|
@ -1172,22 +1176,27 @@ class AxesLockoutTiersTestCase(AxesTestCase):
|
|||
@override_settings(AXES_LOCKOUT_TIERS=SAMPLE_TIERS)
|
||||
def test_get_failure_limit_returns_lowest_tier(self):
|
||||
from axes.helpers import get_failure_limit
|
||||
|
||||
self.assertEqual(get_failure_limit(self.request, self.credentials), 3)
|
||||
|
||||
@override_settings(AXES_LOCKOUT_TIERS=[
|
||||
LockoutTier(failures=10, cooloff=timedelta(days=1)),
|
||||
LockoutTier(failures=5, cooloff=timedelta(hours=1)),
|
||||
])
|
||||
@override_settings(
|
||||
AXES_LOCKOUT_TIERS=[
|
||||
LockoutTier(failures=10, cooloff=timedelta(days=1)),
|
||||
LockoutTier(failures=5, cooloff=timedelta(hours=1)),
|
||||
]
|
||||
)
|
||||
def test_get_failure_limit_sorts_tiers(self):
|
||||
from axes.helpers import get_failure_limit
|
||||
|
||||
self.assertEqual(get_failure_limit(self.request, self.credentials), 5)
|
||||
|
||||
# -- get_lockout_message with tiers --
|
||||
|
||||
@override_settings(AXES_LOCKOUT_TIERS=SAMPLE_TIERS, AXES_COOLOFF_TIME=None)
|
||||
def test_get_lockout_message_with_tiers(self):
|
||||
from axes.helpers import get_lockout_message
|
||||
from axes.conf import settings
|
||||
from axes.helpers import get_lockout_message
|
||||
|
||||
self.assertEqual(get_lockout_message(), settings.AXES_COOLOFF_MESSAGE)
|
||||
|
||||
# -- get_lockout_response with tiers --
|
||||
|
|
@ -1205,7 +1214,7 @@ class AxesLockoutTiersTestCase(AxesTestCase):
|
|||
response = get_lockout_response(request=self.request)
|
||||
self.assertEqual(type(response), JsonResponse)
|
||||
import json
|
||||
|
||||
data = json.loads(response.content)
|
||||
self.assertEqual(data["failure_count"], 3)
|
||||
self.assertIn("cooloff_time", data)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue