-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_cli_config.py
More file actions
223 lines (185 loc) · 8.64 KB
/
Copy pathtest_cli_config.py
File metadata and controls
223 lines (185 loc) · 8.64 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import pytest
from socketsecurity.config import CliConfig
class TestExitCodeOnApiError:
def test_default_is_3(self):
config = CliConfig.from_args(["--api-token", "test"])
assert config.exit_code_on_api_error == 3
def test_custom_value(self):
config = CliConfig.from_args(
["--api-token", "test", "--exit-code-on-api-error", "100"]
)
assert config.exit_code_on_api_error == 100
def test_zero_value(self):
config = CliConfig.from_args(
["--api-token", "test", "--exit-code-on-api-error", "0"]
)
assert config.exit_code_on_api_error == 0
class TestCommitMessageTruncation:
def test_passes_through_under_limit(self):
msg = "a normal short commit message"
config = CliConfig.from_args(["--api-token", "test", "--commit-message", msg])
assert config.commit_message == msg
def test_truncated_above_limit(self):
config = CliConfig.from_args(
["--api-token", "test", "--commit-message", "a" * 250]
)
assert config.commit_message == "a" * 200
def test_quote_strip_runs_before_truncation(self):
quoted = '"' + ("b" * 250) + '"'
config = CliConfig.from_args(
["--api-token", "test", "--commit-message", quoted]
)
assert config.commit_message == "b" * 200
class TestCliConfig:
def test_api_token_from_env(self, monkeypatch):
monkeypatch.setenv("SOCKET_SECURITY_API_KEY", "test-token")
config = CliConfig.from_args([]) # Empty args list
assert config.api_token == "test-token"
def test_required_args(self, monkeypatch):
"""Test that api token is required if not in environment"""
for env_var in (
"SOCKET_SECURITY_API_KEY",
"SOCKET_SECURITY_API_TOKEN",
"SOCKET_API_KEY",
"SOCKET_API_TOKEN",
):
monkeypatch.delenv(env_var, raising=False)
with pytest.raises(ValueError, match="API token is required"):
config = CliConfig.from_args([])
if not config.api_token:
raise ValueError("API token is required")
def test_default_values(self):
# Test that default values are set correctly
config = CliConfig.from_args(["--api-token", "test"])
assert config.branch == ""
assert config.target_path == "./"
assert config.files == "[]"
@pytest.mark.parametrize("scm", ["github", "gitlab"])
def test_scm_infers_scan_integration_when_integration_is_not_explicit(self, scm):
config = CliConfig.from_args(["--api-token", "test", "--scm", scm])
assert config.integration_type == scm
def test_explicit_api_integration_wins_over_scm_inference(self):
config = CliConfig.from_args([
"--api-token", "test",
"--scm", "github",
"--integration", "api",
])
assert config.integration_type == "api"
def test_abbreviated_integration_is_still_treated_as_explicit(self):
config = CliConfig.from_args([
"--api-token", "test",
"--scm", "github",
"--integ", "api",
])
assert config.integration_type == "api"
def test_pr_number_tracks_whether_it_was_explicit(self):
inferred = CliConfig.from_args(["--api-token", "test"])
explicit = CliConfig.from_args([
"--api-token", "test", "--pr-number", "0",
])
assert inferred.pr_number_explicit is False
assert explicit.pr_number_explicit is True
def test_abbreviated_pr_number_is_still_treated_as_explicit(self):
config = CliConfig.from_args([
"--api-token", "test", "--pr-n", "0",
])
assert config.pr_number == "0"
assert config.pr_number_explicit is True
def test_config_file_values_are_treated_as_explicit(self, tmp_path):
config_path = tmp_path / "socketcli.json"
config_path.write_text(
'{"socketcli":{"scm":"github","integration":"api","pr_number":"0"}}'
)
config = CliConfig.from_args([
"--api-token", "test", "--config", str(config_path),
])
assert config.integration_type == "api"
assert config.pr_number_explicit is True
@pytest.mark.parametrize("flag,attr", [
("--enable-debug", "enable_debug"),
("--disable-blocking", "disable_blocking"),
("--allow-unverified", "allow_unverified"),
("--enable-diff", "enable_diff")
])
def test_boolean_flags(self, flag, attr):
config = CliConfig.from_args(["--api-token", "test", flag])
assert getattr(config, attr) is True
def test_enable_diff_default_false(self):
"""Test that enable_diff defaults to False"""
config = CliConfig.from_args(["--api-token", "test"])
assert config.enable_diff is False
def test_enable_diff_with_integration_api(self):
"""Test that enable_diff can be used with integration api"""
config = CliConfig.from_args(["--api-token", "test", "--integration", "api", "--enable-diff"])
assert config.enable_diff is True
assert config.integration_type == "api"
def test_strict_blocking_flag(self):
"""Test that --strict-blocking flag is parsed correctly"""
config = CliConfig.from_args(["--api-token", "test", "--strict-blocking"])
assert config.strict_blocking is True
def test_strict_blocking_default_false(self):
"""Test that strict_blocking defaults to False"""
config = CliConfig.from_args(["--api-token", "test"])
assert config.strict_blocking is False
def test_strict_blocking_with_disable_blocking(self):
"""Test that both flags can be set (disable-blocking should win)"""
config = CliConfig.from_args([
"--api-token", "test",
"--strict-blocking",
"--disable-blocking"
])
assert config.strict_blocking is True
assert config.disable_blocking is True
def test_workspace_flag(self):
"""Test that --workspace is parsed and stored correctly."""
config = CliConfig.from_args(["--api-token", "test", "--workspace", "my-workspace"])
assert config.workspace == "my-workspace"
def test_workspace_default_is_none(self):
"""Test that workspace defaults to None when not supplied."""
config = CliConfig.from_args(["--api-token", "test"])
assert config.workspace is None
def test_workspace_is_independent_of_workspace_name(self):
"""--workspace and --workspace-name are distinct flags with distinct purposes."""
config = CliConfig.from_args([
"--api-token", "test",
"--workspace", "my-workspace",
"--sub-path", ".",
"--workspace-name", "monorepo-suffix",
])
assert config.workspace == "my-workspace"
assert config.workspace_name == "monorepo-suffix"
def test_legal_flag_sets_default_artifact_files(self):
config = CliConfig.from_args(["--api-token", "test", "--legal"])
assert config.legal is True
assert config.legal_format == "socket"
assert config.generate_license is True
assert config.json_file == "socket-report.json"
assert config.summary_file == "socket-summary.txt"
assert config.report_link_file == "socket-report-link.txt"
assert config.sbom_file == "socket-sbom.json"
assert config.license_file_name == "socket-license.json"
def test_legal_flag_preserves_explicit_file_paths(self):
config = CliConfig.from_args([
"--api-token", "test",
"--legal",
"--json-file", "custom-report.json",
"--summary-file", "custom-summary.txt",
"--report-link-file", "custom-link.txt",
"--sbom-file", "custom-sbom.json",
"--license-file-name", "custom-license.json",
])
assert config.json_file == "custom-report.json"
assert config.summary_file == "custom-summary.txt"
assert config.report_link_file == "custom-link.txt"
assert config.sbom_file == "custom-sbom.json"
assert config.license_file_name == "custom-license.json"
def test_fossa_legal_format_enables_legal_defaults(self):
config = CliConfig.from_args(["--api-token", "test", "--legal-format", "fossa"])
assert config.legal is True
assert config.legal_format == "fossa"
assert config.generate_license is True
assert config.json_file == "fossa-analyze.json"
assert config.summary_file == "fossa-test.txt"
assert config.report_link_file == "fossa-link.txt"
assert config.sbom_file is None
assert config.license_file_name == "fossa-sbom.json"