Skip to content

Commit 9963016

Browse files
committed
add tests to cover diff timeout, API failure behaviors
Signed-off-by: lelia <2418071+lelia@users.noreply.github.com>
1 parent f7f2139 commit 9963016

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

tests/core/test_sdk_methods.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from socketdev.exceptions import APIFailure
23
from socketdev.fullscans import FullScanParams
34

45
from socketsecurity.core import Core
@@ -101,6 +102,7 @@ def test_get_added_and_removed_packages(core):
101102
"head",
102103
"new",
103104
use_types=True,
105+
include_license_details="true",
104106
)
105107

106108
# Verify the results
@@ -115,6 +117,25 @@ def test_get_added_and_removed_packages(core):
115117
assert "dp2_t1" in removed # Verify transitive dependencies are also tracked
116118
assert "pypi/direct_package_1@1.6.0" in all_packages # Unchanged package is in full package map
117119

120+
def test_get_added_and_removed_packages_can_exclude_license_details(core):
121+
"""Test that diff scan license detail expansion can be disabled."""
122+
core.get_added_and_removed_packages("head", "new", include_license_details=False)
123+
124+
core.sdk.fullscans.stream_diff.assert_called_once_with(
125+
core.config.org_slug,
126+
"head",
127+
"new",
128+
use_types=True,
129+
include_license_details="false",
130+
)
131+
132+
def test_get_added_and_removed_packages_reraises_api_failures(core):
133+
"""Test that API failures propagate to top-level CLI exit handling."""
134+
core.sdk.fullscans.stream_diff.side_effect = APIFailure("upstream request timeout")
135+
136+
with pytest.raises(APIFailure):
137+
core.get_added_and_removed_packages("head", "new")
138+
118139
def test_empty_alerts_preserved(core):
119140
"""Test that empty alerts arrays stay as empty arrays and don't become None"""
120141
# Get the scan that contains dp2 (which has empty alerts array)

tests/unit/test_cli_config.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import pytest
2+
3+
from socketsecurity import socketcli
24
from socketsecurity.config import CliConfig
35

6+
47
class TestCliConfig:
58
def test_api_token_from_env(self, monkeypatch):
69
monkeypatch.setenv("SOCKET_SECURITY_API_KEY", "test-token")
@@ -81,4 +84,25 @@ def test_workspace_is_independent_of_workspace_name(self):
8184
"--workspace-name", "monorepo-suffix",
8285
])
8386
assert config.workspace == "my-workspace"
84-
assert config.workspace_name == "monorepo-suffix"
87+
assert config.workspace_name == "monorepo-suffix"
88+
89+
def test_api_request_timeout_defaults_to_twenty_minutes(self):
90+
config = CliConfig.from_args(["--api-token", "test"])
91+
assert socketcli.get_api_request_timeout(config) == 1200
92+
93+
def test_socket_sdk_receives_cli_timeout(self, monkeypatch):
94+
captured = {}
95+
96+
def fake_socketdev(**kwargs):
97+
captured.update(kwargs)
98+
return object()
99+
100+
monkeypatch.setattr(socketcli, "socketdev", fake_socketdev)
101+
config = CliConfig.from_args(["--api-token", "test", "--timeout", "1800"])
102+
103+
socketcli.build_socket_sdk(config)
104+
105+
assert captured["token"] == "test"
106+
assert captured["timeout"] == 1800
107+
assert captured["allow_unverified"] is False
108+
assert captured["user_agent"] == f"SocketPythonCLI/{config.version}"

tests/unit/test_socketcli.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
3+
import pytest
4+
from socketdev.exceptions import APIFailure
5+
6+
from socketsecurity import socketcli
7+
8+
9+
def test_cli_honors_disable_blocking_for_api_failures(monkeypatch):
10+
def fail_main_code():
11+
raise APIFailure("upstream request timeout")
12+
13+
monkeypatch.setattr(socketcli, "main_code", fail_main_code)
14+
monkeypatch.setattr(
15+
sys,
16+
"argv",
17+
["socketcli", "--api-token", "test", "--disable-blocking"],
18+
)
19+
20+
with pytest.raises(SystemExit) as exc_info:
21+
socketcli.cli()
22+
23+
assert exc_info.value.code == 0
24+
25+
26+
def test_cli_returns_error_for_api_failures_without_disable_blocking(monkeypatch):
27+
def fail_main_code():
28+
raise APIFailure("upstream request timeout")
29+
30+
monkeypatch.setattr(socketcli, "main_code", fail_main_code)
31+
monkeypatch.setattr(sys, "argv", ["socketcli", "--api-token", "test"])
32+
33+
with pytest.raises(SystemExit) as exc_info:
34+
socketcli.cli()
35+
36+
assert exc_info.value.code == 3

0 commit comments

Comments
 (0)