Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 1a932f9

Browse files
committed
Merge pull request #46 from jammons/master
Add tests and bugfix for b'' messages appearing in python3.
2 parents 006e28f + c81bdfe commit 1a932f9

File tree

10 files changed

+80
-12
lines changed

10 files changed

+80
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ This will print the incoming message json (dict) to the screen where the bot is
6767

6868
Plugins having a method defined as ```catch_all(data)``` will receive ALL events from the websocket. This is useful for learning the names of events and debugging.
6969

70+
Note: If you're using Python 2.x, the incoming data should be a unicode string, be careful you don't coerce it into a normal str object as it will cause errors on output. You can add `from __future__ import unicode_literals` to your plugin file to avoid this.
71+
7072
####Outgoing data
7173
Plugins can send messages back to any channel, including direct messages. This is done by appending a two item array to the outputs global array. The first item in the array is the channel ID and the second is the message text. Example that writes "hello world" when the plugin is started:
7274

doc/example-plugins/canary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import unicode_literals
2+
# don't convert to ascii in py2.7 when creating string to return
3+
14
import time
25
outputs = []
36

doc/example-plugins/counter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import unicode_literals
2+
# don't convert to ascii in py2.7 when creating string to return
3+
14
import time
25
crontable = []
36
outputs = []

doc/example-plugins/repeat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import unicode_literals
2+
# don't convert to ascii in py2.7 when creating string to return
3+
14
crontable = []
25
outputs = []
36

doc/example-plugins/todo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from __future__ import print_function
2+
from __future__ import unicode_literals
3+
# don't convert to ascii in py2.7 when creating string to return
4+
25
import os
36
import pickle
47

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
coveralls==1.1
22
ipdb==0.9.3
33
ipython==4.1.2
4+
mock==2.0.0
45
pdbpp==0.8.3
56
pytest>=2.8.2
67
pytest-cov==2.2.1

rtmbot/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
from __future__ import unicode_literals
23
import sys
34
import glob
45
import os
@@ -101,8 +102,7 @@ def output(self):
101102
if limiter:
102103
time.sleep(.1)
103104
limiter = False
104-
message = output[1].encode('ascii', 'ignore')
105-
channel.send_message("{}".format(message))
105+
channel.send_message(output[1])
106106
limiter = True
107107

108108
def crons(self):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='rtmbot',
7-
version='0.10',
7+
version='0.2.0',
88
description='A Slack bot written in python that connects via the RTM API.',
99
author='Ryan Huber',
1010
author_email='[email protected]',

tests/test_example.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/test_rtmbot_core.py

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
try:
3+
from unittest.mock import Mock, create_autospec
4+
except ImportError:
5+
from mock import Mock, create_autospec
6+
17
from testfixtures import LogCapture
2-
from rtmbot.core import RtmBot
8+
from slackclient import SlackClient, _channel, _server, _util
9+
from rtmbot.core import RtmBot, Plugin
310

11+
def init_rtmbot():
12+
''' Initializes an instance of RTMBot with some default values '''
13+
rtmbot = RtmBot({
14+
'SLACK_TOKEN': 'test-12345',
15+
'BASE_PATH': '/tmp/',
16+
'LOGFILE': '/tmp/rtmbot.log',
17+
'DEBUG': True
18+
})
19+
return rtmbot
420

521
def test_init():
622
with LogCapture() as l:
7-
rtmbot = RtmBot({
8-
'SLACK_TOKEN': 'test-12345',
9-
'BASE_PATH': '/tmp/',
10-
'LOGFILE': '/tmp/rtmbot.log',
11-
'DEBUG': True
12-
})
23+
rtmbot = init_rtmbot()
1324

1425
assert rtmbot.token == 'test-12345'
1526
assert rtmbot.directory == '/tmp/'
@@ -18,3 +29,47 @@ def test_init():
1829
l.check(
1930
('root', 'INFO', 'Initialized in: /tmp/')
2031
)
32+
33+
def test_output():
34+
''' Test that sending a message behaves as expected '''
35+
rtmbot = init_rtmbot()
36+
37+
# Mock the slack_client object with Server, Channel objects and needed methods
38+
slackclient_mock = create_autospec(SlackClient)
39+
server_mock = create_autospec(_server.Server)
40+
41+
# Mock Server with channels method and correct return value
42+
slackclient_mock.server = server_mock
43+
searchlist_mock = create_autospec(_util.SearchList)
44+
server_mock.channels = searchlist_mock
45+
channel_mock = create_autospec(_channel.Channel)
46+
slackclient_mock.server.channels.find.return_value = channel_mock
47+
48+
rtmbot.slack_client = slackclient_mock
49+
50+
# mock the plugin object to return a sample response
51+
plugin_mock = create_autospec(Plugin)
52+
plugin_mock.do_output.return_value = [['C12345678', 'test message']]
53+
rtmbot.bot_plugins.append(plugin_mock)
54+
55+
rtmbot.output()
56+
57+
58+
# test that the output matches the expected value
59+
channel_mock.send_message.assert_called_with('test message')
60+
61+
# test that emoji messages work as expected
62+
channel_mock.reset_mock()
63+
plugin_mock.reset_mock()
64+
plugin_mock.do_output.return_value = [['C12345678', '🚀 testing']]
65+
rtmbot.output()
66+
67+
channel_mock.send_message.assert_called_with('🚀 testing')
68+
69+
# test that unicode messages work as expected
70+
channel_mock.reset_mock()
71+
plugin_mock.reset_mock()
72+
plugin_mock.do_output.return_value = [['C12345678', 'ù hœø3ö']]
73+
rtmbot.output()
74+
75+
channel_mock.send_message.assert_called_with('ù hœø3ö')

0 commit comments

Comments
 (0)