From a6631514c52329157eb98eb1d00bffb6b3bb06f7 Mon Sep 17 00:00:00 2001 From: Jules Lasne Date: Fri, 9 Feb 2024 10:25:18 +0100 Subject: [PATCH] Core: Fixed accept device --- shellhub/__init__.py | 2 +- shellhub/models/base.py | 17 ++++++++++++++++- shellhub/models/device.py | 22 +++++++++++++++++++--- tests/test_devices.py | 4 ++-- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/shellhub/__init__.py b/shellhub/__init__.py index 6915165..3bd34e6 100644 --- a/shellhub/__init__.py +++ b/shellhub/__init__.py @@ -1,5 +1,5 @@ # Increment versions here according to SemVer -__version__ = "0.2.1" +__version__ = "0.2.2" from .models.device import ShellHubDevice, ShellHubDeviceInfo from .models.base import ShellHub diff --git a/shellhub/models/base.py b/shellhub/models/base.py index 799c0ef..18154b0 100644 --- a/shellhub/models/base.py +++ b/shellhub/models/base.py @@ -33,6 +33,12 @@ def __init__(self, username: str, password: str, endpoint_or_url: str, use_ssl: self._login() def _format_and_validate_url(self, endpoint: str) -> Tuple[str, str]: + """ + Format and validate the URL provided by the user. If the URL doesn't start with http:// or https://, it will + :param endpoint: The URL provided by the user for the shellhub instance + :return: A tuple containing the full URL and the base endpoint + """ + # Adjust the endpoint based on the _use_ssl flag if not endpoint.startswith(("http://", "https://")): protocol = "https://" if self._use_ssl else "http://" @@ -50,7 +56,11 @@ def _format_and_validate_url(self, endpoint: str) -> Tuple[str, str]: @staticmethod def _is_valid_url(url: str) -> bool: - # Simple pattern to check if the URL is well-formed + """ + Check if the URL provided is valid + :param url: The URL to be checked + :return: True if the URL is valid, False otherwise + """ pattern = re.compile( r"^https?:\/\/" # http:// or https:// r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|" # domain... @@ -164,6 +174,11 @@ def get_all_devices( return devices def get_device(self, uid: str) -> "shellhub.models.device.ShellHubDevice": + """ + Get a device from ShellHub by its UID + :param uid: The UID of the device + :return: A ShellHubDevice object + """ response = self.make_request(endpoint=f"/api/devices/{uid}", method="GET") if response.status_code == 404: raise DeviceNotFoundError(f"Device {uid} not found.") diff --git a/shellhub/models/device.py b/shellhub/models/device.py index 04d2ef5..4a5a40c 100644 --- a/shellhub/models/device.py +++ b/shellhub/models/device.py @@ -70,6 +70,10 @@ def __init__(self, api_object: shellhub.models.base.ShellHub, device_json): # t self.acceptable = device_json["acceptable"] def delete(self) -> bool: + """ + Delete the device from the API + :return: True if the device was deleted, False otherwise + """ response = self._api.make_request(endpoint=f"/api/devices/{self.uid}", method="DELETE") if response.status_code == 200: return True @@ -106,10 +110,14 @@ def rename(self, name: Optional[str] = None) -> bool: return False def accept(self) -> bool: - if not self.acceptable: - raise ShellHubApiError(f"Device {self.uid} is not acceptable.") + """ + Accept the device if it is pending + :return: True if the device was accepted, False otherwise + """ + if self.status != "pending": + raise ShellHubApiError(f"Device {self.uid} is not pending.") - response = self._api.make_request(endpoint=f"/api/devices/{self.uid}/accept", method="POST") + response = self._api.make_request(endpoint=f"/api/devices/{self.uid}/accept", method="PATCH") if response.status_code == 200: self.refresh() return True @@ -124,6 +132,10 @@ def accept(self) -> bool: return False def refresh(self) -> None: + """ + Refresh the device information from the API + :return: None + """ response = self._api.make_request(endpoint=f"/api/devices/{self.uid}", method="GET") if response.status_code == 404: raise DeviceNotFoundError(f"Device {self.uid} not found.") @@ -137,6 +149,10 @@ def refresh(self) -> None: @property def sshid(self) -> Optional[str]: + """ + Fabricates the SSHID of the devices from the namespace, name and endpoint + :return: SSHID of the device + """ if self.acceptable: return None return f"{self.namespace}.{self.name}@{self._api._endpoint}" diff --git a/tests/test_devices.py b/tests/test_devices.py index 1328866..c7d53f6 100644 --- a/tests/test_devices.py +++ b/tests/test_devices.py @@ -220,7 +220,7 @@ def test_not_acceptable_device(self, shellhub_device): shellhub_device.accept() def test_accept_notfound_device(self, shellhub_device, requests_mock): - requests_mock.post(f"{MOCKED_DOMAIN_URL}/api/devices/1/accept", status_code=404) + requests_mock.patch(f"{MOCKED_DOMAIN_URL}/api/devices/1/accept", status_code=404) shellhub_device.acceptable = True with pytest.raises(ShellHubApiError): shellhub_device.accept() @@ -254,7 +254,7 @@ def test_accept_device(self, shellhub_device, requests_mock): "acceptable": False, } requests_mock.get(f"{MOCKED_DOMAIN_URL}/api/devices/1", json=mock_response) - requests_mock.post(f"{MOCKED_DOMAIN_URL}/api/devices/1/accept", status_code=200) + requests_mock.patch(f"{MOCKED_DOMAIN_URL}/api/devices/1/accept", status_code=200) shellhub_device.acceptable = True shellhub_device.accept()