|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +from time import time |
| 4 | +from urllib.parse import quote |
| 5 | + |
| 6 | +import pytest |
| 7 | +from slack_sdk.web.async_client import AsyncWebClient |
| 8 | +from slack_sdk.http_retry.builtin_async_handlers import AsyncConnectionErrorRetryHandler, AsyncRateLimitErrorRetryHandler |
| 9 | +from slack_sdk.signature import SignatureVerifier |
| 10 | + |
| 11 | +from slack_bolt.request.async_request import AsyncBoltRequest |
| 12 | +from slack_bolt.async_app import AsyncApp |
| 13 | +from tests.mock_web_api_server import ( |
| 14 | + setup_mock_web_api_server, |
| 15 | + cleanup_mock_web_api_server, |
| 16 | + assert_auth_test_count_async, |
| 17 | +) |
| 18 | +from tests.utils import remove_os_env_temporarily, restore_os_env |
| 19 | + |
| 20 | + |
| 21 | +class TestWebClientCustomization: |
| 22 | + valid_token = "xoxb-valid" |
| 23 | + signing_secret = "secret" |
| 24 | + mock_api_server_base_url = "http://localhost:8888" |
| 25 | + signature_verifier = SignatureVerifier(signing_secret) |
| 26 | + web_client = AsyncWebClient( |
| 27 | + token=valid_token, |
| 28 | + base_url=mock_api_server_base_url, |
| 29 | + ) |
| 30 | + |
| 31 | + @pytest.fixture |
| 32 | + def event_loop(self): |
| 33 | + old_os_env = remove_os_env_temporarily() |
| 34 | + try: |
| 35 | + setup_mock_web_api_server(self) |
| 36 | + loop = asyncio.get_event_loop() |
| 37 | + yield loop |
| 38 | + loop.close() |
| 39 | + cleanup_mock_web_api_server(self) |
| 40 | + finally: |
| 41 | + restore_os_env(old_os_env) |
| 42 | + |
| 43 | + def generate_signature(self, body: str, timestamp: str): |
| 44 | + return self.signature_verifier.generate_signature( |
| 45 | + body=body, |
| 46 | + timestamp=timestamp, |
| 47 | + ) |
| 48 | + |
| 49 | + def build_headers(self, timestamp: str, body: str): |
| 50 | + return { |
| 51 | + "content-type": ["application/x-www-form-urlencoded"], |
| 52 | + "x-slack-signature": [self.generate_signature(body, timestamp)], |
| 53 | + "x-slack-request-timestamp": [timestamp], |
| 54 | + } |
| 55 | + |
| 56 | + def build_valid_request(self) -> AsyncBoltRequest: |
| 57 | + timestamp = str(int(time())) |
| 58 | + return AsyncBoltRequest(body=raw_body, headers=self.build_headers(timestamp, raw_body)) |
| 59 | + |
| 60 | + @pytest.mark.asyncio |
| 61 | + async def test_web_client_customization(self): |
| 62 | + self.web_client.retry_handlers = [ |
| 63 | + AsyncConnectionErrorRetryHandler, |
| 64 | + AsyncRateLimitErrorRetryHandler, |
| 65 | + ] |
| 66 | + app = AsyncApp( |
| 67 | + client=self.web_client, |
| 68 | + signing_secret=self.signing_secret, |
| 69 | + ) |
| 70 | + |
| 71 | + @app.action("a") |
| 72 | + async def listener(ack, client): |
| 73 | + assert len(client.retry_handlers) == 2 |
| 74 | + await ack() |
| 75 | + |
| 76 | + request = self.build_valid_request() |
| 77 | + response = await app.async_dispatch(request) |
| 78 | + assert response.status == 200 |
| 79 | + assert response.body == "" |
| 80 | + await assert_auth_test_count_async(self, 1) |
| 81 | + |
| 82 | + |
| 83 | +block_actions_body = { |
| 84 | + "type": "block_actions", |
| 85 | + "user": { |
| 86 | + "id": "W99999", |
| 87 | + "username": "primary-owner", |
| 88 | + "name": "primary-owner", |
| 89 | + "team_id": "T111", |
| 90 | + }, |
| 91 | + "api_app_id": "A111", |
| 92 | + "token": "verification_token", |
| 93 | + "container": { |
| 94 | + "type": "message", |
| 95 | + "message_ts": "111.222", |
| 96 | + "channel_id": "C111", |
| 97 | + "is_ephemeral": True, |
| 98 | + }, |
| 99 | + "trigger_id": "111.222.valid", |
| 100 | + "team": { |
| 101 | + "id": "T111", |
| 102 | + "domain": "workspace-domain", |
| 103 | + "enterprise_id": "E111", |
| 104 | + "enterprise_name": "Sandbox Org", |
| 105 | + }, |
| 106 | + "channel": {"id": "C111", "name": "test-channel"}, |
| 107 | + "response_url": "https://hooks.slack.com/actions/T111/111/random-value", |
| 108 | + "actions": [ |
| 109 | + { |
| 110 | + "action_id": "a", |
| 111 | + "block_id": "b", |
| 112 | + "text": {"type": "plain_text", "text": "Button", "emoji": True}, |
| 113 | + "value": "click_me_123", |
| 114 | + "type": "button", |
| 115 | + "action_ts": "1596530385.194939", |
| 116 | + } |
| 117 | + ], |
| 118 | +} |
| 119 | + |
| 120 | +raw_body = f"payload={quote(json.dumps(block_actions_body))}" |
0 commit comments