ChangeContext

Enables changing context in two ways. The first is to capitalize the first letter of each key in following level of the context key entered. The second is to change context keys to new values.

python · Common Scripts

Details

IDChangeContext
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10404775
TagsUtility

README

Enables changing context in two ways. The first is to capitalize the first letter of each key in following level of the context key entered. The second is to change context keys to new values.

Script Data


Name Description
Script Type python3
Tags Utility
Cortex XSOAR Version 5.0.0

Used In


This script is used in the following playbooks and scripts.

  • QRadar - Get Offense Logs
  • QRadar - Get offense correlations v2
  • QRadarCorrelationLog
  • SafeBreach - Compare and Validate Insight Indicators

Inputs


Argument Name Description
input The context to change (i.e., ${Context.Key}).
inplace If “True” replaces the existing key. The default is “True”.
replace_dict A list of key-values to replace key for value in the following format: {“old_key1”:”new_key1”, “old_key2”:”new_key2”}
capitalize If “True” capitalizes the first letter of the context key.
output_key The context path in which to output the results. Should be in the format of Context.Key.

Outputs


There are no outputs for this script.

import demistomock as demisto
from ChangeContext import replace_context
from CommonServerPython import *


def test_capitalize_context_list_inplace(mocker):
    context = [{"key": "val", "key2": "val2"}, {"list2key1": 1, "list2key2": 2}]
    args = {"input": context, "capitalize": "True", "inplace": "True", "output_key": "Demisto.Test"}
    xcommand_tracker = mocker.patch.object(demisto, "executeCommand")
    replace_context(args)
    assert xcommand_tracker.call_args[0][1] == {
        "key": "Demisto.Test",
        "value": [{"Key": "val", "Key2": "val2"}, {"List2Key1": 1, "List2Key2": 2}],
    }


def test_capitalize_context_list_not_inplace(mocker):
    context = [{"key": "val", "key2": "val2"}, {"list2key1": 1, "list2key2": 2}]
    args = {"input": context, "output_key": "Demisto.Test", "capitalize": "True", "inplace": "False"}
    _, ec, _ = replace_context(args)

    assert ec == {"Demisto.Test": [{"Key": "val", "Key2": "val2"}, {"List2Key1": 1, "List2Key2": 2}]}


def test_change_context_path_inplace(mocker):
    context = [{"key": "val", "key2": "val2"}, {"list2key1": 1, "list2key2": 2}]
    args = {
        "input": context,
        "output_key": "Demisto.Test",
        "capitalize": "True",
        "inplace": "True",
        "replace_dict": json.dumps({"key": "newKey", "list2key2": "newListKey"}),
    }
    mocker.patch.object(demisto, "executeCommand")
    replace_context(args)
    assert demisto.executeCommand.call_args[0][1] == {
        "key": "Demisto.Test",
        "value": [{"newKey": "val", "Key2": "val2"}, {"List2Key1": 1, "newListKey": 2}],
    }


def test_replace_dict_not_inplace(mocker):
    context = [{"key": "val", "key2": "val2"}, {"list2key1": 1, "list2key2": 2}]
    args = {
        "input": context,
        "output_key": "Demisto.Test",
        "capitalize": "False",
        "inplace": "False",
        "replace_dict": json.dumps({"key": "newKey", "list2key2": "newListKey"}),
    }
    _, ec, _ = replace_context(args)

    assert ec == {"Demisto.Test": [{"newKey": "val", "key2": "val2"}, {"list2key1": 1, "newListKey": 2}]}