SummarizeEmailThreads

Dynamic-section script for 'Email Threads' layout. This script searches for email threads stored in the Incident context and outputs a table summarizing them.

python · Email Communication

Details

IDSummarizeEmailThreads
Languagepython
From Version6.2.0
Docker Imagedemisto/python3:3.12.13.10116658
Tagsdynamic-section

README

Summarizes details of all email threads (Thread Number, Subject, Recipients, CC, BCC) present on the current Incident in a markdown table

Script Data


Name Description
Script Type python3
Tags dynamic-section

Inputs


There are no inputs for this script.

Outputs


There are no outputs for this script.

import pytest
from CommonServerPython import *


def util_open_file(path):
    with open(path) as f:
        return f.read()


def util_load_json(path):
    with open(path) as f:
        return json.loads(f.read())


@pytest.mark.parametrize(
    "email_threads, expected_result",
    [
        (util_load_json("test_data/email_threads.json"), util_load_json("test_data/email_threads.json")),
        (util_load_json("test_data/email_threads.json")[0], [util_load_json("test_data/email_threads.json")[0]]),
        ("", None),
    ],
)
def test_fetch_email_threads(email_threads, expected_result, mocker):
    """
    Unit test Scenario 1 - Multiple thread entries present
        Given
        - Function is called to fetch email threads from current incident
        When
        - Context contains multiple mail threads (list of dicts)
        Then
        - Validate that function returns email thread data (list of dicts)
    Unit test Scenario 2 - Single thread entry present
        Given
        - Function is called to fetch email threads from current incident
        When
        - Context contains a single thread entry (dict)
        Then
        - Validate that function returns email thread data (list with one dict)
    Unit test Scenario 3 - No thread entries present
        Given
        - Function is called to fetch email threads from current incident
        When
        - Context contains no thread entries
        Then
        - Validate that function returns None
    """
    import SummarizeEmailThreads
    from SummarizeEmailThreads import fetch_email_threads

    mocker.patch.object(demisto, "executeCommand")
    mocker.patch.object(SummarizeEmailThreads, "dict_safe_get", return_value=email_threads)
    result = fetch_email_threads("1")
    assert result == expected_result


def test_format_threads(mocker):
    """Unit test
    Given
    - Function is called to summarize email threads into markdown format
    When
    - Email thread entries have primary, CC, and BCC recipients
    - Later emails in the chain add new CC users
    Then
    - Validate that function calls tableToMarkdown with correct table contents
    """
    import SummarizeEmailThreads
    from SummarizeEmailThreads import format_threads

    tableToMarkdown_mocker = mocker.patch.object(SummarizeEmailThreads, "tableToMarkdown", return_value=True)
    format_threads(util_load_json("test_data/email_threads.json"))
    tableToMarkdown_call_args = tableToMarkdown_mocker.call_args
    expected = util_load_json("test_data/valid_table_data.txt")
    actual = tableToMarkdown_call_args.kwargs["t"]
    assert actual == expected