forked from slackapi/bolt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dialogs.py
More file actions
404 lines (341 loc) · 15.3 KB
/
test_dialogs.py
File metadata and controls
404 lines (341 loc) · 15.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
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
401
402
403
404
import json
from time import time
from urllib.parse import quote
from slack_sdk.signature import SignatureVerifier
from slack_sdk.web import WebClient
from slack_bolt.app import App
from slack_bolt.request import BoltRequest
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 TestAttachmentActions:
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/x-www-form-urlencoded"],
"x-slack-signature": [self.generate_signature(body, timestamp)],
"x-slack-request-timestamp": [timestamp],
}
def build_valid_request(self, body) -> BoltRequest:
timestamp = str(int(time()))
return BoltRequest(body=body, headers=self.build_headers(timestamp, body))
def test_mock_server_is_running(self):
resp = self.web_client.api_test()
assert resp != None
def test_success_without_type(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
app.options("dialog-callback-id")(handle_suggestion)
app.action("dialog-callback-id")(handle_submission_cancellation)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body != ""
assert response.headers["content-type"][0] == "application/json;charset=utf-8"
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(submission_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(cancellation_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
def test_success(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
app.options({"type": "dialog_suggestion", "callback_id": "dialog-callback-id"})(
handle_suggestion
)
app.action({"type": "dialog_submission", "callback_id": "dialog-callback-id"})(
handle_submission
)
app.action(
{"type": "dialog_cancellation", "callback_id": "dialog-callback-id"}
)(handle_cancellation)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body != ""
assert response.headers["content-type"][0] == "application/json;charset=utf-8"
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(submission_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(cancellation_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
def test_success_2(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
app.dialog_suggestion("dialog-callback-id")(handle_suggestion)
app.dialog_submission("dialog-callback-id")(handle_submission)
app.dialog_cancellation("dialog-callback-id")(handle_cancellation)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body != ""
assert response.headers["content-type"][0] == "application/json;charset=utf-8"
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(submission_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(cancellation_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
def test_process_before_response(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
process_before_response=True,
)
app.options({"type": "dialog_suggestion", "callback_id": "dialog-callback-id"})(
handle_suggestion
)
app.action({"type": "dialog_submission", "callback_id": "dialog-callback-id"})(
handle_submission
)
app.action(
{"type": "dialog_cancellation", "callback_id": "dialog-callback-id"}
)(handle_cancellation)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body != ""
assert response.headers["content-type"][0] == "application/json;charset=utf-8"
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(submission_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(cancellation_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
def test_process_before_response_2(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
process_before_response=True,
)
app.dialog_suggestion("dialog-callback-id")(handle_suggestion)
app.dialog_submission("dialog-callback-id")(handle_submission)
app.dialog_cancellation("dialog-callback-id")(handle_cancellation)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == json.dumps(options_response)
assert response.headers["content-type"][0] == "application/json;charset=utf-8"
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(submission_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
request = self.build_valid_request(cancellation_raw_body)
response = app.dispatch(request)
assert response.status == 200
assert response.body == ""
assert self.mock_received_requests["/auth.test"] == 1
def test_suggestion_failure_without_type(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.options("dialog-callback-iddddd")(handle_suggestion)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
def test_suggestion_failure(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.dialog_suggestion("dialog-callback-iddddd")(handle_suggestion)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
def test_suggestion_failure_2(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.options(
{"type": "dialog_suggestion", "callback_id": "dialog-callback-iddddd"}
)(handle_suggestion)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
def test_submission_failure_without_type(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.action("dialog-callback-iddddd")(handle_submission)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
def test_submission_failure(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.dialog_submission("dialog-callback-iddddd")(handle_submission)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
def test_submission_failure_2(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.action(
{"type": "dialog_submission", "callback_id": "dialog-callback-iddddd"}
)(handle_submission)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
def test_cancellation_failure_without_type(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.action("dialog-callback-iddddd")(handle_cancellation)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
def test_cancellation_failure(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.dialog_cancellation("dialog-callback-iddddd")(handle_cancellation)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
def test_cancellation_failure_2(self):
app = App(client=self.web_client, signing_secret=self.signing_secret,)
request = self.build_valid_request(suggestion_raw_body)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
app.action(
{"type": "dialog_cancellation", "callback_id": "dialog-callback-iddddd"}
)(handle_cancellation)
response = app.dispatch(request)
assert response.status == 404
assert self.mock_received_requests["/auth.test"] == 1
suggestion_body = {
"type": "dialog_suggestion",
"token": "verification_token",
"action_ts": "1596603332.676855",
"team": {
"id": "T111",
"domain": "workspace-domain",
"enterprise_id": "E111",
"enterprise_name": "Sandbox Org",
},
"user": {"id": "W111", "name": "primary-owner", "team_id": "T111"},
"channel": {"id": "C111", "name": "test-channel"},
"name": "types",
"value": "search keyword",
"callback_id": "dialog-callback-id",
"state": "Limo",
}
submission_body = {
"type": "dialog_submission",
"token": "verification_token",
"action_ts": "1596603334.328193",
"team": {
"id": "T111",
"domain": "workspace-domain",
"enterprise_id": "E111",
"enterprise_name": "Sandbox Org",
},
"user": {"id": "W111", "name": "primary-owner", "team_id": "T111"},
"channel": {"id": "C111", "name": "test-channel"},
"submission": {
"loc_origin": "Tokyo",
"loc_destination": "Osaka",
"types": "FE-459",
},
"callback_id": "dialog-callback-id",
"response_url": "https://hooks.slack.com/app/T111/111/xxx",
"state": "Limo",
}
cancellation_body = {
"type": "dialog_cancellation",
"token": "verification_token",
"action_ts": "1596603453.047897",
"team": {
"id": "T111",
"domain": "workspace-domain",
"enterprise_id": "E111",
"enterprise_name": "Sandbox Org",
},
"user": {"id": "W111", "name": "primary-owner", "team_id": "T111"},
"channel": {"id": "C111", "name": "test-channel"},
"callback_id": "dialog-callback-id",
"response_url": "https://hooks.slack.com/app/T111/111/xxx",
"state": "Limo",
}
suggestion_raw_body = f"payload={quote(json.dumps(suggestion_body))}"
submission_raw_body = f"payload={quote(json.dumps(submission_body))}"
cancellation_raw_body = f"payload={quote(json.dumps(cancellation_body))}"
def handle_submission(ack):
ack()
options_response = {
"options": [
{
"label": "[UXD-342] The button color should be artichoke green, not jalapeño",
"value": "UXD-342",
},
{"label": "[FE-459] Remove the marquee tag", "value": "FE-459"},
{"label": "[FE-238] Too many shades of gray in master CSS", "value": "FE-238",},
]
}
def handle_suggestion(ack, body, payload, options):
assert body == options
assert payload == options
ack(options_response)
def handle_cancellation(ack, body, payload, action):
assert body == action
assert payload == action
ack()
def handle_submission_cancellation(ack, body, payload, action):
assert body == action
assert payload == action
ack()