"""Base Script for Cortex XSOAR - Unit Tests file Pytest Unit Tests: all funcion names must start with "test_" More details: https://xsoar.pan.dev/docs/integrations/unit-testing """ import CommonServerPython import CreateHashIndicatorWrapper import pytest test_data = [ CommonServerPython.CommandRunner.Result( command="cs-falcon-search-custom-iocs", args={"values": "hash1,hash2"}, brand="CrowdstrikeFalcon", instance="CrowdstrikeFalcon_instance_1", result={ "errors": None, "resources": [ { "action": "prevent", "applied_globally": True, "description": "Blacklisted based on XSOAR inc 1", "id": "12", "platforms": ["linux", "mac", "windows"], "severity": "high", "type": "sha256", "value": "hash2", } ], }, ) ] @pytest.mark.parametrize("action", ["allow", "block"]) def test_get_crowdstrike_commands_args(mocker, action): """ Given: the action to perform (allow or block) When: Getting the CrowdStrike commands and args list to run the action on Crowdstrike Then: Ensure the right commands and args_list are being returned. """ from CreateHashIndicatorWrapper import CROWDSTRIKE_ACTIONS, demisto, get_crowdstrike_commands_args ioc_metadata = CROWDSTRIKE_ACTIONS.get(action) hashes_dct = { "hash1": ( "cs-falcon-upload-custom-ioc", { "ioc_type": "sha256", "platforms": "linux,mac,windows", "applied_globally": "true", "value": "hash1", **ioc_metadata, }, ), "hash2": ("cs-falcon-update-custom-ioc", {"ioc_id": "12", **ioc_metadata}), } ioc_to_hash = {"12": "hash2"} mocker.patch.object(CreateHashIndicatorWrapper.CommandRunner, "execute_commands", return_value=(test_data, [])) mocker.patch.object(demisto, "incident", return_value={"id": 1}) commands, args_lst = get_crowdstrike_commands_args(list(hashes_dct.keys()), action) for command, args in zip(commands, args_lst): h = args.get("value") or ioc_to_hash.get(args.get("ioc_id")) assert h in hashes_dct expected_command, expected_args = hashes_dct.get(h) assert command == expected_command assert args == expected_args @pytest.mark.parametrize("action", ["allow", "block"]) def test_create_command_executers(mocker, action): """ Given: the action to perform (allow or block) When: Calling `create_command_wrappers` to get all the command wrappers for the script. Then: Ensure the right commands wrappers are being returned. """ from CreateHashIndicatorWrapper import CROWDSTRIKE_ACTIONS, MSDE_ACTIONS, XDR_ACTIONS, create_commands, demisto hashes = ["hash1", "hash2"] mocker.patch.object(CreateHashIndicatorWrapper.CommandRunner, "execute_commands", return_value=(test_data, [])) mocker.patch.object(demisto, "incident", return_value={"id": 1}) commands = create_commands(hashes, action) ioc_metadata = CROWDSTRIKE_ACTIONS.get(action) for command in commands: command_names = set(command.commands) if "microsoft-atp-sc-indicator-create" in command_names: assert command_names == {"microsoft-atp-sc-indicator-create"} assert {args.get("action") for args in command.args_lst} == {MSDE_ACTIONS.get(action)} if XDR_ACTIONS.get(action) in command_names: assert command_names == {XDR_ACTIONS.get(action)} assert command.args_lst == [{"hash_list": ",".join(hashes)}] if "cs-falcon-upload-custom-ioc" in command_names or "cs-falcon-update-custom-ioc" in command_names: assert command_names == {"cs-falcon-upload-custom-ioc", "cs-falcon-update-custom-ioc"} assert command.args_lst == [ { "ioc_type": "sha256", "platforms": "linux,mac,windows", "applied_globally": "true", "value": "hash1", **ioc_metadata, }, {"ioc_id": "12", **ioc_metadata}, ]