Debian Codegraph @

ansible_collections/splunk/es/tests/unit/plugins/module_utils/test_investigation_utils.py

raw
1 # Copyright 2026 Red Hat Inc.
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 """
20 Unit tests for the investigation module utilities.
21
22 These tests verify that the utility functions work correctly for:
23 - Building API paths
24 - Mapping between API and module parameter formats
25 - Status, disposition, and sensitivity value conversions
26
27 Shared utilities are in plugins/module_utils/investigation.py
28 Main-only utilities are in plugins/action/splunk_investigation.py
29 """
30
31
32 # Main-only utilities via ActionModule class
33 from ansible_collections.splunk.es.plugins.action.splunk_investigation import ActionModule
34
35 # Shared utilities (used by both info and main modules)
36 from ansible_collections.splunk.es.plugins.module_utils.investigation import (
37 SENSITIVITY_FROM_API,
38 build_investigation_api_path,
39 map_investigation_from_api,
40 )
41
42 # Shared constants from splunk_utils
43 from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import (
44 DEFAULT_API_APP,
45 DEFAULT_API_NAMESPACE,
46 DEFAULT_API_USER,
47 DISPOSITION_FROM_API,
48 DISPOSITION_TO_API,
49 STATUS_FROM_API,
50 STATUS_TO_API,
51 )
52
53
54 class TestBuildInvestigationApiPath:
55 """Tests for the build_investigation_api_path function.
56
57 This function constructs the REST API path for the investigations endpoint.
58 It should handle default values and custom namespace/user/app overrides.
59 """
60
61 def test_build_investigation_api_path_defaults(self):
62 """Test that default API path is constructed correctly.
63
64 When called without arguments, should use the default namespace,
65 user, and app values defined in the module.
66 """
67 result = build_investigation_api_path()
68
69 expected = (
70 f"{DEFAULT_API_NAMESPACE}/{DEFAULT_API_USER}/{DEFAULT_API_APP}/public/v2/investigations"
71 )
72 assert result == expected
73
74 def test_build_investigation_api_path_custom_namespace(self):
75 """Test API path with custom namespace value."""
76 result = build_investigation_api_path(namespace="customNS")
77
78 expected = f"customNS/{DEFAULT_API_USER}/{DEFAULT_API_APP}/public/v2/investigations"
79 assert result == expected
80
81 def test_build_investigation_api_path_custom_user(self):
82 """Test API path with custom user value."""
83 result = build_investigation_api_path(user="admin")
84
85 expected = f"{DEFAULT_API_NAMESPACE}/admin/{DEFAULT_API_APP}/public/v2/investigations"
86 assert result == expected
87
88 def test_build_investigation_api_path_custom_app(self):
89 """Test API path with custom app value."""
90 result = build_investigation_api_path(app="CustomApp")
91
92 expected = f"{DEFAULT_API_NAMESPACE}/{DEFAULT_API_USER}/CustomApp/public/v2/investigations"
93 assert result == expected
94
95 def test_build_investigation_api_path_all_custom(self):
96 """Test API path with all custom values."""
97 result = build_investigation_api_path(
98 namespace="myNS",
99 user="myuser",
100 app="MyApp",
101 )
102
103 assert result == "myNS/myuser/MyApp/public/v2/investigations"
104
105
106 class TestBuildInvestigationUpdatePath:
107 """Tests for the ActionModule.build_update_path method.
108
109 This method constructs the REST API path for updating a specific investigation.
110 """
111
112 def test_build_investigation_update_path_defaults(self):
113 """Test update API path with default namespace and user."""
114 ref_id = "inv-abc-123"
115 result = ActionModule.build_update_path(ref_id)
116
117 expected = f"{DEFAULT_API_NAMESPACE}/{DEFAULT_API_USER}/{DEFAULT_API_APP}/public/v2/investigations/{ref_id}"
118 assert result == expected
119
120 def test_build_investigation_update_path_custom_namespace(self):
121 """Test update API path with custom namespace."""
122 ref_id = "inv-abc-123"
123 result = ActionModule.build_update_path(ref_id, namespace="customNS")
124
125 expected = (
126 f"customNS/{DEFAULT_API_USER}/{DEFAULT_API_APP}/public/v2/investigations/{ref_id}"
127 )
128 assert result == expected
129
130 def test_build_investigation_update_path_custom_user(self):
131 """Test update API path with custom user."""
132 ref_id = "inv-abc-123"
133 result = ActionModule.build_update_path(ref_id, user="admin")
134
135 expected = (
136 f"{DEFAULT_API_NAMESPACE}/admin/{DEFAULT_API_APP}/public/v2/investigations/{ref_id}"
137 )
138 assert result == expected
139
140 def test_build_investigation_update_path_guid_ref_id(self):
141 """Test update API path with GUID-style ref_id."""
142 ref_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
143 result = ActionModule.build_update_path(ref_id)
144
145 assert ref_id in result
146
147
148 class TestBuildInvestigationFindingsPath:
149 """Tests for the ActionModule.build_findings_path method.
150
151 This method constructs the REST API path for adding findings to an investigation.
152 """
153
154 def test_build_investigation_findings_path_defaults(self):
155 """Test findings API path with default values."""
156 ref_id = "inv-abc-123"
157 result = ActionModule.build_findings_path(ref_id)
158
159 expected = f"{DEFAULT_API_NAMESPACE}/{DEFAULT_API_USER}/{DEFAULT_API_APP}/public/v2/investigations/{ref_id}/findings"
160 assert result == expected
161 assert result.endswith("/findings")
162
163 def test_build_investigation_findings_path_custom_namespace(self):
164 """Test findings API path with custom namespace."""
165 ref_id = "inv-abc-123"
166 result = ActionModule.build_findings_path(ref_id, namespace="customNS")
167
168 assert "customNS" in result
169 assert result.endswith("/findings")
170
171 def test_build_investigation_findings_path_includes_ref_id(self):
172 """Test that ref_id is included in the path."""
173 ref_id = "unique-investigation-id"
174 result = ActionModule.build_findings_path(ref_id)
175
176 assert ref_id in result
177
178
179 class TestMapInvestigationFromApi:
180 """Tests for the map_investigation_from_api function.
181
182 This function converts the API response format to the module parameter format.
183 It handles key renaming, status/disposition/sensitivity conversion.
184 """
185
186 def test_map_investigation_from_api_basic(self):
187 """Test basic field mapping from API to module format."""
188 api_response = {
189 "investigation_guid": "inv-12345",
190 "name": "Test Investigation",
191 "description": "A test description",
192 "owner": "admin",
193 "urgency": "high",
194 }
195
196 result = map_investigation_from_api(api_response)
197
198 assert result["investigation_ref_id"] == "inv-12345"
199 assert result["name"] == "Test Investigation"
200 assert result["description"] == "A test description"
201 assert result["owner"] == "admin"
202 assert result["urgency"] == "high"
203
204 def test_map_investigation_from_api_with_investigation_guid(self):
205 """Test that investigation_guid is extracted as investigation_ref_id."""
206 api_response = {
207 "investigation_guid": "abc-123-def-456",
208 "name": "Test Investigation",
209 }
210
211 result = map_investigation_from_api(api_response)
212
213 assert result["investigation_ref_id"] == "abc-123-def-456"
214
215 def test_map_investigation_from_api_status_conversion(self):
216 """Test that numeric status codes are converted to string names."""
217 api_response = {
218 "name": "Test",
219 "status": "1", # API uses numeric codes
220 }
221
222 result = map_investigation_from_api(api_response)
223
224 assert result["status"] == "new"
225
226 def test_map_investigation_from_api_all_status_values(self):
227 """Test all status value conversions."""
228 for api_value, module_value in STATUS_FROM_API.items():
229 api_response = {"name": "Test", "status": api_value}
230 result = map_investigation_from_api(api_response)
231 assert result["status"] == module_value
232
233 def test_map_investigation_from_api_disposition_conversion(self):
234 """Test that disposition codes are converted to string names."""
235 api_response = {
236 "name": "Test",
237 "disposition": "disposition:6", # API format
238 }
239
240 result = map_investigation_from_api(api_response)
241
242 assert result["disposition"] == "undetermined"
243
244 def test_map_investigation_from_api_all_disposition_values(self):
245 """Test all disposition value conversions."""
246 for api_value, module_value in DISPOSITION_FROM_API.items():
247 api_response = {"name": "Test", "disposition": api_value}
248 result = map_investigation_from_api(api_response)
249 assert result["disposition"] == module_value
250
251 def test_map_investigation_from_api_sensitivity_conversion(self):
252 """Test that capitalized sensitivity is converted to lowercase."""
253 api_response = {
254 "name": "Test",
255 "sensitivity": "Amber", # API uses capitalized
256 }
257
258 result = map_investigation_from_api(api_response)
259
260 assert result["sensitivity"] == "amber"
261
262 def test_map_investigation_from_api_all_sensitivity_values(self):
263 """Test all sensitivity value conversions."""
264 for api_value, module_value in SENSITIVITY_FROM_API.items():
265 api_response = {"name": "Test", "sensitivity": api_value}
266 result = map_investigation_from_api(api_response)
267 assert result["sensitivity"] == module_value
268
269 def test_map_investigation_from_api_with_findings(self):
270 """Test extraction of finding_ids from consolidated_findings."""
271 api_response = {
272 "name": "Test",
273 "consolidated_findings": {
274 "event_id": ["finding-001", "finding-002", "finding-003"],
275 },
276 }
277
278 result = map_investigation_from_api(api_response)
279
280 assert "finding_ids" in result
281 assert result["finding_ids"] == ["finding-001", "finding-002", "finding-003"]
282
283 def test_map_investigation_from_api_with_single_finding(self):
284 """Test extraction of single finding_id (string instead of list)."""
285 api_response = {
286 "name": "Test",
287 "consolidated_findings": {
288 "event_id": "finding-001", # Single string
289 },
290 }
291
292 result = map_investigation_from_api(api_response)
293
294 assert "finding_ids" in result
295 assert result["finding_ids"] == ["finding-001"]
296
297 def test_map_investigation_from_api_empty_config(self):
298 """Test handling of empty configuration."""
299 result = map_investigation_from_api({})
300
301 assert result == {}
302
303 def test_map_investigation_from_api_none_values(self):
304 """Test that None values are excluded."""
305 api_response = {
306 "name": "Test",
307 "description": None,
308 "status": None,
309 }
310
311 result = map_investigation_from_api(api_response)
312
313 assert result["name"] == "Test"
314 assert "description" not in result
315 assert "status" not in result
316
317
318 class TestMapInvestigationToApi:
319 """Tests for the ActionModule.map_to_api method.
320
321 This method converts module parameters to API payload format.
322 """
323
324 def test_map_investigation_to_api_basic(self):
325 """Test basic field mapping from module to API format."""
326 investigation = {
327 "name": "Test Investigation",
328 "description": "A test description",
329 "owner": "admin",
330 "urgency": "high",
331 }
332
333 result = ActionModule.map_to_api(investigation)
334
335 assert result["name"] == "Test Investigation"
336 assert result["description"] == "A test description"
337 assert result["owner"] == "admin"
338 assert result["urgency"] == "high"
339
340 def test_map_investigation_to_api_status_conversion(self):
341 """Test that string status is converted to numeric code."""
342 investigation = {
343 "name": "Test",
344 "status": "new",
345 }
346
347 result = ActionModule.map_to_api(investigation)
348
349 assert result["status"] == "1"
350
351 def test_map_investigation_to_api_all_status_values(self):
352 """Test all status value conversions to API format."""
353 for module_value, api_value in STATUS_TO_API.items():
354 investigation = {"name": "Test", "status": module_value}
355 result = ActionModule.map_to_api(investigation)
356 assert result["status"] == api_value
357
358 def test_map_investigation_to_api_disposition_conversion(self):
359 """Test that string disposition is converted to API format."""
360 investigation = {
361 "name": "Test",
362 "disposition": "true_positive",
363 }
364
365 result = ActionModule.map_to_api(investigation)
366
367 assert result["disposition"] == "disposition:1"
368
369 def test_map_investigation_to_api_all_disposition_values(self):
370 """Test all disposition value conversions to API format."""
371 for module_value, api_value in DISPOSITION_TO_API.items():
372 investigation = {"name": "Test", "disposition": module_value}
373 result = ActionModule.map_to_api(investigation)
374 assert result["disposition"] == api_value
375
376 def test_map_investigation_to_api_sensitivity_conversion(self):
377 """Test that lowercase sensitivity is converted to capitalized."""
378 investigation = {
379 "name": "Test",
380 "sensitivity": "amber",
381 }
382
383 result = ActionModule.map_to_api(investigation)
384
385 assert result["sensitivity"] == "Amber"
386
387 def test_map_investigation_to_api_all_sensitivity_values(self):
388 """Test all sensitivity value conversions to API format."""
389 for module_value, api_value in ActionModule.SENSITIVITY_TO_API.items():
390 investigation = {"name": "Test", "sensitivity": module_value}
391 result = ActionModule.map_to_api(investigation)
392 assert result["sensitivity"] == api_value
393
394
395 class TestMapInvestigationUpdateToApi:
396 """Tests for the ActionModule.map_update_to_api method.
397
398 This method converts module parameters to the update API payload format.
399 Only updatable fields are included in the result.
400 """
401
402 def test_map_investigation_update_to_api_description(self):
403 """Test that description is passed through."""
404 investigation = {"description": "Updated description"}
405
406 result = ActionModule.map_update_to_api(investigation)
407
408 assert result["description"] == "Updated description"
409
410 def test_map_investigation_update_to_api_status(self):
411 """Test that status is converted to numeric code."""
412 investigation = {"status": "resolved"}
413
414 result = ActionModule.map_update_to_api(investigation)
415
416 assert result["status"] == "4"
417
418 def test_map_investigation_update_to_api_urgency(self):
419 """Test that urgency is passed through."""
420 investigation = {"urgency": "high"}
421
422 result = ActionModule.map_update_to_api(investigation)
423
424 assert result["urgency"] == "high"
425
426 def test_map_investigation_update_to_api_owner(self):
427 """Test that owner is passed through."""
428 investigation = {"owner": "analyst1"}
429
430 result = ActionModule.map_update_to_api(investigation)
431
432 assert result["owner"] == "analyst1"
433
434 def test_map_investigation_update_to_api_disposition(self):
435 """Test that disposition is converted to API format."""
436 investigation = {"disposition": "false_positive"}
437
438 result = ActionModule.map_update_to_api(investigation)
439
440 assert result["disposition"] == "disposition:3"
441
442 def test_map_investigation_update_to_api_sensitivity(self):
443 """Test that sensitivity is converted to capitalized format."""
444 investigation = {"sensitivity": "red"}
445
446 result = ActionModule.map_update_to_api(investigation)
447
448 assert result["sensitivity"] == "Red"
449
450 def test_map_investigation_update_to_api_all_updatable_fields(self):
451 """Test mapping with all updatable fields."""
452 investigation = {
453 "description": "Updated desc",
454 "owner": "analyst",
455 "status": "in_progress",
456 "urgency": "critical",
457 "disposition": "true_positive",
458 "sensitivity": "amber",
459 }
460
461 result = ActionModule.map_update_to_api(investigation)
462
463 assert result["description"] == "Updated desc"
464 assert result["owner"] == "analyst"
465 assert result["status"] == "2"
466 assert result["urgency"] == "critical"
467 assert result["disposition"] == "disposition:1"
468 assert result["sensitivity"] == "Amber"
469
470 def test_map_investigation_update_to_api_ignores_non_updatable(self):
471 """Test that non-updatable fields are ignored."""
472 investigation = {
473 "name": "Should be ignored", # name cannot be updated
474 "owner": "admin",
475 }
476
477 result = ActionModule.map_update_to_api(investigation)
478
479 assert "name" not in result
480 assert result["owner"] == "admin"
481
482 def test_map_investigation_update_to_api_none_values(self):
483 """Test that None values are excluded."""
484 investigation = {
485 "owner": None,
486 "status": "new",
487 }
488
489 result = ActionModule.map_update_to_api(investigation)
490
491 assert "owner" not in result
492 assert result["status"] == "1"
493
494 def test_map_investigation_update_to_api_empty_dict(self):
495 """Test handling of empty dictionary."""
496 result = ActionModule.map_update_to_api({})
497
498 assert result == {}
499
500
501 class TestStatusMappings:
502 """Tests for status mapping constants.
503
504 Verify that STATUS_TO_API and STATUS_FROM_API are consistent inverses.
505 """
506
507 def test_status_mappings_are_inverses(self):
508 """Test that TO_API and FROM_API mappings are consistent inverses."""
509 for module_val, api_val in STATUS_TO_API.items():
510 assert STATUS_FROM_API[api_val] == module_val
511
512 def test_status_mappings_complete(self):
513 """Test that all expected status values are mapped."""
514 expected_statuses = ["unassigned", "new", "in_progress", "pending", "resolved", "closed"]
515
516 for status in expected_statuses:
517 assert status in STATUS_TO_API
518
519
520 class TestDispositionMappings:
521 """Tests for disposition mapping constants.
522
523 Verify that DISPOSITION_TO_API and DISPOSITION_FROM_API are consistent inverses.
524 """
525
526 def test_disposition_mappings_are_inverses(self):
527 """Test that TO_API and FROM_API mappings are consistent inverses."""
528 for module_val, api_val in DISPOSITION_TO_API.items():
529 assert DISPOSITION_FROM_API[api_val] == module_val
530
531 def test_disposition_mappings_complete(self):
532 """Test that all expected disposition values are mapped."""
533 expected_dispositions = [
534 "unassigned",
535 "true_positive",
536 "benign_positive",
537 "false_positive",
538 "false_positive_inaccurate_data",
539 "other",
540 "undetermined",
541 ]
542
543 for disposition in expected_dispositions:
544 assert disposition in DISPOSITION_TO_API
545
546
547 class TestSensitivityMappings:
548 """Tests for sensitivity mapping constants.
549
550 Verify that SENSITIVITY_TO_API and SENSITIVITY_FROM_API are consistent inverses.
551 """
552
553 def test_sensitivity_mappings_are_inverses(self):
554 """Test that TO_API and FROM_API mappings are consistent inverses."""
555 for module_val, api_val in ActionModule.SENSITIVITY_TO_API.items():
556 assert SENSITIVITY_FROM_API[api_val] == module_val
557
558 def test_sensitivity_mappings_complete(self):
559 """Test that all expected sensitivity values are mapped."""
560 expected_sensitivities = ["white", "green", "amber", "red", "unassigned"]
561
562 for sensitivity in expected_sensitivities:
563 assert sensitivity in ActionModule.SENSITIVITY_TO_API
564
565
566 class TestUpdatableFields:
567 """Tests for the ActionModule.UPDATABLE_FIELDS constant."""
568
569 def test_updatable_fields_contains_expected(self):
570 """Test that UPDATABLE_FIELDS contains the correct fields."""
571 expected = [
572 "description",
573 "status",
574 "disposition",
575 "owner",
576 "urgency",
577 "sensitivity",
578 "investigation_type",
579 ]
580
581 assert set(ActionModule.UPDATABLE_FIELDS) == set(expected)
582
583 def test_updatable_fields_excludes_name(self):
584 """Test that name is not in UPDATABLE_FIELDS."""
585 assert "name" not in ActionModule.UPDATABLE_FIELDS
586
587 def test_updatable_fields_excludes_finding_ids(self):
588 """Test that finding_ids is not in UPDATABLE_FIELDS (handled separately)."""
589 assert "finding_ids" not in ActionModule.UPDATABLE_FIELDS