summaryrefslogtreecommitdiff
path: root/tests/utils
diff options
context:
space:
mode:
authorroberto berto <rberto@deepcausa.com>2025-11-02 22:54:44 +0000
committerroberto berto <rberto@deepcausa.com>2025-11-02 22:54:44 +0000
commit6b4e9015744ab8c9f17a6b8e23387cd676b1d827 (patch)
tree4c0a634730df2123ff506252cbec050de006dac8 /tests/utils
parent1ada32975f0bb559ec7526e0dd545700dde1cb94 (diff)
downloadpyvyos-6b4e9015744ab8c9f17a6b8e23387cd676b1d827.tar.gz
pyvyos-6b4e9015744ab8c9f17a6b8e23387cd676b1d827.zip
feat: v0.4.0 - Architecture refactor, bug fixes, and quality improvementsfeat/architecture-and-quality-improvements
- Fixed #25: config_file_save/load now include path: [] in payload - Added exception hierarchy (SDKError, HttpError, ApiError, ValidationError) - Added utility functions (json, ids, paths) - Added structured logging with request ID tracking - Added optional Pydantic validation models - Refactored to pyvyos.core.* structure with backward compatibility shims - Moved JSON specs to docs/development/vyos_api/ - Added comprehensive test suite (19 shim tests, 16 utils tests, 6 exception tests) - Updated pyproject.toml for uv sync editable installation - Added development documentation (architecture, roadmap, quality guidelines) Maintains 100% backward compatibility with 0.3.0
Diffstat (limited to 'tests/utils')
-rw-r--r--tests/utils/__init__.py2
-rw-r--r--tests/utils/test_ids.py25
-rw-r--r--tests/utils/test_json.py68
-rw-r--r--tests/utils/test_paths.py34
4 files changed, 129 insertions, 0 deletions
diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py
new file mode 100644
index 0000000..1c6adb1
--- /dev/null
+++ b/tests/utils/__init__.py
@@ -0,0 +1,2 @@
+"""Tests for pyvyos.utils module."""
+
diff --git a/tests/utils/test_ids.py b/tests/utils/test_ids.py
new file mode 100644
index 0000000..19ff3a7
--- /dev/null
+++ b/tests/utils/test_ids.py
@@ -0,0 +1,25 @@
+"""Tests for pyvyos.utils.ids module."""
+
+import uuid
+
+from pyvyos.utils.ids import request_id
+
+
+def test_request_id_returns_string():
+ """Test that request_id returns a string."""
+ rid = request_id()
+ assert isinstance(rid, str)
+
+
+def test_request_id_valid_uuid():
+ """Test that request_id returns a valid UUID."""
+ rid = request_id()
+ uuid.UUID(rid) # Should not raise
+
+
+def test_request_id_unique():
+ """Test that request_id generates unique IDs."""
+ id1 = request_id()
+ id2 = request_id()
+ assert id1 != id2
+
diff --git a/tests/utils/test_json.py b/tests/utils/test_json.py
new file mode 100644
index 0000000..4a158be
--- /dev/null
+++ b/tests/utils/test_json.py
@@ -0,0 +1,68 @@
+"""Tests for pyvyos.utils.json module."""
+
+import pytest
+
+from pyvyos.utils.json import redact_key, safe_dumps
+
+
+def test_redact_key_single_key():
+ """Test redacting a single key."""
+ data = {"key": "secret", "other": "visible"}
+ result = redact_key(data)
+ assert result["key"] == "***REDACTED***"
+ assert result["other"] == "visible"
+
+
+def test_redact_key_multiple_keys():
+ """Test redacting multiple keys."""
+ data = {"key": "secret", "password": "pass123", "apikey": "api_key", "visible": "data"}
+ result = redact_key(data, keys=["key", "password", "apikey"])
+ assert result["key"] == "***REDACTED***"
+ assert result["password"] == "***REDACTED***"
+ assert result["apikey"] == "***REDACTED***"
+ assert result["visible"] == "data"
+
+
+def test_redact_key_default_keys():
+ """Test redacting with default keys."""
+ data = {"key": "secret", "apikey": "api", "password": "pass", "data": "ok"}
+ result = redact_key(data)
+ assert result["key"] == "***REDACTED***"
+ assert result["apikey"] == "***REDACTED***"
+ assert result["password"] == "***REDACTED***"
+ assert result["data"] == "ok"
+
+
+def test_redact_key_copies_data():
+ """Test that redact_key returns a copy, not original."""
+ data = {"key": "secret"}
+ result = redact_key(data)
+ result["new"] = "value"
+ assert "new" not in data
+ assert data["key"] == "secret" # Original unchanged
+
+
+def test_safe_dumps_with_dict():
+ """Test safe_dumps with dictionary."""
+ data = {"key": "secret", "data": "visible"}
+ result = safe_dumps(data)
+ assert "***REDACTED***" in result
+ assert "secret" not in result
+ assert "visible" in result
+
+
+def test_safe_dumps_with_list():
+ """Test safe_dumps with list."""
+ data = [1, 2, 3]
+ result = safe_dumps(data)
+ assert result == "[1, 2, 3]"
+
+
+def test_safe_dumps_with_custom_keys():
+ """Test safe_dumps with custom redaction keys."""
+ data = {"sensitive": "hide", "public": "show"}
+ result = safe_dumps(data, redact_keys=["sensitive"])
+ assert "***REDACTED***" in result
+ assert "hide" not in result
+ assert "show" in result
+
diff --git a/tests/utils/test_paths.py b/tests/utils/test_paths.py
new file mode 100644
index 0000000..e85f7d0
--- /dev/null
+++ b/tests/utils/test_paths.py
@@ -0,0 +1,34 @@
+"""Tests for pyvyos.utils.paths module."""
+
+from pyvyos.utils.paths import build_path
+
+
+def test_build_path_strings():
+ """Test building path from string segments."""
+ result = build_path("interfaces", "ethernet", "eth0")
+ assert result == ["interfaces", "ethernet", "eth0"]
+
+
+def test_build_path_mixed():
+ """Test building path from mixed strings and lists."""
+ result = build_path(["interfaces", "ethernet"], "eth0", "address")
+ assert result == ["interfaces", "ethernet", "eth0", "address"]
+
+
+def test_build_path_empty():
+ """Test building path with no arguments."""
+ result = build_path()
+ assert result == []
+
+
+def test_build_path_single_list():
+ """Test building path with single list."""
+ result = build_path(["interfaces", "ethernet"])
+ assert result == ["interfaces", "ethernet"]
+
+
+def test_build_path_multiple_lists():
+ """Test building path with multiple lists."""
+ result = build_path(["interfaces"], ["ethernet"], ["eth0"])
+ assert result == ["interfaces", "ethernet", "eth0"]
+