|
| 1 | +import asyncio |
| 2 | +from ssl import SSLContext |
| 3 | + |
1 | 4 | import pytest |
2 | 5 | from slack_sdk import WebClient |
3 | 6 | from slack_sdk.oauth.installation_store import FileInstallationStore |
4 | 7 | from slack_sdk.oauth.state_store import FileOAuthStateStore |
| 8 | +from slack_sdk.web.async_client import AsyncWebClient |
5 | 9 |
|
6 | 10 | from slack_bolt.async_app import AsyncApp |
7 | 11 | from slack_bolt.authorization import AuthorizeResult |
| 12 | +from slack_bolt.context.async_context import AsyncBoltContext |
8 | 13 | from slack_bolt.error import BoltError |
9 | 14 | from slack_bolt.oauth.async_oauth_flow import AsyncOAuthFlow |
10 | 15 | from slack_bolt.oauth.async_oauth_settings import AsyncOAuthSettings |
| 16 | +from slack_bolt.request.async_request import AsyncBoltRequest |
| 17 | +from tests.mock_web_api_server import ( |
| 18 | + setup_mock_web_api_server, |
| 19 | + cleanup_mock_web_api_server, |
| 20 | +) |
11 | 21 | from tests.utils import remove_os_env_temporarily, restore_os_env |
12 | 22 |
|
13 | 23 |
|
14 | 24 | class TestAsyncApp: |
| 25 | + signing_secret = "secret" |
| 26 | + valid_token = "xoxb-valid" |
| 27 | + mock_api_server_base_url = "http://localhost:8888" |
| 28 | + |
| 29 | + @pytest.fixture |
| 30 | + def event_loop(self): |
| 31 | + old_os_env = remove_os_env_temporarily() |
| 32 | + try: |
| 33 | + setup_mock_web_api_server(self) |
| 34 | + loop = asyncio.get_event_loop() |
| 35 | + yield loop |
| 36 | + loop.close() |
| 37 | + cleanup_mock_web_api_server(self) |
| 38 | + finally: |
| 39 | + restore_os_env(old_os_env) |
| 40 | + |
15 | 41 | def setup_method(self): |
16 | 42 | self.old_os_env = remove_os_env_temporarily() |
17 | 43 |
|
@@ -163,3 +189,61 @@ def test_installation_store_conflicts(self): |
163 | 189 | installation_store=store1, |
164 | 190 | ) |
165 | 191 | assert app.installation_store is store1 |
| 192 | + |
| 193 | + @pytest.mark.asyncio |
| 194 | + async def test_proxy_ssl_for_respond(self): |
| 195 | + ssl = SSLContext() |
| 196 | + web_client = AsyncWebClient( |
| 197 | + token=self.valid_token, |
| 198 | + base_url=self.mock_api_server_base_url, |
| 199 | + proxy="http://proxy-host:9000/", |
| 200 | + ssl=ssl, |
| 201 | + ) |
| 202 | + |
| 203 | + async def my_authorize(): |
| 204 | + return AuthorizeResult( |
| 205 | + enterprise_id="E111", |
| 206 | + team_id="T111", |
| 207 | + ) |
| 208 | + |
| 209 | + app = AsyncApp( |
| 210 | + signing_secret="valid", |
| 211 | + client=web_client, |
| 212 | + authorize=my_authorize, |
| 213 | + ) |
| 214 | + |
| 215 | + event_body = { |
| 216 | + "token": "verification_token", |
| 217 | + "team_id": "T111", |
| 218 | + "enterprise_id": "E111", |
| 219 | + "api_app_id": "A111", |
| 220 | + "event": { |
| 221 | + "client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63", |
| 222 | + "type": "app_mention", |
| 223 | + "text": "<@W111> Hi there!", |
| 224 | + "user": "W222", |
| 225 | + "ts": "1595926230.009600", |
| 226 | + "team": "T111", |
| 227 | + "channel": "C111", |
| 228 | + "event_ts": "1595926230.009600", |
| 229 | + }, |
| 230 | + "type": "event_callback", |
| 231 | + "event_id": "Ev111", |
| 232 | + "event_time": 1595926230, |
| 233 | + } |
| 234 | + |
| 235 | + result = {"called": False} |
| 236 | + |
| 237 | + @app.event("app_mention") |
| 238 | + async def handle(context: AsyncBoltContext, respond): |
| 239 | + assert context.respond.proxy == "http://proxy-host:9000/" |
| 240 | + assert context.respond.ssl == ssl |
| 241 | + assert respond.proxy == "http://proxy-host:9000/" |
| 242 | + assert respond.ssl == ssl |
| 243 | + result["called"] = True |
| 244 | + |
| 245 | + req = AsyncBoltRequest(body=event_body, headers={}, mode="socket_mode") |
| 246 | + response = await app.async_dispatch(req) |
| 247 | + assert response.status == 200 |
| 248 | + await asyncio.sleep(0.5) # wait a bit after auto ack() |
| 249 | + assert result["called"] is True |
0 commit comments