aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTollef Fog Heen <tfheen@err.no>2015-08-29 16:55:26 +0200
committerTollef Fog Heen <tfheen@err.no>2015-08-29 16:55:26 +0200
commit784ba9da7eef89c429b97b5a59a884b12563163c (patch)
treefff23c135824396622aca73090b2aa6c33b31ee3
parent91de1da5254690e5b3c4d4b2f952d0351e74cd76 (diff)
downloadwee-slack-784ba9da7eef89c429b97b5a59a884b12563163c.tar.gz
Add unit test for unfurl_refs and correct logic
The logic for showing/ignoring alt refs was wrong. Fix that, and to prevent this from showing up again, add a unit test.
-rw-r--r--_pytest/test_unfurl.py38
-rw-r--r--wee_slack.py43
2 files changed, 66 insertions, 15 deletions
diff --git a/_pytest/test_unfurl.py b/_pytest/test_unfurl.py
new file mode 100644
index 0000000..e731afd
--- /dev/null
+++ b/_pytest/test_unfurl.py
@@ -0,0 +1,38 @@
+import wee_slack
+import pytest
+import json
+
+slack = wee_slack
+
+unfurl_map = [
+ { "input": "foo",
+ "output": "foo",
+ },
+ { "input": "<@U2147483697|@othernick>: foo",
+ "output": "@testuser: foo",
+ "ignore_alt_text": True
+ },
+ { "input": "foo <#C2147483705|#otherchannel> foo",
+ "output": "foo #otherchannel foo",
+ },
+ { "input": "url: <https://example.com|example> suffix",
+ "output": "url: https://example.com (example) suffix",
+ },
+ ]
+
+
+def test_unfurl_refs(myservers, mychannels, myusers):
+ slack.servers = myservers
+ slack.channels = mychannels
+ slack.users = myusers
+ slack.message_cache = {}
+ slack.servers[0].users = myusers
+ print mychannels[0].identifier
+
+ for k in unfurl_map:
+ if "ignore_alt_text" in k:
+ assert slack.unfurl_refs(k["input"], ignore_alt_text=k["ignore_alt_text"]) == k["output"]
+ else:
+ assert slack.unfurl_refs(k["input"]) == k["output"]
+
+
diff --git a/wee_slack.py b/wee_slack.py
index eae2e1d..2eccd58 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -1472,32 +1472,45 @@ def unfurl_refs(text, ignore_alt_text=False):
"""
if text and text.find('<') > -1:
newtext = []
- text = text.split(" ")
- for item in text:
- # dbg(item)
+ chunks = text.split(" ")
+ for item in chunks:
+ dbg(item)
prefix = ""
suffix = ""
start = item.find('<')
end = item.find('>')
+ display_text = item
+
if start > -1 and end > -1:
prefix = item[:start]
suffix = item[end+1:]
item = item[start + 1:end]
+ id = item.split('|')[0]
if item.find('|') > -1:
if ignore_alt_text:
- item = item.split('|')[1]
+ if id.startswith('@U'):
+ if users.find(id[1:]):
+ try:
+ display_text = "@{}".format(users.find(id[1:]).name)
+ except:
+ dbg("NAME: {}".format(item))
+ elif id.startswith('#C'):
+ if channels.find(id[1:]):
+ try:
+ display_text = "{}".format(channels.find(id[1:]).name)
+ except:
+ dbg("CHANNEL: {}".format(item))
+ else:
+ # This is probably a URL, we don't want to ignore anything
+ # XXX: fix up nicer formatting to generate more clickable urls
+ display_text = item
else:
- item = item.split('|')[0]
- if item.startswith('@U'):
- if users.find(item[1:]):
- try:
- item = "@{}".format(users.find(item[1:]).name)
- except:
- dbg("NAME: {}".format(item))
- if item.startswith('#C'):
- if channels.find(item[1:]):
- item = "{}".format(channels.find(item[1:]).name)
- newtext.append(prefix + item + suffix)
+ if id.startswith("#C") or id.startswith("@U"):
+ display_text = item.split('|')[1]
+ else:
+ url, desc = item.split('|', 1)
+ display_text = "{} ({})".format(url, desc)
+ newtext.append(prefix + display_text + suffix)
text = " ".join(newtext)
return text
else: