diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2018-01-10 21:48:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-10 21:48:01 +0100 |
commit | fe7832f2453469fe4978b095561d6b7fce07cab8 (patch) | |
tree | 0cc185af3f381d31d40f0f0e741f5d33693bcf3c | |
parent | 81d1270e3a8d50e680dfd8bfb7c23890b846c05f (diff) | |
parent | 867d6134f9ef0d66deb5f5b4da63d5d5a538810a (diff) | |
download | wee-slack-fe7832f2453469fe4978b095561d6b7fce07cab8.tar.gz |
Merge pull request #438 from trygveaa/attachment-tests-and-minor-fixes
Attachment tests and minor fixes
-rw-r--r-- | _pytest/test_unwrap_attachments.py | 150 | ||||
-rw-r--r-- | wee_slack.py | 13 |
2 files changed, 157 insertions, 6 deletions
diff --git a/_pytest/test_unwrap_attachments.py b/_pytest/test_unwrap_attachments.py new file mode 100644 index 0000000..ee5bd8b --- /dev/null +++ b/_pytest/test_unwrap_attachments.py @@ -0,0 +1,150 @@ +import wee_slack +import pytest + + +@pytest.mark.parametrize('case', ( + { + 'input_message': {'attachments': [{ + 'title': 'Title', + }]}, + 'input_text_before': "Text before", + 'output': "\n".join([ + "", + "Title", + ]), + }, + { + 'input_message': {'attachments': [{ + 'title': 'Title', + 'text': 'Attachment text', + 'title_link': 'http://title.link', + 'from_url': 'http://from.url', + 'fallback': 'Fallback', + }]}, + 'input_text_before': "", + 'output': "\n".join([ + "Title (http://title.link)", + "http://from.url", + "Attachment text", + ]), + }, + { + 'input_message': {'attachments': [{ + 'title': 'Title', + 'text': 'Attachment text', + 'title_link': 'http://link?a=1&b=2', + 'from_url': 'http://link?a=1&b=2', + }]}, + 'input_text_before': "http://link?a=1&b=2", + 'output': "\n".join([ + "", + "Title", + "Attachment text", + ]), + }, + { + 'input_message': {'attachments': [{ + 'title': 'Title', + 'text': 'Attachment text', + 'title_link': 'http://link', + 'from_url': 'http://link', + }]}, + 'input_text_before': "", + 'output': "\n".join([ + "Title (http://link)", + "Attachment text", + ]), + }, + { + 'input_message': {'attachments': [{ + 'title': 'Title', + 'text': 'Attachment text\n\n\nWith multiple lines', + }]}, + 'input_text_before': "", + 'output': "\n".join([ + "Title", + "Attachment text\nWith multiple lines", + ]), + }, + { + 'input_message': {'attachments': [{ + 'title': 'Title', + 'author_name': 'Author', + 'pretext': 'Pretext', + 'text': 'Attachment text', + 'title_link': 'http://title.link', + 'from_url': 'http://from.url', + }]}, + 'input_text_before': "", + 'output': "\n".join([ + "Pretext", + "Author: Title (http://title.link)", + "http://from.url", + "Attachment text", + ]), + }, + { + 'input_message': {'attachments': [{ + 'author_name': 'Author', + 'text': 'Attachment text', + 'title_link': 'http://title.link', + 'from_url': 'http://from.url', + }]}, + 'input_text_before': "", + 'output': "\n".join([ + "http://from.url", + "Author: Attachment text", + ]), + }, + { + 'input_message': {'attachments': [{ + 'fallback': 'Fallback', + }]}, + 'input_text_before': "", + 'output': "Fallback", + }, + { + 'input_message': {'attachments': [{ + 'title': 'Title', + 'fields': [{ + 'title': 'First field title', + 'value': 'First field value', + }, { + 'title': '', + 'value': 'Second field value', + }], + }]}, + 'input_text_before': "", + 'output': "\n".join([ + "Title", + "First field title First field value", + "Second field value", + ]), + }, + { + 'input_message': {'attachments': [{ + 'title': 'First attachment title', + 'text': 'First attachment text', + 'title_link': 'http://title.link.1', + 'from_url': 'http://from.url.1', + }, { + 'title': 'Second attachment title', + 'text': 'Second attachment text', + 'title_link': 'http://title.link.2', + 'from_url': 'http://from.url.2', + }]}, + 'input_text_before': "", + 'output': "\n".join([ + "First attachment title (http://title.link.1)", + "http://from.url.1", + "First attachment text", + "Second attachment title (http://title.link.2)", + "http://from.url.2", + "Second attachment text", + ]), + }, +)) +def test_unwrap_attachments(case): + result = wee_slack.unwrap_attachments( + case['input_message'], case['input_text_before']) + assert result == case['output'] diff --git a/wee_slack.py b/wee_slack.py index 0948031..8854e18 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -2667,11 +2667,12 @@ def unhtmlescape(text): def unwrap_attachments(message_json, text_before): - attachment_text = '' + text_before_unescaped = unhtmlescape(text_before) + attachment_texts = [] a = message_json.get("attachments", None) if a: if text_before: - attachment_text = '\n' + attachment_texts.append('') for attachment in a: # Attachments should be rendered roughly like: # @@ -2687,7 +2688,7 @@ def unwrap_attachments(message_json, text_before): t.append(attachment['pretext']) title = attachment.get('title', None) title_link = attachment.get('title_link', '') - if title_link in text_before: + if title_link in text_before_unescaped: title_link = '' if title and title_link: t.append('%s%s (%s)' % (prepend_title_text, title, title_link,)) @@ -2696,7 +2697,7 @@ def unwrap_attachments(message_json, text_before): t.append('%s%s' % (prepend_title_text, title,)) prepend_title_text = '' from_url = attachment.get('from_url', '') - if from_url not in text_before: + if from_url not in text_before_unescaped and from_url != title_link: t.append(from_url) atext = attachment.get("text", None) @@ -2714,8 +2715,8 @@ def unwrap_attachments(message_json, text_before): fallback = attachment.get("fallback", None) if t == [] and fallback: t.append(fallback) - attachment_text += "\n".join([x.strip() for x in t if x]) - return attachment_text + attachment_texts.append("\n".join([x.strip() for x in t if x])) + return "\n".join(attachment_texts) def resolve_ref(ref): |