Add unit tests for checks and output
This commit is contained in:
80
tests/unit/test_output.py
Normal file
80
tests/unit/test_output.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
import json
|
||||
|
||||
from depaudit.output import AuditResult, Formatter
|
||||
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)
|
||||
Reference in New Issue
Block a user