Skip to content

Commit

Permalink
Update unit tests; fix flake errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joshand committed Jun 18, 2019
1 parent c7fae94 commit b0e04fc
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 7 deletions.
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Sphinx==1.8.1
twine==1.12.1
webexteamssdk==1.0.3
white>=0.1.2
requests-mock
58 changes: 58 additions & 0 deletions tests/teams_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
85 changes: 85 additions & 0 deletions tests/test_webexteamsbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
20 changes: 13 additions & 7 deletions webexteamsbot/webexteamsbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def teams_setup(self):
# Setup the Teams Connection
globals()["teams"] = WebexTeamsAPI(access_token=self.teams_bot_token)
globals()["webhook"] = self.setup_webhook(
self.teams_bot_name, self.teams_bot_url, self.webhook_resource, self.webhook_event
self.teams_bot_name, self.teams_bot_url,
self.webhook_resource, self.webhook_event
)
sys.stderr.write("Configuring Webhook. \n")
sys.stderr.write("Webhook ID: " + globals()["webhook"].id + "\n")
Expand Down Expand Up @@ -157,13 +158,15 @@ def setup_webhook(self, name, targeturl, wh_resource, wh_event):
event=wh_event,
)

# if we have an existing webhook, delete and recreate (can't update resource/event)
# if we have an existing webhook, delete and recreate
# (can't update resource/event)
else:
# Need try block because if there are NO webhooks it throws error
try:
wh = self.teams.webhooks.delete(webhookId=wh.id)
wh = self.teams.webhooks.create(
name=name, targetUrl=targeturl, resource=wh_resource, event=wh_event
name=name, targetUrl=targeturl,
resource=wh_resource, event=wh_event
)
# https://github.com/CiscoDevNet/ciscoteamsapi/blob/master/ciscoteamsapi/api/webhooks.py#L237
except Exception as e:
Expand Down Expand Up @@ -249,7 +252,8 @@ def process_incoming_message(self):
if post_data["resource"] != "messages":
if post_data["resource"] in self.commands.keys():
api = WebexTeamsAPI(access_token=self.teams_bot_token)
reply = self.commands[post_data["resource"]]["callback"](api, post_data)
p = post_data
reply = self.commands[p["resource"]]["callback"](api, p)
else:
return ""
elif post_data["resource"] == "messages":
Expand Down Expand Up @@ -316,7 +320,8 @@ def add_command(self, command, help_message, callback):
:param callback: The function to run when this command is given
:return:
"""
self.commands[command.lower()] = {"help": help_message, "callback": callback}
self.commands[command.lower()] = {"help": help_message,
"callback": callback}

def remove_command(self, command):
"""
Expand All @@ -334,7 +339,7 @@ def extract_message(self, command, text):
:return:
"""
cmd_loc = text.find(command)
message = text[cmd_loc + len(command) :]
message = text[cmd_loc + len(command):]
return message

def set_greeting(self, callback):
Expand All @@ -350,7 +355,8 @@ def set_greeting(self, callback):

def set_help_message(self, msg):
"""
Configure the banner for the help message. Command list will be appended to this later.
Configure the banner for the help message.
Command list will be appended to this later.
:return:
"""
self.help_message = msg
Expand Down

0 comments on commit b0e04fc

Please sign in to comment.