def test_dispatch(self): bot = BaseBot(self.token) bot.commands = ( (r'test_command (?P<test_param>[0-9]+)', 'test_command'), ) def test_command(test_param): return test_param bot.test_command = test_command # Assign the testing command bot.__init__(self.token) # Test a valid command text = 'test_command 5' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual('5', result) # Test a valid command with missing param text = 'test_command' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual(bot.no_command_found(text), result) # Test invalid command text = 'invalid' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual(bot.no_command_found(text), result)
def test_dispatch(self): bot = BaseBot(self.token) bot.commands = ((r'test_command (?P<test_param>[0-9]+)', 'test_command'), ) def test_command(test_param): return test_param bot.test_command = test_command # Assign the testing command bot.__init__(self.token) # Test a valid command text = 'test_command 5' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual('5', result) # Test a valid command with missing param text = 'test_command' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual(bot.no_command_found(text), result) # Test invalid command text = 'invalid' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual(bot.no_command_found(text), result)
def test_restaff_command(self, mock_slack, mock_mail): data = get_mock_slack_data(user_id=self.worker.slack_user_id) task = (Task.objects.filter( status=Task.Status.AWAITING_PROCESSING).first()) worker = self.workers[0] task = assign_task(worker.id, task.id) command = 'restaff {} {}'.format(task.id, worker.user.username) data = get_mock_slack_data(text=command, user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assertEqual( load_encoded_json(response.content)['attachments'][0]['text'], self.staffbot.restaffing_success.format(task.id))
def test_staff_command_errors(self): """ Test that the staffing logic errors are raised during staff command. """ bot = StaffBot() data = get_mock_slack_data( text='staff 999999999999', user_id=self.worker.slack_user_id) response = bot.dispatch(data) self.assertEqual(response['attachments'][0]['text'], bot.task_does_not_exist_error.format('999999999999')) data['text'] = 'staff' response = bot.dispatch(data) self.assertTrue(bot.default_error_text in response.get('text')) task = TaskFactory(status=Task.Status.COMPLETE) data['text'] = 'staff {}'.format(task.id) response = bot.dispatch(data) self.assertEquals(response['attachments'][0]['text'], bot.task_assignment_error .format(task.id, 'Status incompatible with new assignment'))
def test_restaff_command_errors(self): """ Test that the staffing logic errors are raised during staff command. """ bot = StaffBot() data = get_mock_slack_data( text='restaff 999999999999 unknown', user_id=self.worker.slack_user_id) response = bot.dispatch(data) self.assertEqual(response.get('text'), bot.worker_does_not_exist.format('unknown')) worker = WorkerFactory(user__username='******') data['text'] = 'restaff 999999999999 slackusername' response = bot.dispatch(data) self.assertEqual(response.get('text'), bot.task_does_not_exist_error.format('999999999999')) data['text'] = 'restaff' response = bot.dispatch(data) self.assertTrue(bot.default_error_text in response.get('text')) task = TaskFactory(status=Task.Status.COMPLETE) command = 'restaff {} {}'.format(task.id, worker.user.username) data['text'] = command response = bot.dispatch(data) self.assertEquals(response.get('text'), (bot.task_assignment_does_not_exist_error .format(worker.user.username, task.id)))
def test_validate(self): """ Ensure we only listen to valid requests. """ mock_slack_data = get_mock_slack_data() # Test all requests allowed bot = BaseBot(self.token) self.assertEqual(mock_slack_data, bot.validate(mock_slack_data)) # verify we validate the token bot = BaseBot('') with self.assertRaises(SlackCommandInvalidRequest): bot.validate(mock_slack_data) # verify that we perform validation on each of the fields validated_fields = [ 'allowed_team_ids', 'allowed_domains', 'allowed_channel_ids', 'allowed_channel_names', 'allowed_user_ids', 'allowed_user_names', 'allowed_commands' ] for field in validated_fields: config = {field: []} bot = BaseBot(self.token, **config) with self.assertRaises(SlackCommandInvalidRequest): bot.validate(mock_slack_data) config = { 'allowed_{}s'.format(field): [mock_slack_data.get(field)] for field in validated_fields } bot = BaseBot(self.token, **config) self.assertEqual(mock_slack_data, bot.validate(mock_slack_data))
def test_commands(self): """ Ensure that the bot can handle the following commands: /staffbot staff <task_id> /staffbot restaff <task_id> <username> This test only validates that the commands are processed, other tests verify the functionality of the command execution. """ bot = StaffBot() # Test staff command mock_slack_data = get_mock_slack_data( text='staff 5', user_id=self.worker.slack_user_id) response = bot.dispatch(mock_slack_data) self.assertFalse(bot.default_error_text in response.get('text', '')) # Test the restaff command mock_slack_data['text'] = 'restaff 5 username' response = bot.dispatch(mock_slack_data) self.assertFalse(bot.default_error_text in response.get('text', '')) # Test we fail gracefully mock_slack_data['text'] = 'invalid command' response = bot.dispatch(mock_slack_data) self.assertTrue(bot.default_error_text in response.get('text', ''))
def test_restaff_command(self, mock_slack, mock_mail): data = get_mock_slack_data(user_id=self.worker.slack_user_id) task = ( Task.objects.filter(status=Task.Status.AWAITING_PROCESSING) .first()) worker = self.workers[0] task = assign_task(worker.id, task.id) command = 'restaff {} {}'.format(task.id, worker.user.username) data = get_mock_slack_data( text=command, user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assertEqual(load_encoded_json(response.content).get('text'), 'Restaffed task {}!'.format(task.id))
def test_validate(self): """ Ensure we only listen to valid requests. """ mock_slack_data = get_mock_slack_data() # Test all requests allowed bot = BaseBot(self.token) self.assertEqual(mock_slack_data, bot.validate(mock_slack_data)) # verify we validate the token bot = BaseBot('') with self.assertRaises(SlackCommandInvalidRequest): bot.validate(mock_slack_data) # verify that we perform validation on each of the fields validated_fields = ['allowed_team_ids', 'allowed_domains', 'allowed_channel_ids', 'allowed_channel_names', 'allowed_user_ids', 'allowed_user_names', 'allowed_commands'] for field in validated_fields: config = {field: []} bot = BaseBot(self.token, **config) with self.assertRaises(SlackCommandInvalidRequest): bot.validate(mock_slack_data) config = {'allowed_{}s'.format(field): [mock_slack_data.get(field)] for field in validated_fields} bot = BaseBot(self.token, **config) self.assertEqual(mock_slack_data, bot.validate(mock_slack_data))
def test_staff_command(self, mock_slack, mock_mail): task = TaskFactory(status=Task.Status.AWAITING_PROCESSING) data = get_mock_slack_data( text='staff {}'.format(task.id), user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assertEqual(load_encoded_json(response.content).get('text'), 'Staffed task {}!'.format(task.id)) task = TaskFactory(status=Task.Status.PENDING_REVIEW) data = get_mock_slack_data( text='staff {}'.format(task.id), user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assertEqual(load_encoded_json(response.content).get('text'), 'Staffed task {}!'.format(task.id))
def test_staff_command(self, mock_slack, mock_mail): task = TaskFactory(status=Task.Status.AWAITING_PROCESSING) data = get_mock_slack_data(text='staff {}'.format(task.id), user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assertEqual( load_encoded_json(response.content)['attachments'][0]['text'], self.staffbot.staffing_success.format(task.id)) task = TaskFactory(status=Task.Status.PENDING_REVIEW) data = get_mock_slack_data(text='staff {}'.format(task.id), user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assertEqual( load_encoded_json(response.content)['attachments'][0]['text'], self.staffbot.staffing_success.format(task.id))
def test_staff_command(self, mock_slack, mock_mail): task = TaskFactory(status=Task.Status.AWAITING_PROCESSING) data = get_mock_slack_data( text='staff {}'.format(task.id), user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assertEqual( load_encoded_json(response.content)['attachments'][0]['text'], self.staffbot.staffing_success.format(task.id)) task = TaskFactory(status=Task.Status.PENDING_REVIEW) data = get_mock_slack_data( text='staff {}'.format(task.id), user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assertEqual( load_encoded_json(response.content)['attachments'][0]['text'], self.staffbot.staffing_success.format(task.id))
def test_unauthorized_user(self): worker1 = self.workers[1] request_client = RequestClient(username=worker1.user.username, password='******') data = get_mock_slack_data(user_id=worker1.slack_user_id) response = request_client.post(self.url, data) self.assert_response(response, default_error_text=StaffBot.not_authorized_error) data['user_id'] = 'fake_id' response = request_client.post(self.url, data) self.assert_response(response, default_error_text='not found')
def test_unauthorized_user(self): worker1 = self.workers[1] request_client = RequestClient(username=worker1.user.username, password='******') data = get_mock_slack_data( user_id=worker1.slack_user_id) response = request_client.post(self.url, data) self.assert_response(response, default_error_text=StaffBot.not_authorized_error) data['user_id'] = 'fake_id' response = request_client.post(self.url, data) self.assert_response(response, default_error_text='not found')
def test_restaff_command_errors(self): """ Test that the staffing logic errors are raised during staff command. """ bot = StaffBot() command = 'restaff 999999999999 unknown' data = get_mock_slack_data( text=command, user_id=self.worker.slack_user_id) response = bot.dispatch(data) self.assertEqual(response.get('text'), command) self.assertEqual(response['attachments'][0]['text'], bot.worker_does_not_exist.format('unknown')) worker = WorkerFactory(user__username='******') data['text'] = 'restaff 999999999999 username' response = bot.dispatch(data) self.assertEqual(response['attachments'][0]['text'], bot.task_does_not_exist_error.format('999999999999')) # making sure it works with slack username as well. worker.slack_username = '******' worker.save() data['text'] = 'restaff 999999999999 slackusername' response = bot.dispatch(data) self.assertEqual(response['attachments'][0]['text'], bot.task_does_not_exist_error.format('999999999999')) data['text'] = 'restaff' response = bot.dispatch(data) self.assertTrue(bot.default_error_text in response.get('text')) task = TaskFactory(status=Task.Status.COMPLETE) command = 'restaff {} {}'.format(task.id, worker.user.username) data['text'] = command response = bot.dispatch(data) self.assertEquals(response['attachments'][0]['text'], (bot.task_assignment_does_not_exist_error .format(worker.user.username, task.id)))
def _test_staffing_requests(self, worker, task, command, can_slack=False, can_mail=False): StaffBotRequest.objects.all().delete() bot = StaffBot() communication_type = (CommunicationPreference.CommunicationType .NEW_TASK_AVAILABLE.value) communication_preference = CommunicationPreference.objects.get( worker=worker, communication_type=communication_type) communication_preference.methods.slack = can_slack communication_preference.methods.email = can_mail communication_preference.save() data = get_mock_slack_data( text=command, user_id=self.worker.slack_user_id) bot.dispatch(data) send_staffing_requests(worker_batch_size=20) self.assertEquals(StaffingRequestInquiry.objects.filter( communication_preference__worker_id=worker, request__task=task).count(), can_slack + can_mail)
def _test_staffing_requests(self, worker, task, command, can_slack=False, can_mail=False): StaffBotRequest.objects.all().delete() bot = StaffBot() communication_type = (CommunicationPreference.CommunicationType .NEW_TASK_AVAILABLE.value) communication_preference = CommunicationPreference.objects.get( worker=worker, communication_type=communication_type) communication_preference.methods.slack = can_slack communication_preference.methods.email = can_mail communication_preference.save() data = get_mock_slack_data( text=command, user_id=self.worker.slack_user_id) bot.dispatch(data) send_staffing_requests(worker_batch_size=20, frequency=timedelta(minutes=0)) self.assertEquals(StaffingRequestInquiry.objects.filter( communication_preference__worker_id=worker, request__task=task).count(), can_slack + can_mail)
def test_post_valid_data(self): data = get_mock_slack_data( user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assert_response(response)
def test_help(self): bot = BaseBot(self.token) mock_slack_data = get_mock_slack_data(text='help') with self.assertRaises(NotImplementedError): bot.dispatch(mock_slack_data)
def test_post_valid_data(self): data = get_mock_slack_data(user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assert_response(response)
def test_post_invalid_token(self): data = get_mock_slack_data(user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assert_response(response, default_error_text='Invalid token')
def test_post_invalid_token(self): data = get_mock_slack_data( user_id=self.worker.slack_user_id) response = self.request_client.post(self.url, data) self.assert_response( response, default_error_text='Invalid token')
class BaseBotTest(OrchestraTestCase): token = get_mock_slack_data().get('token') def test_help(self): bot = BaseBot(self.token) mock_slack_data = get_mock_slack_data(text='help') with self.assertRaises(NotImplementedError): bot.dispatch(mock_slack_data) def test_validate(self): """ Ensure we only listen to valid requests. """ mock_slack_data = get_mock_slack_data() # Test all requests allowed bot = BaseBot(self.token) self.assertEqual(mock_slack_data, bot.validate(mock_slack_data)) # verify we validate the token bot = BaseBot('') with self.assertRaises(SlackCommandInvalidRequest): bot.validate(mock_slack_data) # verify that we perform validation on each of the fields validated_fields = [ 'allowed_team_ids', 'allowed_domains', 'allowed_channel_ids', 'allowed_channel_names', 'allowed_user_ids', 'allowed_user_names', 'allowed_commands' ] for field in validated_fields: config = {field: []} bot = BaseBot(self.token, **config) with self.assertRaises(SlackCommandInvalidRequest): bot.validate(mock_slack_data) config = { 'allowed_{}s'.format(field): [mock_slack_data.get(field)] for field in validated_fields } bot = BaseBot(self.token, **config) self.assertEqual(mock_slack_data, bot.validate(mock_slack_data)) def test_dispatch(self): bot = BaseBot(self.token) bot.commands = ((r'test_command (?P<test_param>[0-9]+)', 'test_command'), ) def test_command(test_param): return test_param bot.test_command = test_command # Assign the testing command bot.__init__(self.token) # Test a valid command text = 'test_command 5' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual('5', result) # Test a valid command with missing param text = 'test_command' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual(bot.no_command_found(text), result) # Test invalid command text = 'invalid' mock_slack_data = get_mock_slack_data(text=text) result = bot.dispatch(mock_slack_data) self.assertEqual(bot.no_command_found(text), result)
def test_assert_validate_error(self): bot = StaffBot() with self.assertRaises(SlackUserUnauthorized): mock_slack_data = get_mock_slack_data(text='staff 5') bot.dispatch(mock_slack_data)