I have the function which handles a specific bot event from Slack. Generally speaking, user clicks a button then my server receives and handles payload of this button.
The question is how should I test it? Do I need to mock make_admin
and build_admins_message
and check that they were called or I need to test real implementations? For example, I can retrieve the user from the database and check that it is actually an admin and also check that build_admins_message returns a dictionary that I expect to receive.
@slack_interactions.on('admin_add')
def handle_admin_add(payload):
team_id = payload['team']['id']
user_id = payload['user']['id']
action_value = payload['actions'][0]['selected_options'][0]['value']
user = SlackUser.objects.find_by_ids(team_id, action_value)
if user and not user.is_bot:
user.make_admin()
return build_admins_message(team_id, user_id)
Currently my tests look like this:
class TestAdminAddHandler(TestCase):
def setUp(self):
team = SlackTeam.objects.create(team_id='TEAMID')
SlackUser.objects.create(team=team, user_id='USERID')
SlackUser.objects.create(team=team, user_id='BOTID', is_bot=True)
SlackUser.objects.create(
team=team, user_id='ADMINID', is_bot_admin=True)
def tearDown(self):
SlackUser.objects.all().delete()
SlackTeam.objects.all().delete()
def test_wrong_callback(self):
payload = {'callback_id': 'wrong_callback'}
message = handle_admin_add(payload)
self.assertIsNone(message)
def test_has_no_user(self):
payload = {
'callback_id': 'admin_add',
'team': {'id': 'TEAMID'},
'user': {'id': 'ADMINID'},
'actions': [{
'selected_options': [{'value': 'BADID'}]
}]
}
message = handle_admin_add(payload)
user = SlackUser.objects.get(user_id='USERID')
self.assertFalse(user.is_bot_admin)
for att in message['attachments']:
self.assertNotIn('BADID', att.get('title', ''))
def test_user_is_bot(self):
payload = {
'callback_id': 'admin_add',
'team': {'id': 'TEAMID'},
'user': {'id': 'ADMINID'},
'actions': [{
'selected_options': [{'value': 'BOTID'}]
}]
}
message = handle_admin_add(payload)
user = SlackUser.objects.get(user_id='BOTID')
self.assertFalse(user.is_bot_admin)
for att in message['attachments']:
self.assertNotIn('BOTID', att.get('title', ''))
def test_add_admin(self):
payload = {
'callback_id': 'admin_add',
'team': {'id': 'TEAMID'},
'user': {'id': 'ADMINID'},
'actions': [{
'selected_options': [{'value': 'USERID'}]
}]
}
message = handle_admin_add(payload)
user = SlackUser.objects.filter(user_id='USERID').first()
self.assertTrue(user.is_bot_admin)
user_in_list = False
for att in message['attachments']:
if 'USERID' in att.get('title', ''):
user_in_list = True
self.assertTrue(user_in_list)
Aucun commentaire:
Enregistrer un commentaire