Skip to content

Commit

Permalink
Add checks for django-axes configuration flags
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksihakli committed Apr 30, 2019
1 parent b10a0af commit 8634e7c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
51 changes: 47 additions & 4 deletions axes/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,34 @@

class Messages:
CACHE_INVALID = 'invalid cache configuration for settings.AXES_CACHE'
MIDDLEWARE_INVALID = 'axes.middleware.AxesMiddleware not in settings.MIDDLEWARE'
BACKEND_INVALID = 'axes.backends.AxesBackend not in settings.AUTHENTICATION_BACKENDS'


class Hints:
CACHE_INVALID = (
'django-axes does not work properly with LocMemCache as the cache backend'
' please add e.g. a DummyCache backend and configure it with settings.AXES_CACHE'
'django-axes does not work properly with LocMemCache as the cache backend.'
' Please check the django-axes documentation and reconfigure settings.AXES_CACHE.'
)
MIDDLEWARE_INVALID = (
'django-axes does not work properly without axes.middleware.AxesMiddleware in settings.MIDDLEWARE.'
' Please check the django-axes documentation and reconfigure settings.MIDDLEWARE.'
)
BACKEND_INVALID = (
'django-axes does not work properly without axes.backends.AxesBackend in settings.AUTHENTICATION_BACKENDS.'
' Please check the django-axes documentation and reconfigure settings.AUTHENTICATION_BACKENDS.'
' Please note that the backend name was changed from AxesModelBackend to AxesBackend in django-axes version 5.'
)


class Codes:
CACHE_INVALID = 'axes.E001'
MIDDLEWARE_INVALID = 'axes.E002'
BACKEND_INVALID = 'axes.E003'


@register(Tags.caches)
def axes_cache_backend_check(app_configs, **kwargs): # pylint: disable=unused-argument
@register(Tags.compatibility, Tags.caches)
def axes_cache_check(app_configs, **kwargs): # pylint: disable=unused-argument
axes_handler = getattr(settings, 'AXES_HANDLER', '')

axes_cache_key = getattr(settings, 'AXES_CACHE', 'default')
Expand All @@ -44,3 +57,33 @@ def axes_cache_backend_check(app_configs, **kwargs): # pylint: disable=unused-a
))

return errors


@register(Tags.compatibility)
def axes_middleware_check(app_configs, **kwargs): # pylint: disable=unused-argument
errors = []

if 'axes.middleware.AxesMiddleware' not in settings.MIDDLEWARE:
errors.append(Error(
msg=Messages.MIDDLEWARE_INVALID,
hint=Hints.MIDDLEWARE_INVALID,
obj=settings.MIDDLEWARE,
id=Codes.MIDDLEWARE_INVALID,
))

return errors


@register(Tags.compatibility)
def axes_backend_check(app_configs, **kwargs): # pylint: disable=unused-argument
errors = []

if 'axes.backends.AxesBackend' not in settings.AUTHENTICATION_BACKENDS:
errors.append(Error(
msg=Messages.BACKEND_INVALID,
hint=Hints.BACKEND_INVALID,
obj=settings.AUTHENTICATION_BACKENDS,
id=Codes.BACKEND_INVALID,
))

return errors
38 changes: 37 additions & 1 deletion axes/tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.checks import run_checks, Error
from django.test import override_settings
from django.test import override_settings, modify_settings

from axes.checks import Messages, Hints, Codes
from axes.conf import settings
Expand Down Expand Up @@ -37,3 +37,39 @@ def test_cache_check_errors(self):
def test_cache_check_does_not_produce_check_errors_with_database_handler(self):
errors = run_checks()
self.assertEqual([], errors)


class MiddlewareCheckTestCase(AxesTestCase):
@modify_settings(
MIDDLEWARE={
'remove': ['axes.middleware.AxesMiddleware']
},
)
def test_cache_check_errors(self):
errors = run_checks()
error = Error(
msg=Messages.MIDDLEWARE_INVALID,
hint=Hints.MIDDLEWARE_INVALID,
obj=settings.MIDDLEWARE,
id=Codes.MIDDLEWARE_INVALID,
)

self.assertEqual([error], errors)


class BackendCheckTestCase(AxesTestCase):
@modify_settings(
AUTHENTICATION_BACKENDS={
'remove': ['axes.backends.AxesBackend']
},
)
def test_cache_check_errors(self):
errors = run_checks()
error = Error(
msg=Messages.BACKEND_INVALID,
hint=Hints.BACKEND_INVALID,
obj=settings.AUTHENTICATION_BACKENDS,
id=Codes.BACKEND_INVALID,
)

self.assertEqual([error], errors)

0 comments on commit 8634e7c

Please sign in to comment.