|
7 | 7 | report and FOSSA-format output. |
8 | 8 | """ |
9 | 9 | import json |
| 10 | +import re |
10 | 11 | from pathlib import Path |
11 | 12 | from types import SimpleNamespace |
12 | 13 | from unittest.mock import Mock |
|
21 | 22 |
|
22 | 23 | from socketsecurity.config import CliConfig |
23 | 24 | from socketsecurity.core import Core |
24 | | -from socketsecurity.core.classes import Diff, Issue |
| 25 | +from socketsecurity.core.classes import Comment, Diff, Issue |
25 | 26 | from socketsecurity.core.messages import Messages |
| 27 | +from socketsecurity.core.scm_comments import Comments |
26 | 28 | from socketsecurity.core.socket_config import SocketConfig |
| 29 | +from socketsecurity.exit_codes import InfrastructureError |
27 | 30 | from socketsecurity.output import OutputHandler |
28 | 31 |
|
29 | 32 | BUCKETS = ("added", "removed", "unchanged", "replaced", "updated") |
@@ -209,6 +212,39 @@ def test_failed_full_scan_fetch_fails_the_run(core): |
209 | 212 | core.create_new_diff(["/repo"], params, explicit_files=["/repo/package.json"]) |
210 | 213 |
|
211 | 214 |
|
| 215 | +@pytest.mark.parametrize("flags", [ |
| 216 | + ("--strict-blocking",), ("--enable-gitlab-security",), ("--legal-format", "fossa"), |
| 217 | +]) |
| 218 | +@pytest.mark.parametrize("response_kind", ["missing", "malformed", "empty"]) |
| 219 | +def test_incomplete_existing_alert_data_fails_the_run(core, flags, response_kind): |
| 220 | + """A successful response can still omit packages, including SDK parse failures.""" |
| 221 | + core.cli_config = CliConfig.from_args(["--api-token", "test", *flags]) |
| 222 | + core.sdk.fullscans.stream_diff.return_value = _diff_response(unchanged=[ |
| 223 | + _diff_artifact("unchanged", "u1", "gamma"), |
| 224 | + _diff_artifact("unchanged", "u2", "delta"), |
| 225 | + ]) |
| 226 | + artifacts = [_scan_artifact("u2", "delta", [])] |
| 227 | + if response_kind == "malformed": |
| 228 | + broken = _scan_artifact("u1", "gamma", [_alert("k3")]) |
| 229 | + del broken["alerts"][0]["key"] |
| 230 | + artifacts.append(broken) |
| 231 | + elif response_kind == "empty": |
| 232 | + artifacts = [] |
| 233 | + core.sdk.fullscans.stream.return_value = _scan_response(*artifacts) |
| 234 | + params = FullScanParams(repo="repo", branch="feature") |
| 235 | + params.include_license_details = True |
| 236 | + |
| 237 | + with pytest.raises(InfrastructureError, match="unchanged packages.*full scan new"): |
| 238 | + core.create_new_diff(["/repo"], params, explicit_files=["/repo/package.json"]) |
| 239 | + |
| 240 | + |
| 241 | +def test_matched_package_without_alerts_is_a_clean_result(core): |
| 242 | + diff = _run(core, "--strict-blocking", scan_alerts=[]) |
| 243 | + |
| 244 | + assert diff.unchanged_alerts == [] |
| 245 | + core.sdk.fullscans.stream.assert_called_once() |
| 246 | + |
| 247 | + |
212 | 248 | # --- PR comment ------------------------------------------------------------------------ |
213 | 249 |
|
214 | 250 |
|
@@ -293,6 +329,44 @@ def test_pr_comment_stays_within_the_size_budget(monkeypatch): |
293 | 329 | assert "Existing alerts (20)" in comment |
294 | 330 |
|
295 | 331 |
|
| 332 | +@pytest.mark.parametrize("budget", [6000, 1]) |
| 333 | +def test_ignoring_visible_alerts_keeps_the_truncation_warning(monkeypatch, budget): |
| 334 | + monkeypatch.setattr(Messages, "SECURITY_COMMENT_SIZE_BUDGET", budget) |
| 335 | + diff = _comment_diff(existing=[_issue(f"pkg{i}") for i in range(20)]) |
| 336 | + body = Messages.security_comment_template(diff, _config()) |
| 337 | + visible = re.findall(r"<!-- start-socket-alert-(.*?) -->", body) |
| 338 | + assert len(visible) < len(diff.unchanged_alerts) |
| 339 | + if budget > 1: |
| 340 | + assert visible |
| 341 | + comments = {"ignore": [ |
| 342 | + Comment(body_list=[f"@SocketSecurity ignore npm/{package}"]) |
| 343 | + for package in visible |
| 344 | + ]} |
| 345 | + assert Comments.remove_alerts(comments, diff.unchanged_alerts) |
| 346 | + |
| 347 | + updated = Comments.process_security_comment(Comment(body_list=body.splitlines()), comments) |
| 348 | + |
| 349 | + assert "No dependency alerts to report" not in updated |
| 350 | + assert "more existing alert(s) are not shown" in updated |
| 351 | + assert "[full report](https://socket.dev/diff?action=error%2Cwarn)" in updated |
| 352 | + # Reprocessing the comment must not lose the undisplayed findings either. |
| 353 | + assert Comments.process_security_comment(Comment(body_list=updated.splitlines()), comments) == updated |
| 354 | + |
| 355 | + |
| 356 | +def test_ignore_all_clears_even_truncated_existing_alerts(monkeypatch): |
| 357 | + monkeypatch.setattr(Messages, "SECURITY_COMMENT_SIZE_BUDGET", 6000) |
| 358 | + diff = _comment_diff(existing=[_issue(f"pkg{i}") for i in range(20)]) |
| 359 | + body = Messages.security_comment_template(diff, _config()) |
| 360 | + assert "more existing alert(s) are not shown" in body |
| 361 | + comments = {"ignore": [Comment(body_list=["@SocketSecurity ignore-all"])]} |
| 362 | + |
| 363 | + updated = Comments.process_security_comment(Comment(body_list=body.splitlines()), comments) |
| 364 | + |
| 365 | + assert "No dependency alerts to report" in updated |
| 366 | + assert "more existing alert(s)" not in updated |
| 367 | + assert "[View full report](https://socket.dev/diff)" in updated |
| 368 | + |
| 369 | + |
296 | 370 | def test_existing_license_violations_use_the_license_row(): |
297 | 371 | license_alert = _issue("gamma") |
298 | 372 | license_alert.type = "licenseSpdxDisj" |
|
0 commit comments