Skip to content

Commit c9960d8

Browse files
committed
Add unit tests for shared selection logic
Signed-off-by: lelia <lelia@socket.dev>
1 parent b0673c8 commit c9960d8

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

tests/unit/test_alert_selection.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import json
2+
3+
from socketsecurity.core.alert_selection import (
4+
filter_alerts_by_reachability,
5+
select_diff_alerts,
6+
)
7+
from socketsecurity.core.classes import Diff, Issue
8+
9+
10+
def _issue(pkg_name: str, ghsa_id: str, error: bool = False) -> Issue:
11+
return Issue(
12+
pkg_name=pkg_name,
13+
pkg_version="1.0.0",
14+
severity="high",
15+
title=f"Vuln in {pkg_name}",
16+
description="test",
17+
type="vulnerability",
18+
manifests="package.json",
19+
pkg_type="npm",
20+
key=f"key-{pkg_name}",
21+
purl=f"pkg:npm/{pkg_name}@1.0.0",
22+
error=error,
23+
props={"ghsaId": ghsa_id},
24+
)
25+
26+
27+
def test_select_diff_alerts_uses_new_only_without_strict():
28+
diff = Diff()
29+
diff.new_alerts = [Issue(title="new")]
30+
diff.unchanged_alerts = [Issue(title="unchanged")]
31+
32+
selected = select_diff_alerts(diff, strict_blocking=False)
33+
assert [a.title for a in selected] == ["new"]
34+
35+
36+
def test_select_diff_alerts_includes_unchanged_with_strict():
37+
diff = Diff()
38+
diff.new_alerts = [Issue(title="new")]
39+
diff.unchanged_alerts = [Issue(title="unchanged")]
40+
41+
selected = select_diff_alerts(diff, strict_blocking=True)
42+
assert {a.title for a in selected} == {"new", "unchanged"}
43+
44+
45+
def test_filter_alerts_by_reachability_supports_reachability_selectors(tmp_path):
46+
facts_path = tmp_path / ".socket.facts.json"
47+
facts_path.write_text(json.dumps({
48+
"components": [
49+
{
50+
"type": "npm",
51+
"name": "reachable-pkg",
52+
"version": "1.0.0",
53+
"vulnerabilities": [{"ghsaId": "GHSA-AAAA-BBBB-CCCC", "severity": "HIGH"}],
54+
"reachability": [{
55+
"ghsa_id": "GHSA-AAAA-BBBB-CCCC",
56+
"reachability": [{"type": "reachable"}],
57+
}],
58+
},
59+
{
60+
"type": "npm",
61+
"name": "potential-pkg",
62+
"version": "1.0.0",
63+
"vulnerabilities": [{"ghsaId": "GHSA-DDDD-EEEE-FFFF", "severity": "HIGH"}],
64+
"reachability": [{
65+
"ghsa_id": "GHSA-DDDD-EEEE-FFFF",
66+
"reachability": [{"type": "potentially_reachable"}],
67+
}],
68+
},
69+
{
70+
"type": "npm",
71+
"name": "unreachable-pkg",
72+
"version": "1.0.0",
73+
"vulnerabilities": [{"ghsaId": "GHSA-GGGG-HHHH-IIII", "severity": "HIGH"}],
74+
"reachability": [{
75+
"ghsa_id": "GHSA-GGGG-HHHH-IIII",
76+
"reachability": [{"type": "unreachable"}],
77+
}],
78+
},
79+
],
80+
}), encoding="utf-8")
81+
82+
alerts = [
83+
_issue("reachable-pkg", "GHSA-AAAA-BBBB-CCCC"),
84+
_issue("potential-pkg", "GHSA-DDDD-EEEE-FFFF"),
85+
_issue("unreachable-pkg", "GHSA-GGGG-HHHH-IIII"),
86+
]
87+
88+
reachable = filter_alerts_by_reachability(
89+
alerts, "reachable", str(tmp_path), ".socket.facts.json"
90+
)
91+
assert [a.pkg_name for a in reachable] == ["reachable-pkg"]
92+
93+
potentially = filter_alerts_by_reachability(
94+
alerts, "potentially", str(tmp_path), ".socket.facts.json"
95+
)
96+
assert [a.pkg_name for a in potentially] == ["potential-pkg"]
97+
98+
reachable_or_potentially = filter_alerts_by_reachability(
99+
alerts, "reachable-or-potentially", str(tmp_path), ".socket.facts.json"
100+
)
101+
assert {a.pkg_name for a in reachable_or_potentially} == {"reachable-pkg", "potential-pkg"}

0 commit comments

Comments
 (0)