forked from dropbox/securitybot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_test.py
More file actions
375 lines (295 loc) · 13.4 KB
/
Copy pathuser_test.py
File metadata and controls
375 lines (295 loc) · 13.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from unittest2 import TestCase
from mock import Mock, patch
from collections import defaultdict
from datetime import timedelta
import securitybot.user as user
import securitybot.bot
import securitybot.chat.chat
import securitybot.auth.auth
# Mock away ignoring alerts
import securitybot.ignored_alerts as ignored_alerts
ignored_alerts.__update_ignored_list = Mock()
ignored_alerts.get_ignored = Mock(return_value={})
ignored_alerts.get_ignored.return_value = {}
ignored_alerts.ignore_task = Mock()
class UserTest(TestCase):
@patch('securitybot.chat.chat.Chat', autospec=True)
@patch('securitybot.bot.SecurityBot', autospec=True)
def setUp(self, bot, chat):
bot.chat = chat
self.bot = bot
def test_construction(self):
'''Tests basic construction of a user.'''
user.User({}, None, None)
def test_get_attributes(self):
'''Tests grabbing attributes like a dictionary.'''
test_user = user.User({'alphabet': 'soup',
'animal': 'crackers'},
None, None)
assert test_user['alphabet'] == 'soup'
assert test_user['animal'] == 'crackers'
def test_name(self):
'''Tests getting a user's name.'''
test_user = user.User({'profile': {'first_name': 'Bot'}}, None, None)
assert test_user.get_name() == 'Bot'
test_user = user.User({'profile': {}, 'name': 'Bot2'}, None, None)
assert test_user.get_name() == 'Bot2'
# User interaction flows
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_basic_flow(self, auth, mock_task):
'''
Tests basic flow through the bot.
This is the most basic flow:
new task => did perform => allow 2FA => valid 2FA => no task
This will ensure that the states progress as expected and the bot
cleans itself up afterwards.
'''
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.NONE
auth.can_auth.return_value = True
self.bot.messages = defaultdict(str)
test_user = user.User({}, auth, self.bot)
task = mock_task.start()
assert str(test_user._fsm.state) == 'need_task'
# Also test not advancing on no queued task
test_user.step()
assert str(test_user._fsm.state) == 'need_task'
test_user.add_task(task)
test_user.step()
assert str(test_user._fsm.state) == 'action_performed_check'
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'auth_permission_check'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'waiting_on_auth'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.AUTHORIZED
test_user.step()
assert str(test_user._fsm.state) == 'task_finished'
test_user.step()
self.bot.cleanup_user.assert_called_with(test_user)
assert str(test_user._fsm.state) == 'need_task'
task.set_verifying.assert_called_with()
mock_task.stop()
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_did_not_do_flow(self, auth, mock_task):
'''
Tests flow if a user did not perform an action.
'''
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.NONE
auth.can_auth.return_value = True
self.bot.messages = defaultdict(str)
self.bot.reporting_channel = None
test_user = user.User({}, auth, self.bot)
task = mock_task.start()
assert str(test_user._fsm.state) == 'need_task'
# Also test not advancing on no queued task
test_user.step()
assert str(test_user._fsm.state) == 'need_task'
test_user.add_task(task)
test_user.step()
assert str(test_user._fsm.state) == 'action_performed_check'
test_user.negative_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'task_finished'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
mock_task.stop()
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_two_task_flow(self, auth, mock_task):
'''
Tests two task. Once the first is completed, the bot should send a
a message announcing that another task exists.
'''
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.NONE
auth.can_auth.return_value = True
self.bot.messages = defaultdict(str)
test_user = user.User({}, auth, self.bot)
test_user.send_message = Mock()
task = mock_task.start()
assert str(test_user._fsm.state) == 'need_task'
# Add two tasks to the queue
test_user.add_task(task)
test_user.add_task(task)
test_user.step()
assert str(test_user._fsm.state) == 'action_performed_check'
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'auth_permission_check'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'waiting_on_auth'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.AUTHORIZED
test_user.step()
assert str(test_user._fsm.state) == 'task_finished'
test_user.step()
test_user.send_message.assert_called_with('bwtm')
assert str(test_user._fsm.state) == 'need_task'
task.set_verifying.assert_called_with()
mock_task.stop()
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_already_authorized_flow(self, auth, mock_task):
'''
Tests already being authorized after confirming an alert.
This is the most basic flow:
new task => did perform => already authorized => no task
'''
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.AUTHORIZED
auth.can_auth.return_value = True
self.bot.messages = defaultdict(str)
test_user = user.User({}, auth, self.bot)
task = mock_task.start()
assert str(test_user._fsm.state) == 'need_task'
test_user.add_task(task)
test_user.step()
assert str(test_user._fsm.state) == 'action_performed_check'
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'task_finished'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
test_user.step()
self.bot.cleanup_user.assert_called_with(test_user)
assert str(test_user._fsm.state) == 'need_task'
task.set_verifying.assert_called_with()
mock_task.stop()
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_no_2fa(self, auth, mock_task):
'''
Tests a user not having 2FA capability.
This is the most basic flow:
new task => did perform => allow 2FA => valid 2FA => no task
'''
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.NONE
auth.can_auth.return_value = False
self.bot.messages = defaultdict(str)
test_user = user.User({}, auth, self.bot)
task = mock_task.start()
assert str(test_user._fsm.state) == 'need_task'
test_user.step()
assert str(test_user._fsm.state) == 'need_task'
test_user.add_task(task)
test_user.step()
assert str(test_user._fsm.state) == 'action_performed_check'
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'task_finished'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
test_user.step()
self.bot.cleanup_user.assert_called_with(test_user)
assert str(test_user._fsm.state) == 'need_task'
task.set_verifying.assert_called_with()
mock_task.stop()
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_not_allow_2fa_flow(self, auth, mock_task):
'''
Tests if the user denies being sent a Duo Push.
'''
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.NONE
auth.can_auth.return_value = True
self.bot.messages = defaultdict(str)
test_user = user.User({}, auth, self.bot)
task = mock_task.start()
assert str(test_user._fsm.state) == 'need_task'
# Also test not advancing on no queued task
test_user.step()
assert str(test_user._fsm.state) == 'need_task'
test_user.add_task(task)
test_user.step()
assert str(test_user._fsm.state) == 'action_performed_check'
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'auth_permission_check'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
test_user.negative_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'task_finished'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
mock_task.stop()
@patch('securitybot.user.ESCALATION_TIME', timedelta(seconds=-1))
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_auto_escalate(self, auth, mock_task):
'''Tests that after some time an alert automatically escalates.'''
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.DENIED
auth.can_auth.return_value = True
self.bot.messages = defaultdict(str)
test_user = user.User({}, auth, self.bot)
task = mock_task.start()
assert str(test_user._fsm.state) == 'need_task'
test_user.add_task(task)
test_user.step()
assert str(test_user._fsm.state) == 'action_performed_check'
# Auto-escalation should happen immediately because escalation time
# is set to be zero seconds
test_user.step()
assert str(test_user._fsm.state) == 'task_finished'
task.set_verifying.assert_called_with()
mock_task.stop()
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_deny_resets_auth(self, auth, mock_task):
'''Tests that receiving a deny from 2FA resets any saved authorization.'''
auth.auth_status.return_value = securitybot.auth.auth.AUTH_STATES.DENIED
auth.can_auth.return_value = True
self.bot.messages = defaultdict(str)
test_user = user.User({}, auth, self.bot)
task = mock_task.start()
assert str(test_user._fsm.state) == 'need_task'
test_user.add_task(task)
test_user.step()
assert str(test_user._fsm.state) == 'action_performed_check'
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'auth_permission_check'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
test_user.positive_response('Dummy explanation.')
test_user.step()
assert str(test_user._fsm.state) == 'waiting_on_auth'
assert (test_user._last_message.answer is None and
test_user._last_message.text == '')
test_user.step()
assert str(test_user._fsm.state) == 'task_finished'
auth.reset.assert_called_with()
mock_task.stop()
# Auth interactions
@patch('securitybot.tasker.tasker.Task')
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_start_auth(self, auth, mock_task):
'''Tests that authorization calls call the auth object.'''
self.bot.messages = defaultdict(str)
test_user = user.User({}, auth, self.bot)
task = mock_task.start()
test_user.pending_task = task
test_user.begin_auth()
auth.auth.assert_called_with(task.description)
mock_task.stop()
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_check_auth(self, auth):
'''Tests that auth status calls interact properly.'''
test_user = user.User({}, auth, None)
test_user.auth_status()
auth.auth_status.assert_called_with()
@patch('securitybot.auth.auth.Auth', autospec=True)
def test_reset_auth(self, auth):
'''Tests that auth is properly reset on `reset_auth`.'''
test_user = user.User({}, auth, None)
test_user.reset_auth()
auth.reset.assert_called_with()