-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest_case_integration.py
More file actions
472 lines (401 loc) · 13.5 KB
/
test_case_integration.py
File metadata and controls
472 lines (401 loc) · 13.5 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
"""Integration tests for the SecOps CLI case commands.
These tests require valid credentials and API access.
They interact with real Chronicle API endpoints via CLI.
"""
import json
import subprocess
import pytest
@pytest.mark.integration
def test_cli_list_and_get_cases_workflow(cli_env, common_args):
"""Test CLI case list and get workflow.
TODO: Remove 401 skip logic once SOAR IAM role issue is fixed.
"""
# Test basic list
list_cmd = ["secops"] + common_args + ["case", "list", "--page-size", "3"]
list_result = subprocess.run(
list_cmd, env=cli_env, capture_output=True, text=True
)
# Check for 401/Unauthorized or auth errors in stderr or stdout
if list_result.returncode != 0:
error_output = list_result.stderr + list_result.stdout
auth_errors = [
"401",
"Unauthorized",
"AuthenticationError",
"Failed to get credentials",
"DefaultCredentialsError",
]
if any(err in error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: {error_output[:200]}"
)
assert list_result.returncode == 0
try:
list_output = json.loads(list_result.stdout)
assert "cases" in list_output
assert isinstance(list_output["cases"], list)
assert "totalSize" in list_output
except json.JSONDecodeError:
assert "Error:" not in list_result.stdout
# Test list with --as-list flag
as_list_cmd = (
["secops"]
+ common_args
+ ["case", "list", "--page-size", "3", "--as-list"]
)
as_list_result = subprocess.run(
as_list_cmd, env=cli_env, capture_output=True, text=True
)
assert as_list_result.returncode == 0
try:
as_list_output = json.loads(as_list_result.stdout)
assert isinstance(as_list_output, list)
if as_list_output:
assert "name" in as_list_output[0]
except json.JSONDecodeError:
assert "Error:" not in as_list_result.stdout
# Test list with filter
filter_cmd = (
["secops"]
+ common_args
+ ["case", "list", "--page-size", "5", "--filter", 'status = "OPENED"']
)
filter_result = subprocess.run(
filter_cmd, env=cli_env, capture_output=True, text=True
)
assert filter_result.returncode == 0
try:
filter_output = json.loads(filter_result.stdout)
assert "cases" in filter_output
except json.JSONDecodeError:
assert "Error:" not in filter_result.stdout
# Test get case by ID
try:
if list_output.get("cases"):
case_name = list_output["cases"][0].get("name", "")
case_id = case_name.split("/")[-1]
if case_id:
get_cmd = (
["secops"] + common_args + ["case", "get", "--id", case_id]
)
get_result = subprocess.run(
get_cmd, env=cli_env, capture_output=True, text=True
)
assert get_result.returncode == 0
get_output = json.loads(get_result.stdout)
assert "id" in get_output or "display_name" in get_output
assert "priority" in get_output
assert "status" in get_output
except (json.JSONDecodeError, KeyError):
pass
@pytest.mark.integration
def test_cli_case_update(cli_env, common_args):
"""Test the case update command.
TODO: Remove 401 skip logic once SOAR IAM role issue is fixed.
"""
# Use dedicated test case ID
case_id = "7418669"
# Get original case state
get_cmd = ["secops"] + common_args + ["case", "get", "--id", case_id]
get_result = subprocess.run(
get_cmd, env=cli_env, capture_output=True, text=True
)
if get_result.returncode != 0:
error_output = get_result.stderr + get_result.stdout
auth_errors = [
"401",
"Unauthorized",
"AuthenticationError",
"Failed to get credentials",
"DefaultCredentialsError",
]
if any(err in error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: {error_output[:200]}"
)
pytest.skip("Unable to get test case")
try:
get_output = json.loads(get_result.stdout)
original_priority = get_output.get("priority", "PRIORITY_MEDIUM")
# Determine new priority
new_priority = (
"PRIORITY_MEDIUM"
if original_priority == "PRIORITY_HIGH"
else "PRIORITY_HIGH"
)
# Update the case
update_cmd = (
["secops"]
+ common_args
+ [
"case",
"update",
"--id",
case_id,
"--data",
f'{{"priority": "{new_priority}"}}',
"--update-mask",
"priority",
]
)
update_result = subprocess.run(
update_cmd, env=cli_env, capture_output=True, text=True
)
# Check for 401/Unauthorized or auth errors
if update_result.returncode != 0:
error_output = update_result.stderr + update_result.stdout
auth_errors = [
"401",
"Unauthorized",
"AuthenticationError",
"Failed to get credentials",
"DefaultCredentialsError",
]
if any(err in error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: "
f"{error_output[:200]}"
)
assert update_result.returncode == 0
update_output = json.loads(update_result.stdout)
assert update_output.get("priority") == new_priority
# Cleanup: Restore original priority
restore_cmd = (
["secops"]
+ common_args
+ [
"case",
"update",
"--id",
case_id,
"--data",
f'{{"priority": "{original_priority}"}}',
"--update-mask",
"priority",
]
)
subprocess.run(restore_cmd, env=cli_env, capture_output=True)
except (json.JSONDecodeError, KeyError):
pytest.skip("Unable to parse JSON output or extract data")
@pytest.mark.integration
def test_cli_case_bulk_add_tag(cli_env, common_args):
"""Test the case bulk-add-tag command.
TODO: Remove 401 skip logic once SOAR IAM role issue is fixed.
"""
# Use dedicated test case ID
case_ids = ["7418669"]
# Test bulk add tag
bulk_cmd = (
["secops"]
+ common_args
+ [
"case",
"bulk-add-tag",
"--ids",
",".join(case_ids),
"--tags",
"cli-integration-test",
]
)
bulk_result = subprocess.run(
bulk_cmd, env=cli_env, capture_output=True, text=True
)
# Check for 401/Unauthorized or auth errors
if bulk_result.returncode != 0:
error_output = bulk_result.stderr + bulk_result.stdout
auth_errors = [
"401",
"Unauthorized",
"AuthenticationError",
"Failed to get credentials",
"DefaultCredentialsError",
]
if any(err in error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: " f"{error_output[:200]}"
)
assert bulk_result.returncode == 0
@pytest.mark.integration
def test_cli_case_bulk_assign(cli_env, common_args):
"""Test the case bulk-assign command.
TODO: Remove 401 skip logic once SOAR IAM role issue is fixed.
"""
# Use dedicated test case ID
case_ids = ["7418669"]
bulk_cmd = (
["secops"]
+ common_args
+ [
"case",
"bulk-assign",
"--ids",
",".join(case_ids),
"--username",
"'@Administrator'",
]
)
bulk_result = subprocess.run(
bulk_cmd, env=cli_env, capture_output=True, text=True
)
# Skip if API returns auth or INTERNAL/500 error
if bulk_result.returncode != 0:
error_output = bulk_result.stderr + bulk_result.stdout
auth_errors = [
"401",
"Unauthorized",
"AuthenticationError",
"Failed to get credentials",
"DefaultCredentialsError",
]
if any(err in error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: " f"{error_output[:200]}"
)
if "INTERNAL" in error_output or "500" in error_output:
pytest.skip(
f"Bulk assign API returned INTERNAL error: "
f"{error_output[:200]}"
)
assert bulk_result.returncode == 0
@pytest.mark.integration
def test_cli_case_bulk_change_priority(cli_env, common_args):
"""Test the case bulk-change-priority command.
TODO: Remove 401 skip logic once SOAR IAM role issue is fixed.
"""
# Use dedicated test case ID
case_ids = ["7418669"]
bulk_cmd = (
["secops"]
+ common_args
+ [
"case",
"bulk-change-priority",
"--ids",
",".join(case_ids),
"--priority",
"MEDIUM",
]
)
bulk_result = subprocess.run(
bulk_cmd, env=cli_env, capture_output=True, text=True
)
# Check for 401/Unauthorized or auth errors
if bulk_result.returncode != 0:
error_output = bulk_result.stderr + bulk_result.stdout
auth_errors = [
"401",
"Unauthorized",
"AuthenticationError",
"Failed to get credentials",
"DefaultCredentialsError",
]
if any(err in error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: " f"{error_output[:200]}"
)
assert bulk_result.returncode == 0
@pytest.mark.integration
def test_cli_case_bulk_change_stage(cli_env, common_args):
"""Test the case bulk-change-stage command.
TODO: Remove 401 skip logic once SOAR IAM role issue is fixed.
"""
# Use dedicated test case ID
case_ids = ["7418669"]
bulk_cmd = (
["secops"]
+ common_args
+ [
"case",
"bulk-change-stage",
"--ids",
",".join(case_ids),
"--stage",
"Triage",
]
)
bulk_result = subprocess.run(
bulk_cmd, env=cli_env, capture_output=True, text=True
)
# Check for 401/Unauthorized or auth errors
if bulk_result.returncode != 0:
error_output = bulk_result.stderr + bulk_result.stdout
auth_errors = [
"401",
"Unauthorized",
"AuthenticationError",
"Failed to get credentials",
"DefaultCredentialsError",
]
if any(err in error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: " f"{error_output[:200]}"
)
assert bulk_result.returncode == 0
@pytest.mark.integration
def test_cli_case_bulk_close_reopen_workflow(cli_env, common_args):
"""Test the case bulk-close and bulk-reopen commands in workflow.
TODO: Remove 401 skip logic once SOAR IAM role issue is fixed.
"""
# Use dedicated test case ID
case_ids = ["7418669"]
auth_errors = [
"401",
"Unauthorized",
"AuthenticationError",
"Failed to get credentials",
"DefaultCredentialsError",
]
try:
# Test bulk close
close_cmd = (
["secops"]
+ common_args
+ [
"case",
"bulk-close",
"--ids",
",".join(case_ids),
"--close-reason",
"MAINTENANCE",
"--root-cause",
"CLI integration test",
]
)
close_result = subprocess.run(
close_cmd, env=cli_env, capture_output=True, text=True
)
# Check for 401/Unauthorized or auth errors
if close_result.returncode != 0:
error_output = close_result.stderr + close_result.stdout
if any(err in error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: "
f"{error_output[:200]}"
)
assert close_result.returncode == 0
finally:
# Cleanup: Test bulk reopen
reopen_cmd = (
["secops"]
+ common_args
+ [
"case",
"bulk-reopen",
"--ids",
",".join(case_ids),
"--reopen-comment",
"CLI integration test cleanup",
]
)
reopen_result = subprocess.run(
reopen_cmd, env=cli_env, capture_output=True, text=True
)
# Check for 401/Unauthorized or auth errors
if reopen_result.returncode != 0:
reopen_error_output = reopen_result.stderr + reopen_result.stdout
if any(err in reopen_error_output for err in auth_errors):
pytest.skip(
f"Skipping due to SOAR IAM/auth issue: "
f"{reopen_error_output[:200]}"
)
assert reopen_result.returncode == 0