Skip to content

Commit 3cf76ad

Browse files
adamchainzsimanto604newscred
authored andcommitted
Don't trigger axes.W003 for subclasses of AxesBackend
The [usage documentation](https://django-axes.readthedocs.io/en/latest/3_usage.html) advises to create subclass of `AxesBackend` to ignore the lack of `request` if necessary. I've done this in a project using `django-oauth-toolkit`, which doesn't pass `request` (though it should as per [this PR](django-oauth/django-oauth-toolkit#643)). This meant that the axes.W003 check was being triggered, so I've fixed it to check for subclasses of `AxesBackend` as well as the class itself.
1 parent d7d34a7 commit 3cf76ad

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changes
33
=======
44

5+
Pending
6+
-------
7+
8+
- Stop axes.W003 check from being triggered for subclasses of ``AxesBackend``.
9+
[adamchainz]
510

611
5.0.7 (2019-06-14)
712
------------------

axes/checks.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django.core.checks import Tags, Warning, register # pylint: disable=redefined-builtin
2+
from django.utils.module_loading import import_string
23

4+
from axes.backends import AxesBackend
35
from axes.conf import settings
46

57

@@ -14,7 +16,7 @@ class Messages:
1416
"You do not have 'axes.middleware.AxesMiddleware' in your settings.MIDDLEWARE."
1517
)
1618
BACKEND_INVALID = (
17-
"You do not have 'axes.backends.AxesBackend' in your settings.AUTHENTICATION_BACKENDS."
19+
"You do not have 'axes.backends.AxesBackend' or a subclass in your settings.AUTHENTICATION_BACKENDS."
1820
)
1921
SETTING_DEPRECATED = (
2022
'You have a deprecated setting {deprecated_setting} configured in your project settings'
@@ -80,7 +82,13 @@ def axes_middleware_check(app_configs, **kwargs): # pylint: disable=unused-argu
8082
def axes_backend_check(app_configs, **kwargs): # pylint: disable=unused-argument
8183
warnings = []
8284

83-
if 'axes.backends.AxesBackend' not in settings.AUTHENTICATION_BACKENDS:
85+
found = False
86+
for name in settings.AUTHENTICATION_BACKENDS:
87+
klass = import_string(name)
88+
if issubclass(klass, AxesBackend):
89+
found = True
90+
91+
if not found:
8492
warnings.append(Warning(
8593
msg=Messages.BACKEND_INVALID,
8694
hint=Hints.BACKEND_INVALID,

axes/tests/test_checks.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.core.checks import run_checks, Warning # pylint: disable=redefined-builtin
22
from django.test import override_settings, modify_settings
33

4+
from axes.backends import AxesBackend
45
from axes.checks import Messages, Hints, Codes
56
from axes.tests.base import AxesTestCase
67

@@ -58,13 +59,17 @@ def test_cache_check_warnings(self):
5859
])
5960

6061

62+
class MyBackend(AxesBackend):
63+
pass
64+
65+
6166
class BackendCheckTestCase(AxesTestCase):
6267
@modify_settings(
6368
AUTHENTICATION_BACKENDS={
6469
'remove': ['axes.backends.AxesBackend']
6570
},
6671
)
67-
def test_cache_check_warnings(self):
72+
def test_backend_missing(self):
6873
warnings = run_checks()
6974
warning = Warning(
7075
msg=Messages.BACKEND_INVALID,
@@ -76,6 +81,13 @@ def test_cache_check_warnings(self):
7681
warning,
7782
])
7883

84+
@override_settings(
85+
AUTHENTICATION_BACKENDS=[__name__ + "." + MyBackend.__name__]
86+
)
87+
def test_custom_backend(self):
88+
warnings = run_checks()
89+
self.assertEqual(warnings, [])
90+
7991

8092
class DeprecatedSettingsTestCase(AxesTestCase):
8193
def setUp(self):

0 commit comments

Comments
 (0)