import pytest from CommonServerPython import * from freezegun import freeze_time from SendEmailReply import ( get_unique_code, apply_direction, process_directions, ensure_markdown_tables_have_spacing, replace_atlassian_tags, ) 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( "inputted_body, expected_body", [ pytest.param( '
Formatted HTML message.\r\n', 'Formatted HTML message.\r\n\r\nTest email signature.\r\n', id="HTML Body", ), pytest.param( "Plain text message.
", "Plain text message.
\r\nTest email signature.\r\n", id="Plain Text Body", ), ], ) def test_append_email_signature(mocker, inputted_body: str, expected_body: str): """ Given - Email signature stored in XSOAR List When - List content is returned correctly Then - Validate that the returned message includes the appended signature content """ from SendEmailReply import append_email_signature signature_list = util_load_json("test_data/getList_signature_success.json") mocker.patch.object(demisto, "executeCommand", return_value=signature_list) email_body = append_email_signature(inputted_body) assert email_body == expected_body def test_append_email_signature_fails(mocker): """ Given - Email signature stored in XSOAR List When - List content results in error when fetched Then - Validate that the returned message remains unchanged - Validate that a debug message is saved indicating the list couldn't be fetched """ from SendEmailReply import append_email_signature get_list_error_response = util_load_json("test_data/getList_signature_error.json") mocker.patch.object(demisto, "executeCommand", return_value=get_list_error_response) debug_mocker = mocker.patch.object(demisto, "debug") html_body = append_email_signature("Simple HTML message.\r\n") debug_mocker_call_args = debug_mocker.call_args assert ( debug_mocker_call_args.args[0] == "Error occurred while trying to load the `XSOAR - Email Communication " "Signature` list. No signature added to email. Error: Item not found (8)." ) assert html_body == "Simple HTML message.\r\n" @pytest.mark.parametrize( "email_cc, email_bcc, expected_result", [ ("", "", "Mail sent successfully. To: test1@gmail.com,test2@gmail.com"), ("cc_user@company.com", "", "Mail sent successfully. To: test1@gmail.com,test2@gmail.com Cc: cc_user@company.com"), ( "cc_user@company.com", "bcc_user@company.com", "Mail sent successfully. To: test1@gmail.com,test2@gmail.com Cc: cc_user@company.com Bcc: bcc_user@company.com", ), ], ) def test_validate_email_sent(email_cc, email_bcc, expected_result, mocker): """ Given - Raw response of an email reply. When - The result is a successful reply with email recipients, cc, and bcc. Then - Validate that the successful message is returned. """ from SendEmailReply import validate_email_sent email_reply_response = util_load_json("test_data/email_reply.json") mocker.patch("SendEmailReply.execute_reply_mail", return_value=email_reply_response) result = validate_email_sent( "123", "email_subject", False, "test1@gmail.com,test2@gmail.com", "reply_body", "html", "test.onmicrosoft.com", email_cc, email_bcc, "reply_html_body", {}, "item_id", "12345678", "test.onmicrosoft.com", "test.from.mail", ) assert result == expected_result def test_validate_email_sent_fails(mocker): """ Given - a random error message which is returned from reply-mail executed command. When - executing the 'send_reply' function Then - an error message would be returned. """ from SendEmailReply import validate_email_sent False, get_error(util_load_json("test_data/reply_mail_error.json")) mocker.patch("SendEmailReply.execute_command", side_effect=Exception()) mocker.patch.object(demisto, "debug") return_error_mock = mocker.patch("SendEmailReply.return_error") with pytest.raises(Exception): validate_email_sent("", "", False, "", "", "html", "", "", "", "", {}, "", "", "", "") assert return_error_mock.call_count == 1 assert ( return_error_mock.call_args[0][0] == 'SetIncident Failed."emailsubject" field was not updated with <> value for incident: ' ) @pytest.mark.parametrize( "test_args, expected_response", [ ( ( 1, "Email Subject", False, "end_user@company.com", "Reply body.", "html", "soc_sender@company.com", "cc_user@company.com", "bcc_user@company.com", "Reply body. Email Subject", "cc": "cc_user@company.com", "from": "from_mail", "bcc": "bcc_user@company.com", "htmlBody": "Reply body.Reply body.