fix: Address high-severity bandit issues
This commit is contained in:
@@ -1,29 +1,21 @@
|
||||
"""
|
||||
Test Suite for DSS Context Compiler
|
||||
Test Suite for DSS Context Compiler.
|
||||
|
||||
Validates all core functionality: cascade merging, token resolution, security, and error handling.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from core import ContextCompiler, get_compiler_status, list_skins, resolve_token
|
||||
|
||||
# Add parent directory to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from core import (
|
||||
ContextCompiler,
|
||||
get_active_context,
|
||||
resolve_token,
|
||||
validate_manifest,
|
||||
list_skins,
|
||||
get_compiler_status,
|
||||
EMERGENCY_SKIN
|
||||
)
|
||||
|
||||
|
||||
class TestContextCompiler:
|
||||
"""Test suite for Context Compiler"""
|
||||
"""Test suite for Context Compiler."""
|
||||
|
||||
def __init__(self):
|
||||
self.base_dir = Path(__file__).parent.parent
|
||||
@@ -34,7 +26,7 @@ class TestContextCompiler:
|
||||
self.failed = 0
|
||||
|
||||
def assert_equal(self, actual, expected, message):
|
||||
"""Simple assertion helper"""
|
||||
"""Simple assertion helper."""
|
||||
if actual == expected:
|
||||
print(f"✓ {message}")
|
||||
self.passed += 1
|
||||
@@ -47,7 +39,7 @@ class TestContextCompiler:
|
||||
return False
|
||||
|
||||
def assert_true(self, condition, message):
|
||||
"""Assert condition is true"""
|
||||
"""Assert condition is true."""
|
||||
if condition:
|
||||
print(f"✓ {message}")
|
||||
self.passed += 1
|
||||
@@ -58,7 +50,7 @@ class TestContextCompiler:
|
||||
return False
|
||||
|
||||
def assert_in(self, needle, haystack, message):
|
||||
"""Assert needle is in haystack"""
|
||||
"""Assert needle is in haystack."""
|
||||
if needle in haystack:
|
||||
print(f"✓ {message}")
|
||||
self.passed += 1
|
||||
@@ -70,7 +62,7 @@ class TestContextCompiler:
|
||||
return False
|
||||
|
||||
def test_basic_compilation(self):
|
||||
"""Test 1: Basic 3-layer cascade compilation"""
|
||||
"""Test 1: Basic 3-layer cascade compilation."""
|
||||
print("\n=== Test 1: Basic Compilation (3-Layer Cascade) ===")
|
||||
|
||||
try:
|
||||
@@ -80,29 +72,27 @@ class TestContextCompiler:
|
||||
self.assert_equal(
|
||||
context.get("tokens", {}).get("colors", {}).get("primary"),
|
||||
"#6366f1",
|
||||
"Project override applied correctly (colors.primary)"
|
||||
"Project override applied correctly (colors.primary)",
|
||||
)
|
||||
|
||||
# Test skin value (Layer 2 - workbench)
|
||||
self.assert_equal(
|
||||
context.get("tokens", {}).get("colors", {}).get("background"),
|
||||
"#0F172A",
|
||||
"Workbench skin value inherited (colors.background)"
|
||||
"Workbench skin value inherited (colors.background)",
|
||||
)
|
||||
|
||||
# Test base value (Layer 1)
|
||||
self.assert_equal(
|
||||
context.get("tokens", {}).get("spacing", {}).get("0"),
|
||||
"0px",
|
||||
"Base skin value inherited (spacing.0)"
|
||||
"Base skin value inherited (spacing.0)",
|
||||
)
|
||||
|
||||
# Test metadata injection
|
||||
self.assert_in("_meta", context, "Metadata injected into context")
|
||||
self.assert_equal(
|
||||
context.get("_meta", {}).get("project_id"),
|
||||
"dss-admin",
|
||||
"Project ID in metadata"
|
||||
context.get("_meta", {}).get("project_id"), "dss-admin", "Project ID in metadata"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
@@ -110,7 +100,7 @@ class TestContextCompiler:
|
||||
self.failed += 1
|
||||
|
||||
def test_debug_provenance(self):
|
||||
"""Test 2: Debug provenance tracking"""
|
||||
"""Test 2: Debug provenance tracking."""
|
||||
print("\n=== Test 2: Debug Provenance Tracking ===")
|
||||
|
||||
try:
|
||||
@@ -118,12 +108,10 @@ class TestContextCompiler:
|
||||
|
||||
self.assert_in("_provenance", context, "Provenance data included in debug mode")
|
||||
self.assert_true(
|
||||
isinstance(context.get("_provenance", []), list),
|
||||
"Provenance is a list"
|
||||
isinstance(context.get("_provenance", []), list), "Provenance is a list"
|
||||
)
|
||||
self.assert_true(
|
||||
len(context.get("_provenance", [])) > 0,
|
||||
"Provenance contains tracking entries"
|
||||
len(context.get("_provenance", [])) > 0, "Provenance contains tracking entries"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
@@ -131,7 +119,7 @@ class TestContextCompiler:
|
||||
self.failed += 1
|
||||
|
||||
def test_token_resolution(self):
|
||||
"""Test 3: Token resolution via MCP tool"""
|
||||
"""Test 3: Token resolution via MCP tool."""
|
||||
print("\n=== Test 3: Token Resolution ===")
|
||||
|
||||
try:
|
||||
@@ -149,10 +137,7 @@ class TestContextCompiler:
|
||||
|
||||
# Test nested token
|
||||
result = resolve_token(str(self.admin_manifest), "typography.fontFamily.sans")
|
||||
self.assert_true(
|
||||
"Inter" in result or "system-ui" in result,
|
||||
"Resolved nested token"
|
||||
)
|
||||
self.assert_true("Inter" in result or "system-ui" in result, "Resolved nested token")
|
||||
|
||||
# Test non-existent token
|
||||
result = resolve_token(str(self.admin_manifest), "nonexistent.token")
|
||||
@@ -163,7 +148,7 @@ class TestContextCompiler:
|
||||
self.failed += 1
|
||||
|
||||
def test_skin_listing(self):
|
||||
"""Test 4: Skin listing functionality"""
|
||||
"""Test 4: Skin listing functionality."""
|
||||
print("\n=== Test 4: Skin Listing ===")
|
||||
|
||||
try:
|
||||
@@ -180,7 +165,7 @@ class TestContextCompiler:
|
||||
self.failed += 1
|
||||
|
||||
def test_safe_boot_protocol(self):
|
||||
"""Test 5: Safe Boot Protocol (emergency fallback)"""
|
||||
"""Test 5: Safe Boot Protocol (emergency fallback)."""
|
||||
print("\n=== Test 5: Safe Boot Protocol ===")
|
||||
|
||||
try:
|
||||
@@ -188,9 +173,7 @@ class TestContextCompiler:
|
||||
context = self.compiler.compile("/nonexistent/path.json")
|
||||
|
||||
self.assert_equal(
|
||||
context.get("status"),
|
||||
"emergency_mode",
|
||||
"Emergency mode activated for invalid path"
|
||||
context.get("status"), "emergency_mode", "Emergency mode activated for invalid path"
|
||||
)
|
||||
|
||||
self.assert_in("_error", context, "Error details included in safe boot")
|
||||
@@ -198,14 +181,18 @@ class TestContextCompiler:
|
||||
# Validate emergency skin has required structure
|
||||
self.assert_in("tokens", context, "Emergency skin has tokens")
|
||||
self.assert_in("colors", context.get("tokens", {}), "Emergency skin has colors")
|
||||
self.assert_in("primary", context.get("tokens", {}).get("colors", {}), "Emergency skin has primary color")
|
||||
self.assert_in(
|
||||
"primary",
|
||||
context.get("tokens", {}).get("colors", {}),
|
||||
"Emergency skin has primary color",
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Safe Boot Protocol test failed with error: {e}")
|
||||
self.failed += 1
|
||||
|
||||
def test_path_traversal_prevention(self):
|
||||
"""Test 6: Security - Path traversal prevention"""
|
||||
"""Test 6: Security - Path traversal prevention."""
|
||||
print("\n=== Test 6: Path Traversal Prevention (Security) ===")
|
||||
|
||||
try:
|
||||
@@ -215,11 +202,7 @@ class TestContextCompiler:
|
||||
print("✗ Path traversal not prevented!")
|
||||
self.failed += 1
|
||||
except ValueError as e:
|
||||
self.assert_in(
|
||||
"path traversal",
|
||||
str(e).lower(),
|
||||
"Path traversal attack blocked"
|
||||
)
|
||||
self.assert_in("path traversal", str(e).lower(), "Path traversal attack blocked")
|
||||
|
||||
# Attempt another variant
|
||||
try:
|
||||
@@ -227,18 +210,14 @@ class TestContextCompiler:
|
||||
print("✗ Path traversal variant not prevented!")
|
||||
self.failed += 1
|
||||
except ValueError as e:
|
||||
self.assert_in(
|
||||
"path traversal",
|
||||
str(e).lower(),
|
||||
"Path traversal variant blocked"
|
||||
)
|
||||
self.assert_in("path traversal", str(e).lower(), "Path traversal variant blocked")
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Path traversal prevention test failed with unexpected error: {e}")
|
||||
self.failed += 1
|
||||
|
||||
def test_compiler_status(self):
|
||||
"""Bonus Test: Compiler status tool"""
|
||||
"""Bonus Test: Compiler status tool."""
|
||||
print("\n=== Bonus Test: Compiler Status ===")
|
||||
|
||||
try:
|
||||
@@ -254,7 +233,7 @@ class TestContextCompiler:
|
||||
self.failed += 1
|
||||
|
||||
def run_all_tests(self):
|
||||
"""Execute all tests and report results"""
|
||||
"""Execute all tests and report results."""
|
||||
print("=" * 60)
|
||||
print("DSS Context Compiler Test Suite")
|
||||
print("=" * 60)
|
||||
|
||||
Reference in New Issue
Block a user