diff --git a/nxdrive/client/remote_client.py b/nxdrive/client/remote_client.py index 17aa34c8de..ec3a555cc0 100644 --- a/nxdrive/client/remote_client.py +++ b/nxdrive/client/remote_client.py @@ -294,37 +294,28 @@ def execute(self, /, **kwargs: Any) -> Any: # Unauthorized and Forbidden exceptions are handled by the Python client. try: resp = self.operations.execute(ssl_verify=Options.ssl_no_verify, **kwargs) - log.info(f"******** self.token: {self.token!r}") if self.token and self.auth: auth_token = self.auth.auth.token - log.info(f"******** auth.token: {auth_token!r}") - if self.token != auth_token: - log.info( - "******** Tokens are different, new token canbe stored into db" - ) - if self.dao: - remote_user = self.dao.get_config("remote_user") - server_url = self.dao.get_config("server_url") - key = f"{remote_user}{server_url}" - stored_token = ( - json.dumps(auth_token) - if isinstance(auth_token, dict) - else auth_token - ) - secure_token = force_decode(encrypt(stored_token, key)) - self.dao.update_config("remote_token", secure_token) - log.info("Token Stored Successfully") - else: - log.info("dao is not available") - - else: - log.info("******** Tokens are same, no need to store into the db") + if self.token != auth_token and self.dao: + self._store_token(auth_token) + self.token = auth_token return resp except HTTPError as e: if e.status == requests.codes.not_found: raise NotFound("Response code not found") raise e + def _store_token(self, auth_token): + remote_user = self.dao.get_config("remote_user") + server_url = self.dao.get_config("server_url") + key = f"{remote_user}{server_url}" + stored_token = ( + json.dumps(auth_token) if isinstance(auth_token, dict) else auth_token + ) + secure_token = force_decode(encrypt(stored_token, key)) + self.dao.update_config("remote_token", secure_token) + log.info("Token Stored Successfully") + @staticmethod def escape(path: str, /) -> str: """Escape any problematic character for a NXQL query. diff --git a/tests/functional/test_remote_client.py b/tests/functional/test_remote_client.py index 57034d6cef..168e3f8423 100644 --- a/tests/functional/test_remote_client.py +++ b/tests/functional/test_remote_client.py @@ -262,3 +262,53 @@ def get_download_(*args, **kwargs): with patch.object(remote.dao, "get_download", new=get_download_): returned_val = remote.transfer_end_callback(obj1_) assert not returned_val + + +def test_store_refresh_token(manager_factory): + manager, engine = manager_factory() + remote = engine.remote + remote.token = { + "access_token": "D0QCbs1aJJsPDXzT\ + 1IrC4oKzjbFevn4s", + "refresh_token": "Fch4TbOM8okl8sLajlN\ + 37L8YHKMSfc9cFe7RMVWRG4ctNvBmSvn2SFXg5CtUJKS2", + "token_type": "bearer", + "expires_in": 3239, + "expires_at": 1711427876, + } + remote.auth = Mock() + remote.auth.auth = Mock() + remote.auth.auth.token = remote.token + old_remote_token = remote.token + with manager: + remote.execute(command="UserWorkspace.Get") + assert old_remote_token == remote.token + + remote.auth.auth.token = { + "access_token": "D0QCbs1aJJsPDXzT\ + 1IrC4oKzjbFevn4s", + "refresh_token": "Fch4TbOM8okl8sLajlP\ + 37L8YHKMSfc9cFe7RMVWRG4ctNvBmSvn2SFXg5CtUJKS2", + "token_type": "bearer", + "expires_in": 3239, + "expires_at": 1711427876, + } + old_remote_token = remote.token + with manager: + remote.execute(command="UserWorkspace.Get") + assert remote.token == remote.auth.auth.token + + remote.auth.auth.token = { + "access_token": "D0QCbs1aJJsPDXzT\ + 1IrC4oKzjbFevn4s", + "refresh_token": "Fch4TbOM8okl8sLajlP\ + 37L8YHKMSfc9cFe7RMVWRG4ctNvBmSvn2SFXg5CtUJKS2", + "token_type": "bearer", + "expires_in": 3239, + "expires_at": 1711427876, + } + old_remote_token = remote.token + remote.dao = None + with manager: + remote.execute(command="UserWorkspace.Get") + assert remote.token == old_remote_token