-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_comprehensive_integration.py
More file actions
354 lines (292 loc) · 12.3 KB
/
test_comprehensive_integration.py
File metadata and controls
354 lines (292 loc) · 12.3 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""
Comprehensive integration tests for the Socket SDK Python client.
These tests require the following environment variables:
- SOCKET_SECURITY_API_KEY: Your Socket.dev API key
- SOCKET_ORG_SLUG: Your organization slug
- SOCKET_REPO_SLUG: A repository slug for testing (optional for some tests)
Run with: python -m pytest tests/integration/test_comprehensive_integration.py -v
"""
import unittest
import os
import tempfile
import shutil
import json
from socketdev import socketdev
from socketdev.fullscans import FullScanParams
class TestSocketSDKIntegration(unittest.TestCase):
"""Comprehensive integration tests for the Socket SDK."""
@classmethod
def setUpClass(cls):
"""Set up test environment and validate required environment variables."""
cls.api_key = os.getenv("SOCKET_SECURITY_API_KEY", "")
cls.org_slug = os.getenv("SOCKET_ORG_SLUG", "")
cls.repo_slug = os.getenv("SOCKET_REPO_SLUG", "")
# Only require API key and org_slug for most tests
missing = []
if not cls.api_key:
missing.append("SOCKET_SECURITY_API_KEY")
if not cls.org_slug:
missing.append("SOCKET_ORG_SLUG")
if missing:
raise RuntimeError(
f"Missing required environment variables: {', '.join(missing)}\n"
f"SOCKET_REPO_SLUG is optional for some tests."
)
cls.sdk = socketdev(token=cls.api_key)
# Prepare temp directory for test files
cls.temp_dir = tempfile.mkdtemp()
cls.package_json_path = os.path.join(cls.temp_dir, "package.json")
cls.package_empty_path = os.path.join(cls.temp_dir, "package-empty.json")
# Create test package.json files
test_package = {
"name": "test-integration-project",
"version": "1.0.0",
"dependencies": {
"lodash": "4.17.21"
}
}
empty_package = {
"name": "test-empty-project",
"version": "1.0.0"
}
with open(cls.package_json_path, 'w') as f:
json.dump(test_package, f, indent=2)
with open(cls.package_empty_path, 'w') as f:
json.dump(empty_package, f, indent=2)
@classmethod
def tearDownClass(cls):
"""Clean up test environment."""
if hasattr(cls, 'temp_dir'):
shutil.rmtree(cls.temp_dir)
def setUp(self):
"""Set up for each test."""
self.created_scan_ids = []
self.created_diff_scan_ids = []
def tearDown(self):
"""Clean up after each test."""
# Clean up any created resources
for diff_scan_id in self.created_diff_scan_ids:
try:
self.sdk.diffscans.delete(self.org_slug, diff_scan_id)
except Exception:
pass # Ignore cleanup errors
for scan_id in self.created_scan_ids:
try:
self.sdk.fullscans.delete(self.org_slug, scan_id)
except Exception:
pass # Ignore cleanup errors
def test_org_endpoints(self):
"""Test organization-related endpoints."""
# Test getting organization info
org_info = self.sdk.org.get(self.org_slug)
self.assertIsInstance(org_info, dict)
def test_quota_endpoint(self):
"""Test quota endpoint."""
try:
quota_info = self.sdk.quota.get()
self.assertIsInstance(quota_info, dict)
except Exception as e:
# Some organizations might not have quota access
print(f"Quota endpoint not available: {e}")
def test_settings_endpoints(self):
"""Test settings endpoints."""
try:
settings = self.sdk.settings.get(self.org_slug)
self.assertIsInstance(settings, dict)
except Exception as e:
# Some organizations might not have settings access
print(f"Settings endpoint not available: {e}")
def test_diffscans_list(self):
"""Test listing diff scans."""
result = self.sdk.diffscans.list(self.org_slug)
self.assertIsInstance(result, dict)
self.assertIn("results", result)
def test_fullscans_basic_workflow(self):
"""Test basic full scan workflow."""
if not self.repo_slug:
self.skipTest("SOCKET_REPO_SLUG not provided")
params = FullScanParams(
org_slug=self.org_slug,
repo=self.repo_slug,
branch="main",
commit_message="Integration test commit",
commit_hash="1234567890abcdef1234567890abcdef12345678",
pull_request=999,
committers=["integration-tester"],
integration_type="api"
)
# Create a full scan
with open(self.package_json_path, "rb") as f:
files = [("file", ("package.json", f))]
result = self.sdk.fullscans.post(files=files, params=params)
self.assertIsInstance(result, dict)
self.assertIn("id", result)
scan_id = result["id"]
self.created_scan_ids.append(scan_id)
# Get the scan
scan_info = self.sdk.fullscans.get(self.org_slug, {"id": scan_id})
self.assertIsInstance(scan_info, dict)
# List scans
scans_list = self.sdk.fullscans.list(self.org_slug)
self.assertIsInstance(scans_list, dict)
def test_diffscans_complete_workflow(self):
"""Test complete diff scan workflow."""
if not self.repo_slug:
self.skipTest("SOCKET_REPO_SLUG not provided")
# Create 'before' scan with empty package.json
params_before = FullScanParams(
org_slug=self.org_slug,
repo=self.repo_slug,
branch="main",
commit_message="Before scan",
commit_hash="before000000000000000000000000000000000000",
pull_request=998,
committers=["integration-tester"],
integration_type="api"
)
with open(self.package_empty_path, "rb") as f:
files_before = [("file", ("package.json", f))]
before_result = self.sdk.fullscans.post(files=files_before, params=params_before)
self.assertIn("id", before_result)
before_id = before_result["id"]
self.created_scan_ids.append(before_id)
# Create 'after' scan with dependencies
params_after = FullScanParams(
org_slug=self.org_slug,
repo=self.repo_slug,
branch="main",
commit_message="After scan",
commit_hash="after0000000000000000000000000000000000000",
pull_request=999,
committers=["integration-tester"],
integration_type="api"
)
with open(self.package_json_path, "rb") as f:
files_after = [("file", ("package.json", f))]
after_result = self.sdk.fullscans.post(files=files_after, params=params_after)
self.assertIn("id", after_result)
after_id = after_result["id"]
self.created_scan_ids.append(after_id)
# Create diff scan from IDs
diff_params = {
"before": before_id,
"after": after_id,
"description": "Integration test diff scan"
}
diff_result = self.sdk.diffscans.create_from_ids(self.org_slug, diff_params)
self.assertIsInstance(diff_result, dict)
# Extract diff scan ID
diff_scan_id = (
diff_result.get("id") or
diff_result.get("diff_scan_id") or
(diff_result.get("diff_scan", {}).get("id") if "diff_scan" in diff_result else None)
)
self.assertIsNotNone(diff_scan_id)
self.created_diff_scan_ids.append(diff_scan_id)
# Get the diff scan
get_result = self.sdk.diffscans.get(self.org_slug, diff_scan_id)
self.assertIsInstance(get_result, dict)
# Get GFM output
gfm_result = self.sdk.diffscans.gfm(self.org_slug, diff_scan_id)
self.assertIsInstance(gfm_result, dict)
def test_diffscans_from_repo(self):
"""Test creating diff scans from repository."""
if not self.repo_slug:
self.skipTest("SOCKET_REPO_SLUG not provided")
with open(self.package_json_path, "rb") as f:
files = [("file", ("package.json", f))]
params = {"description": "Integration test diff scan from repo"}
result = self.sdk.diffscans.create_from_repo(
self.org_slug,
self.repo_slug,
files,
params
)
self.assertIsInstance(result, dict)
# Extract diff scan ID for cleanup
diff_scan_id = (
result.get("id") or
result.get("diff_scan_id") or
(result.get("diff_scan", {}).get("id") if "diff_scan" in result else None)
)
if diff_scan_id:
self.created_diff_scan_ids.append(diff_scan_id)
def test_npm_endpoints(self):
"""Test NPM-related endpoints."""
# Test getting package issues - this should work for most packages
try:
issues = self.sdk.npm.issues("lodash", "4.17.21")
self.assertIsInstance(issues, dict)
except Exception as e:
print(f"NPM issues endpoint not available: {e}")
def test_export_endpoint(self):
"""Test export functionality."""
try:
result = self.sdk.export.get(self.org_slug)
self.assertIsInstance(result, dict)
except Exception as e:
print(f"Export endpoint not available: {e}")
def test_purl_endpoint(self):
"""Test PURL (Package URL) functionality."""
try:
# Test with a common npm package
purl = "pkg:npm/lodash@4.17.21"
result = self.sdk.purl.post([purl])
self.assertIsInstance(result, dict)
except Exception as e:
print(f"PURL endpoint not available: {e}")
def test_report_endpoints(self):
"""Test report endpoints."""
if not self.repo_slug:
self.skipTest("SOCKET_REPO_SLUG not provided")
# List reports
reports = self.sdk.report.list(self.org_slug)
self.assertIsInstance(reports, dict)
def test_new_endpoints_basic(self):
"""Test basic functionality of newer endpoints."""
# Test Labels
try:
labels = self.sdk.labels.list(self.org_slug)
self.assertIsInstance(labels, dict)
except Exception as e:
# Some endpoints might not be available for all organizations
print(f"Labels endpoint not available: {e}")
# Test License Metadata
try:
license_data = self.sdk.licensemetadata.get()
self.assertIsInstance(license_data, dict)
except Exception as e:
print(f"License metadata endpoint not available: {e}")
# Test Threat Feed
try:
threat_feed = self.sdk.threatfeed.get()
self.assertIsInstance(threat_feed, dict)
except Exception as e:
print(f"Threat feed endpoint not available: {e}")
# Test API Tokens (list only, don't create)
try:
tokens = self.sdk.apitokens.list()
self.assertIsInstance(tokens, dict)
except Exception as e:
print(f"API tokens endpoint not available: {e}")
# Test Analytics
try:
analytics = self.sdk.analytics.get(self.org_slug)
self.assertIsInstance(analytics, dict)
except Exception as e:
print(f"Analytics endpoint not available: {e}")
# Test Alert Types
try:
alert_types = self.sdk.alerttypes.get()
self.assertIsInstance(alert_types, dict)
except Exception as e:
print(f"Alert types endpoint not available: {e}")
def test_dependencies_endpoint(self):
"""Test dependencies endpoint."""
try:
deps = self.sdk.dependencies.get(self.org_slug)
self.assertIsInstance(deps, dict)
except Exception as e:
print(f"Dependencies endpoint not available: {e}")
if __name__ == "__main__":
unittest.main()