Skip to content

Commit

Permalink
NXDRIVE-2901: Authorization Error for OAuth --26/03 -Added Test case(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitofanindya committed Mar 26, 2024
1 parent dc8d7df commit a5d234f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
37 changes: 14 additions & 23 deletions nxdrive/client/remote_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 302 in nxdrive/client/remote_client.py

View check run for this annotation

Codecov / codecov/patch

nxdrive/client/remote_client.py#L296-L302

Added lines #L296 - L302 were not covered by tests
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 = (

Check warning on line 312 in nxdrive/client/remote_client.py

View check run for this annotation

Codecov / codecov/patch

nxdrive/client/remote_client.py#L309-L312

Added lines #L309 - L312 were not covered by tests
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")

Check warning on line 317 in nxdrive/client/remote_client.py

View check run for this annotation

Codecov / codecov/patch

nxdrive/client/remote_client.py#L315-L317

Added lines #L315 - L317 were not covered by tests

@staticmethod
def escape(path: str, /) -> str:
"""Escape any problematic character for a NXQL query.
Expand Down
50 changes: 50 additions & 0 deletions tests/functional/test_remote_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a5d234f

Please sign in to comment.