Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathtest_function.py
More file actions
353 lines (300 loc) · 11.3 KB
/
Copy pathtest_function.py
File metadata and controls
353 lines (300 loc) · 11.3 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import json
import re
import time
import pytest
from unittest.mock import Mock
from slack_sdk.signature import SignatureVerifier
from slack_sdk.web import WebClient
import slack_bolt.listener.thread_runner as runner_module
from slack_bolt.app import App
from slack_bolt.request import BoltRequest
from tests.mock_web_api_server import (
assert_received_request_count,
setup_mock_web_api_server,
cleanup_mock_web_api_server,
assert_auth_test_count,
)
from tests.utils import remove_os_env_temporarily, restore_os_env
class TestFunction:
signing_secret = "secret"
valid_token = "xoxb-valid"
mock_api_server_base_url = "http://localhost:8888"
signature_verifier = SignatureVerifier(signing_secret)
web_client = WebClient(
token=valid_token,
base_url=mock_api_server_base_url,
)
def setup_method(self):
self.old_os_env = remove_os_env_temporarily()
setup_mock_web_api_server(self)
def teardown_method(self):
cleanup_mock_web_api_server(self)
restore_os_env(self.old_os_env)
def generate_signature(self, body: str, timestamp: str):
return self.signature_verifier.generate_signature(
body=body,
timestamp=timestamp,
)
def build_headers(self, timestamp: str, body: str):
return {
"content-type": ["application/json"],
"x-slack-signature": [self.generate_signature(body, timestamp)],
"x-slack-request-timestamp": [timestamp],
}
def build_request_from_body(self, message_body: dict) -> BoltRequest:
timestamp, body = str(int(time.time())), json.dumps(message_body)
return BoltRequest(body=body, headers=self.build_headers(timestamp, body))
def setup_time_mocks(self, *, monkeypatch: pytest.MonkeyPatch, time_mock, sleep_mock):
monkeypatch.setattr(runner_module.time, "time", time_mock)
monkeypatch.setattr(runner_module.time, "sleep", sleep_mock)
def test_valid_callback_id_success(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse")(reverse)
request = self.build_request_from_body(function_body)
response = app.dispatch(request)
assert response.status == 200
assert_auth_test_count(self, 1)
assert_received_request_count(self, "/functions.completeSuccess", 1)
def test_valid_callback_id_complete(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse")(complete_it)
request = self.build_request_from_body(function_body)
response = app.dispatch(request)
assert response.status == 200
assert_auth_test_count(self, 1)
assert_received_request_count(self, "/functions.completeSuccess", 1)
def test_valid_callback_id_error(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse")(reverse_error)
request = self.build_request_from_body(function_body)
response = app.dispatch(request)
assert response.status == 200
assert_auth_test_count(self, 1)
assert_received_request_count(self, "/functions.completeError", 1)
def test_invalid_callback_id(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse")(reverse)
request = self.build_request_from_body(wrong_id_function_body)
response = app.dispatch(request)
assert response.status == 404
assert_auth_test_count(self, 1)
def test_invalid_declaration(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
func = app.function("reverse")
with pytest.raises(TypeError):
func("hello world")
def test_auto_acknowledge_false_with_acknowledging(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse", auto_acknowledge=False)(just_ack)
request = self.build_request_from_body(function_body)
response = app.dispatch(request)
assert response.status == 200
assert_auth_test_count(self, 1)
def test_auto_acknowledge_false_without_acknowledging(self, caplog, monkeypatch):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse", auto_acknowledge=False)(just_no_ack)
request = self.build_request_from_body(function_body)
elapsed_seconds = 0
def fake_time():
return float(elapsed_seconds)
def fake_sleep(duration):
nonlocal elapsed_seconds
elapsed_seconds += 1
self.setup_time_mocks(
monkeypatch=monkeypatch,
time_mock=fake_time,
sleep_mock=Mock(side_effect=fake_sleep),
)
response = app.dispatch(request)
assert response.status == 404
assert_auth_test_count(self, 1)
assert f"WARNING {just_no_ack.__name__} didn't call ack()" in caplog.text
def test_function_handler_timeout(self, monkeypatch):
timeout = 5
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse", auto_acknowledge=False, ack_timeout=timeout)(just_no_ack)
request = self.build_request_from_body(function_body)
elapsed_seconds = 0
def fake_time():
return float(elapsed_seconds)
def fake_sleep(duration):
nonlocal elapsed_seconds
elapsed_seconds += 1
self.setup_time_mocks(
monkeypatch=monkeypatch,
time_mock=fake_time,
sleep_mock=Mock(side_effect=fake_sleep),
)
response = app.dispatch(request)
assert response.status == 404
assert_auth_test_count(self, 1)
assert elapsed_seconds == timeout + 1, (
f"Expected handler to time out after {timeout + 1} time.sleep calls, "
f"but it was called {elapsed_seconds} times"
)
def test_warning_when_timeout_improperly_set(self, caplog):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse")(just_no_ack)
assert "WARNING" not in caplog.text
timeout_argument_name = "ack_timeout"
kwargs = {timeout_argument_name: 5}
callback_id = "reverse1"
app.function(callback_id, **kwargs)(just_no_ack)
assert (
f'WARNING On @app.function("{callback_id}"), as `auto_acknowledge` is `True`, `{timeout_argument_name}={kwargs[timeout_argument_name]}` you gave will be unused'
in caplog.text
)
callback_id = re.compile(r"hello \w+")
app.function(callback_id, **kwargs)(just_no_ack)
assert (
f"WARNING On @app.function({callback_id}), as `auto_acknowledge` is `True`, `{timeout_argument_name}={kwargs[timeout_argument_name]}` you gave will be unused"
in caplog.text
)
function_body = {
"token": "verification_token",
"enterprise_id": "E111",
"team_id": "T111",
"api_app_id": "A111",
"event": {
"type": "function_executed",
"function": {
"id": "Fn111",
"callback_id": "reverse",
"title": "Reverse",
"description": "Takes a string and reverses it",
"type": "app",
"input_parameters": [
{
"type": "string",
"name": "stringToReverse",
"description": "The string to reverse",
"title": "String To Reverse",
"is_required": True,
}
],
"output_parameters": [
{
"type": "string",
"name": "reverseString",
"description": "The string in reverse",
"title": "Reverse String",
"is_required": True,
}
],
"app_id": "A111",
"date_updated": 1659054991,
},
"inputs": {"stringToReverse": "hello"},
"function_execution_id": "Fx111",
"event_ts": "1659055013.509853",
"bot_access_token": "xwfp-valid",
},
"type": "event_callback",
"event_id": "Ev111",
"event_time": 1659055013,
"authed_users": ["W111"],
}
wrong_id_function_body = {
"token": "verification_token",
"enterprise_id": "E111",
"team_id": "T111",
"api_app_id": "A111",
"event": {
"type": "function_executed",
"function": {
"id": "Fn111",
"callback_id": "wrong_callback_id",
"title": "Reverse",
"description": "Takes a string and reverses it",
"type": "app",
"input_parameters": [
{
"type": "string",
"name": "stringToReverse",
"description": "The string to reverse",
"title": "String To Reverse",
"is_required": True,
}
],
"output_parameters": [
{
"type": "string",
"name": "reverseString",
"description": "The string in reverse",
"title": "Reverse String",
"is_required": True,
}
],
"app_id": "A111",
"date_updated": 1659054991,
},
"inputs": {"stringToReverse": "hello"},
"function_execution_id": "Fx111",
"event_ts": "1659055013.509853",
"bot_access_token": "xwfp-valid",
},
"type": "event_callback",
"event_id": "Ev111",
"event_time": 1659055013,
"authed_users": ["W111"],
}
def reverse(body, event, context, client, complete, inputs):
assert body == function_body
assert event == function_body["event"]
assert inputs == function_body["event"]["inputs"]
assert context.function_execution_id == "Fx111"
assert complete.function_execution_id == "Fx111"
assert context.function_bot_access_token == "xwfp-valid"
assert context.client.token == "xwfp-valid"
assert client.token == "xwfp-valid"
assert complete.client.token == "xwfp-valid"
assert complete.has_been_called() is False
complete(
outputs={"reverseString": "olleh"},
)
assert complete.has_been_called() is True
def reverse_error(body, event, fail):
assert body == function_body
assert event == function_body["event"]
assert fail.function_execution_id == "Fx111"
assert fail.has_been_called() is False
fail(error="there was an error")
assert fail.has_been_called() is True
def complete_it(body, event, complete):
assert body == function_body
assert event == function_body["event"]
complete(outputs={})
def just_ack(ack, body, event):
assert body == function_body
assert event == function_body["event"]
ack()
def just_no_ack(body, event):
assert body == function_body
assert event == function_body["event"]
You can’t perform that action at this time.
