|
| 1 | +import json |
| 2 | +from time import time |
| 3 | +from urllib.parse import quote |
| 4 | + |
| 5 | +from slack_sdk.signature import SignatureVerifier |
| 6 | +from slack_sdk.web.async_client import AsyncWebClient |
| 7 | +from tornado.httpclient import HTTPRequest, HTTPResponse |
| 8 | +from tornado.testing import AsyncHTTPTestCase, gen_test |
| 9 | +from tornado.web import Application |
| 10 | + |
| 11 | +from slack_bolt.adapter.tornado.async_handler import AsyncSlackEventsHandler |
| 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, |
| 17 | +) |
| 18 | +from tests.utils import remove_os_env_temporarily, restore_os_env |
| 19 | + |
| 20 | +signing_secret = "secret" |
| 21 | +valid_token = "xoxb-valid" |
| 22 | +mock_api_server_base_url = "http://localhost:8888" |
| 23 | + |
| 24 | + |
| 25 | +async def event_handler(): |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +async def shortcut_handler(ack): |
| 30 | + await ack() |
| 31 | + |
| 32 | + |
| 33 | +async def command_handler(ack): |
| 34 | + await ack() |
| 35 | + |
| 36 | + |
| 37 | +class TestTornado(AsyncHTTPTestCase): |
| 38 | + signature_verifier = SignatureVerifier(signing_secret) |
| 39 | + |
| 40 | + def setUp(self): |
| 41 | + self.old_os_env = remove_os_env_temporarily() |
| 42 | + setup_mock_web_api_server(self) |
| 43 | + |
| 44 | + web_client = AsyncWebClient( |
| 45 | + token=valid_token, |
| 46 | + base_url=mock_api_server_base_url, |
| 47 | + ) |
| 48 | + self.app = AsyncApp( |
| 49 | + client=web_client, |
| 50 | + signing_secret=signing_secret, |
| 51 | + ) |
| 52 | + self.app.event("app_mention")(event_handler) |
| 53 | + self.app.shortcut("test-shortcut")(shortcut_handler) |
| 54 | + self.app.command("/hello-world")(command_handler) |
| 55 | + |
| 56 | + AsyncHTTPTestCase.setUp(self) |
| 57 | + |
| 58 | + def tearDown(self): |
| 59 | + AsyncHTTPTestCase.tearDown(self) |
| 60 | + cleanup_mock_web_api_server(self) |
| 61 | + restore_os_env(self.old_os_env) |
| 62 | + |
| 63 | + def get_app(self): |
| 64 | + return Application([("/slack/events", AsyncSlackEventsHandler, dict(app=self.app))]) |
| 65 | + |
| 66 | + def generate_signature(self, body: str, timestamp: str): |
| 67 | + return self.signature_verifier.generate_signature( |
| 68 | + body=body, |
| 69 | + timestamp=timestamp, |
| 70 | + ) |
| 71 | + |
| 72 | + def build_headers(self, timestamp: str, body: str): |
| 73 | + content_type = "application/json" if body.startswith("{") else "application/x-www-form-urlencoded" |
| 74 | + return { |
| 75 | + "content-type": content_type, |
| 76 | + "x-slack-signature": self.generate_signature(body, timestamp), |
| 77 | + "x-slack-request-timestamp": timestamp, |
| 78 | + } |
| 79 | + |
| 80 | + @gen_test |
| 81 | + async def test_events(self): |
| 82 | + input = { |
| 83 | + "token": "verification_token", |
| 84 | + "team_id": "T111", |
| 85 | + "enterprise_id": "E111", |
| 86 | + "api_app_id": "A111", |
| 87 | + "event": { |
| 88 | + "client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63", |
| 89 | + "type": "app_mention", |
| 90 | + "text": "<@W111> Hi there!", |
| 91 | + "user": "W222", |
| 92 | + "ts": "1595926230.009600", |
| 93 | + "team": "T111", |
| 94 | + "channel": "C111", |
| 95 | + "event_ts": "1595926230.009600", |
| 96 | + }, |
| 97 | + "type": "event_callback", |
| 98 | + "event_id": "Ev111", |
| 99 | + "event_time": 1595926230, |
| 100 | + "authed_users": ["W111"], |
| 101 | + } |
| 102 | + timestamp, body = str(int(time())), json.dumps(input) |
| 103 | + |
| 104 | + request = HTTPRequest( |
| 105 | + url=self.get_url("/slack/events"), |
| 106 | + method="POST", |
| 107 | + body=body, |
| 108 | + headers=self.build_headers(timestamp, body), |
| 109 | + ) |
| 110 | + response: HTTPResponse = await self.http_client.fetch(request) |
| 111 | + assert response.code == 200 |
| 112 | + assert_auth_test_count(self, 1) |
| 113 | + |
| 114 | + @gen_test |
| 115 | + async def test_shortcuts(self): |
| 116 | + input = { |
| 117 | + "type": "shortcut", |
| 118 | + "token": "verification_token", |
| 119 | + "action_ts": "111.111", |
| 120 | + "team": { |
| 121 | + "id": "T111", |
| 122 | + "domain": "workspace-domain", |
| 123 | + "enterprise_id": "E111", |
| 124 | + "enterprise_name": "Org Name", |
| 125 | + }, |
| 126 | + "user": {"id": "W111", "username": "primary-owner", "team_id": "T111"}, |
| 127 | + "callback_id": "test-shortcut", |
| 128 | + "trigger_id": "111.111.xxxxxx", |
| 129 | + } |
| 130 | + |
| 131 | + timestamp, body = str(int(time())), f"payload={quote(json.dumps(input))}" |
| 132 | + |
| 133 | + request = HTTPRequest( |
| 134 | + url=self.get_url("/slack/events"), |
| 135 | + method="POST", |
| 136 | + body=body, |
| 137 | + headers=self.build_headers(timestamp, body), |
| 138 | + ) |
| 139 | + response: HTTPResponse = await self.http_client.fetch(request) |
| 140 | + assert response.code == 200 |
| 141 | + assert_auth_test_count(self, 1) |
| 142 | + |
| 143 | + @gen_test |
| 144 | + async def test_commands(self): |
| 145 | + input = ( |
| 146 | + "token=verification_token" |
| 147 | + "&team_id=T111" |
| 148 | + "&team_domain=test-domain" |
| 149 | + "&channel_id=C111" |
| 150 | + "&channel_name=random" |
| 151 | + "&user_id=W111" |
| 152 | + "&user_name=primary-owner" |
| 153 | + "&command=%2Fhello-world" |
| 154 | + "&text=Hi" |
| 155 | + "&enterprise_id=E111" |
| 156 | + "&enterprise_name=Org+Name" |
| 157 | + "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx" |
| 158 | + "&trigger_id=111.111.xxx" |
| 159 | + ) |
| 160 | + timestamp, body = str(int(time())), input |
| 161 | + |
| 162 | + request = HTTPRequest( |
| 163 | + url=self.get_url("/slack/events"), |
| 164 | + method="POST", |
| 165 | + body=body, |
| 166 | + headers=self.build_headers(timestamp, body), |
| 167 | + ) |
| 168 | + response: HTTPResponse = await self.http_client.fetch(request) |
| 169 | + assert response.code == 200 |
| 170 | + assert_auth_test_count(self, 1) |
0 commit comments