Skip to content

Commit

Permalink
feat(auth): added property decorators for token and headers
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Dec 16, 2023
1 parent 43197fa commit d4e914b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/nbiatoolkit/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,27 @@ def getToken(self) -> Union[dict, int]:
self.scope = token_data.get('scope')

return self.api_headers

@property
def token(self):
"""
Returns the access token.
Returns
-------
access_token : str or None
The access token retrieved from the API.
"""
return self.access_token

@property
def headers(self):
"""
Returns the API headers.
Returns
-------
api_headers : dict or None
The authentication headers containing the access token.
"""
return self.api_headers
7 changes: 6 additions & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def failed_oauth2():

def test_getToken(oauth2):
assert oauth2.access_token is not None
assert oauth2.token is not None

def test_expiry(oauth2):
# expiry should be in the form of :'Tue Jun 29 13:58:57 2077'
Expand All @@ -34,8 +35,9 @@ def test_failed_oauth(failed_oauth2):
failed_oauth2.getToken()
assert failed_oauth2.getToken() == 401
assert failed_oauth2.access_token == -1
assert failed_oauth2.token == -1
assert failed_oauth2.getToken() == 401
# self.api_headers
assert failed_oauth2.headers is None
assert failed_oauth2.api_headers is None
assert failed_oauth2.expiry_time is None
assert failed_oauth2.refresh_token is None
Expand All @@ -47,7 +49,10 @@ def test_getToken_valid_token(oauth2):
# Test if the access token is valid and not expired
assert oauth2.getToken() == oauth2.access_token
assert oauth2.getToken() != 401
assert oauth2.access_token != -1
assert oauth2.token != -1
assert oauth2.api_headers is not None
assert oauth2.headers is not None
assert oauth2.expiry_time is not None
assert oauth2.refresh_token is not None
assert oauth2.refresh_expiry is not None
Expand Down

0 comments on commit d4e914b

Please sign in to comment.