forked from slackapi/bolt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_workflow_steps.py
More file actions
400 lines (370 loc) · 13.8 KB
/
test_workflow_steps.py
File metadata and controls
400 lines (370 loc) · 13.8 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import json
import time as time_module
from time import time
from urllib.parse import quote
from slack_sdk.signature import SignatureVerifier
from slack_sdk.web import WebClient, SlackResponse
from slack_bolt import App, BoltRequest, Ack
from slack_bolt.workflows.step import Complete, Fail, Update, Configure
from tests.mock_web_api_server import (
setup_mock_web_api_server,
cleanup_mock_web_api_server,
)
from tests.utils import remove_os_env_temporarily, restore_os_env
class TestWorkflowSteps:
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)
self.app = App(client=self.web_client, signing_secret=self.signing_secret)
self.app.step(callback_id="copy_review", edit=edit, save=save, execute=execute)
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 test_edit(self):
timestamp, body = str(int(time())), f"payload={quote(json.dumps(edit_payload))}"
headers = {
"content-type": ["application/x-www-form-urlencoded"],
"x-slack-signature": [self.generate_signature(body, timestamp)],
"x-slack-request-timestamp": [timestamp],
}
request: BoltRequest = BoltRequest(body=body, headers=headers)
response = self.app.dispatch(request)
assert response.status == 200
assert self.mock_received_requests["/auth.test"] == 1
self.app = App(client=self.web_client, signing_secret=self.signing_secret)
self.app.step(
callback_id="copy_review___", edit=edit, save=save, execute=execute
)
response = self.app.dispatch(request)
assert response.status == 404
def test_save(self):
timestamp, body = str(int(time())), f"payload={quote(json.dumps(save_payload))}"
headers = {
"content-type": ["application/x-www-form-urlencoded"],
"x-slack-signature": [self.generate_signature(body, timestamp)],
"x-slack-request-timestamp": [timestamp],
}
request: BoltRequest = BoltRequest(body=body, headers=headers)
response = self.app.dispatch(request)
assert response.status == 200
assert self.mock_received_requests["/auth.test"] == 1
self.app = App(client=self.web_client, signing_secret=self.signing_secret)
self.app.step(
callback_id="copy_review___", edit=edit, save=save, execute=execute
)
response = self.app.dispatch(request)
assert response.status == 404
def test_execute(self):
timestamp, body = str(int(time())), json.dumps(execute_payload)
headers = {
"content-type": ["application/json"],
"x-slack-signature": [self.generate_signature(body, timestamp)],
"x-slack-request-timestamp": [timestamp],
}
request: BoltRequest = BoltRequest(body=body, headers=headers)
response = self.app.dispatch(request)
assert response.status == 200
assert self.mock_received_requests["/auth.test"] == 1
time_module.sleep(0.5)
assert self.mock_received_requests["/workflows.stepCompleted"] == 1
self.app = App(client=self.web_client, signing_secret=self.signing_secret)
self.app.step(
callback_id="copy_review___", edit=edit, save=save, execute=execute
)
response = self.app.dispatch(request)
assert response.status == 404
edit_payload = {
"type": "workflow_step_edit",
"token": "verification-token",
"action_ts": "1601541356.268786",
"team": {
"id": "T111",
"domain": "subdomain",
"enterprise_id": "E111",
"enterprise_name": "Org Name",
},
"user": {"id": "W111", "username": "primary-owner", "team_id": "T111"},
"callback_id": "copy_review",
"trigger_id": "111.222.xxx",
"workflow_step": {
"workflow_id": "12345",
"step_id": "111-222-333-444-555",
"inputs": {
"taskAuthorEmail": {"value": "seratch@example.com"},
"taskDescription": {"value": "This is the task for you!"},
"taskName": {"value": "The important task"},
},
"outputs": [
{"name": "taskName", "type": "text", "label": "Task Name"},
{"name": "taskDescription", "type": "text", "label": "Task Description"},
{"name": "taskAuthorEmail", "type": "text", "label": "Task Author Email"},
],
},
}
save_payload = {
"type": "view_submission",
"team": {
"id": "T111",
"domain": "subdomain",
"enterprise_id": "E111",
"enterprise_name": "Org Name",
},
"user": {
"id": "W111",
"username": "primary-owner",
"name": "primary-owner",
"team_id": "T111",
},
"api_app_id": "A111",
"token": "verification-token",
"trigger_id": "111.222.xxx",
"view": {
"id": "V111",
"team_id": "T111",
"type": "workflow_step",
"blocks": [
{
"type": "section",
"block_id": "intro-section",
"text": {
"type": "plain_text",
"text": "Create a task in one of the listed projects. The link to the task and other details will be available as variable data in later steps.",
},
},
{
"type": "input",
"block_id": "task_name_input",
"label": {"type": "plain_text", "text": "Task name"},
"optional": False,
"element": {
"type": "plain_text_input",
"action_id": "task_name",
"placeholder": {"type": "plain_text", "text": "Write a task name"},
},
},
{
"type": "input",
"block_id": "task_description_input",
"label": {"type": "plain_text", "text": "Task description"},
"optional": False,
"element": {
"type": "plain_text_input",
"action_id": "task_description",
"placeholder": {
"type": "plain_text",
"text": "Write a description for your task",
},
},
},
{
"type": "input",
"block_id": "task_author_input",
"label": {"type": "plain_text", "text": "Task author"},
"optional": False,
"element": {
"type": "plain_text_input",
"action_id": "task_author",
"placeholder": {"type": "plain_text", "text": "Write a task name"},
},
},
],
"private_metadata": "",
"callback_id": "copy_review",
"state": {
"values": {
"task_name_input": {
"task_name": {
"type": "plain_text_input",
"value": "The important task",
}
},
"task_description_input": {
"task_description": {
"type": "plain_text_input",
"value": "This is the task for you!",
}
},
"task_author_input": {
"task_author": {
"type": "plain_text_input",
"value": "seratch@example.com",
}
},
}
},
"hash": "111.zzz",
"submit_disabled": False,
"app_id": "A111",
"external_id": "",
"app_installed_team_id": "T111",
"bot_id": "B111",
},
"response_urls": [],
"workflow_step": {
"workflow_step_edit_id": "111.222.zzz",
"workflow_id": "12345",
"step_id": "111-222-333-444-555",
},
}
execute_payload = {
"token": "verification-token",
"team_id": "T111",
"enterprise_id": "E111",
"api_app_id": "A111",
"event": {
"type": "workflow_step_execute",
"callback_id": "copy_review",
"workflow_step": {
"workflow_step_execute_id": "zzz-execution",
"workflow_id": "12345",
"workflow_instance_id": "11111",
"step_id": "111-222-333-444-555",
"inputs": {
"taskAuthorEmail": {"value": "ksera@slack-corp.com"},
"taskDescription": {"value": "sdfsdf"},
"taskName": {"value": "a"},
},
"outputs": [
{"name": "taskName", "type": "text", "label": "Task Name"},
{
"name": "taskDescription",
"type": "text",
"label": "Task Description",
},
{
"name": "taskAuthorEmail",
"type": "text",
"label": "Task Author Email",
},
],
},
"event_ts": "1601541373.225894",
},
"type": "event_callback",
"event_id": "Ev111",
"event_time": 1601541373,
}
# https://api.slack.com/tutorials/workflow-builder-steps
def edit(ack: Ack, step, configure: Configure):
assert step is not None
ack()
configure(
blocks=[
{
"type": "section",
"block_id": "intro-section",
"text": {
"type": "plain_text",
"text": "Create a task in one of the listed projects. The link to the task and other details will be available as variable data in later steps.",
},
},
{
"type": "input",
"block_id": "task_name_input",
"element": {
"type": "plain_text_input",
"action_id": "task_name",
"placeholder": {"type": "plain_text", "text": "Write a task name",},
},
"label": {"type": "plain_text", "text": "Task name"},
},
{
"type": "input",
"block_id": "task_description_input",
"element": {
"type": "plain_text_input",
"action_id": "task_description",
"placeholder": {
"type": "plain_text",
"text": "Write a description for your task",
},
},
"label": {"type": "plain_text", "text": "Task description"},
},
{
"type": "input",
"block_id": "task_author_input",
"element": {
"type": "plain_text_input",
"action_id": "task_author",
"placeholder": {"type": "plain_text", "text": "Write a task name",},
},
"label": {"type": "plain_text", "text": "Task author"},
},
]
)
def save(ack: Ack, step: dict, view: dict, update: Update):
assert step is not None
assert view is not None
state_values = view["state"]["values"]
update(
inputs={
"taskName": {
"value": state_values["task_name_input"]["task_name"]["value"],
},
"taskDescription": {
"value": state_values["task_description_input"]["task_description"][
"value"
],
},
"taskAuthorEmail": {
"value": state_values["task_author_input"]["task_author"]["value"],
},
},
outputs=[
{"name": "taskName", "type": "text", "label": "Task Name",},
{"name": "taskDescription", "type": "text", "label": "Task Description",},
{"name": "taskAuthorEmail", "type": "text", "label": "Task Author Email",},
],
)
ack()
pseudo_database = {}
def execute(step: dict, client: WebClient, complete: Complete, fail: Fail):
assert step is not None
try:
complete(
outputs={
"taskName": step["inputs"]["taskName"]["value"],
"taskDescription": step["inputs"]["taskDescription"]["value"],
"taskAuthorEmail": step["inputs"]["taskAuthorEmail"]["value"],
}
)
user: SlackResponse = client.users_lookupByEmail(
email=step["inputs"]["taskAuthorEmail"]["value"]
)
user_id = user["user"]["id"]
new_task = {
"task_name": step["inputs"]["taskName"]["value"],
"task_description": step["inputs"]["taskDescription"]["value"],
}
tasks = pseudo_database.get(user_id, [])
tasks.append(new_task)
pseudo_database[user_id] = tasks
blocks = []
for task in tasks:
blocks.append(
{
"type": "section",
"text": {"type": "plain_text", "text": task["task_name"]},
}
)
blocks.append({"type": "divider"})
client.views_publish(
user_id=user_id,
view={
"type": "home",
"title": {"type": "plain_text", "text": "Your tasks!"},
"blocks": blocks,
},
)
except Exception as err:
fail(error={"message": f"Something wrong! {err}"})