|
| 1 | +import json |
| 2 | +from time import time |
| 3 | + |
| 4 | +import cherrypy |
| 5 | +from cherrypy.test import helper |
| 6 | +from slack_sdk.signature import SignatureVerifier |
| 7 | +from slack_sdk.web import WebClient |
| 8 | + |
| 9 | +from slack_bolt.adapter.cherrypy import SlackRequestHandler |
| 10 | +from slack_bolt.app import App |
| 11 | +from tests.mock_web_api_server import ( |
| 12 | + setup_mock_web_api_server, |
| 13 | + cleanup_mock_web_api_server, |
| 14 | +) |
| 15 | +from tests.utils import remove_os_env_temporarily, restore_os_env |
| 16 | + |
| 17 | + |
| 18 | +class TestCherryPy(helper.CPWebCase): |
| 19 | + helper.CPWebCase.interactive = False |
| 20 | + signing_secret = "secret" |
| 21 | + signature_verifier = SignatureVerifier(signing_secret) |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def setup_server(cls): |
| 25 | + cls.old_os_env = remove_os_env_temporarily() |
| 26 | + setup_mock_web_api_server(cls) |
| 27 | + |
| 28 | + signing_secret = "secret" |
| 29 | + valid_token = "xoxb-valid" |
| 30 | + mock_api_server_base_url = "http://localhost:8888" |
| 31 | + web_client = WebClient(token=valid_token, base_url=mock_api_server_base_url,) |
| 32 | + app = App(client=web_client, signing_secret=signing_secret,) |
| 33 | + |
| 34 | + def event_handler(): |
| 35 | + pass |
| 36 | + |
| 37 | + def shortcut_handler(ack): |
| 38 | + ack() |
| 39 | + |
| 40 | + def command_handler(ack): |
| 41 | + ack() |
| 42 | + |
| 43 | + app.event("app_mention")(event_handler) |
| 44 | + app.shortcut("test-shortcut")(shortcut_handler) |
| 45 | + app.command("/hello-world")(command_handler) |
| 46 | + |
| 47 | + handler = SlackRequestHandler(app) |
| 48 | + |
| 49 | + class SlackApp(object): |
| 50 | + @cherrypy.expose |
| 51 | + @cherrypy.tools.slack_in() |
| 52 | + def events(self, **kwargs): |
| 53 | + return handler.handle() |
| 54 | + |
| 55 | + cherrypy.tree.mount(SlackApp(), "/slack") |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def teardown_class(cls): |
| 59 | + cls.supervisor.stop() |
| 60 | + cleanup_mock_web_api_server(cls) |
| 61 | + restore_os_env(cls.old_os_env) |
| 62 | + |
| 63 | + def generate_signature(self, body: str, timestamp: str): |
| 64 | + return self.signature_verifier.generate_signature( |
| 65 | + body=body, timestamp=timestamp, |
| 66 | + ) |
| 67 | + |
| 68 | + def build_headers(self, timestamp: str, body: str): |
| 69 | + return [ |
| 70 | + ("content-length", str(len(body))), |
| 71 | + ("x-slack-signature", self.generate_signature(body, timestamp)), |
| 72 | + ("x-slack-request-timestamp", timestamp), |
| 73 | + ] |
| 74 | + |
| 75 | + def test_events(self): |
| 76 | + payload = { |
| 77 | + "token": "verification_token", |
| 78 | + "team_id": "T111", |
| 79 | + "enterprise_id": "E111", |
| 80 | + "api_app_id": "A111", |
| 81 | + "event": { |
| 82 | + "client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63", |
| 83 | + "type": "app_mention", |
| 84 | + "text": "<@W111> Hi there!", |
| 85 | + "user": "W222", |
| 86 | + "ts": "1595926230.009600", |
| 87 | + "team": "T111", |
| 88 | + "channel": "C111", |
| 89 | + "event_ts": "1595926230.009600", |
| 90 | + }, |
| 91 | + "type": "event_callback", |
| 92 | + "event_id": "Ev111", |
| 93 | + "event_time": 1595926230, |
| 94 | + "authed_users": ["W111"], |
| 95 | + } |
| 96 | + timestamp, body = str(int(time())), json.dumps(payload) |
| 97 | + cherrypy.request.process_request_body = True |
| 98 | + self.getPage( |
| 99 | + "/slack/events", |
| 100 | + method="POST", |
| 101 | + body=body, |
| 102 | + headers=self.build_headers(timestamp, body), |
| 103 | + ) |
| 104 | + self.assertStatus("200 OK") |
| 105 | + self.assertBody("") |
| 106 | + |
| 107 | + def test_shortcuts(self): |
| 108 | + payload = { |
| 109 | + "type": "shortcut", |
| 110 | + "token": "verification_token", |
| 111 | + "action_ts": "111.111", |
| 112 | + "team": { |
| 113 | + "id": "T111", |
| 114 | + "domain": "workspace-domain", |
| 115 | + "enterprise_id": "E111", |
| 116 | + "enterprise_name": "Org Name", |
| 117 | + }, |
| 118 | + "user": {"id": "W111", "username": "primary-owner", "team_id": "T111"}, |
| 119 | + "callback_id": "test-shortcut", |
| 120 | + "trigger_id": "111.111.xxxxxx", |
| 121 | + } |
| 122 | + |
| 123 | + timestamp, body = str(int(time())), json.dumps(payload) |
| 124 | + cherrypy.request.process_request_body = True |
| 125 | + self.getPage( |
| 126 | + "/slack/events", |
| 127 | + method="POST", |
| 128 | + body=body, |
| 129 | + headers=self.build_headers(timestamp, body), |
| 130 | + ) |
| 131 | + self.assertStatus("200 OK") |
| 132 | + self.assertBody("") |
| 133 | + |
| 134 | + def test_commands(self): |
| 135 | + payload = ( |
| 136 | + "token=verification_token" |
| 137 | + "&team_id=T111" |
| 138 | + "&team_domain=test-domain" |
| 139 | + "&channel_id=C111" |
| 140 | + "&channel_name=random" |
| 141 | + "&user_id=W111" |
| 142 | + "&user_name=primary-owner" |
| 143 | + "&command=%2Fhello-world" |
| 144 | + "&text=Hi" |
| 145 | + "&enterprise_id=E111" |
| 146 | + "&enterprise_name=Org+Name" |
| 147 | + "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx" |
| 148 | + "&trigger_id=111.111.xxx" |
| 149 | + ) |
| 150 | + timestamp, body = str(int(time())), json.dumps(payload) |
| 151 | + cherrypy.request.process_request_body = True |
| 152 | + self.getPage( |
| 153 | + "/slack/events", |
| 154 | + method="POST", |
| 155 | + body=body, |
| 156 | + headers=self.build_headers(timestamp, body), |
| 157 | + ) |
| 158 | + self.assertStatus("200 OK") |
| 159 | + self.assertBody("") |
0 commit comments