From 6b4e9015744ab8c9f17a6b8e23387cd676b1d827 Mon Sep 17 00:00:00 2001 From: roberto berto Date: Sun, 2 Nov 2025 22:54:44 +0000 Subject: feat: v0.4.0 - Architecture refactor, bug fixes, 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 --- tests/utils/test_json.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/utils/test_json.py (limited to 'tests/utils/test_json.py') 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 + -- cgit v1.2.3