forked from scrapinghub/slackbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_functional.py
237 lines (173 loc) · 8.66 KB
/
test_functional.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#coding: UTF-8
"""
These functional tests would start a slackbot, and use the slack web api to
drive the tests against the bot.
"""
import os
from os.path import join, abspath, dirname, basename
import subprocess
import pytest
from tests.functional.driver import Driver
from tests.functional.slackbot_settings import (
testbot_apitoken, testbot_username,
driver_apitoken, driver_username, test_channel, test_private_channel
)
TRAVIS = 'TRAVIS' in os.environ
def stop_proxy():
os.system('slackbot-test-ctl stopproxy')
def start_proxy():
os.system('slackbot-test-ctl startproxy')
def _start_bot_process():
args = [
'python',
'tests/functional/run.py',
]
if TRAVIS:
args = ['slackbot-test-ctl', 'run'] + args
env = dict(os.environ)
env['SLACKBOT_API_TOKEN'] = testbot_apitoken
env['SLACKBOT_TEST'] = 'true'
env['PYTHONPATH'] = ':'.join(
[join(dirname(abspath(__file__))), '../..', env.get('PYTHONPATH', '')])
return subprocess.Popen(args, env=env)
@pytest.yield_fixture(scope='module') # pylint: disable=E1101
def driver():
driver = Driver(driver_apitoken,
driver_username,
testbot_username,
test_channel,
test_private_channel)
driver.start()
p = _start_bot_process()
driver.wait_for_bot_online()
yield driver
p.terminate()
@pytest.fixture(autouse=True) # pylint: disable=E1101
def clear_events(driver):
driver.clear_events()
def test_bot_get_online(driver): # pylint: disable=W0613
pass
def test_bot_respond_to_simple_message(driver):
driver.send_direct_message('hello')
driver.wait_for_bot_direct_message('hello sender!')
def test_bot_respond_to_simple_message_with_webapi(driver):
driver.send_direct_message('reply_webapi')
driver.wait_for_bot_direct_message('hello there!')
def test_bot_respond_to_simple_message_with_formatting(driver):
driver.send_direct_message('hello_formatting')
driver.wait_for_bot_direct_message('_hello_ sender!')
def test_bot_respond_to_simple_message_case_insensitive(driver):
driver.send_direct_message('hEllO')
driver.wait_for_bot_direct_message('hello sender!')
def test_bot_respond_to_simple_message_multiple_plugins(driver):
driver.send_direct_message('hello_formatting hello')
driver.wait_for_bot_direct_messages({'hello sender!', '_hello_ sender!'})
def test_bot_direct_message_with_at_prefix(driver):
driver.send_direct_message('hello', tobot=True)
driver.wait_for_bot_direct_message('hello sender!')
driver.send_direct_message('hello', tobot=True, colon=False)
driver.wait_for_bot_direct_message('hello sender!')
def test_bot_default_reply(driver):
driver.send_direct_message('youdontunderstandthiscommand do you')
driver.wait_for_bot_direct_message('.*You can ask me.*')
def test_bot_upload_file(driver):
driver.send_direct_message('upload slack.png')
driver.wait_for_bot_direct_message('uploading slack.png')
driver.wait_for_file_uploaded('slack.png')
def test_bot_upload_file_from_link(driver):
url = 'https://slack.com/favicon.ico'
fname = basename(url)
driver.send_direct_message('upload favicon')
driver.wait_for_bot_direct_message('uploading <%s>' % url)
driver.wait_for_file_uploaded(fname)
def test_bot_upload_file_from_content(driver):
driver.send_direct_message('send_string_content')
driver.wait_for_file_uploaded('content.txt')
def test_bot_reply_to_channel_message(driver):
driver.send_channel_message('hello')
driver.wait_for_bot_channel_message('hello sender!')
driver.send_channel_message('hello', colon=False)
driver.wait_for_bot_channel_message('hello sender!')
driver.send_channel_message('hello', space=False)
driver.wait_for_bot_channel_message('hello sender!')
# This is hard for a user to do, but why not test it?
driver.send_channel_message('hello', colon=False, space=False)
driver.wait_for_bot_channel_message('hello sender!')
def test_bot_channel_reply_to_name_colon(driver):
driver.send_channel_message('hello', tobot=False, toname=True)
driver.wait_for_bot_channel_message('hello sender!')
driver.send_channel_message('hello', tobot=False, toname=True, space=False)
driver.wait_for_bot_channel_message('hello sender!')
driver.send_channel_message('hello', tobot=False, toname=True, colon=False)
driver.wait_for_bot_channel_message('hello channel!', tosender=False)
driver.send_channel_message('hello', tobot=False, toname=True, colon=False,
space=False)
driver.wait_for_bot_channel_message('hello channel!', tosender=False)
def test_bot_private_channel_reply_to_name_colon(driver):
driver.send_private_channel_message('hello', tobot=False, toname=True)
driver.wait_for_bot_private_channel_message('hello sender!')
driver.send_private_channel_message('hello', tobot=False, toname=True, space=False)
driver.wait_for_bot_private_channel_message('hello sender!')
driver.send_private_channel_message('hello', tobot=False, toname=True, colon=False)
driver.wait_for_bot_private_channel_message('hello channel!', tosender=False)
driver.send_private_channel_message('hello', tobot=False, toname=True, colon=False,
space=False)
driver.wait_for_bot_private_channel_message('hello channel!', tosender=False)
def test_bot_listen_to_channel_message(driver):
driver.send_channel_message('hello', tobot=False)
driver.wait_for_bot_channel_message('hello channel!', tosender=False)
def test_bot_react_to_channel_message(driver):
driver.send_channel_message('hey!', tobot=False)
driver.ensure_reaction_posted('eggplant')
def test_bot_reply_to_private_channel_message(driver):
driver.send_private_channel_message('hello')
driver.wait_for_bot_private_channel_message('hello sender!')
driver.send_private_channel_message('hello', colon=False)
driver.wait_for_bot_private_channel_message('hello sender!')
def test_bot_ignores_non_related_message_response_tosender(driver):
driver.send_channel_message('hello', tobot=True)
driver.ensure_only_specificmessage_from_bot('hello sender!', tosender=True)
def test_bot_ignores_non_related_message_response_tochannel(driver):
driver.send_channel_message('hello', tobot=False)
driver.ensure_only_specificmessage_from_bot('hello channel!', tosender=False)
def test_bot_ignores_unknown_message_noresponse_tochannel(driver):
driver.send_channel_message('unknown message', tobot=False)
driver.ensure_no_channel_reply_from_bot()
def test_bot_send_usage_unknown_message_response_tosender(driver):
driver.send_channel_message('unknown message', tobot=True)
driver.ensure_only_specificmessage_from_bot('Bad command "unknown message".+', tosender=True)
def test_bot_reply_to_message_multiple_decorators(driver):
driver.send_channel_message('hello_decorators')
driver.wait_for_bot_channel_message('hello!', tosender=False)
driver.send_channel_message('hello_decorators', tobot=False)
driver.wait_for_bot_channel_message('hello!', tosender=False)
driver.send_direct_message('hello_decorators')
driver.wait_for_bot_direct_message('hello!')
@pytest.mark.skipif(not TRAVIS, reason="only run reconnect tests on travis builds")
def test_bot_reconnect(driver):
driver.wait_for_bot_online()
stop_proxy()
driver.wait_for_bot_offline()
start_proxy()
driver.wait_for_bot_online()
test_bot_respond_to_simple_message(driver)
def test_bot_reply_with_unicode_message(driver):
driver.send_direct_message(u'你好')
driver.wait_for_bot_direct_message(u'你好')
driver.send_direct_message(u'你不明白,对吗?')
driver.wait_for_bot_direct_message('.*You can ask me.*')
driver.send_channel_message(u'你好')
driver.wait_for_bot_channel_message(u'你好!')
driver.send_channel_message(u'你不明白,对吗?')
driver.wait_for_bot_channel_message(u'.*You can ask me.*')
def test_bot_reply_with_alias_message(driver):
driver.send_channel_message("! hello", tobot=False, colon=False)
driver.wait_for_bot_channel_message("hello sender!", tosender=True)
driver.send_channel_message('!hello', tobot=False, colon=False)
driver.wait_for_bot_channel_message("hello sender!", tosender=True)
def test_bot_reply_thread_in_channel(driver):
driver.send_channel_message('start a thread', tobot=False, colon=False)
driver.wait_for_bot_channel_thread_message('I started a thread', tosender=False)
def test_bot_reply_thread_in_private_channel(driver):
driver.send_private_channel_message('start a thread', tobot=False, colon=False)
driver.wait_for_bot_private_channel_thread_message('I started a thread', tosender=False)