-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_cli_run.py
More file actions
125 lines (83 loc) · 4.44 KB
/
Copy pathtest_cli_run.py
File metadata and controls
125 lines (83 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import json
from unittest.mock import Mock
from socketsecurity.core.cli_client import CliClient
from socketsecurity.core.cli_run import finalize_cli_run, register_cli_run
from socketsecurity.core.exceptions import APIFailure
def _resp(payload):
r = Mock()
r.json.return_value = payload
return r
def test_register_cli_run_returns_run_id_when_enabled():
client = Mock(spec=CliClient)
client.request.return_value = _resp({
"log_streaming_enabled": True,
"run_id": "srv-issued-123",
})
run_id = register_cli_run(client, client_version="1.2.3", upload_logs=True)
assert run_id == "srv-issued-123"
args, kwargs = client.request.call_args
assert kwargs["path"] == "python-cli-runs"
assert kwargs["method"] == "POST"
body = json.loads(kwargs["payload"])
assert body == {"client_version": "1.2.3", "share_logs": True, "decline_logs": False}
def test_register_cli_run_returns_none_when_disabled_by_server():
client = Mock(spec=CliClient)
client.request.return_value = _resp({
"log_streaming_enabled": False,
"run_id": None,
})
assert register_cli_run(client, client_version="1.0.0", upload_logs=None) is None
def test_register_cli_run_sends_both_false_when_unset():
client = Mock(spec=CliClient)
client.request.return_value = _resp({"log_streaming_enabled": False, "run_id": None})
register_cli_run(client, client_version="1.0.0", upload_logs=None)
body = json.loads(client.request.call_args.kwargs["payload"])
assert body == {"client_version": "1.0.0", "share_logs": False, "decline_logs": False}
def test_register_cli_run_sends_decline_logs_true_when_opted_out():
client = Mock(spec=CliClient)
client.request.return_value = _resp({"log_streaming_enabled": False, "run_id": None})
register_cli_run(client, client_version="1.0.0", upload_logs=False)
body = json.loads(client.request.call_args.kwargs["payload"])
assert body == {"client_version": "1.0.0", "share_logs": False, "decline_logs": True}
def test_register_cli_run_returns_none_on_api_failure():
client = Mock(spec=CliClient)
client.request.side_effect = APIFailure("network down")
assert register_cli_run(client, client_version="1.0.0", upload_logs=True) is None
def test_register_cli_run_returns_none_on_missing_run_id_when_enabled():
client = Mock(spec=CliClient)
client.request.return_value = _resp({"log_streaming_enabled": True})
assert register_cli_run(client, client_version="1.0.0", upload_logs=True) is None
def test_register_cli_run_returns_none_on_bad_json():
bad = Mock()
bad.json.side_effect = ValueError("not json")
client = Mock(spec=CliClient)
client.request.return_value = bad
assert register_cli_run(client, client_version="1.0.0", upload_logs=True) is None
def test_register_cli_run_returns_none_on_non_dict_json_body():
# Server (mis)behavior: JSON parses but isn't an object. body.get(...) would
# raise AttributeError; the broad except must catch it so the scan continues.
client = Mock(spec=CliClient)
client.request.return_value = _resp([1, 2, 3])
assert register_cli_run(client, client_version="1.0.0", upload_logs=True) is None
def test_register_cli_run_returns_none_on_unexpected_exception():
# Defense-in-depth: any unexpected exception from client.request must be
# swallowed so streaming registration can never break the scan.
client = Mock(spec=CliClient)
client.request.side_effect = RuntimeError("boom")
assert register_cli_run(client, client_version="1.0.0", upload_logs=True) is None
def test_finalize_cli_run_posts_status_and_null_report_run_id_by_default():
client = Mock(spec=CliClient)
finalize_cli_run(client, "run-x", status="failure")
args, kwargs = client.request.call_args
assert kwargs["path"] == "python-cli-runs/run-x/finalize"
assert kwargs["method"] == "POST"
assert json.loads(kwargs["payload"]) == {"status": "failure", "report_run_id": None}
def test_finalize_cli_run_includes_report_run_id_when_provided():
client = Mock(spec=CliClient)
finalize_cli_run(client, "run-x", status="success", report_run_id="fs-abc")
body = json.loads(client.request.call_args.kwargs["payload"])
assert body == {"status": "success", "report_run_id": "fs-abc"}
def test_finalize_cli_run_swallows_errors():
client = Mock(spec=CliClient)
client.request.side_effect = APIFailure("network down")
finalize_cli_run(client, "run-x") # must not raise