You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2017/07/06 19:31:44 UTC
[3/5] allura git commit: [#7896] refactor and strengthen testing of
existing unescape() usage
[#7896] refactor and strengthen testing of existing unescape() usage
Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/a113534b
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/a113534b
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/a113534b
Branch: refs/heads/db/8157
Commit: a113534b3ce39a4de0dacb88c00bfef85aef0ebf
Parents: 4f4cad0
Author: Dave Brondsema <da...@brondsema.net>
Authored: Thu Jul 6 13:58:59 2017 -0400
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Thu Jul 6 14:00:05 2017 -0400
----------------------------------------------------------------------
Allura/allura/tasks/mail_tasks.py | 37 +++++++++++++-------
.../forgetracker/tests/functional/test_root.py | 4 +++
2 files changed, 28 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/allura/blob/a113534b/Allura/allura/tasks/mail_tasks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tasks/mail_tasks.py b/Allura/allura/tasks/mail_tasks.py
index ce0f98a..16c2f1f 100644
--- a/Allura/allura/tasks/mail_tasks.py
+++ b/Allura/allura/tasks/mail_tasks.py
@@ -103,6 +103,27 @@ def route_email(
log.exception('Error routing mail to %s', addr)
+def create_multipart_msg(text, metalink=None):
+ """
+ Convert Markdown text to plaintext & HTML, combine into a multipart email Message
+ :param text:
+ :param metalink:
+ :return:
+ """
+
+ plain_text = text
+ plain_text = HTMLParser.HTMLParser().unescape(plain_text) # put literal HTML tags back into plaintext
+ plain_msg = mail_util.encode_email_part(plain_text, 'plain')
+
+ html_text = g.forge_markdown(email=True).convert(text)
+ if metalink:
+ html_text = html_text + mail_meta_content(metalink)
+ html_msg = mail_util.encode_email_part(html_text, 'html')
+
+ multi_msg = mail_util.make_multipart_message(plain_msg, html_msg)
+ return multi_msg, html_msg, plain_msg
+
+
@task
def sendmail(fromaddr, destinations, text, reply_to, subject,
message_id, in_reply_to=None, sender=None, references=None, metalink=None):
@@ -157,14 +178,8 @@ def sendmail(fromaddr, destinations, text, reply_to, subject,
addrs_html.append(addr)
else:
addrs_multi.append(addr)
- htmlparser = HTMLParser.HTMLParser()
- plain_msg = mail_util.encode_email_part(htmlparser.unescape(text), 'plain')
- html_text = g.forge_markdown(email=True).convert(text)
- if metalink != None:
- html_text = html_text + mail_meta_content(metalink)
- html_msg = mail_util.encode_email_part(html_text, 'html')
- multi_msg = mail_util.make_multipart_message(plain_msg, html_msg)
+ multi_msg, html_msg, plain_msg = create_multipart_msg(text, metalink)
smtp_client.sendmail(
addrs_multi, fromaddr, reply_to, subject, message_id,
in_reply_to, multi_msg, sender=sender, references=references)
@@ -217,11 +232,7 @@ def sendsimplemail(
else:
toaddr = user.email_address_header()
- htmlparser = HTMLParser.HTMLParser()
- plain_msg = mail_util.encode_email_part(htmlparser.unescape(text), 'plain')
- html_text = g.forge_markdown(email=True).convert(text)
- html_msg = mail_util.encode_email_part(html_text, 'html')
- multi_msg = mail_util.make_multipart_message(plain_msg, html_msg)
+ multi_msg, html_msg, plain_msg = create_multipart_msg(text)
smtp_client.sendmail(
[toaddr], fromaddr, reply_to, subject, message_id,
in_reply_to, multi_msg, sender=sender, references=references, cc=cc, to=toaddr)
@@ -232,7 +243,7 @@ def send_system_mail_to_user(user_or_emailaddr, subject, text):
Sends a standard email from the Allura system itself, to a user.
This is a helper function around sendsimplemail() that generates a new task
- :param user_or_emailaddr: an email addres (str) or a User object
+ :param user_or_emailaddr: an email address (str) or a User object
:param subject: subject of the email
:param text: text of the email (markdown)
'''
http://git-wip-us.apache.org/repos/asf/allura/blob/a113534b/ForgeTracker/forgetracker/tests/functional/test_root.py
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/tests/functional/test_root.py b/ForgeTracker/forgetracker/tests/functional/test_root.py
index 57d840c..8113d02 100644
--- a/ForgeTracker/forgetracker/tests/functional/test_root.py
+++ b/ForgeTracker/forgetracker/tests/functional/test_root.py
@@ -2393,10 +2393,14 @@ class TestFunctionalController(TrackerTestController):
assert_equal(_client.sendmail.call_count, 1)
return_path, rcpts, body = _client.sendmail.call_args[0]
body = body.split('\n')
+ # check subject
assert 'Subject: [test:bugs] #1 test <h2> ticket' in body
+ # check html, need tags escaped
assert_in('<p><strong> <a class="alink" href="http://localhost:8080/p/test/bugs/1/">[bugs:#1]</a>'
' test <h2> ticket</strong></p>',
body)
+ # check plaintext (ok to have "html" tags)
+ assert_in('** [bugs:#1] test <h2> ticket**', body)
@patch('forgetracker.search.query_filter_choices', autospec=True)
def test_multiselect(self, query_filter_choices):