112 lines
2.7 KiB
Python
112 lines
2.7 KiB
Python
"""Pytest configuration and fixtures for AI Code Audit CLI tests."""
|
|
|
|
import pytest
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_python_code():
|
|
"""Sample Python code with various issues."""
|
|
return '''
|
|
import os
|
|
import unused_module
|
|
|
|
def example_function(password="secret123"):
|
|
api_key = "AKIAIOSFODNN7EXAMPLE"
|
|
try:
|
|
result = os.system(f"echo {password}")
|
|
except:
|
|
pass
|
|
return result
|
|
|
|
def bad_function(items=[]):
|
|
for i in range(100):
|
|
pass
|
|
return items
|
|
'''
|
|
|
|
|
|
@pytest.fixture
|
|
def clean_python_code():
|
|
"""Sample clean Python code without issues."""
|
|
return '''
|
|
def calculate_sum(numbers: list[int]) -> int:
|
|
"""Calculate the sum of a list of numbers."""
|
|
total = 0
|
|
for num in numbers:
|
|
total += num
|
|
return total
|
|
|
|
if __name__ == "__main__":
|
|
numbers = [1, 2, 3, 4, 5]
|
|
print(calculate_sum(numbers))
|
|
'''
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_javascript_code():
|
|
"""Sample JavaScript code with various issues."""
|
|
return '''
|
|
const apiKey = "sk-1234567890abcdef";
|
|
const password = "secret123";
|
|
|
|
function processData(data) {
|
|
try {
|
|
const result = eval(data.userInput);
|
|
return result;
|
|
} catch (e) {
|
|
// Silent catch
|
|
}
|
|
}
|
|
|
|
function badExample(items = []) {
|
|
for (let i = 0; i < 100; i++) {
|
|
console.log(i);
|
|
}
|
|
}
|
|
'''
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_directory():
|
|
"""Create a temporary directory with test files."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
yield Path(tmpdir)
|
|
|
|
|
|
@pytest.fixture
|
|
def test_files(temp_directory, sample_python_code, clean_python_code, sample_javascript_code):
|
|
"""Create test files in temp directory."""
|
|
(temp_directory / "bad_code.py").write_text(sample_python_code)
|
|
(temp_directory / "good_code.py").write_text(clean_python_code)
|
|
(temp_directory / "bad_code.js").write_text(sample_javascript_code)
|
|
return temp_directory
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_scan_result():
|
|
"""Create a mock scan result for testing."""
|
|
from src.core.models import ScanResult, Issue, IssueCategory, SeverityLevel
|
|
|
|
result = ScanResult(files_scanned=3, target_path="/test")
|
|
result.add_issue(Issue(
|
|
severity=SeverityLevel.HIGH,
|
|
category=IssueCategory.SECURITY,
|
|
file_path="/test/file.py",
|
|
line_number=5,
|
|
message="Hardcoded credential detected",
|
|
suggestion="Use environment variables",
|
|
scanner_name="test",
|
|
))
|
|
result.add_issue(Issue(
|
|
severity=SeverityLevel.MEDIUM,
|
|
category=IssueCategory.ANTI_PATTERN,
|
|
file_path="/test/file.py",
|
|
line_number=10,
|
|
message="Mutable default argument detected",
|
|
suggestion="Use None as default",
|
|
scanner_name="test",
|
|
))
|
|
return result
|