zerver/lib/notifications.py: Fix string encoding/decoding.

Correctly encode and decode strings in convert_html_to_markdown.
It wasn't possible to use universal_newlines=True since
Popen.communicate doesn't encode/decode strings correctly on
python 2.
This commit is contained in:
Eklavya Sharma 2016-07-13 23:04:24 +05:30 committed by Tim Abbott
parent e6502710b6
commit 4f633bcd0b
2 changed files with 3 additions and 6 deletions

View File

@ -508,11 +508,11 @@ def convert_html_to_markdown(html):
except OSError: except OSError:
continue continue
markdown = p.communicate(input=html.encode("utf-8"))[0].strip() markdown = p.communicate(input=html.encode('utf-8'))[0].decode('utf-8').strip()
# We want images to get linked and inline previewed, but html2text will turn # We want images to get linked and inline previewed, but html2text will turn
# them into links of the form `![](http://foo.com/image.png)`, which is # them into links of the form `![](http://foo.com/image.png)`, which is
# ugly. Run a regex over the resulting description, turning links of the # ugly. Run a regex over the resulting description, turning links of the
# form `![](http://foo.com/image.png?12345)` into # form `![](http://foo.com/image.png?12345)` into
# `[image.png](http://foo.com/image.png)`. # `[image.png](http://foo.com/image.png)`.
return re.sub(r"!\[\]\((\S*)/(\S*)\?(\S*)\)", return re.sub(u"!\\[\\]\\((\\S*)/(\\S*)\\?(\\S*)\\)",
r"[\2](\1/\2)", markdown.decode('utf-8')) u"[\\2](\\1/\\2)", markdown)

View File

@ -763,7 +763,6 @@ class FreshdeskHookTests(WebhookTestCase):
STREAM_NAME = 'freshdesk' STREAM_NAME = 'freshdesk'
URL_TEMPLATE = u"/api/v1/external/freshdesk?stream={stream}" URL_TEMPLATE = u"/api/v1/external/freshdesk?stream={stream}"
@skip_py3
def test_ticket_creation(self): def test_ticket_creation(self):
# type: () -> None # type: () -> None
""" """
@ -830,7 +829,6 @@ Priority: **High** => **Low**"""
# type: () -> None # type: () -> None
self.note_change("public_note", "public") self.note_change("public_note", "public")
@skip_py3
def test_inline_image(self): def test_inline_image(self):
# type: () -> None # type: () -> None
""" """
@ -934,7 +932,6 @@ class PagerDutyHookTests(WebhookTestCase):
expected_message = u':grinning: Incident [48219](https://dropbox.pagerduty.com/incidents/PJKGZF9) resolved\n\n>mp_error_block_down_critical\u2119\u01b4' expected_message = u':grinning: Incident [48219](https://dropbox.pagerduty.com/incidents/PJKGZF9) resolved\n\n>mp_error_block_down_critical\u2119\u01b4'
self.send_and_test_stream_message('mp_fail', u"incident 48219", expected_message) self.send_and_test_stream_message('mp_fail', u"incident 48219", expected_message)
@skip_py3
def test_bad_message(self): def test_bad_message(self):
# type: () -> None # type: () -> None
expected_message = 'Unknown pagerduty message\n``` py\n{u\'type\': u\'incident.triggered\'}\n```' expected_message = 'Unknown pagerduty message\n``` py\n{u\'type\': u\'incident.triggered\'}\n```'