-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest_webexteamsbot.py
289 lines (259 loc) · 9.61 KB
/
test_webexteamsbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/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_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())
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