From 354ff27e2a5882a06f9f9d57bbe0ec3fbe77c4a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Fri, 25 Oct 2024 20:40:04 +0200 Subject: [PATCH] Refactor cache.set to cache.put for improved code readibility --- server/src/uds/REST/methods/actor_v3.py | 2 +- server/src/uds/REST/methods/login_logout.py | 2 +- server/src/uds/REST/methods/system.py | 2 +- server/src/uds/auths/OAuth2/authenticator.py | 6 +++--- server/src/uds/auths/SAML/saml.py | 2 +- server/src/uds/core/auths/callbacks.py | 2 +- .../uds/core/services/generics/dynamic/userservice.py | 2 +- .../uds/core/services/generics/fixed/userservice.py | 2 +- server/src/uds/core/util/cache.py | 4 ++-- server/src/uds/core/util/calendar/__init__.py | 4 ++-- server/src/uds/core/util/decorators.py | 4 ++-- server/src/uds/management/commands/udsfs/stats.py | 4 ++-- server/src/uds/mfas/TOTP/mfa.py | 2 +- server/src/uds/services/OVirt/deployment_linked.py | 2 +- server/src/uds/services/OpenGnsys/og/__init__.py | 2 +- server/src/uds/services/OpenNebula/deployment.py | 2 +- server/src/uds/services/OpenStack/deployment_fixed.py | 2 +- server/src/uds/services/OpenStack/openstack/client.py | 6 +++--- server/src/uds/services/Proxmox/proxmox/client.py | 2 +- server/src/uds/transports/HTML5RDP/html5rdp.py | 4 ++-- server/src/uds/transports/HTML5SSH/html5ssh.py | 4 ++-- server/src/uds/transports/HTML5VNC/html5vnc.py | 4 ++-- server/src/uds/transports/RDP/rdp_base.py | 4 ++-- server/src/uds/transports/SPICE/spice_base.py | 6 +++--- server/src/uds/transports/X2GO/x2go_base.py | 4 ++-- server/src/uds/web/util/authentication.py | 4 ++-- server/tests/core/auths/test_callbacks.py | 2 +- server/tests/core/test_environment.py | 4 ++-- server/tests/core/util/test_cache.py | 10 +++++----- 29 files changed, 50 insertions(+), 50 deletions(-) diff --git a/server/src/uds/REST/methods/actor_v3.py b/server/src/uds/REST/methods/actor_v3.py index 67cc53348..c065f5f7f 100644 --- a/server/src/uds/REST/methods/actor_v3.py +++ b/server/src/uds/REST/methods/actor_v3.py @@ -119,7 +119,7 @@ def check_ip_is_blocked(request: 'ExtendedHttpRequest') -> None: def increase_failed_ip_count(request: 'ExtendedHttpRequest') -> None: fails = cache.get(request.ip, 0) + 1 - cache.set(request.ip, fails, GlobalConfig.LOGIN_BLOCK.as_int()) + cache.put(request.ip, fails, GlobalConfig.LOGIN_BLOCK.as_int()) P = typing.ParamSpec('P') diff --git a/server/src/uds/REST/methods/login_logout.py b/server/src/uds/REST/methods/login_logout.py index 5ec335ddc..90c770cc4 100644 --- a/server/src/uds/REST/methods/login_logout.py +++ b/server/src/uds/REST/methods/login_logout.py @@ -177,7 +177,7 @@ class Login(Handler): # Sleep a while here to "prottect" time.sleep(3) # Wait 3 seconds if credentials fails for "protection" # And store in cache for blocking for a while if fails - fail_cache.set(self._request.ip, fails + 1, GlobalConfig.LOGIN_BLOCK.as_int()) + fail_cache.put(self._request.ip, fails + 1, GlobalConfig.LOGIN_BLOCK.as_int()) return Login.result(error=auth_result.errstr or 'Invalid credentials') return Login.result( diff --git a/server/src/uds/REST/methods/system.py b/server/src/uds/REST/methods/system.py index 23222149c..5675b8bf0 100644 --- a/server/src/uds/REST/methods/system.py +++ b/server/src/uds/REST/methods/system.py @@ -115,7 +115,7 @@ def get_servicepools_counters( # logger.debug('val: %s', val) if len(val) >= 2: - cache.set( + cache.put( cache_key, codecs.encode(pickletools.optimize(pickle.dumps(val, protocol=-1)), 'zip'), CACHE_TIME * 2, diff --git a/server/src/uds/auths/OAuth2/authenticator.py b/server/src/uds/auths/OAuth2/authenticator.py index 6b5b37282..bc2701797 100644 --- a/server/src/uds/auths/OAuth2/authenticator.py +++ b/server/src/uds/auths/OAuth2/authenticator.py @@ -305,11 +305,11 @@ class OAuth2Authenticator(auths.Authenticator): case oauth2_types.ResponseType.CODE | oauth2_types.ResponseType.TOKEN: # Code or token flow # Simply store state, no code_verifier, store "none" as code_verifier to later restore it - self.cache.set(state, 'none', 3600) + self.cache.put(state, 'none', 3600) case oauth2_types.ResponseType.OPENID_CODE | oauth2_types.ResponseType.OPENID_ID_TOKEN: # OpenID flow nonce = secrets.token_urlsafe(oauth2_consts.STATE_LENGTH) - self.cache.set(state, nonce, 3600) # Store nonce + self.cache.put(state, nonce, 3600) # Store nonce # Fix scope to ensure openid is present if 'openid' not in param_dict['scope']: param_dict['scope'] = 'openid ' + param_dict['scope'] @@ -322,7 +322,7 @@ class OAuth2Authenticator(auths.Authenticator): code_verifier, code_challenge = self.code_verifier_and_challenge() param_dict['code_challenge'] = code_challenge param_dict['code_challenge_method'] = 'S256' - self.cache.set(state, code_verifier, 3600) + self.cache.put(state, code_verifier, 3600) # Nonce only is used if False: diff --git a/server/src/uds/auths/SAML/saml.py b/server/src/uds/auths/SAML/saml.py index 72cc2d77e..b79d751af 100644 --- a/server/src/uds/auths/SAML/saml.py +++ b/server/src/uds/auths/SAML/saml.py @@ -489,7 +489,7 @@ class SAMLAuthenticator(auths.Authenticator): ) val = resp.content.decode() # 10 years, unless edited the metadata will be kept - self.cache.set('idpMetadata', val, 86400 * 365 * 10) + self.cache.put('idpMetadata', val, 86400 * 365 * 10) except Exception as e: logger.error('Error fetching idp metadata: %s', e) raise exceptions.auth.AuthenticatorException(gettext('Can\'t access idp metadata')) diff --git a/server/src/uds/core/auths/callbacks.py b/server/src/uds/core/auths/callbacks.py index 75bf5b9d3..8cee3f355 100644 --- a/server/src/uds/core/auths/callbacks.py +++ b/server/src/uds/core/auths/callbacks.py @@ -113,4 +113,4 @@ def weblogin(user: models.User) -> None: except Exception as e: logger.error('Error notifying login to callback URL: %s', e) - FAILURE_CACHE.set('notify_failure', fail_count + 1) + FAILURE_CACHE.put('notify_failure', fail_count + 1) diff --git a/server/src/uds/core/services/generics/dynamic/userservice.py b/server/src/uds/core/services/generics/dynamic/userservice.py index dd5b50207..372688103 100644 --- a/server/src/uds/core/services/generics/dynamic/userservice.py +++ b/server/src/uds/core/services/generics/dynamic/userservice.py @@ -402,7 +402,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable if self.cache.get('ready', '0') == '1': self._set_queue([types.services.Operation.FINISH]) elif self.service().is_running(self, self._vmid): - self.cache.set('ready', '1', consts.cache.SHORT_CACHE_TIMEOUT // 2) # short cache timeout + self.cache.put('ready', '1', consts.cache.SHORT_CACHE_TIMEOUT // 2) # short cache timeout self._set_queue([types.services.Operation.FINISH]) else: self._set_queue( diff --git a/server/src/uds/core/services/generics/fixed/userservice.py b/server/src/uds/core/services/generics/fixed/userservice.py index e450e2704..7a29ebc1a 100644 --- a/server/src/uds/core/services/generics/fixed/userservice.py +++ b/server/src/uds/core/services/generics/fixed/userservice.py @@ -278,7 +278,7 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable, if self.cache.get('ready', '0') == '1': self._queue = [types.services.Operation.FINISH] elif self.service().is_ready(self._vmid): - self.cache.set('ready', '1', consts.cache.SHORT_CACHE_TIMEOUT // 2) # short cache timeout + self.cache.put('ready', '1', consts.cache.SHORT_CACHE_TIMEOUT // 2) # short cache timeout self._queue = [types.services.Operation.FINISH] else: self._queue = [types.services.Operation.START, types.services.Operation.FINISH] diff --git a/server/src/uds/core/util/cache.py b/server/src/uds/core/util/cache.py index 324de737f..d6377858b 100644 --- a/server/src/uds/core/util/cache.py +++ b/server/src/uds/core/util/cache.py @@ -162,7 +162,7 @@ class Cache: with transaction.atomic(): Cache.delete(self._owner) - def set( + def put( self, skey: typing.Union[str, bytes], value: typing.Any, @@ -206,7 +206,7 @@ class Cache: """ Stores a value in the cache using the [] operator with default validity """ - self.set(key, value) + self.put(key, value) def refresh(self, skey: typing.Union[str, bytes]) -> None: # logger.debug('Refreshing key "%s" for cache "%s"' % (skey, self._owner,)) diff --git a/server/src/uds/core/util/calendar/__init__.py b/server/src/uds/core/util/calendar/__init__.py index c25356597..4cfb974f8 100644 --- a/server/src/uds/core/util/calendar/__init__.py +++ b/server/src/uds/core/util/calendar/__init__.py @@ -166,7 +166,7 @@ class CalendarChecker: # Now data can be accessed as an array of booleans. # Store data on persistent cache - CalendarChecker.cache.set(cache_key, state_on_minute.tobytes(), ONE_DAY) + CalendarChecker.cache.put(cache_key, state_on_minute.tobytes(), ONE_DAY) memcache_storage.set(cache_key, state_on_minute.tobytes(), ONE_DAY) return bool(state_on_minute[dtime.hour * 60 + dtime.minute]) @@ -202,7 +202,7 @@ class CalendarChecker: ) # We substract on checkin, so we can take into account for next execution the "offset" on start & end (just the inverse of current, so we substract it) if next_event: next_event += offset - CalendarChecker.cache.set(cache_key, next_event, 3600) + CalendarChecker.cache.put(cache_key, next_event, 3600) else: CalendarChecker.hits += 1 diff --git a/server/src/uds/core/util/decorators.py b/server/src/uds/core/util/decorators.py index c528846fa..2bc8d8bd1 100644 --- a/server/src/uds/core/util/decorators.py +++ b/server/src/uds/core/util/decorators.py @@ -304,7 +304,7 @@ def cached( try: # Maybe returned data is not serializable. In that case, cache will fail but no harm is done with this - cache.set(cache_key, data, effective_timeout) + cache.put(cache_key, data, effective_timeout) except Exception as e: logger.debug( 'Data for %s is not serializable on call to %s, not cached. %s (%s)', @@ -387,7 +387,7 @@ def blocker( result = f(*args, **kwargs) except uds.core.exceptions.rest.BlockAccess: # Increment - mycache.set(ip, failures_count + 1, GlobalConfig.LOGIN_BLOCK.as_int()) + mycache.put(ip, failures_count + 1, GlobalConfig.LOGIN_BLOCK.as_int()) raise exceptions.rest.AccessDenied # Any other exception will be raised except Exception: diff --git a/server/src/uds/management/commands/udsfs/stats.py b/server/src/uds/management/commands/udsfs/stats.py index 445256fcc..4a0867457 100644 --- a/server/src/uds/management/commands/udsfs/stats.py +++ b/server/src/uds/management/commands/udsfs/stats.py @@ -207,7 +207,7 @@ class StatsFS(types.UDSFSInterface): else: logger.debug('Cache miss for %s', path[0]) data = dispatcher(interval, extension, 0, 0) - self._cache.set(path[0] + extension, data, cache_time) + self._cache.put(path[0] + extension, data, cache_time) # Calculate the size of the file size = len(data) @@ -239,7 +239,7 @@ class StatsFS(types.UDSFSInterface): else: logger.debug('Cache miss for %s', path[0]) data = dispatcher(interval, extension, 0, 0) - self._cache.set(path[0] + extension, data, cache_time) + self._cache.put(path[0] + extension, data, cache_time) # Dispatch the read to the dispatcher data = dispatcher(interval, extension, size, offset) diff --git a/server/src/uds/mfas/TOTP/mfa.py b/server/src/uds/mfas/TOTP/mfa.py index db6b73930..4423fa465 100644 --- a/server/src/uds/mfas/TOTP/mfa.py +++ b/server/src/uds/mfas/TOTP/mfa.py @@ -205,7 +205,7 @@ class TOTP_MFA(mfas.MFA): ): raise exceptions.auth.MFAError(gettext('Invalid code')) - self.cache.set(userid + code, True, self.valid_window.as_int() * (TOTP_INTERVAL + 1)) + self.cache.put(userid + code, True, self.valid_window.as_int() * (TOTP_INTERVAL + 1)) if qr_has_been_shown is False: self._save_user_data(userid, (secret, True)) # Update user data to show QR code only once diff --git a/server/src/uds/services/OVirt/deployment_linked.py b/server/src/uds/services/OVirt/deployment_linked.py index c06383c58..ac2549955 100644 --- a/server/src/uds/services/OVirt/deployment_linked.py +++ b/server/src/uds/services/OVirt/deployment_linked.py @@ -246,7 +246,7 @@ class OVirtLinkedUserService(services.UserService, autoserializable.AutoSerializ self._queue = [Operation.START, Operation.FINISH] return self._execute_queue() - self.cache.set('ready', '1') + self.cache.put('ready', '1') except Exception as e: self.do_log(types.log.LogLevel.ERROR, f'Error on setReady: {e}') # Treat as operation done, maybe the machine is ready and we can continue diff --git a/server/src/uds/services/OpenGnsys/og/__init__.py b/server/src/uds/services/OpenGnsys/og/__init__.py index 03485dafa..799e6da49 100644 --- a/server/src/uds/services/OpenGnsys/og/__init__.py +++ b/server/src/uds/services/OpenGnsys/og/__init__.py @@ -177,7 +177,7 @@ class OpenGnsysClient: ) self.auth = auth['apikey'] - self.cache.set(cache_key, self.auth, CACHE_VALIDITY) + self.cache.put(cache_key, self.auth, CACHE_VALIDITY) @property def version(self) -> str: diff --git a/server/src/uds/services/OpenNebula/deployment.py b/server/src/uds/services/OpenNebula/deployment.py index 945b2967f..91393362e 100644 --- a/server/src/uds/services/OpenNebula/deployment.py +++ b/server/src/uds/services/OpenNebula/deployment.py @@ -145,7 +145,7 @@ class OpenNebulaLiveDeployment(services.UserService, autoserializable.AutoSerial self.service().start_machine(self._vmid) - self.cache.set('ready', '1') + self.cache.put('ready', '1') except Exception as e: self.do_log(types.log.LogLevel.ERROR, 'Error on set_ready: {}'.format(e)) # Treat as operation done, maybe the machine is ready and we can continue diff --git a/server/src/uds/services/OpenStack/deployment_fixed.py b/server/src/uds/services/OpenStack/deployment_fixed.py index 00a505ba8..136a1f88c 100644 --- a/server/src/uds/services/OpenStack/deployment_fixed.py +++ b/server/src/uds/services/OpenStack/deployment_fixed.py @@ -81,7 +81,7 @@ class OpenStackUserServiceFixed(FixedUserService, autoserializable.AutoSerializa self._queue = [types.services.Operation.START, types.services.Operation.FINISH] return self._execute_queue() - self.cache.set('ready', '1') + self.cache.put('ready', '1') return types.states.TaskState.FINISHED def reset(self) -> types.states.TaskState: diff --git a/server/src/uds/services/OpenStack/openstack/client.py b/server/src/uds/services/OpenStack/openstack/client.py index d8286ee35..01c059786 100644 --- a/server/src/uds/services/OpenStack/openstack/client.py +++ b/server/src/uds/services/OpenStack/openstack/client.py @@ -253,7 +253,7 @@ class OpenStackClient: # pylint: disable=too-many-public-methods try: # set first cached endpoint as prefered # Note that if fails, cached endpoint is removed and next one is tried - self.cache.set(cache_key, endpoint, consts.cache.EXTREME_CACHE_TIMEOUT) + self.cache.put(cache_key, endpoint, consts.cache.EXTREME_CACHE_TIMEOUT) logger.debug( 'Requesting from endpoint: %s and path %s using %s: %s', endpoint, path, type, data ) @@ -299,7 +299,7 @@ class OpenStackClient: # pylint: disable=too-many-public-methods for i, endpoint in enumerate(found_endpoints): try: # If fails, cached endpoint is removed and next one is tried - self.cache.set( + self.cache.put( cache_key, endpoint, consts.cache.EXTREME_CACHE_TIMEOUT ) # Cache endpoint for a very long time yield from OpenStackClient._get_recurring_url_json( @@ -420,7 +420,7 @@ class OpenStackClient: # pylint: disable=too-many-public-methods dateutil.parser.parse(token['expires_at']).replace(tzinfo=None) - dateutil.parser.parse(token['issued_at']).replace(tzinfo=None) ).seconds - 60 - self.cache.set( + self.cache.put( 'auth', (self._authenticated_projectid, self._projectid, self._tokenid, self._userid, self._catalog), validity, diff --git a/server/src/uds/services/Proxmox/proxmox/client.py b/server/src/uds/services/Proxmox/proxmox/client.py index ecd4b2bd3..13e98f42a 100644 --- a/server/src/uds/services/Proxmox/proxmox/client.py +++ b/server/src/uds/services/Proxmox/proxmox/client.py @@ -152,7 +152,7 @@ class ProxmoxClient: csrf = data['CSRFPreventionToken'] if self.cache: - self.cache.set(self._host + 'conn', (ticket, csrf), validity=1800) # 30 minutes + self.cache.put(self._host + 'conn', (ticket, csrf), validity=1800) # 30 minutes _update_session(ticket, csrf) except requests.RequestException as e: diff --git a/server/src/uds/transports/HTML5RDP/html5rdp.py b/server/src/uds/transports/HTML5RDP/html5rdp.py index e8a1f0051..d2c5db282 100644 --- a/server/src/uds/transports/HTML5RDP/html5rdp.py +++ b/server/src/uds/transports/HTML5RDP/html5rdp.py @@ -324,9 +324,9 @@ class HTML5RDPTransport(transports.Transport): if not ready: # Check again for readyness if self.test_connectivity(userservice, ip, self.rdp_port.as_int()) is True: - self.cache.set(ip, 'Y', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'Y', READY_CACHE_TIMEOUT) return True - self.cache.set(ip, 'N', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'N', READY_CACHE_TIMEOUT) return ready == 'Y' def processed_username(self, userservice: 'models.UserService', user: 'models.User') -> str: diff --git a/server/src/uds/transports/HTML5SSH/html5ssh.py b/server/src/uds/transports/HTML5SSH/html5ssh.py index aff157891..e455f0eae 100644 --- a/server/src/uds/transports/HTML5SSH/html5ssh.py +++ b/server/src/uds/transports/HTML5SSH/html5ssh.py @@ -169,9 +169,9 @@ class HTML5SSHTransport(transports.Transport): if not ready: # Check again for readyness if self.test_connectivity(userservice, ip, self.ssh_port.value) is True: - self.cache.set(ip, 'Y', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'Y', READY_CACHE_TIMEOUT) return True - self.cache.set(ip, 'N', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'N', READY_CACHE_TIMEOUT) return ready == 'Y' def get_link( diff --git a/server/src/uds/transports/HTML5VNC/html5vnc.py b/server/src/uds/transports/HTML5VNC/html5vnc.py index 7e50b170f..24d9b032a 100644 --- a/server/src/uds/transports/HTML5VNC/html5vnc.py +++ b/server/src/uds/transports/HTML5VNC/html5vnc.py @@ -152,9 +152,9 @@ class HTML5VNCTransport(transports.Transport): if not ready: # Check again for readyness if self.test_connectivity(userservice, ip, self.vnc_port.value) is True: - self.cache.set(ip, 'Y', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'Y', READY_CACHE_TIMEOUT) return True - self.cache.set(ip, 'N', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'N', READY_CACHE_TIMEOUT) return ready == 'Y' def get_link( diff --git a/server/src/uds/transports/RDP/rdp_base.py b/server/src/uds/transports/RDP/rdp_base.py index 24edd7b3f..66add4e43 100644 --- a/server/src/uds/transports/RDP/rdp_base.py +++ b/server/src/uds/transports/RDP/rdp_base.py @@ -372,9 +372,9 @@ class BaseRDPTransport(transports.Transport): if ready is None: # Check again for ready if self.test_connectivity(userservice, ip, self.rdp_port.as_int()) is True: - self.cache.set(ip, 'Y', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'Y', READY_CACHE_TIMEOUT) return True - self.cache.set(ip, 'N', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'N', READY_CACHE_TIMEOUT) return ready == 'Y' def processed_username(self, userservice: 'models.UserService', user: 'models.User') -> str: diff --git a/server/src/uds/transports/SPICE/spice_base.py b/server/src/uds/transports/SPICE/spice_base.py index 5a231563f..babb479ea 100644 --- a/server/src/uds/transports/SPICE/spice_base.py +++ b/server/src/uds/transports/SPICE/spice_base.py @@ -161,13 +161,13 @@ class BaseSpiceTransport(transports.Transport): # test ANY of the ports port_to_test = con.port if con.port != -1 else con.secure_port if port_to_test == -1: - self.cache.set( + self.cache.put( 'cached_message', 'Could not find the PORT for connection', 120 ) # Write a message, that will be used from getCustom logger.info('SPICE didn\'t find has any port: %s', con) return False - self.cache.set( + self.cache.put( 'cached_message', 'Could not reach server "{}" on port "{}" from broker (prob. causes are name resolution & firewall rules)'.format( con.address, port_to_test @@ -176,7 +176,7 @@ class BaseSpiceTransport(transports.Transport): ) if self.test_connectivity(userservice, con.address, port_to_test) is True: - self.cache.set(ip, 'Y', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'Y', READY_CACHE_TIMEOUT) ready = 'Y' return ready == 'Y' diff --git a/server/src/uds/transports/X2GO/x2go_base.py b/server/src/uds/transports/X2GO/x2go_base.py index 253ca9883..bc6d19892 100644 --- a/server/src/uds/transports/X2GO/x2go_base.py +++ b/server/src/uds/transports/X2GO/x2go_base.py @@ -211,9 +211,9 @@ class BaseX2GOTransport(transports.Transport): if ready is None: # Check again for ready if net.test_connectivity(ip, 22): - self.cache.set(ip, 'Y', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'Y', READY_CACHE_TIMEOUT) return True - self.cache.set(ip, 'N', READY_CACHE_TIMEOUT) + self.cache.put(ip, 'N', READY_CACHE_TIMEOUT) return ready == 'Y' def get_screen_size(self) -> tuple[int, int]: diff --git a/server/src/uds/web/util/authentication.py b/server/src/uds/web/util/authentication.py index 3c71e15e9..7da762ae0 100644 --- a/server/src/uds/web/util/authentication.py +++ b/server/src/uds/web/util/authentication.py @@ -88,8 +88,8 @@ def check_login( # pylint: disable=too-many-branches, too-many-statements if auth_result.user is None: logger.debug("Invalid user %s (access denied)", username) - cache.set(tries_cache_key, tries + 1, GlobalConfig.LOGIN_BLOCK.as_int()) - cache.set(request.ip, tries_in_this_ip + 1, GlobalConfig.LOGIN_BLOCK.as_int()) + cache.put(tries_cache_key, tries + 1, GlobalConfig.LOGIN_BLOCK.as_int()) + cache.put(request.ip, tries_in_this_ip + 1, GlobalConfig.LOGIN_BLOCK.as_int()) log_login( request, authenticator, diff --git a/server/tests/core/auths/test_callbacks.py b/server/tests/core/auths/test_callbacks.py index 0853424cd..40765aaaa 100644 --- a/server/tests/core/auths/test_callbacks.py +++ b/server/tests/core/auths/test_callbacks.py @@ -67,7 +67,7 @@ class AuthCallbackTest(UDSTestCase): def test_callback_failed_url(self) -> None: config.GlobalConfig.NOTIFY_CALLBACK_URL.set('http://localhost:1234') # Sample non existent url - callbacks.FAILURE_CACHE.set('notify_failure', 3) # Already failed 3 times + callbacks.FAILURE_CACHE.put('notify_failure', 3) # Already failed 3 times with mock.patch('uds.core.util.security.secure_requests_session') as session_mock: callbacks.weblogin(self.user) diff --git a/server/tests/core/test_environment.py b/server/tests/core/test_environment.py index 1154433a3..b261281ff 100644 --- a/server/tests/core/test_environment.py +++ b/server/tests/core/test_environment.py @@ -58,7 +58,7 @@ class TestEnvironment(UDSTransactionTestCase): env.storage.put('test', 'test') self.assertEqual(env.storage.read('test'), 'test') - env.cache.set('test', 'test') + env.cache.put('test', 'test') self.assertEqual(env.cache.get('test'), 'test') # Recreate environment @@ -110,7 +110,7 @@ class TestEnvironment(UDSTransactionTestCase): env.storage.put('test', 'test') self.assertEqual(env.storage.read('test'), 'test') - env.cache.set('test', 'test') + env.cache.put('test', 'test') self.assertEqual(env.cache.get('test'), 'test') # Environment is cleared after exit, ensure it diff --git a/server/tests/core/util/test_cache.py b/server/tests/core/util/test_cache.py index ef9070fc7..fb28f87a4 100644 --- a/server/tests/core/util/test_cache.py +++ b/server/tests/core/util/test_cache.py @@ -57,13 +57,13 @@ class CacheTest(UDSTransactionTestCase): self.assertEqual(cache.remove('non-existing-1'), False, 'Removing unexisting key') # Add new key (non existing) with default duration (60 seconds probable) - cache.set(UNICODE_CHARS_2, VALUE_1) + cache.put(UNICODE_CHARS_2, VALUE_1) # checks it self.assertEqual(cache.get(UNICODE_CHARS_2), VALUE_1, 'Put a key and recover it') # Add new "str" key, with 1 second life, wait 2 seconds and recover - cache.set(b'key', VALUE_1, 1) + cache.put(b'key', VALUE_1, 1) time.sleep(1.1) self.assertEqual( cache.get(b'key'), @@ -80,17 +80,17 @@ class CacheTest(UDSTransactionTestCase): ) # Checks cache clean - cache.set('key', VALUE_1) + cache.put('key', VALUE_1) cache.clear() self.assertEqual(cache.get('key'), None, 'Get key from cleaned cache') # Checks cache purge - cache.set('key', 'test') + cache.put('key', 'test') Cache.purge() self.assertEqual(cache.get('key'), None, 'Get key from purged cache') # Checks cache cleanup (remove expired keys) - cache.set('key', 'test', 0) + cache.put('key', 'test', 0) time.sleep(0.1) Cache.purge_outdated() cache.refresh('key')