Skip to content

Commit 02012d6

Browse files
committed
add unit test coverage for new config defaults and outputs
Signed-off-by: lelia <2418071+lelia@users.noreply.github.com>
1 parent a01ab19 commit 02012d6

2 files changed

Lines changed: 132 additions & 1 deletion

File tree

tests/unit/test_cli_config.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,30 @@ def test_workspace_is_independent_of_workspace_name(self):
8181
"--workspace-name", "monorepo-suffix",
8282
])
8383
assert config.workspace == "my-workspace"
84-
assert config.workspace_name == "monorepo-suffix"
84+
assert config.workspace_name == "monorepo-suffix"
85+
86+
def test_legal_flag_sets_default_artifact_files(self):
87+
config = CliConfig.from_args(["--api-token", "test", "--legal"])
88+
assert config.legal is True
89+
assert config.generate_license is True
90+
assert config.json_file == "socket-report.json"
91+
assert config.summary_file == "socket-summary.txt"
92+
assert config.report_link_file == "socket-report-link.txt"
93+
assert config.sbom_file == "socket-sbom.json"
94+
assert config.license_file_name == "socket-license.json"
95+
96+
def test_legal_flag_preserves_explicit_file_paths(self):
97+
config = CliConfig.from_args([
98+
"--api-token", "test",
99+
"--legal",
100+
"--json-file", "custom-report.json",
101+
"--summary-file", "custom-summary.txt",
102+
"--report-link-file", "custom-link.txt",
103+
"--sbom-file", "custom-sbom.json",
104+
"--license-file-name", "custom-license.json",
105+
])
106+
assert config.json_file == "custom-report.json"
107+
assert config.summary_file == "custom-summary.txt"
108+
assert config.report_link_file == "custom-link.txt"
109+
assert config.sbom_file == "custom-sbom.json"
110+
assert config.license_file_name == "custom-license.json"

tests/unit/test_output.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,111 @@ def test_sbom_file_saving(self, handler, tmp_path):
123123
handler.save_sbom_file(diff, str(sbom_path))
124124
assert sbom_path.exists()
125125

126+
def test_json_file_saving(self, tmp_path):
127+
from socketsecurity.config import CliConfig
128+
from unittest.mock import Mock
129+
130+
json_path = tmp_path / "report.json"
131+
132+
config = Mock(spec=CliConfig)
133+
config.disable_blocking = False
134+
config.strict_blocking = False
135+
config.json_file = str(json_path)
136+
config.summary_file = None
137+
config.report_link_file = None
138+
config.sbom_file = None
139+
config.legal = True
140+
config.repo = "owner/repo"
141+
config.branch = "main"
142+
config.commit_sha = "abc123"
143+
config.enable_json = False
144+
config.enable_sarif = False
145+
config.enable_gitlab_security = False
146+
config.enable_debug = False
147+
148+
handler = OutputHandler(config, Mock())
149+
150+
diff = Diff()
151+
diff.id = "scan-123"
152+
diff.diff_url = "https://socket.dev/diff/123"
153+
diff.report_url = "https://socket.dev/report/123"
154+
diff.new_alerts = [
155+
Issue(
156+
title="Test",
157+
severity="high",
158+
description="desc",
159+
error=True,
160+
key="test-key",
161+
type="vulnerability",
162+
pkg_type="npm",
163+
pkg_name="test-package",
164+
pkg_version="1.0.0",
165+
purl="pkg:npm/test-package@1.0.0",
166+
url="https://socket.dev/npm/package/test-package/alerts/1.0.0",
167+
)
168+
]
169+
170+
handler.save_json_file(diff, str(json_path))
171+
172+
saved = json.loads(json_path.read_text())
173+
assert saved["full_scan_id"] == "scan-123"
174+
assert saved["report_url"] == "https://socket.dev/report/123"
175+
assert saved["repo"] == "owner/repo"
176+
assert saved["branch"] == "main"
177+
assert saved["commit_sha"] == "abc123"
178+
assert saved["legal_mode"] is True
179+
180+
def test_summary_and_report_link_files_are_written(self, tmp_path):
181+
from socketsecurity.config import CliConfig
182+
from unittest.mock import Mock
183+
184+
summary_path = tmp_path / "summary.txt"
185+
report_link_path = tmp_path / "report-link.txt"
186+
187+
config = Mock(spec=CliConfig)
188+
config.disable_blocking = False
189+
config.strict_blocking = False
190+
config.json_file = None
191+
config.summary_file = str(summary_path)
192+
config.report_link_file = str(report_link_path)
193+
config.sbom_file = None
194+
config.legal = False
195+
config.repo = None
196+
config.branch = ""
197+
config.commit_sha = ""
198+
config.enable_json = False
199+
config.enable_sarif = False
200+
config.enable_gitlab_security = False
201+
config.enable_debug = False
202+
203+
handler = OutputHandler(config, Mock())
204+
205+
diff = Diff()
206+
diff.id = "scan-123"
207+
diff.diff_url = "https://socket.dev/diff/123"
208+
diff.report_url = "https://socket.dev/report/123"
209+
diff.new_alerts = [
210+
Issue(
211+
title="Test",
212+
severity="high",
213+
description="desc",
214+
error=True,
215+
key="test-key",
216+
type="vulnerability",
217+
pkg_type="npm",
218+
pkg_name="test-package",
219+
pkg_version="1.0.0",
220+
purl="pkg:npm/test-package@1.0.0",
221+
url="https://socket.dev/npm/package/test-package/alerts/1.0.0",
222+
)
223+
]
224+
225+
handler.save_summary_file(diff, str(summary_path))
226+
handler.save_report_link_file(diff, str(report_link_path))
227+
228+
assert "Security issues detected by Socket Security:" in summary_path.read_text()
229+
assert report_link_path.read_text().strip() == "https://socket.dev/report/123"
230+
126231
def test_report_pass_with_strict_blocking_new_alerts(self):
127232
"""Test that strict-blocking fails on new blocking alerts"""
128233
from socketsecurity.config import CliConfig

0 commit comments

Comments
 (0)