mirror of
https://github.com/jazzband/django-axes.git
synced 2026-03-16 22:30:23 +00:00
Use Django checks for cache configuration
Fixes #383 Signed-off-by: Aleksi Häkli <aleksi.hakli@iki.fi>
This commit is contained in:
parent
6a05d5318c
commit
9c328713e7
4 changed files with 110 additions and 10 deletions
10
axes/apps.py
10
axes/apps.py
|
|
@ -7,16 +7,6 @@ class AppConfig(apps.AppConfig):
|
|||
name = 'axes'
|
||||
|
||||
def ready(self):
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
if settings.CACHES[getattr(settings, 'AXES_CACHE', 'default')]['BACKEND'] == \
|
||||
'django.core.cache.backends.locmem.LocMemCache':
|
||||
raise ImproperlyConfigured(
|
||||
'django-axes does not work properly with LocMemCache as the default cache backend'
|
||||
' please add e.g. a DummyCache backend for axes and configure it with AXES_CACHE'
|
||||
)
|
||||
|
||||
from django.contrib.auth.views import LoginView
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
|
|
|
|||
56
axes/checks.py
Normal file
56
axes/checks.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.core.checks import Error, Tags, register
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Messages:
|
||||
CACHE_MISSING = 'missing cache configuration for AXES_CACHE'
|
||||
CACHE_INVALID = 'invalid cache configuration for settings.AXES_CACHE'
|
||||
|
||||
|
||||
class Hints:
|
||||
CACHE_MISSING = (
|
||||
'django-axes needs to have a cache configured with settings.AXES_CACHE'
|
||||
)
|
||||
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'
|
||||
)
|
||||
|
||||
|
||||
class Codes:
|
||||
CACHE_MISSING = 'axes.E001'
|
||||
CACHE_INVALID = 'axes.E002'
|
||||
|
||||
|
||||
@register(Tags.caches)
|
||||
def axes_cache_backend_check(app_configs, **kwargs): # pylint: disable=unused-argument
|
||||
errors = []
|
||||
|
||||
axes_cache_key = getattr(settings, 'AXES_CACHE', 'default')
|
||||
axes_cache_config = settings.CACHES.get(axes_cache_key, {})
|
||||
axes_cache_backend = axes_cache_config.get('BACKEND', '')
|
||||
|
||||
axes_cache_incompatible_backends = [
|
||||
'django.core.cache.backends.locmem.LocMemCache',
|
||||
]
|
||||
|
||||
if not axes_cache_config:
|
||||
errors.append(Error(
|
||||
msg=Messages.CACHE_MISSING,
|
||||
hint=Hints.CACHE_MISSING,
|
||||
obj=settings.CACHES,
|
||||
id=Codes.CACHE_MISSING,
|
||||
))
|
||||
|
||||
if axes_cache_backend in axes_cache_incompatible_backends:
|
||||
errors.append(Error(
|
||||
msg=Messages.CACHE_INVALID,
|
||||
hint=Hints.CACHE_INVALID,
|
||||
obj=settings.CACHES,
|
||||
id=Codes.CACHE_INVALID,
|
||||
))
|
||||
|
||||
return errors
|
||||
42
axes/tests/test_checks.py
Normal file
42
axes/tests/test_checks.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.core.checks import run_checks, Error
|
||||
from django.conf import settings
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from axes.checks import Messages, Hints, Codes
|
||||
|
||||
|
||||
class CacheCheckTestCase(TestCase):
|
||||
@override_settings(
|
||||
AXES_CACHE='nonexistent',
|
||||
)
|
||||
def test_cache_missing_produces_check_error(self):
|
||||
errors = run_checks()
|
||||
error = Error(
|
||||
msg=Messages.CACHE_MISSING,
|
||||
hint=Hints.CACHE_MISSING,
|
||||
obj=settings.CACHES,
|
||||
id=Codes.CACHE_MISSING,
|
||||
)
|
||||
|
||||
self.assertIn(error, errors)
|
||||
|
||||
@override_settings(
|
||||
AXES_CACHE='axes',
|
||||
CACHES={
|
||||
'axes': {
|
||||
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
||||
},
|
||||
},
|
||||
)
|
||||
def test_cache_misconfiguration_produces_check_error(self):
|
||||
errors = run_checks()
|
||||
error = Error(
|
||||
msg=Messages.CACHE_INVALID,
|
||||
hint=Hints.CACHE_INVALID,
|
||||
obj=settings.CACHES,
|
||||
id=Codes.CACHE_INVALID,
|
||||
)
|
||||
|
||||
self.assertIn(error, errors)
|
||||
|
|
@ -27,6 +27,18 @@ Add ``axes.backends.AxesModelBackend`` to the top of ``AUTHENTICATION_BACKENDS``
|
|||
|
||||
Run ``python manage.py migrate`` to sync the database.
|
||||
|
||||
Running checks
|
||||
--------------
|
||||
|
||||
Use the ``python manage.py check`` command to verify the correct configuration in both
|
||||
development and production environments. It is probably best to use this step as part
|
||||
of your regular CI workflows to verify that your project is not misconfigured.
|
||||
|
||||
django-axes uses the checks to verify your cache configuration to see that your caches
|
||||
should be functional with the configuration axes. Many people have different configurations
|
||||
for their development and production environments.
|
||||
|
||||
|
||||
Known configuration problems
|
||||
----------------------------
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue