| 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_response_plan action plugin. |
| 21 |
""" |
| 22 |
|
| 23 |
|
| 24 |
import copy |
| 25 |
import tempfile |
| 26 |
|
| 27 |
from unittest.mock import MagicMock, patch |
| 28 |
|
| 29 |
from ansible.playbook.task import Task |
| 30 |
from ansible.template import Templar |
| 31 |
|
| 32 |
from ansible_collections.splunk.es.plugins.action.splunk_response_plan import ( |
| 33 |
ActionModule, |
| 34 |
_build_phase_payload, |
| 35 |
_build_response_plan_api_path, |
| 36 |
_build_response_plan_update_path, |
| 37 |
_build_search_payload, |
| 38 |
_build_task_payload, |
| 39 |
_find_task_id_by_name, |
| 40 |
_map_phase_from_api, |
| 41 |
_map_response_plan_from_api, |
| 42 |
_map_response_plan_to_api, |
| 43 |
_map_task_from_api, |
| 44 |
) |
| 45 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest |
| 46 |
|
| 47 |
|
| 48 |
def _get_msg_str(result: dict) -> str: |
| 49 |
"""Get message from result as a lowercase string. |
| 50 |
|
| 51 |
Handles both string and list message formats that Ansible can return. |
| 52 |
|
| 53 |
Args: |
| 54 |
result: The result dictionary from module execution. |
| 55 |
|
| 56 |
Returns: |
| 57 |
The message as a lowercase string. |
| 58 |
""" |
| 59 |
msg = result.get("msg", "") |
| 60 |
if isinstance(msg, list): |
| 61 |
return " ".join(str(m) for m in msg).lower() |
| 62 |
return str(msg).lower() |
| 63 |
|
| 64 |
|
| 65 |
# Test data: API Response Payloads |
| 66 |
# These represent what the Splunk API returns for response plans. |
| 67 |
RESPONSE_PLAN_API_RESPONSE = { |
| 68 |
"id": "rp-001-uuid", |
| 69 |
"name": "Incident Response Plan", |
| 70 |
"description": "Standard incident response procedure", |
| 71 |
"template_status": "published", |
| 72 |
"incident_types": [], |
| 73 |
"phases": [ |
| 74 |
{ |
| 75 |
"id": "phase-001-uuid", |
| 76 |
"name": "Investigation", |
| 77 |
"template_id": "", |
| 78 |
"sla": None, |
| 79 |
"sla_type": "minutes", |
| 80 |
"create_time": "", |
| 81 |
"order": 1, |
| 82 |
"tasks": [ |
| 83 |
{ |
| 84 |
"id": "task-001-uuid", |
| 85 |
"task_id": "", |
| 86 |
"phase_id": "", |
| 87 |
"name": "Initial Triage", |
| 88 |
"description": "Perform initial assessment of the incident", |
| 89 |
"sla": None, |
| 90 |
"sla_type": "minutes", |
| 91 |
"order": 1, |
| 92 |
"status": "Pending", |
| 93 |
"is_note_required": True, |
| 94 |
"owner": "admin", |
| 95 |
"isNewTask": False, |
| 96 |
"files": [], |
| 97 |
"notes": [], |
| 98 |
"suggestions": { |
| 99 |
"actions": [], |
| 100 |
"playbooks": [], |
| 101 |
"searches": [ |
| 102 |
{ |
| 103 |
"name": "Access Over Time", |
| 104 |
"description": "Check access patterns", |
| 105 |
"spl": "| tstats count from datamodel=Authentication by _time span=10m", |
| 106 |
}, |
| 107 |
], |
| 108 |
}, |
| 109 |
}, |
| 110 |
], |
| 111 |
}, |
| 112 |
{ |
| 113 |
"id": "phase-002-uuid", |
| 114 |
"name": "Containment", |
| 115 |
"template_id": "", |
| 116 |
"sla": None, |
| 117 |
"sla_type": "minutes", |
| 118 |
"create_time": "", |
| 119 |
"order": 2, |
| 120 |
"tasks": [ |
| 121 |
{ |
| 122 |
"id": "task-002-uuid", |
| 123 |
"task_id": "", |
| 124 |
"phase_id": "", |
| 125 |
"name": "Isolate Affected Systems", |
| 126 |
"description": "Isolate compromised hosts from network", |
| 127 |
"sla": None, |
| 128 |
"sla_type": "minutes", |
| 129 |
"order": 1, |
| 130 |
"status": "Pending", |
| 131 |
"is_note_required": True, |
| 132 |
"owner": "unassigned", |
| 133 |
"isNewTask": False, |
| 134 |
"files": [], |
| 135 |
"notes": [], |
| 136 |
"suggestions": { |
| 137 |
"actions": [], |
| 138 |
"playbooks": [], |
| 139 |
"searches": [], |
| 140 |
}, |
| 141 |
}, |
| 142 |
], |
| 143 |
}, |
| 144 |
], |
| 145 |
} |
| 146 |
|
| 147 |
RESPONSE_PLAN_LIST_RESPONSE = { |
| 148 |
"items": [ |
| 149 |
{ |
| 150 |
"id": "rp-001-uuid", |
| 151 |
"name": "Incident Response Plan", |
| 152 |
"description": "Standard incident response procedure", |
| 153 |
"template_status": "published", |
| 154 |
"incident_types": [], |
| 155 |
"phases": [ |
| 156 |
{ |
| 157 |
"id": "phase-001-uuid", |
| 158 |
"name": "Investigation", |
| 159 |
"tasks": [ |
| 160 |
{ |
| 161 |
"id": "task-001-uuid", |
| 162 |
"name": "Initial Triage", |
| 163 |
"description": "Perform initial assessment", |
| 164 |
"is_note_required": True, |
| 165 |
"owner": "admin", |
| 166 |
"suggestions": {"actions": [], "playbooks": [], "searches": []}, |
| 167 |
}, |
| 168 |
], |
| 169 |
}, |
| 170 |
], |
| 171 |
}, |
| 172 |
{ |
| 173 |
"id": "rp-002-uuid", |
| 174 |
"name": "Data Breach Response", |
| 175 |
"description": "Data breach handling procedure", |
| 176 |
"template_status": "draft", |
| 177 |
"incident_types": [], |
| 178 |
"phases": [], |
| 179 |
}, |
| 180 |
], |
| 181 |
} |
| 182 |
|
| 183 |
# Test data: Module Request Payloads |
| 184 |
CREATE_RESPONSE_PLAN_PARAMS = { |
| 185 |
"name": "Incident Response Plan", |
| 186 |
"description": "Standard incident response procedure", |
| 187 |
"template_status": "published", |
| 188 |
"phases": [ |
| 189 |
{ |
| 190 |
"name": "Investigation", |
| 191 |
"tasks": [ |
| 192 |
{ |
| 193 |
"name": "Initial Triage", |
| 194 |
"description": "Perform initial assessment of the incident", |
| 195 |
"is_note_required": True, |
| 196 |
"owner": "admin", |
| 197 |
"searches": [ |
| 198 |
{ |
| 199 |
"name": "Access Over Time", |
| 200 |
"description": "Check access patterns", |
| 201 |
"spl": "| tstats count from datamodel=Authentication by _time span=10m", |
| 202 |
}, |
| 203 |
], |
| 204 |
}, |
| 205 |
], |
| 206 |
}, |
| 207 |
{ |
| 208 |
"name": "Containment", |
| 209 |
"tasks": [ |
| 210 |
{ |
| 211 |
"name": "Isolate Affected Systems", |
| 212 |
"description": "Isolate compromised hosts from network", |
| 213 |
"is_note_required": True, |
| 214 |
}, |
| 215 |
], |
| 216 |
}, |
| 217 |
], |
| 218 |
} |
| 219 |
|
| 220 |
MINIMAL_RESPONSE_PLAN_PARAMS = { |
| 221 |
"name": "Minimal Response Plan", |
| 222 |
"phases": [ |
| 223 |
{ |
| 224 |
"name": "Phase 1", |
| 225 |
"tasks": [ |
| 226 |
{ |
| 227 |
"name": "Task 1", |
| 228 |
"description": "First task", |
| 229 |
}, |
| 230 |
], |
| 231 |
}, |
| 232 |
], |
| 233 |
} |
| 234 |
|
| 235 |
UPDATE_RESPONSE_PLAN_PARAMS = { |
| 236 |
"name": "Incident Response Plan", |
| 237 |
"description": "Updated incident response procedure", |
| 238 |
"template_status": "published", |
| 239 |
"phases": [ |
| 240 |
{ |
| 241 |
"name": "Investigation", |
| 242 |
"tasks": [ |
| 243 |
{ |
| 244 |
"name": "Initial Triage", |
| 245 |
"description": "Updated: Perform thorough initial assessment", |
| 246 |
"is_note_required": True, |
| 247 |
"owner": "analyst", |
| 248 |
}, |
| 249 |
{ |
| 250 |
"name": "New Analysis Task", |
| 251 |
"description": "This task will be created", |
| 252 |
"is_note_required": False, |
| 253 |
}, |
| 254 |
], |
| 255 |
}, |
| 256 |
{ |
| 257 |
"name": "Containment", |
| 258 |
"tasks": [ |
| 259 |
{ |
| 260 |
"name": "Isolate Affected Systems", |
| 261 |
"description": "Isolate compromised hosts from network", |
| 262 |
}, |
| 263 |
], |
| 264 |
}, |
| 265 |
], |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
class TestSplunkResponsePlan: |
| 270 |
"""Test class for the splunk_response_plan action plugin.""" |
| 271 |
|
| 272 |
def setup_method(self): |
| 273 |
"""Set up test fixtures before each test method. |
| 274 |
|
| 275 |
This creates the mock Ansible environment needed to test the action plugin: |
| 276 |
- task: Represents the Ansible task being executed |
| 277 |
- play_context: Contains playbook execution context (like check_mode) |
| 278 |
- connection: The connection to the target (mocked for unit tests) |
| 279 |
- templar: Ansible's template engine |
| 280 |
""" |
| 281 |
# Create a mock Task object |
| 282 |
task = MagicMock(Task) |
| 283 |
task.check_mode = False |
| 284 |
|
| 285 |
# Create mock play context (controls check_mode behavior) |
| 286 |
play_context = MagicMock() |
| 287 |
play_context.check_mode = False |
| 288 |
|
| 289 |
# Create a mock connection |
| 290 |
connection = patch( |
| 291 |
"ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", |
| 292 |
) |
| 293 |
|
| 294 |
# Ansible's template engine (not used much in these tests) |
| 295 |
fake_loader = {} |
| 296 |
templar = Templar(loader=fake_loader) |
| 297 |
|
| 298 |
# Create the action plugin instance |
| 299 |
self._plugin = ActionModule( |
| 300 |
task=task, |
| 301 |
connection=connection, |
| 302 |
play_context=play_context, |
| 303 |
loader=fake_loader, |
| 304 |
templar=templar, |
| 305 |
shared_loader_obj=None, |
| 306 |
) |
| 307 |
|
| 308 |
# Set required task attributes |
| 309 |
self._plugin._task.action = "splunk_response_plan" |
| 310 |
self._plugin._task.async_val = False |
| 311 |
|
| 312 |
# Task variables (empty for most tests) |
| 313 |
self._task_vars = {} |
| 314 |
|
| 315 |
# Create Response Plan Tests |
| 316 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 317 |
def test_response_plan_create_success(self, connection, monkeypatch): |
| 318 |
"""Test successful creation of a new response plan. |
| 319 |
|
| 320 |
When creating a response plan (name not found), the module should: |
| 321 |
1. Call the response templates API to create the resource |
| 322 |
2. Return changed=True |
| 323 |
3. Include the created response plan in the result |
| 324 |
""" |
| 325 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 326 |
self._plugin._connection._shell = MagicMock() |
| 327 |
|
| 328 |
# Mock get_by_path to return empty list (no existing response plans) |
| 329 |
def get_by_path(self, path, query_params=None): |
| 330 |
return {"items": []} |
| 331 |
|
| 332 |
# Mock create_update to return the created response plan |
| 333 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 334 |
return copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 335 |
|
| 336 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 337 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 338 |
|
| 339 |
self._plugin._task.args = copy.deepcopy(CREATE_RESPONSE_PLAN_PARAMS) |
| 340 |
|
| 341 |
result = self._plugin.run(task_vars=self._task_vars) |
| 342 |
|
| 343 |
assert result["changed"] is True |
| 344 |
assert "response_plan" in result |
| 345 |
assert result["response_plan"]["after"] is not None |
| 346 |
assert result["response_plan"]["before"] is None |
| 347 |
assert result.get("failed") is not True |
| 348 |
assert "created" in _get_msg_str(result) |
| 349 |
|
| 350 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 351 |
def test_response_plan_create_minimal(self, connection, monkeypatch): |
| 352 |
"""Test creation with only required parameters. |
| 353 |
|
| 354 |
The module requires: name and phases for creating a new response plan. |
| 355 |
""" |
| 356 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 357 |
self._plugin._connection._shell = MagicMock() |
| 358 |
|
| 359 |
def get_by_path(self, path, query_params=None): |
| 360 |
return {"items": []} |
| 361 |
|
| 362 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 363 |
return { |
| 364 |
"id": "new-rp-uuid", |
| 365 |
"name": "Minimal Response Plan", |
| 366 |
"description": "", |
| 367 |
"template_status": "draft", |
| 368 |
"phases": [ |
| 369 |
{ |
| 370 |
"id": "phase-uuid", |
| 371 |
"name": "Phase 1", |
| 372 |
"tasks": [ |
| 373 |
{ |
| 374 |
"id": "task-uuid", |
| 375 |
"name": "Task 1", |
| 376 |
"description": "First task", |
| 377 |
"is_note_required": False, |
| 378 |
"owner": "unassigned", |
| 379 |
"suggestions": {"actions": [], "playbooks": [], "searches": []}, |
| 380 |
}, |
| 381 |
], |
| 382 |
}, |
| 383 |
], |
| 384 |
} |
| 385 |
|
| 386 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 387 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 388 |
|
| 389 |
self._plugin._task.args = copy.deepcopy(MINIMAL_RESPONSE_PLAN_PARAMS) |
| 390 |
|
| 391 |
result = self._plugin.run(task_vars=self._task_vars) |
| 392 |
|
| 393 |
assert result["changed"] is True |
| 394 |
assert result.get("failed") is not True |
| 395 |
|
| 396 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 397 |
def test_response_plan_create_missing_name(self, connection): |
| 398 |
"""Test that missing name returns an error. |
| 399 |
|
| 400 |
Name is a required field when creating a response plan. |
| 401 |
""" |
| 402 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 403 |
self._plugin._connection._shell = MagicMock() |
| 404 |
|
| 405 |
self._plugin._task.args = { |
| 406 |
"phases": [ |
| 407 |
{ |
| 408 |
"name": "Phase 1", |
| 409 |
"tasks": [{"name": "Task 1", "description": "First task"}], |
| 410 |
}, |
| 411 |
], |
| 412 |
} |
| 413 |
|
| 414 |
result = self._plugin.run(task_vars=self._task_vars) |
| 415 |
|
| 416 |
assert result["failed"] is True |
| 417 |
assert "name" in _get_msg_str(result) |
| 418 |
|
| 419 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 420 |
def test_response_plan_create_missing_phases(self, connection, monkeypatch): |
| 421 |
"""Test that missing phases returns an error when state=present. |
| 422 |
|
| 423 |
Phases are required when creating or updating a response plan. |
| 424 |
""" |
| 425 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 426 |
self._plugin._connection._shell = MagicMock() |
| 427 |
|
| 428 |
def get_by_path(self, path, query_params=None): |
| 429 |
return {"items": []} |
| 430 |
|
| 431 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 432 |
|
| 433 |
self._plugin._task.args = { |
| 434 |
"name": "Test Response Plan", |
| 435 |
"description": "A test response plan", |
| 436 |
} |
| 437 |
|
| 438 |
result = self._plugin.run(task_vars=self._task_vars) |
| 439 |
|
| 440 |
assert result["failed"] is True |
| 441 |
assert "phases" in _get_msg_str(result) |
| 442 |
|
| 443 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 444 |
def test_response_plan_create_with_searches(self, connection, monkeypatch): |
| 445 |
"""Test creation with searches in tasks.""" |
| 446 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 447 |
self._plugin._connection._shell = MagicMock() |
| 448 |
|
| 449 |
def get_by_path(self, path, query_params=None): |
| 450 |
return {"items": []} |
| 451 |
|
| 452 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 453 |
return copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 454 |
|
| 455 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 456 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 457 |
|
| 458 |
self._plugin._task.args = copy.deepcopy(CREATE_RESPONSE_PLAN_PARAMS) |
| 459 |
|
| 460 |
result = self._plugin.run(task_vars=self._task_vars) |
| 461 |
|
| 462 |
assert result["changed"] is True |
| 463 |
assert result.get("failed") is not True |
| 464 |
# Verify searches are in the result |
| 465 |
after = result["response_plan"]["after"] |
| 466 |
assert len(after["phases"]) > 0 |
| 467 |
assert len(after["phases"][0]["tasks"]) > 0 |
| 468 |
assert len(after["phases"][0]["tasks"][0]["searches"]) > 0 |
| 469 |
|
| 470 |
# Update Response Plan Tests |
| 471 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 472 |
def test_response_plan_update_success(self, connection, monkeypatch): |
| 473 |
"""Test successful update of an existing response plan.""" |
| 474 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 475 |
self._plugin._connection._shell = MagicMock() |
| 476 |
|
| 477 |
# Mock get_by_path to return existing response plan |
| 478 |
def get_by_path(self, path, query_params=None): |
| 479 |
return copy.deepcopy(RESPONSE_PLAN_LIST_RESPONSE) |
| 480 |
|
| 481 |
# Mock create_update for the update operation |
| 482 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 483 |
# Return updated response plan |
| 484 |
updated = copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 485 |
updated["description"] = "Updated incident response procedure" |
| 486 |
return updated |
| 487 |
|
| 488 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 489 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 490 |
|
| 491 |
self._plugin._task.args = copy.deepcopy(UPDATE_RESPONSE_PLAN_PARAMS) |
| 492 |
|
| 493 |
result = self._plugin.run(task_vars=self._task_vars) |
| 494 |
|
| 495 |
assert result["changed"] is True |
| 496 |
assert result.get("failed") is not True |
| 497 |
assert "response_plan" in result |
| 498 |
# Should have both before and after states |
| 499 |
assert result["response_plan"]["before"] is not None |
| 500 |
assert result["response_plan"]["after"] is not None |
| 501 |
|
| 502 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 503 |
def test_response_plan_update_idempotent(self, connection, monkeypatch): |
| 504 |
"""Test that updating with same values returns changed=False.""" |
| 505 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 506 |
self._plugin._connection._shell = MagicMock() |
| 507 |
|
| 508 |
# Create response plan that matches the request exactly |
| 509 |
existing_response = { |
| 510 |
"items": [ |
| 511 |
{ |
| 512 |
"id": "rp-001-uuid", |
| 513 |
"name": "Minimal Response Plan", |
| 514 |
"description": "", |
| 515 |
"template_status": "draft", |
| 516 |
"phases": [ |
| 517 |
{ |
| 518 |
"id": "phase-uuid", |
| 519 |
"name": "Phase 1", |
| 520 |
"tasks": [ |
| 521 |
{ |
| 522 |
"id": "task-uuid", |
| 523 |
"name": "Task 1", |
| 524 |
"description": "First task", |
| 525 |
"is_note_required": False, |
| 526 |
"owner": "unassigned", |
| 527 |
"suggestions": { |
| 528 |
"actions": [], |
| 529 |
"playbooks": [], |
| 530 |
"searches": [], |
| 531 |
}, |
| 532 |
}, |
| 533 |
], |
| 534 |
}, |
| 535 |
], |
| 536 |
}, |
| 537 |
], |
| 538 |
} |
| 539 |
|
| 540 |
def get_by_path(self, path, query_params=None): |
| 541 |
return copy.deepcopy(existing_response) |
| 542 |
|
| 543 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 544 |
|
| 545 |
# Request same values that already exist |
| 546 |
self._plugin._task.args = copy.deepcopy(MINIMAL_RESPONSE_PLAN_PARAMS) |
| 547 |
|
| 548 |
result = self._plugin.run(task_vars=self._task_vars) |
| 549 |
|
| 550 |
# No changes should be made |
| 551 |
assert result["changed"] is False |
| 552 |
assert result.get("failed") is not True |
| 553 |
|
| 554 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 555 |
def test_response_plan_update_preserves_ids(self, connection, monkeypatch): |
| 556 |
"""Test that updating preserves existing phase and task IDs.""" |
| 557 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 558 |
self._plugin._connection._shell = MagicMock() |
| 559 |
|
| 560 |
captured_payloads = [] |
| 561 |
|
| 562 |
def get_by_path(self, path, query_params=None): |
| 563 |
return copy.deepcopy(RESPONSE_PLAN_LIST_RESPONSE) |
| 564 |
|
| 565 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 566 |
captured_payloads.append(copy.deepcopy(data)) |
| 567 |
return copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 568 |
|
| 569 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 570 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 571 |
|
| 572 |
self._plugin._task.args = copy.deepcopy(UPDATE_RESPONSE_PLAN_PARAMS) |
| 573 |
|
| 574 |
result = self._plugin.run(task_vars=self._task_vars) |
| 575 |
|
| 576 |
assert result["changed"] is True |
| 577 |
assert len(captured_payloads) > 0 |
| 578 |
|
| 579 |
# Verify the payload preserves IDs for matching items |
| 580 |
payload = captured_payloads[0] |
| 581 |
assert payload.get("id") == "rp-001-uuid" |
| 582 |
|
| 583 |
# Delete Response Plan Tests |
| 584 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 585 |
def test_response_plan_delete_success(self, connection, monkeypatch): |
| 586 |
"""Test successful deletion of an existing response plan.""" |
| 587 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 588 |
self._plugin._connection._shell = MagicMock() |
| 589 |
|
| 590 |
def get_by_path(self, path, query_params=None): |
| 591 |
return copy.deepcopy(RESPONSE_PLAN_LIST_RESPONSE) |
| 592 |
|
| 593 |
delete_called = [] |
| 594 |
|
| 595 |
def delete_by_path(self, path): |
| 596 |
delete_called.append(path) |
| 597 |
return {} |
| 598 |
|
| 599 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 600 |
monkeypatch.setattr(SplunkRequest, "delete_by_path", delete_by_path) |
| 601 |
|
| 602 |
self._plugin._task.args = { |
| 603 |
"name": "Incident Response Plan", |
| 604 |
"state": "absent", |
| 605 |
} |
| 606 |
|
| 607 |
result = self._plugin.run(task_vars=self._task_vars) |
| 608 |
|
| 609 |
assert result["changed"] is True |
| 610 |
assert result.get("failed") is not True |
| 611 |
assert len(delete_called) == 1 |
| 612 |
assert "rp-001-uuid" in delete_called[0] |
| 613 |
assert result["response_plan"]["before"] is not None |
| 614 |
assert result["response_plan"]["after"] is None |
| 615 |
|
| 616 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 617 |
def test_response_plan_delete_not_found(self, connection, monkeypatch): |
| 618 |
"""Test deleting a non-existent response plan returns changed=False.""" |
| 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 {"items": []} |
| 624 |
|
| 625 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 626 |
|
| 627 |
self._plugin._task.args = { |
| 628 |
"name": "Non-Existent Plan", |
| 629 |
"state": "absent", |
| 630 |
} |
| 631 |
|
| 632 |
result = self._plugin.run(task_vars=self._task_vars) |
| 633 |
|
| 634 |
# Should not be changed since it doesn't exist |
| 635 |
assert result["changed"] is False |
| 636 |
assert result.get("failed") is not True |
| 637 |
assert "already absent" in _get_msg_str(result) or "not found" in _get_msg_str(result) |
| 638 |
|
| 639 |
# Validation Tests |
| 640 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 641 |
def test_response_plan_duplicate_phase_names(self, connection, monkeypatch): |
| 642 |
"""Test that duplicate phase names returns an error.""" |
| 643 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 644 |
self._plugin._connection._shell = MagicMock() |
| 645 |
|
| 646 |
def get_by_path(self, path, query_params=None): |
| 647 |
return {"items": []} |
| 648 |
|
| 649 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 650 |
|
| 651 |
self._plugin._task.args = { |
| 652 |
"name": "Test Plan", |
| 653 |
"phases": [ |
| 654 |
{"name": "Investigation", "tasks": [{"name": "Task 1", "description": "Desc"}]}, |
| 655 |
{"name": "Investigation", "tasks": [{"name": "Task 2", "description": "Desc"}]}, |
| 656 |
], |
| 657 |
} |
| 658 |
|
| 659 |
result = self._plugin.run(task_vars=self._task_vars) |
| 660 |
|
| 661 |
assert result["failed"] is True |
| 662 |
assert "duplicate" in _get_msg_str(result) |
| 663 |
assert "phase" in _get_msg_str(result) |
| 664 |
|
| 665 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 666 |
def test_response_plan_duplicate_task_names_within_phase(self, connection, monkeypatch): |
| 667 |
"""Test that duplicate task names within a phase returns an error.""" |
| 668 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 669 |
self._plugin._connection._shell = MagicMock() |
| 670 |
|
| 671 |
def get_by_path(self, path, query_params=None): |
| 672 |
return {"items": []} |
| 673 |
|
| 674 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 675 |
|
| 676 |
self._plugin._task.args = { |
| 677 |
"name": "Test Plan", |
| 678 |
"phases": [ |
| 679 |
{ |
| 680 |
"name": "Investigation", |
| 681 |
"tasks": [ |
| 682 |
{"name": "Initial Triage", "description": "First triage"}, |
| 683 |
{"name": "Initial Triage", "description": "Duplicate triage"}, |
| 684 |
], |
| 685 |
}, |
| 686 |
], |
| 687 |
} |
| 688 |
|
| 689 |
result = self._plugin.run(task_vars=self._task_vars) |
| 690 |
|
| 691 |
assert result["failed"] is True |
| 692 |
assert "duplicate" in _get_msg_str(result) |
| 693 |
assert "task" in _get_msg_str(result) |
| 694 |
|
| 695 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 696 |
def test_response_plan_same_task_names_different_phases(self, connection, monkeypatch): |
| 697 |
"""Test that same task names in different phases is allowed.""" |
| 698 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 699 |
self._plugin._connection._shell = MagicMock() |
| 700 |
|
| 701 |
def get_by_path(self, path, query_params=None): |
| 702 |
return {"items": []} |
| 703 |
|
| 704 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 705 |
return { |
| 706 |
"id": "new-uuid", |
| 707 |
"name": "Test Plan", |
| 708 |
"description": "", |
| 709 |
"template_status": "draft", |
| 710 |
"phases": [ |
| 711 |
{ |
| 712 |
"id": "p1", |
| 713 |
"name": "Phase 1", |
| 714 |
"tasks": [ |
| 715 |
{ |
| 716 |
"id": "t1", |
| 717 |
"name": "Review", |
| 718 |
"description": "", |
| 719 |
"is_note_required": False, |
| 720 |
"owner": "unassigned", |
| 721 |
"suggestions": {"actions": [], "playbooks": [], "searches": []}, |
| 722 |
}, |
| 723 |
], |
| 724 |
}, |
| 725 |
{ |
| 726 |
"id": "p2", |
| 727 |
"name": "Phase 2", |
| 728 |
"tasks": [ |
| 729 |
{ |
| 730 |
"id": "t2", |
| 731 |
"name": "Review", # Same name allowed in different phase |
| 732 |
"description": "", |
| 733 |
"is_note_required": False, |
| 734 |
"owner": "unassigned", |
| 735 |
"suggestions": {"actions": [], "playbooks": [], "searches": []}, |
| 736 |
}, |
| 737 |
], |
| 738 |
}, |
| 739 |
], |
| 740 |
} |
| 741 |
|
| 742 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 743 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 744 |
|
| 745 |
self._plugin._task.args = { |
| 746 |
"name": "Test Plan", |
| 747 |
"phases": [ |
| 748 |
{"name": "Phase 1", "tasks": [{"name": "Review", "description": "Phase 1 review"}]}, |
| 749 |
{"name": "Phase 2", "tasks": [{"name": "Review", "description": "Phase 2 review"}]}, |
| 750 |
], |
| 751 |
} |
| 752 |
|
| 753 |
result = self._plugin.run(task_vars=self._task_vars) |
| 754 |
|
| 755 |
# Should succeed - same task name allowed in different phases |
| 756 |
assert result["changed"] is True |
| 757 |
assert result.get("failed") is not True |
| 758 |
|
| 759 |
# Check Mode Tests |
| 760 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 761 |
def test_response_plan_check_mode_create(self, connection, monkeypatch): |
| 762 |
"""Test check mode for creating a response plan. |
| 763 |
|
| 764 |
In check mode, the module should report what would happen without |
| 765 |
actually making API calls. It should return changed=True but not |
| 766 |
create the response plan. |
| 767 |
""" |
| 768 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 769 |
self._plugin._connection._shell = MagicMock() |
| 770 |
|
| 771 |
# Enable check mode |
| 772 |
self._plugin._task.check_mode = True |
| 773 |
|
| 774 |
def get_by_path(self, path, query_params=None): |
| 775 |
return {"items": []} |
| 776 |
|
| 777 |
# Track if create_update is called (it shouldn't be) |
| 778 |
create_called = [] |
| 779 |
|
| 780 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 781 |
create_called.append(True) |
| 782 |
return copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 783 |
|
| 784 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 785 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 786 |
|
| 787 |
self._plugin._task.args = copy.deepcopy(CREATE_RESPONSE_PLAN_PARAMS) |
| 788 |
|
| 789 |
result = self._plugin.run(task_vars=self._task_vars) |
| 790 |
|
| 791 |
# Should report changed but not actually call API |
| 792 |
assert result["changed"] is True |
| 793 |
assert result.get("failed") is not True |
| 794 |
assert len(create_called) == 0 # API should not be called |
| 795 |
assert "check mode" in _get_msg_str(result) |
| 796 |
|
| 797 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 798 |
def test_response_plan_check_mode_update(self, connection, monkeypatch): |
| 799 |
"""Test check mode for updating a response plan.""" |
| 800 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 801 |
self._plugin._connection._shell = MagicMock() |
| 802 |
|
| 803 |
# Enable check mode |
| 804 |
self._plugin._task.check_mode = True |
| 805 |
|
| 806 |
def get_by_path(self, path, query_params=None): |
| 807 |
return copy.deepcopy(RESPONSE_PLAN_LIST_RESPONSE) |
| 808 |
|
| 809 |
update_called = [] |
| 810 |
|
| 811 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 812 |
update_called.append(True) |
| 813 |
return copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 814 |
|
| 815 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 816 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 817 |
|
| 818 |
self._plugin._task.args = copy.deepcopy(UPDATE_RESPONSE_PLAN_PARAMS) |
| 819 |
|
| 820 |
result = self._plugin.run(task_vars=self._task_vars) |
| 821 |
|
| 822 |
assert result["changed"] is True |
| 823 |
assert result.get("failed") is not True |
| 824 |
assert len(update_called) == 0 # Update API should not be called |
| 825 |
assert "check mode" in _get_msg_str(result) |
| 826 |
assert result["response_plan"]["after"] is not None |
| 827 |
|
| 828 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 829 |
def test_response_plan_check_mode_delete(self, connection, monkeypatch): |
| 830 |
"""Test check mode for deleting a response plan.""" |
| 831 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 832 |
self._plugin._connection._shell = MagicMock() |
| 833 |
|
| 834 |
# Enable check mode |
| 835 |
self._plugin._task.check_mode = True |
| 836 |
|
| 837 |
def get_by_path(self, path, query_params=None): |
| 838 |
return copy.deepcopy(RESPONSE_PLAN_LIST_RESPONSE) |
| 839 |
|
| 840 |
delete_called = [] |
| 841 |
|
| 842 |
def delete_by_path(self, path): |
| 843 |
delete_called.append(True) |
| 844 |
return {} |
| 845 |
|
| 846 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 847 |
monkeypatch.setattr(SplunkRequest, "delete_by_path", delete_by_path) |
| 848 |
|
| 849 |
self._plugin._task.args = { |
| 850 |
"name": "Incident Response Plan", |
| 851 |
"state": "absent", |
| 852 |
} |
| 853 |
|
| 854 |
result = self._plugin.run(task_vars=self._task_vars) |
| 855 |
|
| 856 |
assert result["changed"] is True |
| 857 |
assert result.get("failed") is not True |
| 858 |
assert len(delete_called) == 0 # Delete API should not be called |
| 859 |
assert "check mode" in _get_msg_str(result) |
| 860 |
|
| 861 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 862 |
def test_response_plan_check_mode_no_changes(self, connection, monkeypatch): |
| 863 |
"""Test check mode when no changes are needed.""" |
| 864 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 865 |
self._plugin._connection._shell = MagicMock() |
| 866 |
|
| 867 |
# Enable check mode |
| 868 |
self._plugin._task.check_mode = True |
| 869 |
|
| 870 |
existing_response = { |
| 871 |
"items": [ |
| 872 |
{ |
| 873 |
"id": "rp-001-uuid", |
| 874 |
"name": "Minimal Response Plan", |
| 875 |
"description": "", |
| 876 |
"template_status": "draft", |
| 877 |
"phases": [ |
| 878 |
{ |
| 879 |
"id": "phase-uuid", |
| 880 |
"name": "Phase 1", |
| 881 |
"tasks": [ |
| 882 |
{ |
| 883 |
"id": "task-uuid", |
| 884 |
"name": "Task 1", |
| 885 |
"description": "First task", |
| 886 |
"is_note_required": False, |
| 887 |
"owner": "unassigned", |
| 888 |
"suggestions": { |
| 889 |
"actions": [], |
| 890 |
"playbooks": [], |
| 891 |
"searches": [], |
| 892 |
}, |
| 893 |
}, |
| 894 |
], |
| 895 |
}, |
| 896 |
], |
| 897 |
}, |
| 898 |
], |
| 899 |
} |
| 900 |
|
| 901 |
def get_by_path(self, path, query_params=None): |
| 902 |
return copy.deepcopy(existing_response) |
| 903 |
|
| 904 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 905 |
|
| 906 |
self._plugin._task.args = copy.deepcopy(MINIMAL_RESPONSE_PLAN_PARAMS) |
| 907 |
|
| 908 |
result = self._plugin.run(task_vars=self._task_vars) |
| 909 |
|
| 910 |
assert result["changed"] is False |
| 911 |
assert result.get("failed") is not True |
| 912 |
|
| 913 |
# Custom API Path Tests |
| 914 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 915 |
def test_response_plan_custom_api_path(self, connection, monkeypatch): |
| 916 |
"""Test that custom API path parameters are used.""" |
| 917 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 918 |
self._plugin._connection._shell = MagicMock() |
| 919 |
|
| 920 |
captured_paths = [] |
| 921 |
|
| 922 |
def get_by_path(self, path, query_params=None): |
| 923 |
captured_paths.append(path) |
| 924 |
return {"items": []} |
| 925 |
|
| 926 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 927 |
captured_paths.append(rest_path) |
| 928 |
return copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 929 |
|
| 930 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 931 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 932 |
|
| 933 |
params = copy.deepcopy(CREATE_RESPONSE_PLAN_PARAMS) |
| 934 |
params["api_namespace"] = "customNS" |
| 935 |
params["api_user"] = "customuser" |
| 936 |
params["api_app"] = "CustomApp" |
| 937 |
|
| 938 |
self._plugin._task.args = params |
| 939 |
|
| 940 |
result = self._plugin.run(task_vars=self._task_vars) |
| 941 |
|
| 942 |
assert result["changed"] is True |
| 943 |
# Verify the custom path was used |
| 944 |
assert len(captured_paths) > 0 |
| 945 |
assert "customNS" in captured_paths[-1] |
| 946 |
assert "customuser" in captured_paths[-1] |
| 947 |
assert "CustomApp" in captured_paths[-1] |
| 948 |
|
| 949 |
# Template Status Tests |
| 950 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 951 |
def test_response_plan_draft_status(self, connection, monkeypatch): |
| 952 |
"""Test creating a response plan with draft status.""" |
| 953 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 954 |
self._plugin._connection._shell = MagicMock() |
| 955 |
|
| 956 |
captured_payloads = [] |
| 957 |
|
| 958 |
def get_by_path(self, path, query_params=None): |
| 959 |
return {"items": []} |
| 960 |
|
| 961 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 962 |
captured_payloads.append(copy.deepcopy(data)) |
| 963 |
response = copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 964 |
response["template_status"] = "draft" |
| 965 |
return response |
| 966 |
|
| 967 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 968 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 969 |
|
| 970 |
params = copy.deepcopy(MINIMAL_RESPONSE_PLAN_PARAMS) |
| 971 |
params["template_status"] = "draft" |
| 972 |
|
| 973 |
self._plugin._task.args = params |
| 974 |
|
| 975 |
result = self._plugin.run(task_vars=self._task_vars) |
| 976 |
|
| 977 |
assert result["changed"] is True |
| 978 |
assert len(captured_payloads) > 0 |
| 979 |
assert captured_payloads[0]["template_status"] == "draft" |
| 980 |
|
| 981 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 982 |
def test_response_plan_published_status(self, connection, monkeypatch): |
| 983 |
"""Test creating a response plan with published status.""" |
| 984 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 985 |
self._plugin._connection._shell = MagicMock() |
| 986 |
|
| 987 |
captured_payloads = [] |
| 988 |
|
| 989 |
def get_by_path(self, path, query_params=None): |
| 990 |
return {"items": []} |
| 991 |
|
| 992 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 993 |
captured_payloads.append(copy.deepcopy(data)) |
| 994 |
return copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 995 |
|
| 996 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 997 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 998 |
|
| 999 |
params = copy.deepcopy(CREATE_RESPONSE_PLAN_PARAMS) |
| 1000 |
params["template_status"] = "published" |
| 1001 |
|
| 1002 |
self._plugin._task.args = params |
| 1003 |
|
| 1004 |
result = self._plugin.run(task_vars=self._task_vars) |
| 1005 |
|
| 1006 |
assert result["changed"] is True |
| 1007 |
assert len(captured_payloads) > 0 |
| 1008 |
assert captured_payloads[0]["template_status"] == "published" |
| 1009 |
|
| 1010 |
|
| 1011 |
class TestResponsePlanUtilityFunctions: |
| 1012 |
"""Tests for the utility functions in the response plan action plugin. |
| 1013 |
|
| 1014 |
These functions handle API path building and data transformations. |
| 1015 |
""" |
| 1016 |
|
| 1017 |
# API Path Building Tests |
| 1018 |
def test_build_response_plan_api_path_defaults(self): |
| 1019 |
"""Test that default API path is constructed correctly.""" |
| 1020 |
result = _build_response_plan_api_path() |
| 1021 |
|
| 1022 |
assert result == "servicesNS/nobody/missioncontrol/v1/responsetemplates" |
| 1023 |
|
| 1024 |
def test_build_response_plan_api_path_custom_namespace(self): |
| 1025 |
"""Test API path with custom namespace value.""" |
| 1026 |
result = _build_response_plan_api_path(namespace="customNS") |
| 1027 |
|
| 1028 |
assert result == "customNS/nobody/missioncontrol/v1/responsetemplates" |
| 1029 |
|
| 1030 |
def test_build_response_plan_api_path_custom_user(self): |
| 1031 |
"""Test API path with custom user value.""" |
| 1032 |
result = _build_response_plan_api_path(user="admin") |
| 1033 |
|
| 1034 |
assert result == "servicesNS/admin/missioncontrol/v1/responsetemplates" |
| 1035 |
|
| 1036 |
def test_build_response_plan_api_path_custom_app(self): |
| 1037 |
"""Test API path with custom app value.""" |
| 1038 |
result = _build_response_plan_api_path(app="CustomApp") |
| 1039 |
|
| 1040 |
assert result == "servicesNS/nobody/CustomApp/v1/responsetemplates" |
| 1041 |
|
| 1042 |
def test_build_response_plan_api_path_all_custom(self): |
| 1043 |
"""Test API path with all custom values.""" |
| 1044 |
result = _build_response_plan_api_path( |
| 1045 |
namespace="myNS", |
| 1046 |
user="myuser", |
| 1047 |
app="MyApp", |
| 1048 |
) |
| 1049 |
|
| 1050 |
assert result == "myNS/myuser/MyApp/v1/responsetemplates" |
| 1051 |
|
| 1052 |
def test_build_response_plan_update_path(self): |
| 1053 |
"""Test update API path includes ref_id.""" |
| 1054 |
result = _build_response_plan_update_path("rp-001-uuid") |
| 1055 |
|
| 1056 |
assert "rp-001-uuid" in result |
| 1057 |
assert result.endswith("/rp-001-uuid") |
| 1058 |
|
| 1059 |
def test_build_response_plan_update_path_custom_params(self): |
| 1060 |
"""Test update API path with custom namespace/user/app.""" |
| 1061 |
result = _build_response_plan_update_path( |
| 1062 |
"rp-001-uuid", |
| 1063 |
namespace="customNS", |
| 1064 |
user="customuser", |
| 1065 |
app="CustomApp", |
| 1066 |
) |
| 1067 |
|
| 1068 |
assert "customNS" in result |
| 1069 |
assert "customuser" in result |
| 1070 |
assert "CustomApp" in result |
| 1071 |
assert "rp-001-uuid" in result |
| 1072 |
|
| 1073 |
# Task ID Lookup Tests |
| 1074 |
def test_find_task_id_by_name_found(self): |
| 1075 |
"""Test finding existing task ID by name.""" |
| 1076 |
existing_tasks = [ |
| 1077 |
{"id": "task-001", "name": "Initial Triage"}, |
| 1078 |
{"id": "task-002", "name": "Gather Evidence"}, |
| 1079 |
] |
| 1080 |
|
| 1081 |
result = _find_task_id_by_name(existing_tasks, "Initial Triage") |
| 1082 |
|
| 1083 |
assert result == "task-001" |
| 1084 |
|
| 1085 |
def test_find_task_id_by_name_not_found(self): |
| 1086 |
"""Test that non-existent task returns None.""" |
| 1087 |
existing_tasks = [ |
| 1088 |
{"id": "task-001", "name": "Initial Triage"}, |
| 1089 |
] |
| 1090 |
|
| 1091 |
result = _find_task_id_by_name(existing_tasks, "Non-Existent") |
| 1092 |
|
| 1093 |
assert result is None |
| 1094 |
|
| 1095 |
def test_find_task_id_by_name_empty_list(self): |
| 1096 |
"""Test that empty task list returns None.""" |
| 1097 |
result = _find_task_id_by_name([], "Any Task") |
| 1098 |
|
| 1099 |
assert result is None |
| 1100 |
|
| 1101 |
# Search Payload Building Tests |
| 1102 |
def test_build_search_payload_complete(self): |
| 1103 |
"""Test building search payload with all fields.""" |
| 1104 |
search = { |
| 1105 |
"name": "Access Over Time", |
| 1106 |
"description": "Check access patterns", |
| 1107 |
"spl": "| tstats count from datamodel=Authentication", |
| 1108 |
} |
| 1109 |
|
| 1110 |
result = _build_search_payload(search) |
| 1111 |
|
| 1112 |
assert result["name"] == "Access Over Time" |
| 1113 |
assert result["description"] == "Check access patterns" |
| 1114 |
assert result["spl"] == "| tstats count from datamodel=Authentication" |
| 1115 |
|
| 1116 |
def test_build_search_payload_minimal(self): |
| 1117 |
"""Test building search payload with minimal fields.""" |
| 1118 |
search = {"name": "Search", "spl": "index=main"} |
| 1119 |
|
| 1120 |
result = _build_search_payload(search) |
| 1121 |
|
| 1122 |
assert result["name"] == "Search" |
| 1123 |
assert result["spl"] == "index=main" |
| 1124 |
assert result["description"] == "" # Default empty string |
| 1125 |
|
| 1126 |
def test_build_search_payload_empty(self): |
| 1127 |
"""Test building search payload from empty dict.""" |
| 1128 |
result = _build_search_payload({}) |
| 1129 |
|
| 1130 |
assert result["name"] == "" |
| 1131 |
assert result["description"] == "" |
| 1132 |
assert result["spl"] == "" |
| 1133 |
|
| 1134 |
# Task Payload Building Tests |
| 1135 |
def test_build_task_payload_new_task(self): |
| 1136 |
"""Test building payload for a new task.""" |
| 1137 |
task = { |
| 1138 |
"name": "Initial Triage", |
| 1139 |
"description": "Perform initial assessment", |
| 1140 |
"is_note_required": True, |
| 1141 |
"owner": "admin", |
| 1142 |
} |
| 1143 |
|
| 1144 |
result = _build_task_payload(task, order=1, existing_id=None) |
| 1145 |
|
| 1146 |
assert result["name"] == "Initial Triage" |
| 1147 |
assert result["description"] == "Perform initial assessment" |
| 1148 |
assert result["is_note_required"] is True |
| 1149 |
assert result["owner"] == "admin" |
| 1150 |
assert result["order"] == 1 |
| 1151 |
assert result["isNewTask"] is True |
| 1152 |
assert result["id"] is not None # Should have generated UUID |
| 1153 |
|
| 1154 |
def test_build_task_payload_existing_task(self): |
| 1155 |
"""Test building payload for an existing task preserves ID.""" |
| 1156 |
task = { |
| 1157 |
"name": "Initial Triage", |
| 1158 |
"description": "Updated description", |
| 1159 |
} |
| 1160 |
|
| 1161 |
result = _build_task_payload(task, order=2, existing_id="existing-task-uuid") |
| 1162 |
|
| 1163 |
assert result["id"] == "existing-task-uuid" |
| 1164 |
assert result["isNewTask"] is False |
| 1165 |
assert result["order"] == 2 |
| 1166 |
|
| 1167 |
def test_build_task_payload_with_searches(self): |
| 1168 |
"""Test building task payload with searches.""" |
| 1169 |
task = { |
| 1170 |
"name": "Analysis Task", |
| 1171 |
"description": "Run analysis", |
| 1172 |
"searches": [ |
| 1173 |
{"name": "Search 1", "spl": "index=main"}, |
| 1174 |
{"name": "Search 2", "spl": "index=security"}, |
| 1175 |
], |
| 1176 |
} |
| 1177 |
|
| 1178 |
result = _build_task_payload(task, order=1) |
| 1179 |
|
| 1180 |
assert len(result["suggestions"]["searches"]) == 2 |
| 1181 |
assert result["suggestions"]["searches"][0]["name"] == "Search 1" |
| 1182 |
assert result["suggestions"]["searches"][1]["name"] == "Search 2" |
| 1183 |
|
| 1184 |
def test_build_task_payload_defaults(self): |
| 1185 |
"""Test that task payload uses correct defaults.""" |
| 1186 |
task = {"name": "Minimal Task"} |
| 1187 |
|
| 1188 |
result = _build_task_payload(task, order=1) |
| 1189 |
|
| 1190 |
assert result["is_note_required"] is False |
| 1191 |
assert result["owner"] == "unassigned" |
| 1192 |
assert result["status"] == "Pending" |
| 1193 |
assert result["sla"] is None |
| 1194 |
assert result["sla_type"] == "minutes" |
| 1195 |
assert result["files"] == [] |
| 1196 |
assert result["notes"] == [] |
| 1197 |
|
| 1198 |
# Phase Payload Building Tests |
| 1199 |
def test_build_phase_payload_new_phase(self): |
| 1200 |
"""Test building payload for a new phase.""" |
| 1201 |
phase = { |
| 1202 |
"name": "Investigation", |
| 1203 |
"tasks": [ |
| 1204 |
{"name": "Task 1", "description": "First task"}, |
| 1205 |
], |
| 1206 |
} |
| 1207 |
|
| 1208 |
result = _build_phase_payload(phase, order=1, existing_phase=None) |
| 1209 |
|
| 1210 |
assert result["name"] == "Investigation" |
| 1211 |
assert result["order"] == 1 |
| 1212 |
assert len(result["tasks"]) == 1 |
| 1213 |
assert result["id"] is not None # Should have generated UUID |
| 1214 |
|
| 1215 |
def test_build_phase_payload_existing_phase(self): |
| 1216 |
"""Test building payload for an existing phase preserves ID.""" |
| 1217 |
phase = { |
| 1218 |
"name": "Investigation", |
| 1219 |
"tasks": [], |
| 1220 |
} |
| 1221 |
existing_phase = { |
| 1222 |
"id": "existing-phase-uuid", |
| 1223 |
"name": "Investigation", |
| 1224 |
"tasks": [], |
| 1225 |
} |
| 1226 |
|
| 1227 |
result = _build_phase_payload(phase, order=1, existing_phase=existing_phase) |
| 1228 |
|
| 1229 |
assert result["id"] == "existing-phase-uuid" |
| 1230 |
|
| 1231 |
def test_build_phase_payload_with_existing_tasks(self): |
| 1232 |
"""Test that existing task IDs are preserved during phase build.""" |
| 1233 |
phase = { |
| 1234 |
"name": "Investigation", |
| 1235 |
"tasks": [ |
| 1236 |
{"name": "Initial Triage", "description": "Updated"}, |
| 1237 |
{"name": "New Task", "description": "Brand new"}, |
| 1238 |
], |
| 1239 |
} |
| 1240 |
existing_phase = { |
| 1241 |
"id": "phase-uuid", |
| 1242 |
"name": "Investigation", |
| 1243 |
"tasks": [ |
| 1244 |
{"id": "existing-task-uuid", "name": "Initial Triage"}, |
| 1245 |
], |
| 1246 |
} |
| 1247 |
|
| 1248 |
result = _build_phase_payload(phase, order=1, existing_phase=existing_phase) |
| 1249 |
|
| 1250 |
# First task should keep existing ID |
| 1251 |
assert result["tasks"][0]["id"] == "existing-task-uuid" |
| 1252 |
assert result["tasks"][0]["isNewTask"] is False |
| 1253 |
# Second task should be new |
| 1254 |
assert result["tasks"][1]["isNewTask"] is True |
| 1255 |
|
| 1256 |
# Response Plan to API Mapping Tests |
| 1257 |
def test_map_response_plan_to_api_create(self): |
| 1258 |
"""Test mapping response plan for creation (no existing data).""" |
| 1259 |
response_plan = { |
| 1260 |
"name": "Test Plan", |
| 1261 |
"description": "A test plan", |
| 1262 |
"template_status": "draft", |
| 1263 |
"phases": [ |
| 1264 |
{ |
| 1265 |
"name": "Phase 1", |
| 1266 |
"tasks": [{"name": "Task 1", "description": "Desc"}], |
| 1267 |
}, |
| 1268 |
], |
| 1269 |
} |
| 1270 |
|
| 1271 |
result = _map_response_plan_to_api(response_plan) |
| 1272 |
|
| 1273 |
assert result["name"] == "Test Plan" |
| 1274 |
assert result["description"] == "A test plan" |
| 1275 |
assert result["template_status"] == "draft" |
| 1276 |
assert result["incident_types"] == [] |
| 1277 |
assert len(result["phases"]) == 1 |
| 1278 |
assert "id" not in result # No ID for new plan |
| 1279 |
|
| 1280 |
def test_map_response_plan_to_api_update(self): |
| 1281 |
"""Test mapping response plan for update (with existing data).""" |
| 1282 |
response_plan = { |
| 1283 |
"name": "Test Plan", |
| 1284 |
"description": "Updated description", |
| 1285 |
"phases": [], |
| 1286 |
} |
| 1287 |
existing = { |
| 1288 |
"id": "existing-plan-uuid", |
| 1289 |
"name": "Test Plan", |
| 1290 |
"phases": [], |
| 1291 |
} |
| 1292 |
|
| 1293 |
result = _map_response_plan_to_api(response_plan, existing) |
| 1294 |
|
| 1295 |
assert result["id"] == "existing-plan-uuid" |
| 1296 |
|
| 1297 |
# API to Response Plan Mapping Tests |
| 1298 |
def test_map_task_from_api(self): |
| 1299 |
"""Test mapping task from API format to module format.""" |
| 1300 |
api_task = { |
| 1301 |
"id": "task-uuid", |
| 1302 |
"name": "Initial Triage", |
| 1303 |
"description": "Perform triage", |
| 1304 |
"is_note_required": True, |
| 1305 |
"owner": "admin", |
| 1306 |
"suggestions": { |
| 1307 |
"actions": [], |
| 1308 |
"playbooks": [], |
| 1309 |
"searches": [ |
| 1310 |
{"name": "Search 1", "description": "Desc", "spl": "index=main"}, |
| 1311 |
], |
| 1312 |
}, |
| 1313 |
} |
| 1314 |
|
| 1315 |
result = _map_task_from_api(api_task) |
| 1316 |
|
| 1317 |
assert result["name"] == "Initial Triage" |
| 1318 |
assert result["description"] == "Perform triage" |
| 1319 |
assert result["is_note_required"] is True |
| 1320 |
assert result["owner"] == "admin" |
| 1321 |
assert len(result["searches"]) == 1 |
| 1322 |
assert result["searches"][0]["name"] == "Search 1" |
| 1323 |
|
| 1324 |
def test_map_task_from_api_defaults(self): |
| 1325 |
"""Test mapping task with missing optional fields.""" |
| 1326 |
api_task = {"name": "Minimal Task"} |
| 1327 |
|
| 1328 |
result = _map_task_from_api(api_task) |
| 1329 |
|
| 1330 |
assert result["name"] == "Minimal Task" |
| 1331 |
assert result["description"] == "" |
| 1332 |
assert result["is_note_required"] is False |
| 1333 |
assert result["owner"] == "unassigned" |
| 1334 |
assert result["searches"] == [] |
| 1335 |
|
| 1336 |
def test_map_phase_from_api(self): |
| 1337 |
"""Test mapping phase from API format to module format.""" |
| 1338 |
api_phase = { |
| 1339 |
"id": "phase-uuid", |
| 1340 |
"name": "Investigation", |
| 1341 |
"tasks": [ |
| 1342 |
{ |
| 1343 |
"name": "Task 1", |
| 1344 |
"description": "Desc", |
| 1345 |
"is_note_required": False, |
| 1346 |
"owner": "admin", |
| 1347 |
"suggestions": {"actions": [], "playbooks": [], "searches": []}, |
| 1348 |
}, |
| 1349 |
], |
| 1350 |
} |
| 1351 |
|
| 1352 |
result = _map_phase_from_api(api_phase) |
| 1353 |
|
| 1354 |
assert result["name"] == "Investigation" |
| 1355 |
assert len(result["tasks"]) == 1 |
| 1356 |
assert result["tasks"][0]["name"] == "Task 1" |
| 1357 |
|
| 1358 |
def test_map_response_plan_from_api_complete(self): |
| 1359 |
"""Test mapping complete response plan from API format.""" |
| 1360 |
api_response = copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 1361 |
|
| 1362 |
result = _map_response_plan_from_api(api_response) |
| 1363 |
|
| 1364 |
assert result["name"] == "Incident Response Plan" |
| 1365 |
assert result["description"] == "Standard incident response procedure" |
| 1366 |
assert result["template_status"] == "published" |
| 1367 |
assert len(result["phases"]) == 2 |
| 1368 |
assert result["phases"][0]["name"] == "Investigation" |
| 1369 |
assert result["phases"][1]["name"] == "Containment" |
| 1370 |
|
| 1371 |
def test_map_response_plan_from_api_empty(self): |
| 1372 |
"""Test mapping empty response plan from API format.""" |
| 1373 |
result = _map_response_plan_from_api({}) |
| 1374 |
|
| 1375 |
assert result["name"] == "" |
| 1376 |
assert result["description"] == "" |
| 1377 |
assert result["template_status"] == "draft" |
| 1378 |
assert result["phases"] == [] |
| 1379 |
|
| 1380 |
def test_map_response_plan_from_api_with_searches(self): |
| 1381 |
"""Test that searches are correctly extracted from tasks.""" |
| 1382 |
api_response = copy.deepcopy(RESPONSE_PLAN_API_RESPONSE) |
| 1383 |
|
| 1384 |
result = _map_response_plan_from_api(api_response) |
| 1385 |
|
| 1386 |
# First phase, first task should have searches |
| 1387 |
task = result["phases"][0]["tasks"][0] |
| 1388 |
assert len(task["searches"]) == 1 |
| 1389 |
assert task["searches"][0]["name"] == "Access Over Time" |
| 1390 |
assert task["searches"][0]["spl"].startswith("| tstats") |