def test_get_cache_key(self, get_ip_mock): """ Test the cache key format""" # Getting cache key from request ip = '127.0.0.1'.encode('utf-8') cache_hash_key_checker = 'axes-{}'.format(md5((ip)).hexdigest()) request_factory = RequestFactory() request = request_factory.post('/admin/login/', data={ 'username': self.VALID_USERNAME, 'password': '******' }) cache_hash_key = get_cache_key(request) self.assertEqual(cache_hash_key_checker, cache_hash_key) # Getting cache key from AccessAttempt Object attempt = AccessAttempt( user_agent='<unknown>', ip_address='127.0.0.1', username=self.VALID_USERNAME, get_data='', post_data='', http_accept=request.META.get('HTTP_ACCEPT', '<unknown>'), path_info=request.META.get('PATH_INFO', '<unknown>'), failures_since_start=0, ) cache_hash_key = get_cache_key(attempt) self.assertEqual(cache_hash_key_checker, cache_hash_key)
def reset(ip=None, username=None): """Reset records that match ip or username, and return the count of removed attempts. """ count = 0 attempts = AccessAttempt.objects.all() if ip: attempts = attempts.filter(ip_address=ip) if username: attempts = attempts.filter(username=username) if attempts: count = attempts.count() from axes.decorators import get_cache_key for attempt in attempts: cache_hash_key = get_cache_key(attempt) if cache.get(cache_hash_key): cache.delete(cache_hash_key) attempts.delete() return count
def delete_cache_after_delete(instance, **kwargs): from axes.decorators import get_cache_key cache_hash_key = get_cache_key(instance) cache.delete(cache_hash_key)
def update_cache_after_save(instance, **kwargs): from axes.decorators import get_cache_timeout, get_cache_key cache_hash_key = get_cache_key(instance) if not cache.get(cache_hash_key): cache_timeout = get_cache_timeout() cache.set(cache_hash_key, instance.failures_since_start, cache_timeout)