forked from dropbox/securitybot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_test.py
More file actions
329 lines (284 loc) · 12.4 KB
/
Copy pathbot_test.py
File metadata and controls
329 lines (284 loc) · 12.4 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from unittest2 import TestCase
from mock import Mock, patch
import yaml
import types
import os.path
import pytz
from datetime import datetime
import securitybot.bot as bot
import securitybot.commands as commands
import securitybot.user
import securitybot.chat.chat
MAIN_CONFIG = 'config/bot.yaml'
COMMAND_CONFIG = 'config/commands.yaml'
MESSAGE_CONFIG = 'config/messages.yaml'
@patch('securitybot.chat.chat.Chat', autospec=True)
def fake_init(self, tasker, auth_builder, reporting_channel, client):
self.username = 'testing-bot'
self.tasker = tasker
self.auth_builder = auth_builder
self.reporting_channel = reporting_channel
self._last_task_poll = datetime.min.replace(tzinfo=pytz.utc)
self._last_report = datetime.min.replace(tzinfo=pytz.utc)
self.chat = client
self.users = {}
self.users_by_name = {}
self.active_users = {}
self.commands = {}
self.messages = {}
bot.SecurityBot.__init__ = fake_init
class ConfigTest(TestCase):
# Validate configuration files
def test_config(self):
'''Tests bot.yaml.'''
with open(MAIN_CONFIG) as f:
config = yaml.safe_load(f)
for path in [s + '_path' for s in ['messages', 'commands']]:
assert path in config, 'No {0} provided'.format(path.replace('_', ' '))
# Test that files exist
assert os.path.isfile(config[path]), '{0} missing'.format(path.replace('_', ' '))
def test_commands(self):
'''Tests commands.yaml'''
with open(COMMAND_CONFIG) as f:
config = yaml.safe_load(f)
for name, items in config.items():
assert 'info' in items, 'No info provided for {0}'.format(name)
assert 'fn' in items, 'No function provided for {0}'.format(name)
assert isinstance(getattr(commands, items['fn'], None), types.FunctionType), \
'{0}: {1} is not a function'.format(name, items['fn'])
def test_messages(self):
'''Tests messages.yaml.'''
with open(MESSAGE_CONFIG) as f:
config = yaml.safe_load(f)
for name, string in config.items():
assert type(string) is str, 'All messages must be strings.'
class BotMessageTest(TestCase):
'''
Tests different kinds of message handling.
'''
def setUp(self):
self.bot = bot.SecurityBot(None, None, None)
self.bot.messages['bad_command'] = 'bad-command'
self.bot.users = {'id': {'id': 'id', 'name': 'name'}}
def test_handle_messages_command(self):
'''Test receiving a command.'''
self.bot.commands = {'test': None}
self.bot.handle_command = Mock()
self.bot.chat.get_messages.return_value = [{'type': 'message',
'user': 'id',
'channel': 'D12345',
'text': 'test command'}]
self.bot.handle_messages()
self.bot.handle_command.assert_called_with(self.bot.users['id'], 'test command')
def test_handle_messages_not_command(self):
'''Test receiving a message that isn't a command.'''
self.bot.commands = {'test': None}
self.bot.message_user = Mock()
self.bot.chat.get_messages.return_value = [{'type': 'message',
'user': 'id',
'channel': 'D12345',
'text': 'not a command'}]
self.bot.handle_messages()
self.bot.chat.message_user.assert_called_with(self.bot.users['id'], 'bad-command')
def test_handle_messages_not_dm(self):
'''Test receiving a message that's not from a DM channel.'''
self.bot.user_lookup = Mock()
self.bot.chat.get_messages.return_value = []
self.bot.handle_messages()
assert not self.bot.user_lookup.called, 'No user should have been looked up'
class BotCommandTest(TestCase):
'''
Tests handling a command.
'''
def test_command_success(self):
b = bot.SecurityBot(None, None, None)
mock_command = Mock()
mock_command.return_value = True
b.commands = {'test': {'fn': mock_command, 'success_msg': 'success_msg'}}
user = {'id': '123', 'name': 'test-user'}
b.handle_command(user, 'test command')
mock_command.assert_called_with(b, user, ['command'])
b.chat.message_user.assert_called_with(user, 'success_msg')
def test_command_failure(self):
b = bot.SecurityBot(None, None, None)
mock_command = Mock()
mock_command.return_value = False
b.commands = {'test': {'fn': mock_command, 'failure_msg': 'failure_msg'}}
user = {'id': '123', 'name': 'test-user'}
b.handle_command(user, 'test command')
mock_command.assert_called_with(b, user, ['command'])
b.chat.message_user.assert_called_with(user, 'failure_msg')
class BotTaskTest(TestCase):
'''
Tests handling of tasks.
'''
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.tasker.tasker.Tasker', autospec=True)
def setUp(self, tasker, patch_task):
self.bot = bot.SecurityBot(tasker, None, None)
self.bot.greet_user = Mock()
self.bot.blacklist = Mock()
self.bot.blacklist.is_present.return_value = False
self.patch_task = patch_task
self.task = patch_task.start()
self.task.title = 'title'
self.task.username = 'user'
self.task.comment = ''
tasker.get_new_tasks.return_value = [self.task]
tasker.get_pending_tasks.return_value = [self.task]
self.user = securitybot.user.User({'id': 'id', 'name': 'user'}, None, self.bot)
self.bot.users_by_name = {'user': self.user}
import securitybot.ignored_alerts as ignored_alerts
self.ignored_alerts = ignored_alerts
ignored_alerts.__update_ignored_list = Mock()
ignored_alerts.get_ignored = Mock(return_value={})
ignored_alerts.ignore_task = Mock()
def tearDown(self):
self.patch_task.stop()
def test_new_task(self):
'''
Tests receiving a new task that is neither for a blacklisted
user or an ignored task.
'''
self.bot.handle_new_tasks()
self.bot.greet_user.assert_called_with(self.user)
assert self.user['id'] in self.bot.active_users
def test_blacklisted_task(self):
'''Tests receiving a new task that is blacklisted.'''
self.bot.blacklist.is_present.return_value = True
self.bot.handle_new_tasks()
assert self.task.comment == 'blacklisted'
self.task.set_verifying.assert_called_with()
def test_ignored_task(self):
'''Tests receiving a new task that is ignored by the user.'''
self.ignored_alerts.get_ignored = Mock(return_value={'title': 'ignored'})
self.bot.handle_new_tasks()
assert self.task.comment == 'ignored'
self.task.set_verifying.assert_called_with()
def test_no_user_task(self):
'''Tests a task assigned to an unknown or invalid username.'''
self.task.username = 'another user'
self.bot.handle_new_tasks()
assert self.task.comment == 'invalid user'
self.task.set_verifying.assert_called_with()
class BotUserTest(TestCase):
def test_populate(self):
'''
Tests populating users.
'''
sb = bot.SecurityBot(None, lambda *args: None, None)
user = {'id': 'id', 'name': 'name'}
sb._api_call = Mock()
sb.chat.get_users.return_value = [user]
sb._populate_users()
sb.chat.get_users.assert_called_with()
assert user['id'] in sb.users
assert user['name'] in sb.users_by_name
@patch('securitybot.user.User', autospec=True)
def test_step(self, user):
'''
Tests stepping over all users on a user step.
'''
sb = bot.SecurityBot(None, None, None)
sb.active_users = {'key': user}
sb.handle_users()
user.step.assert_called_with()
class BotHelperTest(TestCase):
'''
Test cases for help functions in the bot that don't require
actually constructing a bot properly.
'''
# User handling tests
def test_user_lookup(self):
'''Tests user lookup on ID.'''
sb = bot.SecurityBot(None, None, None)
user = {'id': 'id', 'name': 'user'}
sb.users = {user['id']: user}
assert sb.user_lookup('id') == user
try:
sb.user_lookup('not-a-real-id')
except Exception:
return
assert False, 'A user should not have been found.'
def test_user_lookup_by_name(self):
'''Tests user lookup on ID.'''
sb = bot.SecurityBot(None, None, None)
user = {'id': 'id', 'name': 'user'}
sb.users_by_name = {user['name']: user}
assert sb.user_lookup_by_name('user') == user
try:
sb.user_lookup_by_name('not-a-real-user')
except Exception:
return
assert False, 'A user should not have been found.'
def test_valid_user_valid(self):
'''Tests valid_user with a valid user.'''
sb = bot.SecurityBot(None, None, None)
user = {'id': '1234', 'name': 'mock-user'}
sb.users = {user['id']: user}
sb.users_by_name = {user['name']: user}
assert sb.valid_user('mock-user')
def test_valid_user_invalid(self):
'''Tests valid_user with an invalid user.'''
sb = bot.SecurityBot(None, None, None)
user = {'id': '1234', 'name': 'mock-user'}
sb.users = {user['id']: user}
sb.users_by_name = {user['name']: user}
assert not sb.valid_user('fake-user')
def test_valid_user_malformed(self):
'''Tests valid_user with a malformed user.'''
sb = bot.SecurityBot(None, None, None)
user = {'id': '1234', 'name': 'mock-user'}
sb.users = {user['id']: user}
sb.users_by_name = {user['name']: user}
assert not sb.valid_user('mock-user\nfake-user')
def test_valid_user_empty(self):
'''Tests valid_user with a malformed user.'''
sb = bot.SecurityBot(None, None, None)
user = {'id': '1234', 'name': 'mock-user'}
sb.users = {user['id']: user}
sb.users_by_name = {user['name']: user}
assert not sb.valid_user('')
def test_cleanup_user(self):
'''
Test users being cleaned up properly.
... or at least the active_users dictionary having an element removed.
'''
# We'll mock a user using a dictionary since that's easier...
user = {'id': '1234', 'name': 'mock-user'}
fake_user = {'id': '5678', 'name': 'fake-user'}
sb = bot.SecurityBot(None, None, None)
sb.active_users = {user['id']: user}
assert len(sb.active_users) == 1
sb.cleanup_user(fake_user)
assert len(sb.active_users) == 1
sb.cleanup_user(user)
assert len(sb.active_users) == 0
# Command parsing tests
def test_parse_command(self):
'''Test parsing simple commands.'''
sb = bot.SecurityBot(None, None, None)
assert sb.parse_command('command text') == ('command', ['text'])
assert sb.parse_command('command unquoted text') == ('command', ['unquoted', 'text'])
assert sb.parse_command('command "quoted text"') == ('command', ['quoted text'])
def test_parse_punctuation(self):
'''Test parsing with punctuation in the command.'''
sb = bot.SecurityBot(None, None, None)
root = 'command'
# Minimal set of punctuation to always avoid
for char in bot.PUNCTUATION:
assert sb.parse_command(root + char) == (root, [])
def test_parse_nl_command(self):
'''Test parsing commands that contain text in natural language.'''
sb = bot.SecurityBot(None, None, None)
command = 'command With some language.'
assert sb.parse_command(command) == ('command', ['With', 'some', 'language.'])
command = 'command I\'m cool.'
assert sb.parse_command(command) == ('command', ['I\'m', 'cool.'])
def test_parse_unicode(self):
'''Tests parsing a command with unicode.'''
sb = bot.SecurityBot(None, None, None)
command = u'command \u2014flag'
assert sb.parse_command(command) == ('command', ['--flag'])
command = u'command \u201cquoted text\u201d'
assert sb.parse_command(command) == ('command', ['quoted text'])