import demistomock as demisto import pytest EMAIL_HTML = """
image 1:
image_1.png
image 2:
image_2.png

On Thu, Oct 22, 2020 at 1:56 AM Some Person < some.person@demistodev.onmicrosoft.com> wrote:

please add multiple inline images

""" EMAIL_HTML_NO_ALT = """
""" # noqa: RUF001 EXPECTED_RESULT_1 = """
image 1:
image_1.png
image 2:
image_2.png

On Thu, Oct 22, 2020 at 1:56 AM Some Person < some.person@demistodev.onmicrosoft.com> wrote:

please add multiple inline images

""" EXPECTED_RESULT_2 = """
image 1:
image_1.png
\
image 2:
image_2.png
\

On Thu, Oct 22, 2020 at 1:56 AM Some Person < some.person@demistodev.onmicrosoft.com> wrote:

please add multiple inline images

""" EXPECTED_RESULT_NO_ALT = """
""" # noqa: RUF001 @pytest.mark.parametrize("email_html,expected", [(EMAIL_HTML, EXPECTED_RESULT_2), (EMAIL_HTML_NO_ALT, EXPECTED_RESULT_NO_ALT)]) def test_main_mt(mocker, email_html, expected): """ Given - Html contained images src When - All images were uploaded to the server Then - The images' src attribute would be replaced as expected with account tenant name """ import DisplayHTMLWithImages from DisplayHTMLWithImages import main mocked_incident = { "CustomFields": {"emailbody": email_html}, "attachment": [{"name": "image_1.png"}, {"name": "image_2.png"}], } mocked_files = [{"Name": "image_1.png", "EntryID": "37@119"}, {"Name": "image_2.png", "EntryID": "38@120"}] mocker.patch.object(demisto, "demistoUrls", return_value={"server": "https://localhost:8443:/acc_test_tenant"}) mocker.patch.object(demisto, "incident", return_value=mocked_incident) mocker.patch.object(demisto, "context", return_value={"File": mocked_files}) mocker.patch.object(DisplayHTMLWithImages, "return_results") main() assert expected in DisplayHTMLWithImages.return_results.call_args[0][0]["Contents"] @pytest.mark.parametrize("email_html,expected", [(EMAIL_HTML, EXPECTED_RESULT_2), (EMAIL_HTML_NO_ALT, EXPECTED_RESULT_NO_ALT)]) def test_imgaes_not_attached_to_incident(mocker, email_html, expected): """ Given - Html contained images src but not attached to incident. When - All images were uploaded to the server Then - The images' src attribute would be replaced as expected with account tenant name """ import DisplayHTMLWithImages from DisplayHTMLWithImages import main mocked_incident = {"CustomFields": {"emailbody": email_html}, "attachment": []} mocked_files = [{"Name": "image_1.png", "EntryID": "37@119"}, {"Name": "image_2.png", "EntryID": "38@120"}] mocked_context = { "Email": { "AttachmentsData": [ { "Content-Disposition": 'attachment; filename="image_1.png"', "Content-ID": "", "Name": "image_1.png", }, { "Content-Disposition": 'attachment; filename="image_2.png"', "Content-ID": "", "Name": "image_2.png", }, ] }, "File": mocked_files, } mocker.patch.object(demisto, "demistoUrls", return_value={"server": "https://localhost:8443:/acc_test_tenant"}) mocker.patch.object(demisto, "incident", return_value=mocked_incident) mocker.patch.object(demisto, "context", return_value=mocked_context) mocker.patch.object(DisplayHTMLWithImages, "return_results") main() assert expected in DisplayHTMLWithImages.return_results.call_args[0][0]["Contents"] def test_2_imgaes_with_same_name(mocker): """ Given - Html contained 2 images with same name and another file. When - All images were uploaded to the server Then - The images' src attribute would be replaced as expected with the correct entry id """ import DisplayHTMLWithImages from DisplayHTMLWithImages import main mocked_incident = { "CustomFields": {"emailbody": ''}, } mocked_files = [ {"Name": "test.pdf", "EntryID": "36@119"}, {"Name": "image_1.png", "EntryID": "37@119"}, {"Name": "image_1.png", "EntryID": "38@119"}, ] mocked_context = { "Email": { "AttachmentsData": [ {"Content-ID": "", "Name": "test.pdf"}, {"Content-ID": "", "Name": "image_1.png"}, {"Content-ID": "", "Name": "image_1.png"}, ] }, "File": mocked_files, } mocker.patch.object(demisto, "demistoUrls", return_value={"server": "test_url"}) mocker.patch.object(demisto, "incident", return_value=mocked_incident) mocker.patch.object(demisto, "context", return_value=mocked_context) mocker.patch.object(DisplayHTMLWithImages, "return_results") main() expected = "" assert expected in DisplayHTMLWithImages.return_results.call_args[0][0]["Contents"] def test_2_imgaes_with_same_name_gmail_format(mocker): """ Given - Html contained 2 images with same name with the Gmail format (with alt=image name). When - All images were uploaded to the server Then - The images' src attribute would be replaced as expected with the correct entry id """ import DisplayHTMLWithImages from DisplayHTMLWithImages import main mocked_incident = { "CustomFields": {"emailbody": 'image.pngimage.png'}, } mocked_files = [{"Name": "image.png", "EntryID": "37@119"}, {"Name": "image.png", "EntryID": "38@119"}] mocked_context = { "Email": { "AttachmentsData": [ {"Content-ID": "", "Name": "image.png"}, {"Content-ID": "", "Name": "image.png"}, ] }, "File": mocked_files, } mocker.patch.object(demisto, "demistoUrls", return_value={"server": "test_url"}) mocker.patch.object(demisto, "incident", return_value=mocked_incident) mocker.patch.object(demisto, "context", return_value=mocked_context) mocker.patch.object(DisplayHTMLWithImages, "return_results") main() expected = 'image.pngimage.png' assert expected in DisplayHTMLWithImages.return_results.call_args[0][0]["Contents"] def test_one_imgae_in_emailhtml(mocker): """ Given - Incident contained the Html which contained one image in the emailhtml filed. When - Image were uploaded to the server Then - The image' src attribute would be replaced as expected with the correct entry id """ import DisplayHTMLWithImages from DisplayHTMLWithImages import main mocked_incident = { "CustomFields": {"emailhtml": ''}, } mocked_file = {"Name": "image_1.png", "EntryID": "38@119"} mocked_context = { "Email": {"AttachmentsData": [{"Content-ID": "", "Name": "image_1.png"}]}, "File": mocked_file, } mocker.patch.object(demisto, "demistoUrls", return_value={"server": "test_url"}) mocker.patch.object(demisto, "incident", return_value=mocked_incident) mocker.patch.object(demisto, "context", return_value=mocked_context) mocker.patch.object(DisplayHTMLWithImages, "return_results") main() expected = "" assert expected in DisplayHTMLWithImages.return_results.call_args[0][0]["Contents"] def test_content_id_none(mocker): """ Given - The COntent ID of attachment in context are None. When - Image were uploaded to the server Then - The image' src attribute would not be replaced. """ import DisplayHTMLWithImages from DisplayHTMLWithImages import main mocked_incident = { "CustomFields": {"emailhtml": ''}, } mocked_file = {"Name": "image_1.png", "EntryID": "38@119"} mocked_context = {"Email": {"AttachmentsData": [{"Content-ID": None, "Name": "image_1.png"}]}, "File": mocked_file} mocker.patch.object(demisto, "demistoUrls", return_value={"server": "test_url"}) mocker.patch.object(demisto, "incident", return_value=mocked_incident) mocker.patch.object(demisto, "context", return_value=mocked_context) mocker.patch.object(DisplayHTMLWithImages, "return_results") main() expected = '' assert expected in DisplayHTMLWithImages.return_results.call_args[0][0]["Contents"] @pytest.mark.parametrize("is_xsoar_saas, expected_prefix", [(True, "/xsoar"), (False, "")]) def test_xsoar_saas(mocker, is_xsoar_saas, expected_prefix): """ Given - The is_xsiam_or_xsoar_saas is True or False. When - Image were uploaded to the server Then - The image' src attribute would be replaced with the right download url (with /xsoar in case xsoar saas). """ import DisplayHTMLWithImages from DisplayHTMLWithImages import main mocked_incident = { "CustomFields": {"emailhtml": ''}, } mocked_file = {"Name": "image_1.png", "EntryID": "38@119"} mocked_context = {"Email": {"AttachmentsData": {"Content-ID": "", "Name": "image_1.png"}}, "File": mocked_file} mocker.patch.object(demisto, "demistoUrls", return_value={"server": "test_url"}) mocker.patch.object(demisto, "incident", return_value=mocked_incident) mocker.patch.object(demisto, "context", return_value=mocked_context) mocker.patch.object(DisplayHTMLWithImages, "return_results") mocker.patch.object(DisplayHTMLWithImages, "is_xsiam_or_xsoar_saas", return_value=is_xsoar_saas) main() expected = f"" assert expected in DisplayHTMLWithImages.return_results.call_args[0][0]["Contents"] class TestSanitizeHtml: """Tests for HTML sanitization in _sanitize_html.""" def test_strips_script_tags(self, mocker): """ Given - An HTML body containing script tags When - _sanitize_html is called with bleach available Then - Script tags and their content are removed while safe content is preserved """ import types import DisplayHTMLWithImages # Create a mock bleach module that simulates real bleach.clean behavior mock_bleach = types.ModuleType("bleach") def mock_clean(html, tags=None, strip=False, attributes=None): """Simulate bleach: strip disallowed tags, keep allowed ones.""" import re # Remove script tags and their content (not in allowed tags) result = re.sub(r"<\s*script[^>]*>.*?<\s*/\s*script[^>]*>", "", html, flags=re.DOTALL | re.IGNORECASE) return result mock_bleach.clean = mock_clean mocker.patch.dict("sys.modules", {"bleach": mock_bleach}) # Re-import to pick up the mock result = DisplayHTMLWithImages._sanitize_html('

Hello

World

') assert "

World

' result = DisplayHTMLWithImages._sanitize_html(html_input) # Without bleach, the fallback regex strips script tags assert "