|
1 | 1 | import pytest |
2 | 2 | from socketsecurity.output import OutputHandler |
3 | | -from socketsecurity.core.classes import Diff, Issue |
| 3 | +from socketsecurity.core.classes import Diff, Issue, Package |
4 | 4 | import json |
5 | 5 |
|
6 | 6 | class TestOutputHandler: |
@@ -144,6 +144,7 @@ def test_json_file_saving(self, tmp_path): |
144 | 144 | config.report_link_file = None |
145 | 145 | config.sbom_file = None |
146 | 146 | config.legal = True |
| 147 | + config.legal_format = "socket" |
147 | 148 | config.repo = "owner/repo" |
148 | 149 | config.branch = "main" |
149 | 150 | config.commit_sha = "abc123" |
@@ -199,6 +200,7 @@ def test_summary_and_report_link_files_are_written(self, tmp_path): |
199 | 200 | config.report_link_file = str(report_link_path) |
200 | 201 | config.sbom_file = None |
201 | 202 | config.legal = False |
| 203 | + config.legal_format = "socket" |
202 | 204 | config.repo = None |
203 | 205 | config.branch = "" |
204 | 206 | config.commit_sha = "" |
@@ -235,6 +237,105 @@ def test_summary_and_report_link_files_are_written(self, tmp_path): |
235 | 237 | assert "Security issues detected by Socket Security:" in summary_path.read_text() |
236 | 238 | assert report_link_path.read_text().strip() == "https://socket.dev/report/123" |
237 | 239 |
|
| 240 | + def test_json_file_saving_in_fossa_format(self, tmp_path): |
| 241 | + from socketsecurity.config import CliConfig |
| 242 | + from unittest.mock import Mock |
| 243 | + |
| 244 | + json_path = tmp_path / "fossa-report.json" |
| 245 | + |
| 246 | + config = Mock(spec=CliConfig) |
| 247 | + config.disable_blocking = False |
| 248 | + config.strict_blocking = False |
| 249 | + config.json_file = str(json_path) |
| 250 | + config.summary_file = None |
| 251 | + config.report_link_file = None |
| 252 | + config.sbom_file = None |
| 253 | + config.legal = True |
| 254 | + config.legal_format = "fossa" |
| 255 | + config.repo = "owner/repo" |
| 256 | + config.branch = "refs/heads/main" |
| 257 | + config.commit_sha = "abc123" |
| 258 | + config.enable_json = False |
| 259 | + config.enable_sarif = False |
| 260 | + config.enable_gitlab_security = False |
| 261 | + config.enable_debug = False |
| 262 | + |
| 263 | + handler = OutputHandler(config, Mock()) |
| 264 | + |
| 265 | + diff = Diff() |
| 266 | + diff.id = "scan-123" |
| 267 | + diff.report_url = "https://socket.dev/report/123" |
| 268 | + diff.packages = { |
| 269 | + "pkg-1": Package( |
| 270 | + id="pkg-1", |
| 271 | + name="requests", |
| 272 | + version="2.31.0", |
| 273 | + type="pypi", |
| 274 | + score={}, |
| 275 | + alerts=[], |
| 276 | + direct=True, |
| 277 | + url="https://socket.dev/pypi/package/requests/overview/2.31.0", |
| 278 | + license="Apache-2.0", |
| 279 | + purl="pkg:pypi/requests@2.31.0", |
| 280 | + ) |
| 281 | + } |
| 282 | + diff.new_alerts = [ |
| 283 | + Issue( |
| 284 | + title="Prototype Pollution", |
| 285 | + severity="high", |
| 286 | + description="Upgrade to a fixed version.", |
| 287 | + error=True, |
| 288 | + key="alert-1", |
| 289 | + type="vulnerability", |
| 290 | + pkg_type="pypi", |
| 291 | + pkg_name="requests", |
| 292 | + pkg_version="2.31.0", |
| 293 | + pkg_id="pkg-1", |
| 294 | + purl="pkg:pypi/requests@2.31.0", |
| 295 | + url="https://socket.dev/npm/package/requests/alerts/2.31.0", |
| 296 | + props={ |
| 297 | + "ghsaId": "GHSA-1234", |
| 298 | + "cveId": "CVE-2026-1234", |
| 299 | + "cvssScore": 8.2, |
| 300 | + "fixedVersion": "2.31.1", |
| 301 | + "references": ["https://github.com/advisories/GHSA-1234"], |
| 302 | + }, |
| 303 | + ), |
| 304 | + Issue( |
| 305 | + title="License Policy Violation", |
| 306 | + severity="medium", |
| 307 | + description="Package license violates policy.", |
| 308 | + warn=True, |
| 309 | + key="license-1", |
| 310 | + type="licenseSpdxDisj", |
| 311 | + pkg_type="pypi", |
| 312 | + pkg_name="requests", |
| 313 | + pkg_version="2.31.0", |
| 314 | + pkg_id="pkg-1", |
| 315 | + purl="pkg:pypi/requests@2.31.0", |
| 316 | + url="https://socket.dev/pypi/package/requests/license/2.31.0", |
| 317 | + ), |
| 318 | + ] |
| 319 | + |
| 320 | + handler.save_json_file(diff, str(json_path)) |
| 321 | + |
| 322 | + saved = json.loads(json_path.read_text()) |
| 323 | + assert saved["project"] == { |
| 324 | + "branch": "refs/heads/main", |
| 325 | + "id": "owner/repo-scan-123", |
| 326 | + "project": "owner/repo", |
| 327 | + "projectId": "owner/repo", |
| 328 | + "revision": "scan-123", |
| 329 | + "url": "https://socket.dev/report/123", |
| 330 | + } |
| 331 | + assert len(saved["vulnerability"]) == 1 |
| 332 | + assert saved["vulnerability"][0]["vulnId"] == "GHSA-1234" |
| 333 | + assert saved["vulnerability"][0]["cve"] == "CVE-2026-1234" |
| 334 | + assert saved["vulnerability"][0]["source"]["packageManager"] == "pip" |
| 335 | + assert saved["vulnerability"][0]["remediation"]["completeFix"] == "2.31.1" |
| 336 | + assert saved["licensing"][0]["type"] == "policy_conflict" |
| 337 | + assert saved["quality"] == [] |
| 338 | + |
238 | 339 | def test_report_pass_with_strict_blocking_new_alerts(self): |
239 | 340 | """Test that strict-blocking fails on new blocking alerts""" |
240 | 341 | from socketsecurity.config import CliConfig |
|
0 commit comments