Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions averbis/core/_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,19 @@ def invalidate_api_token(self, user: str, password: str) -> None:
)
return None

def is_token_valid(self, api_token: str) -> bool:
"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method uses the clients pre-configured API token for the request, so it checks that one and not the one given as a parameter. In my opinion it would be better to remove the api_token paramter from this method to avoid confusion.

Checks if the given API token is valid.

:return: True if the token is valid, False otherwise.
"""

try:
self.__request_with_json_response("get", "/v1/license")
return True
except RequestException:
return False

def get_api_token_status(self, user: str, password: str) -> str:
"""
Obtains the status of the given API token.
Expand Down
26 changes: 26 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,32 @@ def test_upload_license(client, requests_mock):
assert license_info == payload


def test_is_token_valid(client, requests_mock):
requests_mock.get(
f"{API_BASE}/license",
headers={"Content-Type": "application/json"},
json={
"payload": {"valid": True},
"errorMessages": [],
},
)

is_valid = client.is_token_valid(TEST_API_TOKEN)
assert is_valid is True


def test_is_token_valid_false(client, requests_mock):
requests_mock.get(
f"{API_BASE}/license",
headers={"Content-Type": "application/json"},
status_code=401,
json={},
)

is_valid = client.is_token_valid(TEST_API_TOKEN)
assert is_valid is False


def test_list_resource_containers_not_supported(client, requests_mock):
requests_mock.get(
f"{API_BASE}/buildInfo",
Expand Down