-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
157 additions
and
7 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 |
---|---|---|
|
@@ -11,3 +11,4 @@ Sphinx==1.8.1 | |
twine==1.12.1 | ||
webexteamssdk==1.0.3 | ||
white>=0.1.2 | ||
requests-mock |
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 |
---|---|---|
|
@@ -66,6 +66,64 @@ def incoming_msg(cls): | |
} | ||
return json.dumps(data) | ||
|
||
@classmethod | ||
def incoming_membership_fail(cls): | ||
data = { | ||
'id': 'newwebhook', | ||
'name': 'My Awesome Webhook', | ||
'targetUrl': 'https://example.com/mywebhook', | ||
'resource': 'memberships', | ||
'event': 'created', | ||
'orgId': 'OTZhYmMyYWEtM2RjYy0xMWU1LWExNTItZmUzNDgxOWNkYzlh', | ||
'createdBy': 'fdsafdsf', | ||
'appId': 'asdfasdfsadf', | ||
'ownedBy': 'creator', | ||
'status': 'active', | ||
'created': '2018-09-13T19:35:51.248Z', | ||
'actorId': 'OTZhYmMyYWEtM2RjYy0xMWU1LWExNTItZmUzNDgxOWNkYzlh', | ||
'data': { | ||
'id': 'incoming_membership_id', | ||
'roomId': 'some_room_id', | ||
'personId': 'some_person_id', | ||
'personEmail': '[email protected]', | ||
'personDisplayName': 'Matt', | ||
'personOrgId': 'OTZhYmMyYWEtM2RjYy0xMWU1LWExNTItZmUzNDgxOWNk', | ||
'isModerator': False, | ||
'isMonitor': False, | ||
'created': '2018-09-13T19:35:58.803Z' | ||
} | ||
} | ||
return json.dumps(data) | ||
|
||
@classmethod | ||
def incoming_membership_pass(cls): | ||
data = { | ||
'id': 'newwebhook', | ||
'name': 'My Awesome Webhook', | ||
'targetUrl': 'https://example.com/mywebhook', | ||
'resource': 'memberships', | ||
'event': 'created', | ||
'orgId': 'OTZhYmMyYWEtM2RjYy0xMWU1LWExNTItZmUzNDgxOWNkYzlh', | ||
'createdBy': 'fdsafdsf', | ||
'appId': 'asdfasdfsadf', | ||
'ownedBy': 'creator', | ||
'status': 'active', | ||
'created': '2018-09-13T19:35:51.248Z', | ||
'actorId': 'OTZhYmMyYWEtM2RjYy0xMWU1LWExNTItZmUzNDgxOWNkYzlh', | ||
'data': { | ||
'id': 'incoming_membership_id', | ||
'roomId': 'some_room_id', | ||
'personId': 'some_person_id', | ||
'personEmail': '[email protected]', | ||
'personDisplayName': 'Matt', | ||
'personOrgId': 'OTZhYmMyYWEtM2RjYy0xMWU1LWExNTItZmUzNDgxOWNk', | ||
'isModerator': False, | ||
'isMonitor': False, | ||
'created': '2018-09-13T19:35:58.803Z' | ||
} | ||
} | ||
return json.dumps(data) | ||
|
||
@classmethod | ||
def get_message_help(cls): | ||
data = { | ||
|
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 |
---|---|---|
|
@@ -167,6 +167,91 @@ def test_process_incoming_message_default_command(self, m): | |
print(resp.data) | ||
self.assertIn(b"I understand the following commands", resp.data) | ||
|
||
@requests_mock.mock() | ||
def test_process_incoming_membership_check_sender_fail(self, m): | ||
m.get('https://api.ciscospark.com/v1/webhooks', | ||
json=MockTeamsAPI.list_webhooks()) | ||
m.post('https://api.ciscospark.com/v1/webhooks', | ||
json=MockTeamsAPI.create_webhook()) | ||
m.post('//api.ciscospark.com/v1/messages', json={}) | ||
bot_email = "[email protected]" | ||
teams_token = "somefaketoken" | ||
bot_url = "http://fakebot.com" | ||
bot_app_name = "testbot" | ||
# Create a new bot | ||
bot = TeamsBot(bot_app_name, | ||
teams_bot_token=teams_token, | ||
teams_bot_url=bot_url, | ||
teams_bot_email=bot_email, | ||
debug=True, | ||
webhook_resource="memberships", | ||
webhook_event="all") | ||
|
||
# Add new command | ||
bot.add_command('memberships', | ||
'*', | ||
self.check_membership) | ||
bot.testing = True | ||
self.app = bot.test_client() | ||
|
||
resp = self.app.post('/', | ||
data=MockTeamsAPI.incoming_membership_fail(), | ||
content_type="application/json") | ||
self.assertEqual(resp.status_code, 200) | ||
print(resp.data) | ||
self.assertIn(b"failed", resp.data) | ||
|
||
@requests_mock.mock() | ||
def test_process_incoming_membership_check_sender_pass(self, m): | ||
m.get('https://api.ciscospark.com/v1/webhooks', | ||
json=MockTeamsAPI.list_webhooks()) | ||
m.post('https://api.ciscospark.com/v1/webhooks', | ||
json=MockTeamsAPI.create_webhook()) | ||
m.post('//api.ciscospark.com/v1/messages', json={}) | ||
bot_email = "[email protected]" | ||
teams_token = "somefaketoken" | ||
bot_url = "http://fakebot.com" | ||
bot_app_name = "testbot" | ||
# Create a new bot | ||
bot = TeamsBot(bot_app_name, | ||
teams_bot_token=teams_token, | ||
teams_bot_url=bot_url, | ||
teams_bot_email=bot_email, | ||
debug=True, | ||
webhook_resource="memberships", | ||
webhook_event="all") | ||
|
||
# Add new command | ||
bot.add_command('memberships', | ||
'*', | ||
self.check_membership) | ||
bot.testing = True | ||
self.app = bot.test_client() | ||
|
||
resp = self.app.post('/', | ||
data=MockTeamsAPI.incoming_membership_pass(), | ||
content_type="application/json") | ||
self.assertEqual(resp.status_code, 200) | ||
print(resp.data) | ||
self.assertIn(b"success", resp.data) | ||
|
||
def check_membership(self, ob, incoming_msg): | ||
""" | ||
Sample function to do some action. | ||
:param incoming_msg: The incoming message object from Spark | ||
:param ob: Spark API object | ||
:return: A text or markdown based reply | ||
""" | ||
|
||
whitelist = ["cisco.com"] | ||
pemail = incoming_msg["data"]["personEmail"] | ||
pdom = pemail.split("@")[1] | ||
|
||
if pdom in whitelist: | ||
return "success" | ||
else: | ||
return "failed" | ||
|
||
@requests_mock.mock() | ||
def test_process_incoming_message_match_command(self, m): | ||
m.get("//api.ciscospark.com/v1/people/me", json=MockTeamsAPI.me()) | ||
|
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