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'])
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_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 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.bot.test_username = None self.patch_task = patch_task self.task = patch_task.start() self.task.title = 'title' self.task.username = '******' self.task.comment = '' self.task.event_time = datetime.now(tz=pytz.utc) tasker.get_new_tasks.return_value = [self.task] tasker.get_pending_tasks.return_value = [self.task] tasker.get_active_tasks.return_value = [self.task] self.bot._store_or_update_active_task = MagicMock( 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 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_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_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()
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_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')
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_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
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
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'}}