Debian Codegraph @

ansible_collections/splunk/es/tests/unit/plugins/action/test_es_investigation.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 splunk_investigation action plugin.
21
22 These tests verify that the splunk_investigation action plugin works correctly for:
23 - Creating new investigations (without investigation_ref_id)
24 - Updating existing investigations (with investigation_ref_id)
25 - Idempotency (no changes when state matches)
26 - Error handling (missing parameters, investigation not found)
27 - Check mode behavior
28 - Adding findings to investigations
29
30 The tests use mocking to simulate Splunk API responses without requiring
31 a real Splunk server connection.
32 """
33
34
35 import copy
36 import tempfile
37
38 from unittest.mock import MagicMock, patch
39
40 from ansible.playbook.task import Task
41 from ansible.template import Templar
42
43 from ansible_collections.splunk.es.plugins.action.splunk_investigation import ActionModule
44 from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest
45
46
47 # Test data: API Response Payloads
48 # These represent what the Splunk API returns for investigations.
49 INVESTIGATION_API_RESPONSE = {
50 "investigation_guid": "inv-12345-abcde",
51 "name": "Security Incident 2026-01",
52 "description": "Investigation into suspicious login activity",
53 "owner": "admin",
54 "status": "1", # "new" in API format
55 "urgency": "high",
56 "disposition": "disposition:6", # "undetermined" in API format
57 "sensitivity": "Amber", # API uses capitalized
58 "consolidated_findings": {
59 "event_id": ["finding-001@@notable@@time123"],
60 },
61 }
62
63 INVESTIGATION_API_RESPONSE_UPDATED = {
64 "investigation_guid": "inv-12345-abcde",
65 "name": "Security Incident 2026-01",
66 "description": "Updated investigation description",
67 "owner": "analyst1",
68 "status": "4", # "resolved" in API format
69 "urgency": "high",
70 "disposition": "disposition:1", # "true_positive" in API format
71 "sensitivity": "Red",
72 "consolidated_findings": {
73 "event_id": ["finding-001@@notable@@time123", "finding-002@@notable@@time456"],
74 },
75 }
76
77
78 # Test data: Module Request Payloads
79 # These represent what users provide to the Ansible module.
80
81 CREATE_INVESTIGATION_PARAMS = {
82 "name": "Security Incident 2026-01",
83 "description": "Investigation into suspicious login activity",
84 "status": "new",
85 "owner": "admin",
86 "urgency": "high",
87 "disposition": "undetermined",
88 "sensitivity": "amber",
89 }
90
91 UPDATE_INVESTIGATION_PARAMS = {
92 "investigation_ref_id": "inv-12345-abcde",
93 "owner": "analyst1",
94 "status": "resolved",
95 "disposition": "true_positive",
96 "sensitivity": "red",
97 }
98
99 MINIMAL_CREATE_PARAMS = {
100 "name": "Minimal Investigation",
101 }
102
103
104 class TestSplunkInvestigation:
105 """Test class for the splunk_investigation action plugin.
106
107 Each test follows this pattern:
108 1. setup_method: Creates mock Ansible components (task, connection, play_context)
109 2. Test method: Sets task.args, mocks API methods, runs plugin, asserts results
110
111 """
112
113 def setup_method(self):
114 """Set up test fixtures before each test method.
115
116 This creates the mock Ansible environment needed to test the action plugin:
117 - task: Represents the Ansible task being executed
118 - play_context: Contains playbook execution context (like check_mode)
119 - connection: The connection to the target (mocked for unit tests)
120 - templar: Ansible's template engine
121 """
122 # Create a mock Task object
123 task = MagicMock(Task)
124 task.check_mode = False
125
126 # Create mock play context (controls check_mode behavior)
127 play_context = MagicMock()
128 play_context.check_mode = False
129
130 # Create a mock connection (we'll set socket_path in each test)
131 connection = patch(
132 "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection",
133 )
134
135 # Ansible's template engine (not used much in these tests)
136 fake_loader = {}
137 templar = Templar(loader=fake_loader)
138
139 # Create the action plugin instance
140 self._plugin = ActionModule(
141 task=task,
142 connection=connection,
143 play_context=play_context,
144 loader=fake_loader,
145 templar=templar,
146 shared_loader_obj=None,
147 )
148
149 # Set required task attributes
150 self._plugin._task.action = "splunk_investigation"
151 self._plugin._task.async_val = False
152
153 # Task variables (empty for most tests)
154 self._task_vars = {}
155
156 # Create Investigation Tests #
157 @patch("ansible.module_utils.connection.Connection.__rpc__")
158 def test_investigation_create_success(self, connection, monkeypatch):
159 """Test successful creation of a new investigation.
160
161 When creating an investigation (no investigation_ref_id), the module should:
162 1. Call the investigations API to create the resource
163 2. Return changed=True
164 3. Include the created investigation in the result
165 """
166 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
167 self._plugin._connection._shell = MagicMock()
168
169 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
170 return copy.deepcopy(INVESTIGATION_API_RESPONSE)
171
172 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
173
174 self._plugin._task.args = CREATE_INVESTIGATION_PARAMS.copy()
175
176 result = self._plugin.run(task_vars=self._task_vars)
177
178 assert result["changed"] is True
179 assert "investigation" in result
180 assert result["investigation"]["after"] is not None
181 assert result.get("failed") is not True
182
183 @patch("ansible.module_utils.connection.Connection.__rpc__")
184 def test_investigation_create_minimal(self, connection, monkeypatch):
185 """Test creation with only required parameters.
186
187 The module requires only 'name' for creating a new investigation.
188 """
189 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
190 self._plugin._connection._shell = MagicMock()
191
192 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
193 return {
194 "investigation_guid": "inv-new-12345",
195 "name": "Minimal Investigation",
196 }
197
198 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
199
200 self._plugin._task.args = MINIMAL_CREATE_PARAMS.copy()
201
202 result = self._plugin.run(task_vars=self._task_vars)
203
204 assert result["changed"] is True
205 assert result.get("failed") is not True
206
207 @patch("ansible.module_utils.connection.Connection.__rpc__")
208 def test_investigation_create_missing_name(self, connection):
209 """Test that missing name returns an error.
210
211 Name is a required field when creating a new investigation.
212 The module should fail with a clear error message.
213 """
214 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
215 self._plugin._connection._shell = MagicMock()
216
217 # Create params without name
218 params = CREATE_INVESTIGATION_PARAMS.copy()
219 del params["name"]
220
221 self._plugin._task.args = params
222
223 result = self._plugin.run(task_vars=self._task_vars)
224
225 assert result["failed"] is True
226 assert "name" in result["msg"].lower()
227
228 @patch("ansible.module_utils.connection.Connection.__rpc__")
229 def test_investigation_create_with_findings(self, connection, monkeypatch):
230 """Test creation with finding_ids attached."""
231 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
232 self._plugin._connection._shell = MagicMock()
233
234 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
235 return copy.deepcopy(INVESTIGATION_API_RESPONSE)
236
237 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
238
239 params = CREATE_INVESTIGATION_PARAMS.copy()
240 params["finding_ids"] = [
241 "A265ED94-AE9E-428C-91D2-64BB956EB7CB@@notable@@time1234567890",
242 ]
243
244 self._plugin._task.args = params
245
246 result = self._plugin.run(task_vars=self._task_vars)
247
248 assert result["changed"] is True
249 assert result.get("failed") is not True
250
251 # Update Investigation Tests #
252 @patch("ansible.module_utils.connection.Connection.__rpc__")
253 def test_investigation_update_success(self, connection, monkeypatch):
254 """Test successful update of an existing investigation."""
255 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
256 self._plugin._connection._shell = MagicMock()
257
258 # Mock get_by_path to return existing investigation
259 def get_by_path(self, path, query_params=None):
260 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
261
262 # Mock create_update for the update operation
263 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
264 return copy.deepcopy(INVESTIGATION_API_RESPONSE_UPDATED)
265
266 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
267 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
268
269 self._plugin._task.args = UPDATE_INVESTIGATION_PARAMS.copy()
270
271 result = self._plugin.run(task_vars=self._task_vars)
272
273 assert result["changed"] is True
274 assert result.get("failed") is not True
275 assert "investigation" in result
276 # Should have both before and after states
277 assert result["investigation"]["before"] is not None
278 assert result["investigation"]["after"] is not None
279
280 @patch("ansible.module_utils.connection.Connection.__rpc__")
281 def test_investigation_update_idempotent(self, connection, monkeypatch):
282 """Test that updating with same values returns changed=False."""
283 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
284 self._plugin._connection._shell = MagicMock()
285
286 # Mock get_by_path to return investigation that already has the desired values
287 def get_by_path(self, path, query_params=None):
288 return [copy.deepcopy(INVESTIGATION_API_RESPONSE_UPDATED)]
289
290 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
291
292 # Request the same values that already exist
293 self._plugin._task.args = {
294 "investigation_ref_id": "inv-12345-abcde",
295 "owner": "analyst1",
296 "status": "resolved",
297 "disposition": "true_positive",
298 "sensitivity": "red",
299 }
300
301 result = self._plugin.run(task_vars=self._task_vars)
302
303 # No changes should be made
304 assert result["changed"] is False
305 assert result.get("failed") is not True
306
307 @patch("ansible.module_utils.connection.Connection.__rpc__")
308 def test_investigation_update_not_found(self, connection, monkeypatch):
309 """Test updating a non-existent investigation returns an error."""
310 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
311 self._plugin._connection._shell = MagicMock()
312
313 # Mock get_by_path to return empty (investigation not found)
314 def get_by_path(self, path, query_params=None):
315 return []
316
317 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
318
319 self._plugin._task.args = {
320 "investigation_ref_id": "non-existent-id",
321 "status": "resolved",
322 }
323
324 result = self._plugin.run(task_vars=self._task_vars)
325
326 assert result["failed"] is True
327 assert "not found" in result["msg"].lower()
328
329 @patch("ansible.module_utils.connection.Connection.__rpc__")
330 def test_investigation_update_ignores_name(self, connection, monkeypatch):
331 """Test that name is ignored during update (cannot be updated)."""
332 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
333 self._plugin._connection._shell = MagicMock()
334
335 def get_by_path(self, path, query_params=None):
336 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
337
338 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
339 return copy.deepcopy(INVESTIGATION_API_RESPONSE_UPDATED)
340
341 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
342 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
343
344 # Include both name (non-updatable) and status (updatable)
345 self._plugin._task.args = {
346 "investigation_ref_id": "inv-12345-abcde",
347 "name": "This should be ignored", # Not updatable
348 "status": "resolved", # Updatable - should trigger change
349 }
350
351 result = self._plugin.run(task_vars=self._task_vars)
352
353 # Should still succeed with the updatable field
354 assert result["changed"] is True
355 assert result.get("failed") is not True
356
357 @patch("ansible.module_utils.connection.Connection.__rpc__")
358 def test_investigation_update_no_updatable_fields(self, connection, monkeypatch):
359 """Test that providing only non-updatable fields returns an error."""
360 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
361 self._plugin._connection._shell = MagicMock()
362
363 def get_by_path(self, path, query_params=None):
364 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
365
366 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
367
368 # Only provide non-updatable fields
369 self._plugin._task.args = {
370 "investigation_ref_id": "inv-12345-abcde",
371 "name": "Cannot update this",
372 }
373
374 result = self._plugin.run(task_vars=self._task_vars)
375
376 assert result["failed"] is True
377
378 # Add Findings Tests #
379 @patch("ansible.module_utils.connection.Connection.__rpc__")
380 def test_investigation_add_findings(self, connection, monkeypatch):
381 """Test adding findings to an existing investigation."""
382 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
383 self._plugin._connection._shell = MagicMock()
384
385 def get_by_path(self, path, query_params=None):
386 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
387
388 api_calls = []
389
390 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
391 api_calls.append({"path": rest_path, "data": data})
392 return copy.deepcopy(INVESTIGATION_API_RESPONSE_UPDATED)
393
394 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
395 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
396
397 self._plugin._task.args = {
398 "investigation_ref_id": "inv-12345-abcde",
399 "finding_ids": ["finding-002@@notable@@time456"],
400 }
401
402 result = self._plugin.run(task_vars=self._task_vars)
403
404 assert result["changed"] is True
405 assert result.get("failed") is not True
406 # Should have called the findings endpoint
407 assert any("/findings" in call["path"] for call in api_calls)
408
409 @patch("ansible.module_utils.connection.Connection.__rpc__")
410 def test_investigation_add_existing_findings_skipped(self, connection, monkeypatch):
411 """Test that existing findings are skipped (not duplicated)."""
412 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
413 self._plugin._connection._shell = MagicMock()
414
415 def get_by_path(self, path, query_params=None):
416 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
417
418 api_calls = []
419
420 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
421 api_calls.append({"path": rest_path, "data": data})
422 return {}
423
424 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
425 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
426
427 # Try to add a finding that already exists
428 self._plugin._task.args = {
429 "investigation_ref_id": "inv-12345-abcde",
430 "finding_ids": ["finding-001@@notable@@time123"], # Already exists
431 }
432
433 result = self._plugin.run(task_vars=self._task_vars)
434
435 # Should not report changes (finding already exists)
436 assert result["changed"] is False
437 assert result.get("failed") is not True
438
439 # Check Mode Tests #
440 @patch("ansible.module_utils.connection.Connection.__rpc__")
441 def test_investigation_check_mode_create(self, connection, monkeypatch):
442 """Test check mode for creating an investigation."""
443 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
444 self._plugin._connection._shell = MagicMock()
445
446 # Enable check mode
447 self._plugin._task.check_mode = True
448
449 # Track if create_update is called (it shouldn't be)
450 create_called = []
451
452 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
453 create_called.append(True)
454 return copy.deepcopy(INVESTIGATION_API_RESPONSE)
455
456 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
457
458 self._plugin._task.args = CREATE_INVESTIGATION_PARAMS.copy()
459
460 result = self._plugin.run(task_vars=self._task_vars)
461
462 # Should report changed but not actually call API
463 assert result["changed"] is True
464 assert result.get("failed") is not True
465 assert len(create_called) == 0 # API should not be called
466 assert "check mode" in result["msg"].lower()
467
468 @patch("ansible.module_utils.connection.Connection.__rpc__")
469 def test_investigation_check_mode_update(self, connection, monkeypatch):
470 """Test check mode for updating an investigation."""
471 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
472 self._plugin._connection._shell = MagicMock()
473
474 # Enable check mode
475 self._plugin._task.check_mode = True
476
477 def get_by_path(self, path, query_params=None):
478 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
479
480 # Track if create_update is called (it shouldn't be for the update)
481 update_called = []
482
483 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
484 update_called.append(True)
485 return copy.deepcopy(INVESTIGATION_API_RESPONSE_UPDATED)
486
487 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
488 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
489
490 self._plugin._task.args = UPDATE_INVESTIGATION_PARAMS.copy()
491
492 result = self._plugin.run(task_vars=self._task_vars)
493
494 # Should report changed but not call update API
495 assert result["changed"] is True
496 assert result.get("failed") is not True
497 assert len(update_called) == 0 # Update API should not be called
498 assert "check mode" in result["msg"].lower()
499 # Should show what the after state would be
500 assert result["investigation"]["after"] is not None
501
502 @patch("ansible.module_utils.connection.Connection.__rpc__")
503 def test_investigation_check_mode_no_changes(self, connection, monkeypatch):
504 """Test check mode when no changes are needed."""
505 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
506 self._plugin._connection._shell = MagicMock()
507
508 # Enable check mode
509 self._plugin._task.check_mode = True
510
511 # Return investigation that already matches desired state
512 def get_by_path(self, path, query_params=None):
513 return [copy.deepcopy(INVESTIGATION_API_RESPONSE_UPDATED)]
514
515 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
516
517 self._plugin._task.args = {
518 "investigation_ref_id": "inv-12345-abcde",
519 "owner": "analyst1",
520 "status": "resolved",
521 "disposition": "true_positive",
522 "sensitivity": "red",
523 }
524
525 result = self._plugin.run(task_vars=self._task_vars)
526
527 assert result["changed"] is False
528 assert result.get("failed") is not True
529
530 # Custom API Path Tests #
531 @patch("ansible.module_utils.connection.Connection.__rpc__")
532 def test_investigation_custom_api_path(self, connection, monkeypatch):
533 """Test that custom API path parameters are used."""
534 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
535 self._plugin._connection._shell = MagicMock()
536
537 # Capture the path used in the API call
538 captured_paths = []
539
540 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
541 captured_paths.append(rest_path)
542 return copy.deepcopy(INVESTIGATION_API_RESPONSE)
543
544 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
545
546 params = CREATE_INVESTIGATION_PARAMS.copy()
547 params["api_namespace"] = "customNS"
548 params["api_user"] = "customuser"
549 params["api_app"] = "CustomApp"
550
551 self._plugin._task.args = params
552
553 result = self._plugin.run(task_vars=self._task_vars)
554
555 assert result["changed"] is True
556 # Verify the custom path was used
557 assert len(captured_paths) > 0
558 assert "customNS" in captured_paths[0]
559 assert "customuser" in captured_paths[0]
560 assert "CustomApp" in captured_paths[0]
561
562 # Field Update Tests #
563 @patch("ansible.module_utils.connection.Connection.__rpc__")
564 def test_investigation_update_single_field(self, connection, monkeypatch):
565 """Test updating just one field."""
566 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
567 self._plugin._connection._shell = MagicMock()
568
569 def get_by_path(self, path, query_params=None):
570 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
571
572 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
573 return copy.deepcopy(INVESTIGATION_API_RESPONSE_UPDATED)
574
575 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
576 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
577
578 # Only update status
579 self._plugin._task.args = {
580 "investigation_ref_id": "inv-12345-abcde",
581 "status": "resolved",
582 }
583
584 result = self._plugin.run(task_vars=self._task_vars)
585
586 assert result["changed"] is True
587 assert result.get("failed") is not True
588
589 @patch("ansible.module_utils.connection.Connection.__rpc__")
590 def test_investigation_update_urgency(self, connection, monkeypatch):
591 """Test updating urgency field."""
592 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
593 self._plugin._connection._shell = MagicMock()
594
595 def get_by_path(self, path, query_params=None):
596 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
597
598 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
599 response = copy.deepcopy(INVESTIGATION_API_RESPONSE)
600 response["urgency"] = "critical"
601 return response
602
603 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
604 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
605
606 self._plugin._task.args = {
607 "investigation_ref_id": "inv-12345-abcde",
608 "urgency": "critical",
609 }
610
611 result = self._plugin.run(task_vars=self._task_vars)
612
613 assert result["changed"] is True
614 assert result.get("failed") is not True
615
616 @patch("ansible.module_utils.connection.Connection.__rpc__")
617 def test_investigation_update_description(self, connection, monkeypatch):
618 """Test updating description field."""
619 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
620 self._plugin._connection._shell = MagicMock()
621
622 def get_by_path(self, path, query_params=None):
623 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
624
625 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
626 response = copy.deepcopy(INVESTIGATION_API_RESPONSE)
627 response["description"] = "Updated description"
628 return response
629
630 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
631 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
632
633 self._plugin._task.args = {
634 "investigation_ref_id": "inv-12345-abcde",
635 "description": "Updated description",
636 }
637
638 result = self._plugin.run(task_vars=self._task_vars)
639
640 assert result["changed"] is True
641 assert result.get("failed") is not True
642
643 @patch("ansible.module_utils.connection.Connection.__rpc__")
644 def test_investigation_update_sensitivity(self, connection, monkeypatch):
645 """Test updating sensitivity field."""
646 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
647 self._plugin._connection._shell = MagicMock()
648
649 def get_by_path(self, path, query_params=None):
650 return [copy.deepcopy(INVESTIGATION_API_RESPONSE)]
651
652 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
653 response = copy.deepcopy(INVESTIGATION_API_RESPONSE)
654 response["sensitivity"] = "Red"
655 return response
656
657 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
658 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
659
660 self._plugin._task.args = {
661 "investigation_ref_id": "inv-12345-abcde",
662 "sensitivity": "red",
663 }
664
665 result = self._plugin.run(task_vars=self._task_vars)
666
667 assert result["changed"] is True
668 assert result.get("failed") is not True
669
670 # Result Message Tests #
671 @patch("ansible.module_utils.connection.Connection.__rpc__")
672 def test_investigation_create_success_message(self, connection, monkeypatch):
673 """Test that successful create returns appropriate message."""
674 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
675 self._plugin._connection._shell = MagicMock()
676
677 def create_update(self, rest_path, data=None, query_params=None, json_payload=False):
678 return copy.deepcopy(INVESTIGATION_API_RESPONSE)
679
680 monkeypatch.setattr(SplunkRequest, "create_update", create_update)
681
682 self._plugin._task.args = CREATE_INVESTIGATION_PARAMS.copy()
683
684 result = self._plugin.run(task_vars=self._task_vars)
685
686 assert "msg" in result
687 assert "successfully" in result["msg"].lower() or "created" in result["msg"].lower()
688
689 @patch("ansible.module_utils.connection.Connection.__rpc__")
690 def test_investigation_no_changes_message(self, connection, monkeypatch):
691 """Test that no changes returns appropriate message."""
692 self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name
693 self._plugin._connection._shell = MagicMock()
694
695 def get_by_path(self, path, query_params=None):
696 return [copy.deepcopy(INVESTIGATION_API_RESPONSE_UPDATED)]
697
698 monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path)
699
700 self._plugin._task.args = {
701 "investigation_ref_id": "inv-12345-abcde",
702 "owner": "analyst1",
703 "status": "resolved",
704 }
705
706 result = self._plugin.run(task_vars=self._task_vars)
707
708 assert "msg" in result
709 assert "no changes" in result["msg"].lower()