diff options
author | Ryan Huber <rhuber@gmail.com> | 2017-02-01 11:58:13 -0800 |
---|---|---|
committer | Ryan Huber <rhuber@gmail.com> | 2017-02-01 11:58:13 -0800 |
commit | 681dbc193bae47429662a9504d75768ab1fdda4c (patch) | |
tree | dda3b3f9b180e93d5365fe8c7d4ea6b07ae2e8a2 | |
parent | e63a037a323221e24ba0f422d8d81ee77f0b3ce0 (diff) | |
download | wee-slack-681dbc193bae47429662a9504d75768ab1fdda4c.tar.gz |
add first basic tests
-rw-r--r-- | _pytest/conftest.py | 84 | ||||
-rw-r--r-- | _pytest/test_eventrouter.py | 76 | ||||
-rw-r--r-- | _pytest/test_linkifytext.py | 6 | ||||
-rw-r--r-- | _pytest/test_presencechange.py | 31 | ||||
-rw-r--r-- | _pytest/test_process_message.py | 44 | ||||
-rw-r--r-- | _pytest/test_processreply.py | 33 | ||||
-rw-r--r-- | _pytest/test_sendmessage.py | 21 | ||||
-rw-r--r-- | _pytest/test_slackrequest.py | 11 | ||||
-rw-r--r-- | _pytest/test_slackteam.py | 10 | ||||
-rw-r--r-- | _pytest/test_unfurl.py | 53 | ||||
-rw-r--r-- | wee_slack.py | 2 |
11 files changed, 370 insertions, 1 deletions
diff --git a/_pytest/conftest.py b/_pytest/conftest.py new file mode 100644 index 0000000..0212601 --- /dev/null +++ b/_pytest/conftest.py @@ -0,0 +1,84 @@ +import pickle +import json +import pytest +import sys + +sys.path.append(".") + +#New stuff +from wee_slack import EventRouter +from wee_slack import SlackRequest +import wee_slack + +class fakewebsocket(object): + def __init__(self): + self.returndata = [] + pass + def add(self, data): + self.returndata.append(data) + def recv(self): + return json.dumps(self.returndata.pop(0)) + def send(self, data): + print "websocket received: {}".format(data) + return + +@pytest.fixture +def mock_websocket(): + return fakewebsocket() + +@pytest.fixture +def realish_eventrouter(): + e = EventRouter() + pickled_data = pickle.dumps(SlackRequest('xoxoxoxox', "rtm.start", {"meh": "blah"})) + rtmstartdata = open('_pytest/data/http/rtm.start.json', 'r').read() + e.receive_httprequest_callback(pickled_data, 1, 0, rtmstartdata, 4) + e.handle_next() + return e + + +class FakeWeechat(): + """ + this is the thing that acts as "w." everywhere.. + basically mock out all of the weechat calls here i guess + """ + WEECHAT_RC_OK = True + + def __init__(self): + pass + #print "INITIALIZE FAKE WEECHAT" + def prnt(*args): + output = "(" + for arg in args: + if arg != None: + output += "{}, ".format(arg) + print "w.prnt {}".format(output) + def hdata_get(*args): + return "0x000001" + def hdata_pointer(*args): + return "0x000002" + def hdata_time(*args): + return "1355517519" + def hdata_string(*args): + return "testuser" + def buffer_new(*args): + return "0x8a8a8a8b" + + def __getattr__(self, name): + def method(*args): + pass + #print "called {}".format(name) + #if args: + # print "\twith args: {}".format(args) + return method + +@pytest.fixture +def mock_weechat(): + wee_slack.w = FakeWeechat() + wee_slack.config = wee_slack.PluginConfig() + wee_slack.debug_string = None + wee_slack.slack_debug = "debug_buffer_ptr" + wee_slack.STOP_TALKING_TO_SLACK = False + wee_slack.proc = {} + pass + + diff --git a/_pytest/test_eventrouter.py b/_pytest/test_eventrouter.py new file mode 100644 index 0000000..fc6a6a9 --- /dev/null +++ b/_pytest/test_eventrouter.py @@ -0,0 +1,76 @@ +import pytest +import pickle +from wee_slack import EventRouter, ProcessNotImplemented, SlackRequest + +def test_EventRouter(mock_weechat): + # Sending valid json adds to the queue. + e = EventRouter() + e.receive_json('{}') + assert len(e.queue) == 1 + + # Handling an event removes from the queue. + e = EventRouter() + # Create a function to test we are called + e.proc['testfunc'] = lambda x, y: x + e.receive_json('{"type": "testfunc"}') + e.handle_next() + assert len(e.queue) == 0 + + # Handling a local event removes from the queue. + e = EventRouter() + # Create a function to test we are called + e.proc['local_testfunc'] = lambda x, y: x + e.receive_json('{"type": "local_testfunc"}') + e.handle_next() + assert len(e.queue) == 0 + + # Handling an event without an associated processor + # raises an exception. + e = EventRouter() + # Create a function to test we are called + e.receive_json('{"type": "testfunc"}') + with pytest.raises(ProcessNotImplemented): + e.handle_next() + +def test_EventRouterReceivedata(mock_weechat): + + e = EventRouter() + pickled_data = pickle.dumps(SlackRequest('xoxoxoxox', "rtm.startold", {"meh": "blah"})) + e.receive_httprequest_callback(pickled_data, 1, -1, ' {"JSON": "MEH", ', 4) + #print len(e.reply_buffer) + e.receive_httprequest_callback(pickled_data, 1, -1, ' "JSON2": "MEH", ', 4) + #print len(e.reply_buffer) + e.receive_httprequest_callback(pickled_data, 1, 0, ' "JSON3": "MEH"}', 4) + #print len(e.reply_buffer) + try: + e.handle_next() + except: + pass + + pickled_data = pickle.dumps(SlackRequest('xoxoxoxox', "rtm.start", {"meh": "blah"})) + rtmstartdata = open('_pytest/data/http/rtm.start.json', 'r').read() + e.receive_httprequest_callback(pickled_data, 1, 0, rtmstartdata[:5000], 4) + e.receive_httprequest_callback(pickled_data, 1, 0, rtmstartdata[5000:], 4) + e.handle_next() + + #print len(e.reply_buffer) + + #print e.teams + + for t in e.teams: + #print vars(e.teams[t]) + for c in e.teams[t].channels: + pass + #print c + for u in e.teams[t].users: + pass + #print vars(u) + + +# e = EventRouter() +# # Create a function to test we are called +# e.receive_json('{"type": "message"}') +# e.handle_next() +# assert False + + #assert False diff --git a/_pytest/test_linkifytext.py b/_pytest/test_linkifytext.py new file mode 100644 index 0000000..f9da3f9 --- /dev/null +++ b/_pytest/test_linkifytext.py @@ -0,0 +1,6 @@ +from wee_slack import linkify_text + +#def test_linkifytext(): +# linkify_text('@ryan') + +# assert False diff --git a/_pytest/test_presencechange.py b/_pytest/test_presencechange.py new file mode 100644 index 0000000..b4202fa --- /dev/null +++ b/_pytest/test_presencechange.py @@ -0,0 +1,31 @@ + +def test_PresenceChange(monkeypatch, realish_eventrouter, mock_websocket): + + e = realish_eventrouter + + t = e.teams.keys()[0] + u = e.teams[t].users.keys()[0] + + user = e.teams[t].users[u] + + socket = mock_websocket + e.teams[t].ws = socket + + socket.add({ + "type": "presence_change", + "user": user.identifier, + "presence": "active", + }) + socket.add({ + "type": "presence_change", + "user": user.identifier, + "presence": "away", + }) + + e.receive_ws_callback(t) + e.handle_next() + assert e.teams[t].users[u].presence == "active" + + e.receive_ws_callback(t) + e.handle_next() + assert e.teams[t].users[u].presence == "away" diff --git a/_pytest/test_process_message.py b/_pytest/test_process_message.py new file mode 100644 index 0000000..9d238d6 --- /dev/null +++ b/_pytest/test_process_message.py @@ -0,0 +1,44 @@ +import json + +from wee_slack import render + +def test_process_message(monkeypatch, realish_eventrouter, mock_websocket): + + e = realish_eventrouter + + t = e.teams.keys()[0] + u = e.teams[t].users.keys()[0] + + user = e.teams[t].users[u] + #print user + + socket = mock_websocket + e.teams[t].ws = socket + + messages = [] + messages.append(json.loads(open('_pytest/data/websocket/1485975421.33-message.json', 'r').read())) + + # test message and then change + messages.append(json.loads(open('_pytest/data/websocket/1485976157.18-message.json', 'r').read())) + messages.append(json.loads(open('_pytest/data/websocket/1485976151.6-message.json', 'r').read())) + + # test message then deletion + messages.append(json.loads(open('_pytest/data/websocket/1485975698.45-message.json', 'r').read())) + messages.append(json.loads(open('_pytest/data/websocket/1485975723.85-message.json', 'r').read())) + + for m in messages: + m["user"] = user.id + socket.add(m) + + e.receive_ws_callback(t) + e.handle_next() + + e.receive_ws_callback(t) + e.handle_next() + + e.receive_ws_callback(t) + e.handle_next() + + e.receive_ws_callback(t) + e.handle_next() + diff --git a/_pytest/test_processreply.py b/_pytest/test_processreply.py new file mode 100644 index 0000000..a725f23 --- /dev/null +++ b/_pytest/test_processreply.py @@ -0,0 +1,33 @@ +#from wee_slack import process_reply + +def test_process_reply(monkeypatch, realish_eventrouter, mock_websocket): + + e = realish_eventrouter + + t = e.teams.keys()[0] + #u = e.teams[t].users.keys()[0] + + #user = e.teams[t].users[u] + #print user + + socket = mock_websocket + e.teams[t].ws = socket + + c = e.teams[t].channels.keys()[0] + channel = e.teams[t].channels[c] + channel.send_message('asdf') + + socket = mock_websocket + socket.add({"reply_to": 1, "_team": t, "ts": "12341234.111"}) + + print e.teams[t].ws_replies + + e.receive_ws_callback(t) + e.handle_next() + + #reply = {"reply_to": 1, "_team": t, "ts": "12341234.111"} + #print reply + #process_reply(reply, e) + #print e.teams[t].ws_replies + #assert False + pass diff --git a/_pytest/test_sendmessage.py b/_pytest/test_sendmessage.py new file mode 100644 index 0000000..a87942d --- /dev/null +++ b/_pytest/test_sendmessage.py @@ -0,0 +1,21 @@ + +def test_send_message(monkeypatch, realish_eventrouter, mock_websocket): + e = realish_eventrouter + + t = e.teams.keys()[0] + #u = e.teams[t].users.keys()[0] + + #user = e.teams[t].users[u] + #print user + + socket = mock_websocket + e.teams[t].ws = socket + + c = e.teams[t].channels.keys()[0] + + channel = e.teams[t].channels[c] + channel.send_message('asdf') + + print c + + #assert False diff --git a/_pytest/test_slackrequest.py b/_pytest/test_slackrequest.py new file mode 100644 index 0000000..081330e --- /dev/null +++ b/_pytest/test_slackrequest.py @@ -0,0 +1,11 @@ +from wee_slack import SlackRequest, EventRouter + +def test_SlackRequest(): + s = SlackRequest('xoxoxoxox', "blah.get", {"meh": "blah"}) + print s + + e = EventRouter() + e.receive(s) + e.handle_next() + #assert False + diff --git a/_pytest/test_slackteam.py b/_pytest/test_slackteam.py new file mode 100644 index 0000000..fa6ac6a --- /dev/null +++ b/_pytest/test_slackteam.py @@ -0,0 +1,10 @@ +from wee_slack import SlackTeam, EventRouter + +def test_SlackTeam(): + e = EventRouter() + #s = SlackTeam('xoxo') + #e.register_team(s) + #print e.queue + #print e.handle_next() + #assert False + diff --git a/_pytest/test_unfurl.py b/_pytest/test_unfurl.py new file mode 100644 index 0000000..b631888 --- /dev/null +++ b/_pytest/test_unfurl.py @@ -0,0 +1,53 @@ +import wee_slack +import pytest + +slack = wee_slack + + +@pytest.mark.parametrize('case', ( + { + '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': "foo <#C2147483705> foo", + 'output': "foo #test-chan foo", + }, + { + 'input': "url: <https://example.com|example> suffix", + 'output': "url: https://example.com (example) suffix", + }, + { + 'input': "url: <https://example.com|example with spaces> suffix", + 'output': "url: https://example.com (example with spaces) suffix", + }, + { + 'input': "<@U2147483697|@othernick> multiple unfurl <https://example.com|example with spaces>", + 'output': "@othernick multiple unfurl https://example.com (example with spaces)", + }, + { + 'input': "try the #test-chan channel", + 'output': "try the #test-chan channel", + }, +)) +def test_unfurl_refs(case): + pass + #print myslack + #slack.servers = myslack.server + #slack.channels = myslack.channel + #slack.users = myslack.user + #slack.message_cache = {} + #slack.servers[0].users = myslack.user + #print myslack.channel[0].identifier + + #assert slack.unfurl_refs(case['input'], ignore_alt_text=case.get('ignore_alt_text', False)) == case['output'] + diff --git a/wee_slack.py b/wee_slack.py index 51c12b6..2d059a1 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -591,7 +591,7 @@ class SlackChannel(object): m.change_text(text) if suffix: m.change_suffix(suffix) - text = m.render(force=True) + text = m.render(force=True) timestamp, time_id = ts.split(".", 2) timestamp = int(timestamp) modify_buffer_line(self.channel_buffer, text, timestamp, time_id) |