-
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
370 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,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Unit test package for webexteamsbot.""" |
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,39 @@ | ||
import unittest | ||
from webexteamsbot.models import Response | ||
|
||
|
||
class ModelTests(unittest.TestCase): | ||
def test_response_text(self): | ||
r = Response() | ||
r.text = "hello" | ||
self.assertEqual(r.text, "hello") | ||
|
||
def test_response_files(self): | ||
r = Response() | ||
r.files = "someurl" | ||
self.assertEqual(r.files[0], "someurl") | ||
|
||
def test_response_roomid(self): | ||
r = Response() | ||
r.roomId = "someid" | ||
self.assertEqual(r.roomId, "someid") | ||
|
||
def test_response_markdown(self): | ||
r = Response() | ||
r.markdown = "**some markdown**" | ||
self.assertEqual(r.markdown, "**some markdown**") | ||
|
||
def test_response_html(self): | ||
r = Response() | ||
r.html = "<h1>some html</h1>" | ||
self.assertEqual(r.html, "<h1>some html</h1>") | ||
|
||
def test_response_json(self): | ||
r = Response() | ||
r.text = "foo" | ||
self.assertIn("text", r.json()) | ||
|
||
def test_response_as_dict(self): | ||
r = Response() | ||
r.text = "foo" | ||
self.assertIn("text", r.as_dict()) |
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,124 @@ | ||
import json | ||
|
||
|
||
class MockTeamsAPI: | ||
@classmethod | ||
def list_webhooks(cls): | ||
response = { | ||
"items": [ | ||
{ | ||
"id": "someID", | ||
"name": "My Awesome Webhook", | ||
"targetUrl": "https://example.com/mywebhook", | ||
"resource": "messages", | ||
"event": "created", | ||
"filter": "roomId=someroomID", | ||
"secret": "86dacc007724d8ea666f88fc77d918dad9537a15", | ||
"status": "active", | ||
"created": "2015-10-18T14:26:16+00:00", | ||
} | ||
] | ||
} | ||
return response | ||
|
||
@classmethod | ||
def list_webhooks_exist(cls): | ||
data = MockTeamsAPI.list_webhooks() | ||
data["items"][0]["name"] = "testbot" | ||
return data | ||
|
||
@classmethod | ||
def create_webhook(cls): | ||
response = { | ||
"id": "newwebhook", | ||
"name": "My Awesome Webhook", | ||
"targetUrl": "https://example.com/mywebhook", | ||
"resource": "messages", | ||
"event": "created", | ||
"filter": "roomId=someRoomId", | ||
"secret": "86dacc007724d8ea666f88fc77d918dad9537a15", | ||
"status": "active", | ||
"created": "2015-10-18T14:26:16+00:00", | ||
} | ||
return response | ||
|
||
@classmethod | ||
def incoming_msg(cls): | ||
data = { | ||
"id": "asdfadfsadfasdf", | ||
"name": "New message in 'Project Unicorn' room", | ||
"resource": "messages", | ||
"event": "created", | ||
"filter": "roomId=sadfasdfsdffsadfd", | ||
"orgId": "OTZhYmMyYWEtM2RjYy0xMWU1LWExNTItZmUzNDgxOWNkYzlh", | ||
"createdBy": "fdsafdsf", | ||
"appId": "asdfasdfsadf", | ||
"ownedBy": "creator", | ||
"status": "active", | ||
"actorId": "asdffsdfsdsafd", | ||
"data": { | ||
"id": "incoming_message_id", | ||
"roomId": "some_room_id", | ||
"personId": "some_person_id", | ||
"personEmail": "[email protected]", | ||
"created": "2015-10-18T14:26:16.000Z", | ||
}, | ||
} | ||
return json.dumps(data) | ||
|
||
@classmethod | ||
def get_message_help(cls): | ||
data = { | ||
"id": "some_message_id", | ||
"roomId": "some_room_id", | ||
"roomType": "group", | ||
"toPersonId": "some_person_id", | ||
"toPersonEmail": "[email protected]", | ||
"text": "/help", | ||
"personEmail": "[email protected]", | ||
"personId": "some_person_id", | ||
"created": "2015-10-18T14:26:16+00:00", | ||
} | ||
return data | ||
|
||
# @classmethod | ||
# def get_message_echo(cls): | ||
# data = MockTeamsAPI.get_message_help() | ||
# data['text'] = "/echo foo" | ||
# return data | ||
|
||
@classmethod | ||
def empty_message(cls): | ||
data = MockTeamsAPI.get_message_help() | ||
data["text"] = "" | ||
return data | ||
|
||
@classmethod | ||
def get_message_dosomething(cls): | ||
data = MockTeamsAPI.get_message_help() | ||
data["text"] = "/echo imtheecho" | ||
return data | ||
|
||
@classmethod | ||
def get_message_from_bot(cls): | ||
data = MockTeamsAPI.get_message_help() | ||
data["personEmail"] = "[email protected]" | ||
return data | ||
|
||
@classmethod | ||
def me(cls): | ||
data = { | ||
"id": "myid", | ||
"emails": ["[email protected]"], | ||
"displayName": "Some User", | ||
"nickName": "SomeUser", | ||
"firstName": "Some", | ||
"lastName": "User", | ||
"avatar": "https://some-avatar-url.com/sdflkjsdflkajs", | ||
"orgId": "orgid", | ||
"created": "2012-06-15T20:32:02.438Z", | ||
"lastActivity": "2018-04-09T21:15:44.677Z", | ||
"status": "inactive", | ||
"type": "person", | ||
} | ||
return 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 |
---|---|---|
@@ -0,0 +1,204 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Tests for `webexteamsbot` package.""" | ||
|
||
|
||
import unittest | ||
from webexteamsbot import TeamsBot | ||
import requests_mock | ||
from .teams_mock import MockTeamsAPI | ||
|
||
|
||
class TeamsBotTests(unittest.TestCase): | ||
@requests_mock.mock() | ||
def setUp(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(), | ||
) | ||
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, | ||
) | ||
|
||
# Add new command | ||
bot.add_command( | ||
"/dosomething", "help for do something", self.do_something | ||
) | ||
bot.testing = True | ||
self.app = bot.test_client() | ||
|
||
def do_something(self, incoming_msg): | ||
""" | ||
Sample function to do some action. | ||
:param incoming_msg: The incoming message object from Teams | ||
:return: A text or markdown based reply | ||
""" | ||
return "i did what you said - {}".format(incoming_msg.text) | ||
|
||
@requests_mock.mock() | ||
def test_webhook_already_exists(self, m): | ||
m.get( | ||
"https://api.ciscospark.com/v1/webhooks", | ||
json=MockTeamsAPI.list_webhooks_exist(), | ||
) | ||
m.post( | ||
"https://api.ciscospark.com/v1/webhooks", | ||
json=MockTeamsAPI.create_webhook(), | ||
) | ||
|
||
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, | ||
) | ||
|
||
# Add new command | ||
bot.add_command( | ||
"/dosomething", "help for do something", self.do_something | ||
) | ||
bot.testing = True | ||
self.app = bot.test_client() | ||
|
||
@requests_mock.mock() | ||
def test_bad_config_raises_valueerror(self, m): | ||
with self.assertRaises(ValueError): | ||
m.get( | ||
"https://api.ciscospark.com/v1/webhooks", | ||
json=MockTeamsAPI.list_webhooks_exist(), | ||
) | ||
m.post( | ||
"https://api.ciscospark.com/v1/webhooks", | ||
json=MockTeamsAPI.create_webhook(), | ||
) | ||
|
||
bot_email = None | ||
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, | ||
) | ||
|
||
# Add new command | ||
bot.add_command( | ||
"/dosomething", "help for do something", self.do_something | ||
) | ||
bot.testing = True | ||
self.app = bot.test_client() | ||
|
||
@requests_mock.mock() | ||
def test_teams_setup(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(), | ||
) | ||
|
||
def test_health_endpoint(self): | ||
resp = self.app.get("/health") | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertIn(b"I'm Alive", resp.data) | ||
|
||
def test_config_endpoint(self): | ||
resp = self.app.get("/config") | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertIn(b"[email protected]", resp.data) | ||
|
||
@requests_mock.mock() | ||
def test_process_incoming_message_send_help(self, m): | ||
m.get("//api.ciscospark.com/v1/people/me", json=MockTeamsAPI.me()) | ||
m.get( | ||
"//api.ciscospark.com/v1/messages/incoming_message_id", | ||
json=MockTeamsAPI.get_message_help(), | ||
) | ||
m.post("//api.ciscospark.com/v1/messages", json={}) | ||
resp = self.app.post( | ||
"/", | ||
data=MockTeamsAPI.incoming_msg(), | ||
content_type="application/json", | ||
) | ||
self.assertEqual(resp.status_code, 200) | ||
print(resp.data) | ||
self.assertIn(b"I understand the following commands", resp.data) | ||
|
||
@requests_mock.mock() | ||
def test_process_incoming_message_default_command(self, m): | ||
m.get("//api.ciscospark.com/v1/people/me", json=MockTeamsAPI.me()) | ||
m.get( | ||
"//api.ciscospark.com/v1/messages/incoming_message_id", | ||
json=MockTeamsAPI.empty_message(), | ||
) | ||
m.post("//api.ciscospark.com/v1/messages", json={}) | ||
resp = self.app.post( | ||
"/", | ||
data=MockTeamsAPI.incoming_msg(), | ||
content_type="application/json", | ||
) | ||
self.assertEqual(resp.status_code, 200) | ||
print(resp.data) | ||
self.assertIn(b"I understand the following commands", resp.data) | ||
|
||
@requests_mock.mock() | ||
def test_process_incoming_message_match_command(self, m): | ||
m.get("//api.ciscospark.com/v1/people/me", json=MockTeamsAPI.me()) | ||
m.get( | ||
"//api.ciscospark.com/v1/messages/incoming_message_id", | ||
json=MockTeamsAPI.get_message_dosomething(), | ||
) | ||
m.post("//api.ciscospark.com/v1/messages", json={}) | ||
resp = self.app.post( | ||
"/", | ||
data=MockTeamsAPI.incoming_msg(), | ||
content_type="application/json", | ||
) | ||
self.assertEqual(resp.status_code, 200) | ||
print(resp.data) | ||
# self.assertIn(b'I understand the following commands', resp.data) | ||
|
||
@requests_mock.mock() | ||
def test_process_incoming_message_from_bot(self, m): | ||
m.get("//api.ciscospark.com/v1/people/me", json=MockTeamsAPI.me()) | ||
m.get( | ||
"//api.ciscospark.com/v1/messages/incoming_message_id", | ||
json=MockTeamsAPI.get_message_from_bot(), | ||
) | ||
m.post("//api.ciscospark.com/v1/messages", json={}) | ||
resp = self.app.post( | ||
"/", | ||
data=MockTeamsAPI.incoming_msg(), | ||
content_type="application/json", | ||
) | ||
self.assertEqual(resp.status_code, 200) | ||
print(resp.data) | ||
|
||
def tearDown(self): | ||
pass |