-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Rmcreyes/#29 event handler
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |