-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathtest_app.py
More file actions
345 lines (293 loc) · 11.1 KB
/
Copy pathtest_app.py
File metadata and controls
345 lines (293 loc) · 11.1 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
import logging
import time
from concurrent.futures import Executor
import ssl
import pytest
from slack_sdk import WebClient
from slack_sdk.oauth.installation_store import FileInstallationStore
from slack_sdk.oauth.state_store import FileOAuthStateStore
from slack_bolt import App, BoltContext, BoltRequest, Say
from slack_bolt.authorization import AuthorizeResult
from slack_bolt.error import BoltError
from slack_bolt.oauth import OAuthFlow
from slack_bolt.oauth.oauth_settings import OAuthSettings
from tests.mock_web_api_server import cleanup_mock_web_api_server, setup_mock_web_api_server
from tests.utils import remove_os_env_temporarily, restore_os_env
class TestApp:
signing_secret = "secret"
valid_token = "xoxb-valid"
mock_api_server_base_url = "http://localhost:8888"
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)
@staticmethod
def handle_app_mention(body, say: Say, payload, event):
assert body["event"] == payload
assert payload == event
say("What's up?")
# --------------------------
# basic tests
# --------------------------
def simple_listener(self, ack):
ack()
def test_listener_registration_error(self):
app = App(signing_secret="valid", client=self.web_client)
with pytest.raises(BoltError):
app.action({"type": "invalid_type", "action_id": "a"})(self.simple_listener)
def test_listener_executor(self):
class TestExecutor(Executor):
"""A executor that does nothing for testing"""
pass
executor = TestExecutor()
app = App(
signing_secret="valid",
client=self.web_client,
listener_executor=executor,
)
assert app.listener_runner.listener_executor == executor
assert app.listener_runner.lazy_listener_runner.executor == executor
# --------------------------
# single team auth
# --------------------------
def test_valid_single_auth(self):
app = App(signing_secret="valid", client=self.web_client)
assert app is not None
def test_token_absence(self):
with pytest.raises(BoltError):
App(signing_secret="valid", token=None)
with pytest.raises(BoltError):
App(signing_secret="valid", token="")
def test_token_verification_enabled_False(self):
App(
signing_secret="valid",
client=self.web_client,
token_verification_enabled=False,
)
App(
signing_secret="valid",
token="xoxb-invalid",
token_verification_enabled=False,
)
assert self.received_requests.get("/auth.test") is None
# --------------------------
# multi teams auth
# --------------------------
def test_valid_multi_auth(self):
app = App(
signing_secret="valid",
oauth_settings=OAuthSettings(client_id="111.222", client_secret="valid"),
)
assert app is not None
def test_valid_multi_auth_oauth_flow(self):
oauth_flow = OAuthFlow(
settings=OAuthSettings(
client_id="111.222",
client_secret="valid",
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
)
)
app = App(signing_secret="valid", oauth_flow=oauth_flow)
assert app is not None
def test_valid_multi_auth_client_id_absence(self):
with pytest.raises(BoltError):
App(
signing_secret="valid",
oauth_settings=OAuthSettings(client_id=None, client_secret="valid"),
)
def test_valid_multi_auth_secret_absence(self):
with pytest.raises(BoltError):
App(
signing_secret="valid",
oauth_settings=OAuthSettings(client_id="111.222", client_secret=None),
)
def test_authorize_conflicts(self):
oauth_settings = OAuthSettings(
client_id="111.222",
client_secret="valid",
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
)
# no error with this
App(signing_secret="valid", oauth_settings=oauth_settings)
def authorize() -> AuthorizeResult:
return AuthorizeResult(enterprise_id="E111", team_id="T111")
with pytest.raises(BoltError):
App(
signing_secret="valid",
authorize=authorize,
oauth_settings=oauth_settings,
)
oauth_flow = OAuthFlow(settings=oauth_settings)
# no error with this
App(signing_secret="valid", oauth_flow=oauth_flow)
with pytest.raises(BoltError):
App(signing_secret="valid", authorize=authorize, oauth_flow=oauth_flow)
def test_installation_store_conflicts(self):
store1 = FileInstallationStore()
store2 = FileInstallationStore()
app = App(
signing_secret="valid",
oauth_settings=OAuthSettings(
client_id="111.222",
client_secret="valid",
installation_store=store1,
),
installation_store=store2,
)
assert app.installation_store is store1
app = App(
signing_secret="valid",
oauth_flow=OAuthFlow(
settings=OAuthSettings(
client_id="111.222",
client_secret="valid",
installation_store=store1,
)
),
installation_store=store2,
)
assert app.installation_store is store1
app = App(
signing_secret="valid",
oauth_flow=OAuthFlow(
settings=OAuthSettings(
client_id="111.222",
client_secret="valid",
)
),
installation_store=store1,
)
assert app.installation_store is store1
def test_none_body(self):
app = App(signing_secret="valid", client=self.web_client)
req = BoltRequest(body=None, headers={}, mode="http")
response = app.dispatch(req)
# request verification failure
assert response.status == 401
assert response.body == '{"error": "invalid request"}'
req = BoltRequest(body=None, headers={}, mode="socket_mode")
response = app.dispatch(req)
# request verification is skipped for Socket Mode
assert response.status == 404
assert response.body == '{"error": "unhandled request"}'
def test_none_body_no_middleware(self):
app = App(
signing_secret="valid",
client=self.web_client,
ssl_check_enabled=False,
ignoring_self_events_enabled=False,
request_verification_enabled=False,
token_verification_enabled=False,
url_verification_enabled=False,
)
req = BoltRequest(body=None, headers={}, mode="http")
response = app.dispatch(req)
assert response.status == 404
assert response.body == '{"error": "unhandled request"}'
req = BoltRequest(body=None, headers={}, mode="socket_mode")
response = app.dispatch(req)
assert response.status == 404
assert response.body == '{"error": "unhandled request"}'
def test_proxy_ssl_for_respond(self):
ssl_context = ssl.create_default_context()
web_client = WebClient(
token=self.valid_token,
base_url=self.mock_api_server_base_url,
proxy="http://proxy-host:9000/",
ssl=ssl_context,
)
app = App(
signing_secret="valid",
client=web_client,
authorize=lambda: AuthorizeResult(
enterprise_id="E111",
team_id="T111",
),
)
result = {"called": False}
@app.event("app_mention")
def handle(context: BoltContext, respond):
assert context.respond.proxy == "http://proxy-host:9000/"
assert context.respond.ssl == ssl_context
assert respond.proxy == "http://proxy-host:9000/"
assert respond.ssl == ssl_context
result["called"] = True
req = BoltRequest(body=app_mention_event_body, headers={}, mode="socket_mode")
response = app.dispatch(req)
assert response.status == 200
assert result["called"] is True
def test_argument_logger_propagation(self):
custom_logger = logging.getLogger(f"{__name__}-{time.time()}-logger-test")
custom_logger.setLevel(logging.INFO)
added_handler = logging.NullHandler()
custom_logger.addHandler(added_handler)
added_filter = logging.Filter()
custom_logger.addFilter(added_filter)
app = App(
signing_secret="valid",
client=WebClient(
token=self.valid_token,
base_url=self.mock_api_server_base_url,
),
authorize=lambda: AuthorizeResult(
enterprise_id="E111",
team_id="T111",
),
logger=custom_logger,
)
result = {"called": False}
def _verify_logger(logger: logging.Logger):
assert logger.level == custom_logger.level
assert len(logger.handlers) == len(custom_logger.handlers)
assert logger.handlers[-1] == custom_logger.handlers[-1]
assert len(logger.filters) == len(custom_logger.filters)
assert logger.filters[-1] == custom_logger.filters[-1]
@app.use
def global_middleware(logger, next):
_verify_logger(logger)
next()
def listener_middleware(logger, next):
_verify_logger(logger)
next()
def listener_matcher(logger):
_verify_logger(logger)
return True
@app.event(
"app_mention",
middleware=[listener_middleware],
matchers=[listener_matcher],
)
def handle(logger: logging.Logger):
_verify_logger(logger)
result["called"] = True
req = BoltRequest(body=app_mention_event_body, headers={}, mode="socket_mode")
response = app.dispatch(req)
assert response.status == 200
assert result["called"] is True
app_mention_event_body = {
"token": "verification_token",
"team_id": "T111",
"enterprise_id": "E111",
"api_app_id": "A111",
"event": {
"client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63",
"type": "app_mention",
"text": "<@W111> Hi there!",
"user": "W222",
"ts": "1595926230.009600",
"team": "T111",
"channel": "C111",
"event_ts": "1595926230.009600",
},
"type": "event_callback",
"event_id": "Ev111",
"event_time": 1595926230,
}