79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
import json
|
|
|
|
from depaudit.output import AuditResult
|
|
from depaudit.output.json_formatter import JSONFormatter
|
|
from depaudit.output.table_formatter import TableFormatter
|
|
from depaudit.output.factory import FormatterFactory
|
|
|
|
|
|
class TestAuditResult:
|
|
def test_audit_result_default(self):
|
|
result = AuditResult()
|
|
assert result.vulnerabilities == []
|
|
assert result.outdated == []
|
|
assert result.license_issues == []
|
|
assert result.unused == []
|
|
assert result.scanned_count == 0
|
|
|
|
def test_audit_result_with_data(self):
|
|
result = AuditResult()
|
|
result.vulnerabilities.append({
|
|
"id": "CVE-2023-0001",
|
|
"severity": "critical",
|
|
"package_name": "test-pkg",
|
|
})
|
|
result.scanned_count = 5
|
|
|
|
summary = result.get_summary()
|
|
assert summary["total_vulnerabilities"] == 1
|
|
assert summary["severity_breakdown"]["critical"] == 1
|
|
|
|
|
|
class TestJSONFormatter:
|
|
def test_format_json(self):
|
|
result = AuditResult()
|
|
result.vulnerabilities.append({
|
|
"id": "CVE-2023-0001",
|
|
"severity": "critical",
|
|
"package_name": "lodash",
|
|
"current_version": "4.17.15",
|
|
"fixed_version": "4.17.21",
|
|
})
|
|
|
|
formatter = JSONFormatter()
|
|
output = formatter.format(result)
|
|
|
|
assert isinstance(output, str)
|
|
parsed = json.loads(output)
|
|
assert "vulnerabilities" in parsed
|
|
assert len(parsed["vulnerabilities"]) == 1
|
|
|
|
|
|
class TestTableFormatter:
|
|
def test_format_table(self):
|
|
result = AuditResult()
|
|
result.scanned_count = 3
|
|
result.vulnerabilities.append({
|
|
"id": "CVE-2023-0001",
|
|
"severity": "critical",
|
|
"package_name": "lodash",
|
|
"current_version": "4.17.15",
|
|
"fixed_version": "4.17.21",
|
|
})
|
|
|
|
formatter = TableFormatter(use_color=False, verbosity="info")
|
|
output = formatter.format(result)
|
|
|
|
assert isinstance(output, str)
|
|
assert "lodash" in output
|
|
|
|
|
|
class TestFormatterFactory:
|
|
def test_get_json_formatter(self):
|
|
formatter = FormatterFactory.get_formatter("json", use_color=False)
|
|
assert isinstance(formatter, JSONFormatter)
|
|
|
|
def test_get_table_formatter(self):
|
|
formatter = FormatterFactory.get_formatter("table", use_color=False)
|
|
assert isinstance(formatter, TableFormatter)
|