|
| 1 | +"""Existing alerts under ``--strict-blocking --disable-blocking`` are listed, not failed on. |
| 2 | +
|
| 3 | +Report-only pipelines run with both flags so the PR comment and console show existing |
| 4 | +alerts. Neither may claim those alerts fail the run when the exit code stays 0. |
| 5 | +""" |
| 6 | +from types import SimpleNamespace |
| 7 | +from unittest.mock import Mock |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from socketsecurity.config import CliConfig |
| 12 | +from socketsecurity.core.classes import Diff, Issue |
| 13 | +from socketsecurity.core.messages import Messages |
| 14 | +from socketsecurity.output import OutputHandler |
| 15 | + |
| 16 | + |
| 17 | +def _issue(name, error=True, warn=False): |
| 18 | + return Issue( |
| 19 | + pkg_type="npm", |
| 20 | + pkg_name=name, |
| 21 | + pkg_version="1.0.0", |
| 22 | + purl=f"{name}@1.0.0", |
| 23 | + type="malware", |
| 24 | + severity="critical", |
| 25 | + title="Known malware", |
| 26 | + description="Bad.", |
| 27 | + suggestion="Remove it.", |
| 28 | + url=f"https://socket.dev/npm/package/{name}", |
| 29 | + props={}, |
| 30 | + key=f"key-{name}", |
| 31 | + error=error, |
| 32 | + warn=warn, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def _diff(existing): |
| 37 | + diff = Diff() |
| 38 | + diff.new_alerts = [] |
| 39 | + diff.unchanged_alerts = list(existing) |
| 40 | + diff.diff_url = "https://socket.dev/diff" |
| 41 | + diff.report_url = "https://socket.dev/report" |
| 42 | + return diff |
| 43 | + |
| 44 | + |
| 45 | +def _comment(existing, disable_blocking): |
| 46 | + config = SimpleNamespace( |
| 47 | + strict_blocking=True, disable_blocking=disable_blocking, disable_ignore=False, scm="github" |
| 48 | + ) |
| 49 | + return Messages.security_comment_template(_diff(existing), config) |
| 50 | + |
| 51 | + |
| 52 | +def _summary(existing, disable_blocking): |
| 53 | + config = Mock(spec=CliConfig) |
| 54 | + config.strict_blocking = True |
| 55 | + config.disable_blocking = disable_blocking |
| 56 | + return OutputHandler(config, Mock()).build_summary_text(_diff(existing)) |
| 57 | + |
| 58 | + |
| 59 | +def test_comment_says_blocking_existing_alerts_fail_the_run(): |
| 60 | + comment = _comment([_issue("delta"), _issue("gamma", error=False, warn=True)], disable_blocking=False) |
| 61 | + |
| 62 | + assert "`--strict-blocking` fails the run on the 1 with a **Block** action." in comment |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("count, verb", [(1, "has"), (2, "have")]) |
| 66 | +def test_comment_does_not_claim_a_failure_under_disable_blocking(count, verb): |
| 67 | + comment = _comment([_issue(f"pkg{i}") for i in range(count)], disable_blocking=True) |
| 68 | + |
| 69 | + assert "fails the run" not in comment |
| 70 | + assert f"{count} {verb} a **Block** action but don't fail the run" in comment |
| 71 | + assert "`--disable-blocking` is set" in comment |
| 72 | + |
| 73 | + |
| 74 | +def test_comment_with_only_warnings_says_they_do_not_fail_the_run(): |
| 75 | + comment = _comment([_issue("gamma", error=False, warn=True)], disable_blocking=False) |
| 76 | + |
| 77 | + assert "None has a **Block** action, so they don't fail the run." in comment |
| 78 | + assert "fails the run on" not in comment |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("disable_blocking, reason", [ |
| 82 | + (False, "causing failure due to --strict-blocking"), |
| 83 | + (True, "not failing the run: --disable-blocking is set"), |
| 84 | +]) |
| 85 | +def test_console_summary_states_whether_existing_blockers_fail_the_run(disable_blocking, reason): |
| 86 | + summary = _summary([_issue("delta")], disable_blocking) |
| 87 | + |
| 88 | + assert f"EXISTING blocking issues: 1 ({reason})" in summary |
0 commit comments