def check_failures(self, failures, username=None, ip_address=None): if ip_address is None and username is None: raise NotImplementedError("Must supply ip_address or username") try: prev_ip = self.request.META["REMOTE_ADDR"] credentials = {"username": username} if username else {} if ip_address is not None: self.request.META["REMOTE_ADDR"] = ip_address self.assertEqual( failures, AxesProxyHandler.get_failures(self.request, credentials=credentials), ) finally: self.request.META["REMOTE_ADDR"] = prev_ip
def test_handler_reset_attempts_username(self): self.set_up_login_attemtps() self.assertEqual( 2, AxesProxyHandler.get_failures( self.request, credentials={"username": self.USERNAME_1}), ) self.assertEqual( 2, AxesProxyHandler.get_failures( self.request, credentials={"username": self.USERNAME_2}), ) self.assertEqual( 1, AxesProxyHandler.reset_attempts(username=self.USERNAME_1)) self.assertEqual( 0, AxesProxyHandler.get_failures( self.request, credentials={"username": self.USERNAME_1}), ) self.assertEqual( 2, AxesProxyHandler.get_failures( self.request, credentials={"username": self.USERNAME_2}), )
def __call__(self, request): response = self.get_response(request) if "rest_framework" in settings.INSTALLED_APPS: AxesProxyHandler.update_request(request) username = get_client_username(request) credentials = get_credentials(username) failures_since_start = AxesProxyHandler.get_failures( request, credentials) if (settings.AXES_LOCK_OUT_AT_FAILURE and failures_since_start >= get_failure_limit( request, credentials)): request.axes_locked_out = True if getattr(request, "axes_locked_out", None): response = get_lockout_response(request) # type: ignore return response
def test_handler_get_failures(self): self.assertEqual(0, AxesProxyHandler.get_failures(self.request, {}))