Skip to content

Commit

Permalink
Merge pull request #68 from ubclaunchpad/rmcreyes/#29-event-handler
Browse files Browse the repository at this point in the history
Rmcreyes/#29 event handler
  • Loading branch information
Cheuk Yin Ng committed Nov 3, 2018
2 parents c97db19 + 27f60ad commit 52c0f66
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
26 changes: 26 additions & 0 deletions command/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Calls the appropriate handler depending on the event data."""
from command.commands.user import UserCommand


class Core:
"""Encapsulate methods for handling events."""

def __init__(self):
"""Initialize the dictionary of command handlers."""
self.__commands = {}
self.__commands["user"] = UserCommand()

def handle_app_mention(self, event_data):
"""Handle the events associated with mentions of @rocket."""
message = event_data["event"]["text"]
s = message.split(' ', 2)
if s[0] != "@rocket":
return 0
else:
command_type = s[1]
command = command_type + ' ' + s[2]
try:
self.__commands[command_type].handle(command)
return 1
except KeyError:
return -1
74 changes: 74 additions & 0 deletions tests/command/core_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""Test the core event handler."""
from unittest import mock
from command.core import Core


def test_handle_invalid_mention():
"""Test the instance of handle_app_mention being called inappropriately."""
event = {
"token": "XXYYZZ",
"team_id": "TXXXXXXXX",
"api_app_id": "AXXXXXXXXX",
"event": {
"type": "app_mention",
"user": "U061F7AUR",
"text": "hello world",
"ts": "1515449522.000016",
"channel": "C0LAN2Q65",
"event_ts": "1515449522000016"
},
"type": "app_mention",
"authed_users": ["UXXXXXXX1", "UXXXXXXX2"],
"event_id": "Ev08MFMKH6",
"event_time": 1234567890
}
core = Core()
assert core.handle_app_mention(event) == 0


def test_handle_invalid_command():
"""Test that invalid commands are being handled appropriately."""
event = {
"token": "XXYYZZ",
"team_id": "TXXXXXXXX",
"api_app_id": "AXXXXXXXXX",
"event": {
"type": "app_mention",
"user": "U061F7AUR",
"text": "@rocket fake command",
"ts": "1515449522.000016",
"channel": "C0LAN2Q65",
"event_ts": "1515449522000016"
},
"type": "app_mention",
"authed_users": ["UXXXXXXX1", "UXXXXXXX2"],
"event_id": "Ev08MFMKH6",
"event_time": 123456789
}
core = Core()
assert core.handle_app_mention(event) == -1


@mock.patch('command.core.UserCommand')
def test_handle_user_command(MockUserCommand):
"""Test that UserCommand.handle is called appropriately."""
event = {
"token": "XXYYZZ",
"team_id": "TXXXXXXXX",
"api_app_id": "AXXXXXXXXX",
"event": {
"type": "app_mention",
"user": "U061F7AUR",
"text": "@rocket user name",
"ts": "1515449522.000016",
"channel": "C0LAN2Q65",
"event_ts": "1515449522000016"
},
"type": "app_mention",
"authed_users": ["UXXXXXXX1", "UXXXXXXX2"],
"event_id": "Ev08MFMKH6",
"event_time": 1234567890
}
core = Core()
assert core.handle_app_mention(event) == 1
MockUserCommand.return_value.handle.assert_called_once_with("user name")

0 comments on commit 52c0f66

Please sign in to comment.