palisade-scanner / tests /test_certification.py
Carlos
Add mcp-taxonomy adapter module
dd38c6a
Raw
History Blame Contribute Delete
2.67 kB
import pytest
from scanner.certification import CertificationPipeline
from scanner.pipeline import PipelineOrchestrator
@pytest.mark.asyncio
async def test_certification_apply_clean_url():
"""Should reject URLs with risk > 20."""
orchestrator = PipelineOrchestrator()
cert = CertificationPipeline(orchestrator, db_path=":memory:")
# example.com is clean (risk ≈ 0)
result = await cert.apply("https://example.com", owner_email="test@example.com")
assert "certificate_id" in result
assert result["status"] == "monitoring"
assert result["initial_risk_score"] == 0
@pytest.mark.asyncio
async def test_certification_apply_rejected():
"""Should handle non-existent URLs gracefully."""
orchestrator = PipelineOrchestrator()
cert = CertificationPipeline(orchestrator, db_path=":memory:")
try:
result = await cert.apply("https://evil.example.com/attack")
assert "error" in result or "certificate_id" in result
except Exception:
# DNS errors are acceptable — it means the URL actually doesn't exist
assert True
def test_certificate_generate_id():
orchestrator = PipelineOrchestrator()
cert = CertificationPipeline(orchestrator, db_path=":memory:")
cid = cert._generate_id("example.com")
assert cid.startswith("AS-")
assert len(cid) > 10
def test_certificate_verify_not_found():
orchestrator = PipelineOrchestrator()
cert = CertificationPipeline(orchestrator, db_path=":memory:")
result = cert.verify("NONEXISTENT")
assert result["valid"] is False
assert "not found" in result.get("error", "")
@pytest.mark.asyncio
async def test_certification_full_flow():
"""Test the full flow: apply using clean content (no network)."""
orchestrator = PipelineOrchestrator()
cert = CertificationPipeline(orchestrator, db_path=":memory:")
# Use scan_content directly to avoid DNS dependency
class MockOrchestrator:
async def scan_url(self, url):
from scanner.domain.models import ScanReport
return ScanReport(url=url, risk_score=0, risk_category="none")
cert.orchestrator = MockOrchestrator()
result = await cert.apply("https://safe-site.com")
cid = result.get("certificate_id")
if not cid:
pytest.skip("Application failed")
assert result["status"] == "monitoring"
# Simulate monitoring scans
for _ in range(5):
await cert.record_monitoring_scan(cid)
# Evaluate and issue
eval_result = cert.evaluate(cid)
assert eval_result is not None
# Verify
verification = cert.verify(cid)
assert isinstance(verification["valid"], bool)