|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """Simple test script to verify npm audit functionality""" |
3 | 3 |
|
4 | | -import tempfile |
5 | 4 | import json |
| 5 | +import tempfile |
6 | 6 | from pathlib import Path |
| 7 | + |
| 8 | +import npm_audit |
7 | 9 | from npm_audit import NPMAuditChecker |
8 | | -from main import Vulnerability |
9 | | - |
10 | | -def create_test_package_json(temp_dir: Path) -> Path: |
11 | | - """Create a test package.json with known vulnerable packages""" |
12 | | - package_json_content = { |
13 | | - "name": "test-package", |
14 | | - "version": "1.0.0", |
15 | | - "dependencies": { |
16 | | - # Using an older version that might have known vulnerabilities |
17 | | - "lodash": "4.17.0" |
18 | | - } |
19 | | - } |
20 | | - |
21 | | - package_json_path = temp_dir / "package.json" |
22 | | - with open(package_json_path, 'w') as f: |
23 | | - json.dump(package_json_content, f, indent=2) |
24 | | - |
25 | | - return package_json_path |
26 | | - |
27 | | -def test_npm_audit_basic(): |
| 10 | + |
| 11 | + |
| 12 | +class Vulnerability: |
| 13 | + def __init__(self, **kwargs): |
| 14 | + self.__dict__.update(kwargs) |
| 15 | + |
| 16 | + |
| 17 | +def write_package_json(path: Path, content: dict) -> Path: |
| 18 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 19 | + path.write_text(json.dumps(content, indent=2)) |
| 20 | + return path |
| 21 | + |
| 22 | + |
| 23 | +def test_find_package_json_files_skips_nested_manifests() -> None: |
| 24 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 25 | + temp_path = Path(temp_dir) |
| 26 | + root_package = write_package_json( |
| 27 | + temp_path / "deps" / "pkg" / "package.json", |
| 28 | + {"name": "pkg", "version": "1.0.0", "dependencies": {"lodash": "4.17.0"}}, |
| 29 | + ) |
| 30 | + write_package_json( |
| 31 | + temp_path / "deps" / "pkg" / "src" / "package.json", |
| 32 | + {"name": "pkg-src", "version": "1.0.0", "devDependencies": {"esbuild": "0.27.0"}}, |
| 33 | + ) |
| 34 | + |
| 35 | + checker = NPMAuditChecker(temp_path, timeout=60) |
| 36 | + package_files = checker.find_package_json_files() |
| 37 | + |
| 38 | + assert package_files == [root_package], package_files |
| 39 | + |
| 40 | + |
| 41 | +def test_npm_commands_omit_dev() -> None: |
| 42 | + calls = [] |
| 43 | + |
| 44 | + class Result: |
| 45 | + def __init__(self, returncode: int = 0, stdout: str = "", stderr: str = ""): |
| 46 | + self.returncode = returncode |
| 47 | + self.stdout = stdout |
| 48 | + self.stderr = stderr |
| 49 | + |
| 50 | + def fake_run(cmd, **kwargs): |
| 51 | + calls.append(cmd) |
| 52 | + if cmd == ["npm", "--version"]: |
| 53 | + return Result(stdout="10.0.0\n") |
| 54 | + if cmd[:2] == ["npm", "install"]: |
| 55 | + return Result() |
| 56 | + if cmd[:2] == ["npm", "audit"]: |
| 57 | + return Result(stdout='{"vulnerabilities": {}}') |
| 58 | + raise AssertionError(cmd) |
| 59 | + |
| 60 | + original_run = npm_audit.subprocess.run |
| 61 | + npm_audit.subprocess.run = fake_run |
| 62 | + try: |
| 63 | + checker = NPMAuditChecker(Path("/tmp"), timeout=60) |
| 64 | + assert checker.run_npm_install(Path("/tmp")) |
| 65 | + assert checker.run_npm_audit(Path("/tmp")) == {"vulnerabilities": {}} |
| 66 | + assert checker.run_npm_audit(Path("/tmp"), package_lock_only=True) == {"vulnerabilities": {}} |
| 67 | + finally: |
| 68 | + npm_audit.subprocess.run = original_run |
| 69 | + |
| 70 | + assert ["npm", "install", "--omit=dev", "--ignore-scripts", "--no-audit", "--no-fund", "--silent"] in calls |
| 71 | + assert ["npm", "audit", "--omit=dev", "--json"] in calls |
| 72 | + assert ["npm", "audit", "--omit=dev", "--package-lock-only", "--json"] in calls |
| 73 | + |
| 74 | + |
| 75 | +def test_lockfile_skips_install() -> None: |
| 76 | + calls = [] |
| 77 | + |
| 78 | + class Result: |
| 79 | + def __init__(self, returncode: int = 0, stdout: str = "", stderr: str = ""): |
| 80 | + self.returncode = returncode |
| 81 | + self.stdout = stdout |
| 82 | + self.stderr = stderr |
| 83 | + |
| 84 | + def fake_run(cmd, **kwargs): |
| 85 | + calls.append(cmd) |
| 86 | + if cmd[:2] == ["npm", "audit"]: |
| 87 | + return Result(stdout='{"vulnerabilities": {}}') |
| 88 | + if cmd == ["npm", "--version"]: |
| 89 | + raise AssertionError("install should be skipped when package-lock.json exists") |
| 90 | + if cmd[:2] == ["npm", "install"]: |
| 91 | + raise AssertionError("install should be skipped when package-lock.json exists") |
| 92 | + raise AssertionError(cmd) |
| 93 | + |
| 94 | + original_run = npm_audit.subprocess.run |
| 95 | + npm_audit.subprocess.run = fake_run |
| 96 | + try: |
| 97 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 98 | + package_dir = Path(temp_dir) / "deps" / "pkg" |
| 99 | + write_package_json( |
| 100 | + package_dir / "package.json", |
| 101 | + {"name": "pkg", "version": "1.0.0", "dependencies": {"lodash": "4.17.0"}}, |
| 102 | + ) |
| 103 | + write_package_json( |
| 104 | + package_dir / "package-lock.json", |
| 105 | + {"name": "pkg", "lockfileVersion": 3}, |
| 106 | + ) |
| 107 | + |
| 108 | + checker = NPMAuditChecker(Path(temp_dir), timeout=60) |
| 109 | + vulnerabilities = checker.check_npm_vulnerabilities(Vulnerability) |
| 110 | + assert vulnerabilities == [], vulnerabilities |
| 111 | + finally: |
| 112 | + npm_audit.subprocess.run = original_run |
| 113 | + |
| 114 | + assert ["npm", "audit", "--omit=dev", "--package-lock-only", "--json"] in calls |
| 115 | + |
| 116 | + |
| 117 | +def test_npm_audit_basic() -> None: |
28 | 118 | """Test basic npm audit functionality""" |
29 | 119 | print("Testing npm audit functionality...") |
30 | | - |
| 120 | + test_find_package_json_files_skips_nested_manifests() |
| 121 | + test_npm_commands_omit_dev() |
| 122 | + test_lockfile_skips_install() |
| 123 | + |
31 | 124 | with tempfile.TemporaryDirectory() as temp_dir: |
32 | 125 | temp_path = Path(temp_dir) |
33 | | - |
34 | | - # Create test package.json |
35 | | - package_json = create_test_package_json(temp_path) |
| 126 | + package_json = write_package_json( |
| 127 | + temp_path / "deps" / "pkg" / "package.json", |
| 128 | + {"name": "pkg", "version": "1.0.0", "dependencies": {"lodash": "4.17.0"}}, |
| 129 | + ) |
36 | 130 | print(f"Created test package.json at: {package_json}") |
37 | | - |
38 | | - # Test NPM audit checker |
| 131 | + |
39 | 132 | checker = NPMAuditChecker(temp_path, timeout=60) |
40 | | - |
41 | | - # Test finding package.json files |
42 | 133 | package_files = checker.find_package_json_files() |
43 | 134 | print(f"Found {len(package_files)} package.json files") |
44 | | - assert len(package_files) == 1, "Should find exactly one package.json file" |
45 | | - |
46 | | - # Test npm audit (this will only work if npm is available) |
| 135 | + assert package_files == [package_json], package_files |
| 136 | + |
47 | 137 | try: |
48 | 138 | vulnerabilities = checker.check_npm_vulnerabilities(Vulnerability) |
49 | 139 | print(f"Found {len(vulnerabilities)} vulnerabilities") |
50 | | - |
51 | | - # Print vulnerability details |
52 | 140 | for vuln in vulnerabilities: |
53 | 141 | print(f"- {vuln.dependency} ({vuln.version}): {vuln.id} - {vuln.severity}") |
54 | | - |
55 | 142 | except Exception as e: |
56 | 143 | print(f"npm audit test failed (this is expected if npm is not available): {e}") |
57 | | - |
| 144 | + |
58 | 145 | print("Basic npm audit test completed!") |
59 | 146 |
|
| 147 | + |
60 | 148 | if __name__ == "__main__": |
61 | 149 | test_npm_audit_basic() |
0 commit comments