Fix bug in missing cache backends

Signed-off-by: Aleksi Häkli <aleksi.hakli@iki.fi>
This commit is contained in:
Aleksi Häkli 2019-02-25 16:45:56 +02:00
parent ac9950cdf3
commit 1ab8d89869
No known key found for this signature in database
GPG key ID: 3E7146964D726BBE
5 changed files with 18 additions and 41 deletions

View file

@ -4,14 +4,10 @@ from axes.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'
@ -19,8 +15,7 @@ class Hints:
class Codes:
CACHE_MISSING = 'axes.E001'
CACHE_INVALID = 'axes.E002'
CACHE_INVALID = 'axeslE001'
@register(Tags.caches)
@ -35,20 +30,15 @@ def axes_cache_backend_check(app_configs, **kwargs): # pylint: disable=unused-a
'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,
))
axes_handler = getattr(settings, 'AXES_HANDLER', '')
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,
))
if axes_handler == 'axes.handlers.cache.AxesCacheHandler':
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

View file

@ -5,7 +5,8 @@ from axes.exceptions import AxesSignalPermissionDenied
from axes.handlers.base import AxesBaseHandler
from axes.signals import user_locked_out
from axes.utils import (
get_axes_cache,
get_cache,
get_cache_timeout,
get_client_cache_key,
get_client_ip_address,
get_client_path_info,
@ -13,7 +14,6 @@ from axes.utils import (
get_client_username,
get_client_user_agent,
get_credentials,
get_cool_off,
)
log = getLogger(settings.AXES_LOGGER)
@ -25,8 +25,8 @@ class AxesCacheHandler(AxesBaseHandler): # pylint: disable=too-many-locals
"""
def __init__(self):
self.cache = get_axes_cache()
self.cache_timeout = get_cool_off().total_seconds()
self.cache = get_cache()
self.cache_timeout = get_cache_timeout()
def get_failures(self, request, credentials=None, attempt_time=None) -> int:
cache_key = get_client_cache_key(request, credentials)

View file

@ -9,7 +9,7 @@ from django.urls import reverse
from axes.attempts import reset
from axes.conf import settings
from axes.utils import get_axes_cache, get_cool_off, get_credentials
from axes.utils import get_cache, get_cool_off, get_credentials
from axes.models import AccessLog, AccessAttempt
@ -64,7 +64,7 @@ class AxesTestCase(TestCase):
self.credentials = get_credentials(self.username)
def tearDown(self):
get_axes_cache().clear()
get_cache().clear()
def get_kwargs_with_defaults(self, **kwargs):
defaults = {

View file

@ -8,20 +8,7 @@ from axes.tests.base import AxesTestCase
class CacheCheckTestCase(AxesTestCase):
@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_HANDLER='axes.handlers.cache.AxesCacheHandler',
AXES_CACHE='axes',
CACHES={
'axes': {

View file

@ -16,7 +16,7 @@ from axes.conf import settings
logger = getLogger(__name__)
def get_axes_cache() -> BaseCache:
def get_cache() -> BaseCache:
"""
Get the cache instance Axes is configured to use with ``settings.AXES_CACHE`` and use ``'default'`` if not set.
"""