You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by gc...@apache.org on 2026/03/13 14:30:32 UTC

(allura) branch gc/8596 updated: [#8596] add test for email notification that send markdown content

This is an automated email from the ASF dual-hosted git repository.

gcruz pushed a commit to branch gc/8596
in repository https://gitbox.apache.org/repos/asf/allura.git


The following commit(s) were added to refs/heads/gc/8596 by this push:
     new ff91998da [#8596] add test for email notification that send markdown content
ff91998da is described below

commit ff91998da72685f152d3248c8707b8dc711c0e19
Author: Guillermo Cruz <gu...@slashdotmedia.com>
AuthorDate: Thu Mar 12 19:36:41 2026 -0600

    [#8596] add test for email notification that send markdown content
---
 Allura/allura/lib/helpers.py                   |  9 ++----
 Allura/allura/tests/model/test_notification.py | 41 ++++++++++++++++++++++++--
 2 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 8e1890168..ecb6d063c 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -1440,14 +1440,9 @@ def clean_html(value: str) -> Markup:
 def escape_markdown(content: str) -> str:
     if content is None:
         return ''
-    md = markdown.Markdown()
-    escaped_chars = md.ESCAPED_CHARS
-    if isinstance(escaped_chars, (list, tuple, set)):
-        escaped_chars = "".join(escaped_chars)
-    else:
-        escaped_chars = str(escaped_chars)
+    escaped_chars = r"\\`*_{}[]()#!"
     # escape html tag like <b> or <img>
-    html_escaped = html.escape(content, quote=True)
+    html_escaped = html.escape(content, quote=False)
     pattern = re.compile(rf"([{re.escape(escaped_chars)}])")
     # Escape markdown special characters to prevent unintended formatting
     return pattern.sub(r"\\\1", html_escaped)
diff --git a/Allura/allura/tests/model/test_notification.py b/Allura/allura/tests/model/test_notification.py
index 42b14728c..995d68f74 100644
--- a/Allura/allura/tests/model/test_notification.py
+++ b/Allura/allura/tests/model/test_notification.py
@@ -284,13 +284,50 @@ def test_footer(self):
         footer = MailFooter.standard(M.Notification())
         assert 'Sent from localhost because you indicated interest' in footer
 
+    @pytest.mark.parametrize('markdown, result',
+                             [('![img](https://static.vecteezy.com/system/resources/thumbnails/012/042/301/small_2x/warning-sign-icon-transparent-background-free-png.png)',
+                               '![img](https://static.vecteezy.com/system/resources/thumbnails/012/042/301/small_2x/warning-sign-icon-transparent-background-free-png.png)'),
+                                 ('[link text](https://example.com)', '[link text](https://example.com)'),
+                                 # make sure no errors thrown
+                             ])
+    def test_email_markdown_not_parsed(self, markdown, result):
+        """
+        strings like `![img](url)` must not be converted to HTML
+        (e.g., `<img ...>`). We just expect the raw string to appear in the
+        outgoing email body.
+        """
+        self._subscribe()  # current user: test-admin
+        self._post_notification(text=markdown, subject=markdown)
+        ThreadLocalODMSession.flush_all()
+
+        M.MonQTask.run_ready()
+        ThreadLocalODMSession.flush_all()
+
+        email_task = M.MonQTask.query.get(
+            task_name='allura.tasks.mail_tasks.sendmail',
+            state='ready')
+        assert email_task is not None
+        body = email_task.kwargs['text']
+        subject = email_task.kwargs['subject']
+        # no markdown rendering in email
+        assert '<img' not in body
+        assert '<a ' not in body
+        assert markdown in body
+
+        # subject should also not render html/markdown
+        assert '<img' not in subject
+        assert '<a ' not in subject
+        assert '<' not in subject
+        assert '>' not in subject
+        assert markdown in subject
+
     def _subscribe(self, **kw):
         self.pg.subscribe(type='direct', **kw)
         ThreadLocalODMSession.flush_all()
         ThreadLocalODMSession.close_all()
 
-    def _post_notification(self):
-        return M.Notification.post(self.pg, 'metadata')
+    def _post_notification(self, **kwargs):
+        return M.Notification.post(self.pg, 'metadata', **kwargs)
 
 
 class TestSubscriptionTypes: