def test_get_cache_key_credentials(self): """ Test the cache key format. """ ip_address = self.ip_address cache_hash_digest = md5(ip_address.encode()).hexdigest() cache_hash_key = f"axes-{cache_hash_digest}" # Getting cache key from request request_factory = RequestFactory() request = request_factory.post("/admin/login/", data={ "username": self.username, "password": "******" }) # Difference between the upper test: new call signature with credentials credentials = {"username": self.username} self.assertEqual([cache_hash_key], get_client_cache_key(request, credentials)) # Getting cache key from AccessAttempt Object attempt = AccessAttempt( user_agent="<unknown>", ip_address=ip_address, username=self.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, ) self.assertEqual([cache_hash_key], get_client_cache_key(attempt))
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_attempts( self, *, ip_address: str = None, username: str = None, ip_or_username: bool = False, ) -> int: cache_keys: list = [] count = 0 if ip_address is None and username is None: raise NotImplementedError("Cannot clear all entries from cache") if ip_or_username: raise NotImplementedError( "Due to the cache key ip_or_username=True is not supported") cache_keys.extend( get_client_cache_key( AccessAttempt(username=username, ip_address=ip_address))) for cache_key in cache_keys: deleted = self.cache.delete(cache_key) count += int(deleted) if deleted is not None else 1 log.info("AXES: Reset %d access attempts from database.", count) return count
def test_get_cache_key_credentials(self): """ Test the cache key format. """ # Getting cache key from request ip_address = self.ip_address cache_hash_key = 'axes-{}'.format(md5(ip_address.encode()).hexdigest()) request_factory = RequestFactory() request = request_factory.post('/admin/login/', data={ 'username': self.username, 'password': '******' }) # Difference between the upper test: new call signature with credentials credentials = {'username': self.username} self.assertEqual(cache_hash_key, get_client_cache_key(request, credentials)) # Getting cache key from AccessAttempt Object attempt = AccessAttempt( user_agent='<unknown>', ip_address=ip_address, username=self.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, ) self.assertEqual(cache_hash_key, get_client_cache_key(attempt))
def test_get_cache_key_empty_ip_address(self): """ Simulate an empty IP address in the request. """ empty_ip_address = "" cache_hash_digest = md5(empty_ip_address.encode()).hexdigest() cache_hash_key = f"axes-{cache_hash_digest}" # Getting cache key from request request_factory = RequestFactory() request = request_factory.post( "/admin/login/", data={"username": self.username, "password": "******"}, REMOTE_ADDR=empty_ip_address, ) self.assertEqual(cache_hash_key, get_client_cache_key(request)) # Getting cache key from AccessAttempt Object attempt = AccessAttempt( user_agent="<unknown>", ip_address=empty_ip_address, username=self.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, ) self.assertEqual(cache_hash_key, get_client_cache_key(attempt))
def test_get_cache_key(self): """ Test the cache key format. """ cache_hash_digest = md5(self.ip_address.encode()).hexdigest() # Getting cache key from request cache_hash_key = f'axes-{cache_hash_digest}' request_factory = RequestFactory() request = request_factory.post( '/admin/login/', data={ 'username': self.username, 'password': '******', }, ) self.assertEqual(cache_hash_key, get_client_cache_key(request)) # Getting cache key from AccessAttempt Object attempt = AccessAttempt( user_agent='<unknown>', ip_address=self.ip_address, username=self.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, ) self.assertEqual(cache_hash_key, get_client_cache_key(attempt))
def setUp(self): self.failures_since_start = 42 self.access_attempt = AccessAttempt( failures_since_start=self.failures_since_start) self.access_log = AccessLog()