SendEmailOnSLABreach

Sends an email informing the user of an SLA breach. The email is sent to the user who is assigned to the incident. It includes the incident name, ID, name of the SLA field that was breached, duration of that SLA field, and the date and time when that SLA was started. In order to run successfully, the script should be configured to trigger on SLA breach, through field edit mode.

python · Common Scripts

Details

IDSendEmailOnSLABreach
Languagepython
From Version6.5.0
Docker Imagedemisto/python3:3.12.13.10404775
Tagssla example

README

Sends an email informing the user of an SLA breach. The email is sent to the user who is assigned to the incident. It includes the incident name, ID, and name of the SLA field that was breached, the duration of that SLA field, and the date and time when that SLA was started.
In order to run successfully, the script should be configured to trigger on a SLA breach, through field edit mode.

Script Data


Name Description
Script Type python
Tags sla, example

Inputs


There are no inputs for this script.

Outputs


There are no outputs for this script.

import demistomock as demisto

incident_with_owner = {"id": 1, "name": "incident1", "owner": "admin"}
incident_without_owner = {"id": 2, "name": "incident2", "owner": ""}


def test_get_owner_email(mocker):
    """
    Given:
        - Incident with owner.
    When:
        - Running get_owner_email function.
    Then:
        - Validating the return value as expected.
    """
    from SendEmailOnSLABreach import get_owner_email

    mocker.patch.object(demisto, "incidents", return_value=[incident_with_owner])
    mocker.patch.object(
        demisto, "executeCommand", return_value=[{"EntryContext": {"UserByUsername": {"email": "test@gmail.com"}}, "Type": ""}]
    )
    results_mock = mocker.patch.object(demisto, "results")
    assert get_owner_email() == "test@gmail.com"
    results_mock.assert_not_called()


def test_get_no_email_owner(mocker):
    """
    Given:
        - Incident without owner.
    When:
        - Running get_owner_email function.
    Then:
        - Validating calling to 'demisto.results' once with the right arguments.
    """
    from SendEmailOnSLABreach import get_owner_email

    mocker.patch.object(demisto, "incidents", return_value=[incident_without_owner])
    demisto_results_mocker = mocker.patch.object(demisto, "results")
    get_owner_email()
    demisto_results_mocker.assert_called_once()
    results = demisto_results_mocker.call_args[0][0]
    assert results == {
        "Type": 4,
        "ContentsFormat": "text",
        "Contents": "An email can't be sent to the owner of the incident, because no owner was assigned.",
    }


def test_get_subject(mocker):
    """
    Given:
        - Incident
    When:
        - Running get_subject function.
    Then:
        - Validating the return value as expected.
    """
    from SendEmailOnSLABreach import get_subject

    mocker.patch.object(demisto, "incidents", return_value=[incident_with_owner])
    excepted_subject = 'SLA Breached in incident "{}" #{}'.format(incident_with_owner["name"], incident_with_owner["id"])
    assert get_subject() == excepted_subject


def test_send_email(mocker):
    """
    Given:
        - The function's arguments
    When:
        - Running send_email function.
    Then:
         - Validating calling to 'demisto.results' once with the right arguments.
    """
    from SendEmailOnSLABreach import send_email

    mocker.patch.object(demisto, "executeCommand", return_value="send-mail")
    results_mock = mocker.patch.object(demisto, "results")
    send_email(to="to_mail_test", subject="subject_test", body="body_test")
    results_mock.assert_called_once()
    assert results_mock.call_args[0][0] == "send-mail"


def test_get_body(mocker):
    """
    Given:
        - The function's arguments
    When:
        - Running get_body function.
    Then:
        - Validating the return value as expected.
    """
    from SendEmailOnSLABreach import get_body

    field_name, sla, start_date = "cliNameTest", "slaTest", "2022-09-07T15:10:04.000Z"
    args = {"field": {"cliName": "cliNameTest"}, "fieldValue": {"sla": "slaTest", "startDate": "2022-09-07T15:10:04.000Z"}}
    mocker.patch.object(demisto, "args", return_value=args)
    excepted_body = 'We have detected a breach in your SLA "{}".\nThe SLA was set to {} minute and was started on {}.'.format(
        field_name, sla, start_date.split(".")[0]
    )
    assert get_body() == excepted_body