Skip to content

Commit ece3aa5

Browse files
committed
Add unit tests for new Slack behavior
Signed-off-by: lelia <lelia@socket.dev>
1 parent c9960d8 commit ece3aa5

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

tests/unit/test_slack_plugin.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import json
2+
from types import SimpleNamespace
3+
from unittest.mock import Mock, patch
4+
5+
from socketsecurity.core.classes import Diff, Issue
6+
from socketsecurity.plugins.slack import SlackPlugin
7+
8+
9+
def _issue(pkg_name: str, ghsa_id: str, error: bool = False) -> Issue:
10+
return Issue(
11+
pkg_name=pkg_name,
12+
pkg_version="1.0.0",
13+
severity="high",
14+
title=f"Vuln in {pkg_name}",
15+
description="test",
16+
type="vulnerability",
17+
manifests="package.json",
18+
pkg_type="npm",
19+
key=f"key-{pkg_name}",
20+
purl=f"pkg:npm/{pkg_name}@1.0.0",
21+
error=error,
22+
introduced_by=[("dep", "package.json")],
23+
url="https://socket.dev/test",
24+
props={"ghsaId": ghsa_id},
25+
)
26+
27+
28+
def test_slack_diff_alerts_include_unchanged_when_strict_blocking():
29+
plugin = SlackPlugin({
30+
"enabled": True,
31+
"mode": "webhook",
32+
"url": "https://hooks.slack.com/services/test",
33+
"url_configs": {"default": {}},
34+
})
35+
cfg = SimpleNamespace(
36+
repo="example-repo",
37+
reach=False,
38+
strict_blocking=True,
39+
enable_debug=False,
40+
target_path=".",
41+
reach_output_file=".socket.facts.json",
42+
)
43+
44+
diff = Diff()
45+
diff.new_alerts = [_issue("new-pkg", "GHSA-AAAA-BBBB-CCCC", error=True)]
46+
diff.unchanged_alerts = [_issue("old-pkg", "GHSA-DDDD-EEEE-FFFF", error=True)]
47+
48+
captured_titles = []
49+
50+
def _capture(diff_arg, _config):
51+
captured_titles.extend([a.title for a in diff_arg.new_alerts])
52+
return [{"type": "section", "text": {"type": "mrkdwn", "text": "ok"}}]
53+
54+
with patch.object(SlackPlugin, "create_slack_blocks_from_diff", side_effect=_capture), \
55+
patch("socketsecurity.plugins.slack.requests.post") as mock_post:
56+
mock_post.return_value = Mock(status_code=200, text="ok")
57+
plugin._send_webhook_alerts(diff, cfg)
58+
59+
assert "Vuln in new-pkg" in captured_titles
60+
assert "Vuln in old-pkg" in captured_titles
61+
62+
63+
def test_slack_reachability_alerts_only_uses_facts_reachability(tmp_path):
64+
facts_path = tmp_path / ".socket.facts.json"
65+
facts_path.write_text(json.dumps({
66+
"components": [
67+
{
68+
"type": "npm",
69+
"name": "reachable-pkg",
70+
"version": "1.0.0",
71+
"vulnerabilities": [{"ghsaId": "GHSA-AAAA-BBBB-CCCC", "severity": "HIGH"}],
72+
"reachability": [{
73+
"ghsa_id": "GHSA-AAAA-BBBB-CCCC",
74+
"reachability": [{"type": "reachable"}],
75+
}],
76+
},
77+
{
78+
"type": "npm",
79+
"name": "unreachable-pkg",
80+
"version": "1.0.0",
81+
"vulnerabilities": [{"ghsaId": "GHSA-DDDD-EEEE-FFFF", "severity": "HIGH"}],
82+
"reachability": [{
83+
"ghsa_id": "GHSA-DDDD-EEEE-FFFF",
84+
"reachability": [{"type": "unreachable"}],
85+
}],
86+
},
87+
],
88+
}), encoding="utf-8")
89+
90+
plugin = SlackPlugin({
91+
"enabled": True,
92+
"mode": "webhook",
93+
"url": "https://hooks.slack.com/services/test",
94+
"url_configs": {"default": {"reachability_alerts_only": True}},
95+
})
96+
cfg = SimpleNamespace(
97+
repo="example-repo",
98+
reach=True,
99+
strict_blocking=True,
100+
enable_debug=False,
101+
target_path=str(tmp_path),
102+
reach_output_file=".socket.facts.json",
103+
)
104+
105+
diff = Diff()
106+
# Strict mode should include unchanged alert set before reachability filtering.
107+
diff.new_alerts = [_issue("unreachable-pkg", "GHSA-DDDD-EEEE-FFFF", error=True)]
108+
diff.unchanged_alerts = [_issue("reachable-pkg", "GHSA-AAAA-BBBB-CCCC", error=False)]
109+
110+
captured_titles = []
111+
112+
def _capture(diff_arg, _config):
113+
captured_titles.extend([a.title for a in diff_arg.new_alerts])
114+
return [{"type": "section", "text": {"type": "mrkdwn", "text": "ok"}}]
115+
116+
with patch.object(SlackPlugin, "create_slack_blocks_from_diff", side_effect=_capture), \
117+
patch.object(SlackPlugin, "_send_reachability_alerts"), \
118+
patch("socketsecurity.plugins.slack.requests.post") as mock_post:
119+
mock_post.return_value = Mock(status_code=200, text="ok")
120+
plugin._send_webhook_alerts(diff, cfg)
121+
122+
assert captured_titles == ["Vuln in reachable-pkg"]

0 commit comments

Comments
 (0)