summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreduardormorais <eduardoromorais@gmail.com>2025-09-02 15:52:59 -0300
committereduardormorais <eduardoromorais@gmail.com>2025-09-02 15:52:59 -0300
commit8b22a629816885313e929346f0c7a382b6ba6823 (patch)
tree9da2c393f0a6092ecab521e183551fad656b7d58
parentc2b12490527ef60db79250d9d7524a3b970713e1 (diff)
downloadpyvyos-8b22a629816885313e929346f0c7a382b6ba6823.tar.gz
pyvyos-8b22a629816885313e929346f0c7a382b6ba6823.zip
Feature - Increased test coverage in case of error in responses
-rw-r--r--pyvyos/rest.py10
-rw-r--r--tests/modules/test_vy_device.py43
2 files changed, 48 insertions, 5 deletions
diff --git a/pyvyos/rest.py b/pyvyos/rest.py
index 8c1724e..04c786e 100644
--- a/pyvyos/rest.py
+++ b/pyvyos/rest.py
@@ -277,7 +277,7 @@ class RestClient(ABC):
def _validate_schema(response_json: Dict[str, Any]) -> None:
"""Validates response structure against API contract."""
required_keys = {"success", "data", "error"}
- if not required_keys.issubset(response_json.keys()):
+ if isinstance(response_json, dict) and not required_keys.issubset(response_json.keys()):
missing = required_keys - response_json.keys()
raise ValueError(f"Invalid response structure. Missing keys: {missing}")
@@ -298,11 +298,13 @@ class RestClient(ABC):
except JSONDecodeError as exc:
error = f"Invalid response format: {str(exc)}"
- status = resp.status_code if resp else 500
+ status = resp.status_code if resp is not None and isinstance(resp, Response) else 500
+
except HTTPError as exc:
- status = exc.response.status_code if exc.response else 500
- error = f"HTTP Error {status}: {exc.response.text[:200] if exc.response else 'Unknown error'}"
+ response = exc.response
+ status = response.status_code if response is not None and isinstance(response, Response) else 500
+ error = f"HTTP Error {status}: {response.text[:200] if response else 'Unknown error'}"
except ValueError as exc:
error = f"Validation Error: {str(exc)}"
diff --git a/tests/modules/test_vy_device.py b/tests/modules/test_vy_device.py
index f5f923e..096e924 100644
--- a/tests/modules/test_vy_device.py
+++ b/tests/modules/test_vy_device.py
@@ -291,7 +291,7 @@ def test_device_configure_multiple_op(monkeypatch, test_device):
assert api_resp
-def test_device_invalid_path_configure_set(monkeypatch, test_device):
+def test_device_invalid_path_multiple_op(monkeypatch, test_device):
def mock_configure_set(*args, **kwargs):
response = requests.Response()
response.status_code = 200
@@ -310,3 +310,44 @@ def test_device_invalid_path_configure_set(monkeypatch, test_device):
)
with pytest.raises(ValueError):
test_device.configure_multiple_op(op_path="interfaces")
+
+
+def test_device_invalid_request(monkeypatch, test_device):
+ def mock_invalid_request(*args, **kwargs):
+ response = requests.Response()
+ response.status_code = 400
+ response.json = lambda: {
+ "success": False,
+ "data": None,
+ "error": "Bad Request",
+ }
+ return response
+
+ monkeypatch.setattr(
+ RestClient,
+ "_execute_request",
+ mock_invalid_request,
+ )
+
+ api_resp = test_device.show(path=["invalid", "path"])
+ assert not api_resp.result
+ assert api_resp.status == 400
+ assert "HTTP Error" in api_resp.error
+
+
+def test_device_error_json_response(monkeypatch, test_device):
+ def mock_error_json_response(*args, **kwargs):
+ response = requests.Response()
+ response.status_code = 200
+ response.data = "This is not a JSON response"
+ return response
+
+ monkeypatch.setattr(
+ RestClient,
+ "_execute_request",
+ mock_error_json_response,
+ )
+
+ api_resp = test_device.show(path=["system", "image"])
+ assert not api_resp.result
+ assert "Invalid response format" in api_resp.error \ No newline at end of file