|
| 1 | +import datetime |
| 2 | +import json |
| 3 | +import logging |
| 4 | +from time import time, sleep |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +from slack_sdk import WebClient |
| 8 | +from slack_sdk.oauth import InstallationStore |
| 9 | +from slack_sdk.oauth.installation_store import Installation, Bot |
| 10 | +from slack_sdk.oauth.state_store import FileOAuthStateStore |
| 11 | +from slack_sdk.signature import SignatureVerifier |
| 12 | + |
| 13 | +from slack_bolt import App, BoltRequest, Say |
| 14 | +from slack_bolt.oauth import OAuthFlow |
| 15 | +from slack_bolt.oauth.oauth_settings import OAuthSettings |
| 16 | +from tests.mock_web_api_server import ( |
| 17 | + setup_mock_web_api_server, |
| 18 | + cleanup_mock_web_api_server, |
| 19 | +) |
| 20 | +from tests.utils import remove_os_env_temporarily, restore_os_env |
| 21 | + |
| 22 | + |
| 23 | +class LegacyMemoryInstallationStore(InstallationStore): |
| 24 | + @property |
| 25 | + def logger(self) -> logging.Logger: |
| 26 | + return logging.getLogger(__name__) |
| 27 | + |
| 28 | + def save(self, installation: Installation): |
| 29 | + pass |
| 30 | + |
| 31 | + def find_bot( |
| 32 | + self, |
| 33 | + *, |
| 34 | + enterprise_id: Optional[str], |
| 35 | + team_id: Optional[str], |
| 36 | + is_enterprise_install: Optional[bool] = False, |
| 37 | + ) -> Optional[Bot]: |
| 38 | + return Bot( |
| 39 | + app_id="A111", |
| 40 | + enterprise_id="E111", |
| 41 | + team_id="T0G9PQBBK", |
| 42 | + bot_token="xoxb-valid", |
| 43 | + bot_id="B", |
| 44 | + bot_user_id="W", |
| 45 | + bot_scopes=["commands", "chat:write"], |
| 46 | + installed_at=datetime.datetime.now().timestamp(), |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +class MemoryInstallationStore(LegacyMemoryInstallationStore): |
| 51 | + def find_installation( |
| 52 | + self, |
| 53 | + *, |
| 54 | + enterprise_id: Optional[str], |
| 55 | + team_id: Optional[str], |
| 56 | + user_id: Optional[str] = None, |
| 57 | + is_enterprise_install: Optional[bool] = False, |
| 58 | + ) -> Optional[Installation]: |
| 59 | + return Installation( |
| 60 | + app_id="A111", |
| 61 | + enterprise_id="E111", |
| 62 | + team_id="T0G9PQBBK", |
| 63 | + bot_token="xoxb-valid-2", |
| 64 | + bot_id="B", |
| 65 | + bot_user_id="W", |
| 66 | + bot_scopes=["commands", "chat:write"], |
| 67 | + user_id="W11111", |
| 68 | + user_token="xoxp-valid", |
| 69 | + user_scopes=["search:read"], |
| 70 | + installed_at=datetime.datetime.now().timestamp(), |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +class BotOnlyMemoryInstallationStore(LegacyMemoryInstallationStore): |
| 75 | + def find_installation( |
| 76 | + self, |
| 77 | + *, |
| 78 | + enterprise_id: Optional[str], |
| 79 | + team_id: Optional[str], |
| 80 | + user_id: Optional[str] = None, |
| 81 | + is_enterprise_install: Optional[bool] = False, |
| 82 | + ) -> Optional[Installation]: |
| 83 | + raise ValueError |
| 84 | + |
| 85 | + |
| 86 | +class TestApp: |
| 87 | + signing_secret = "secret" |
| 88 | + valid_token = "xoxb-valid" |
| 89 | + mock_api_server_base_url = "http://localhost:8888" |
| 90 | + signature_verifier = SignatureVerifier(signing_secret) |
| 91 | + web_client = WebClient(token=valid_token, base_url=mock_api_server_base_url,) |
| 92 | + |
| 93 | + def setup_method(self): |
| 94 | + self.old_os_env = remove_os_env_temporarily() |
| 95 | + setup_mock_web_api_server(self) |
| 96 | + |
| 97 | + def teardown_method(self): |
| 98 | + cleanup_mock_web_api_server(self) |
| 99 | + restore_os_env(self.old_os_env) |
| 100 | + |
| 101 | + def generate_signature(self, body: str, timestamp: str): |
| 102 | + return self.signature_verifier.generate_signature( |
| 103 | + body=body, timestamp=timestamp, |
| 104 | + ) |
| 105 | + |
| 106 | + def build_headers(self, timestamp: str, body: str): |
| 107 | + return { |
| 108 | + "content-type": ["application/json"], |
| 109 | + "x-slack-signature": [self.generate_signature(body, timestamp)], |
| 110 | + "x-slack-request-timestamp": [timestamp], |
| 111 | + } |
| 112 | + |
| 113 | + app_mention_request_body = { |
| 114 | + "token": "verification_token", |
| 115 | + "team_id": "T111", |
| 116 | + "enterprise_id": "E111", |
| 117 | + "api_app_id": "A111", |
| 118 | + "event": { |
| 119 | + "client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63", |
| 120 | + "type": "app_mention", |
| 121 | + "text": "<@W111> Hi there!", |
| 122 | + "user": "W222", |
| 123 | + "ts": "1595926230.009600", |
| 124 | + "team": "T111", |
| 125 | + "channel": "C111", |
| 126 | + "event_ts": "1595926230.009600", |
| 127 | + }, |
| 128 | + "type": "event_callback", |
| 129 | + "event_id": "Ev111", |
| 130 | + "event_time": 1595926230, |
| 131 | + "authed_users": ["W111"], |
| 132 | + } |
| 133 | + |
| 134 | + @staticmethod |
| 135 | + def handle_app_mention(body, say: Say, payload, event): |
| 136 | + assert body["event"] == payload |
| 137 | + assert payload == event |
| 138 | + say("What's up?") |
| 139 | + |
| 140 | + oauth_settings_bot_only = OAuthSettings( |
| 141 | + client_id="111.222", |
| 142 | + client_secret="valid", |
| 143 | + installation_store=BotOnlyMemoryInstallationStore(), |
| 144 | + installation_store_bot_only=True, |
| 145 | + state_store=FileOAuthStateStore(expiration_seconds=120), |
| 146 | + ) |
| 147 | + |
| 148 | + oauth_settings = OAuthSettings( |
| 149 | + client_id="111.222", |
| 150 | + client_secret="valid", |
| 151 | + installation_store=BotOnlyMemoryInstallationStore(), |
| 152 | + installation_store_bot_only=False, |
| 153 | + state_store=FileOAuthStateStore(expiration_seconds=120), |
| 154 | + ) |
| 155 | + |
| 156 | + def build_app_mention_request(self): |
| 157 | + timestamp, body = str(int(time())), json.dumps(self.app_mention_request_body) |
| 158 | + return BoltRequest(body=body, headers=self.build_headers(timestamp, body)) |
| 159 | + |
| 160 | + def test_installation_store_bot_only_default(self): |
| 161 | + app = App( |
| 162 | + client=self.web_client, |
| 163 | + signing_secret=self.signing_secret, |
| 164 | + installation_store=MemoryInstallationStore(), |
| 165 | + ) |
| 166 | + |
| 167 | + app.event("app_mention")(self.handle_app_mention) |
| 168 | + response = app.dispatch(self.build_app_mention_request()) |
| 169 | + assert response.status == 200 |
| 170 | + assert self.mock_received_requests["/auth.test"] == 1 |
| 171 | + sleep(1) # wait a bit after auto ack() |
| 172 | + assert self.mock_received_requests["/chat.postMessage"] == 1 |
| 173 | + |
| 174 | + def test_installation_store_bot_only_false(self): |
| 175 | + app = App( |
| 176 | + client=self.web_client, |
| 177 | + signing_secret=self.signing_secret, |
| 178 | + installation_store=MemoryInstallationStore(), |
| 179 | + # the default is False |
| 180 | + installation_store_bot_only=False, |
| 181 | + ) |
| 182 | + |
| 183 | + app.event("app_mention")(self.handle_app_mention) |
| 184 | + response = app.dispatch(self.build_app_mention_request()) |
| 185 | + assert response.status == 200 |
| 186 | + assert self.mock_received_requests["/auth.test"] == 1 |
| 187 | + sleep(1) # wait a bit after auto ack() |
| 188 | + assert self.mock_received_requests["/chat.postMessage"] == 1 |
| 189 | + |
| 190 | + def test_installation_store_bot_only(self): |
| 191 | + app = App( |
| 192 | + client=self.web_client, |
| 193 | + signing_secret=self.signing_secret, |
| 194 | + installation_store=BotOnlyMemoryInstallationStore(), |
| 195 | + installation_store_bot_only=True, |
| 196 | + ) |
| 197 | + |
| 198 | + app.event("app_mention")(self.handle_app_mention) |
| 199 | + response = app.dispatch(self.build_app_mention_request()) |
| 200 | + assert response.status == 200 |
| 201 | + assert self.mock_received_requests["/auth.test"] == 1 |
| 202 | + sleep(1) # wait a bit after auto ack() |
| 203 | + assert self.mock_received_requests["/chat.postMessage"] == 1 |
| 204 | + |
| 205 | + def test_installation_store_bot_only_oauth_settings(self): |
| 206 | + app = App( |
| 207 | + client=self.web_client, |
| 208 | + signing_secret=self.signing_secret, |
| 209 | + oauth_settings=self.oauth_settings_bot_only, |
| 210 | + ) |
| 211 | + |
| 212 | + app.event("app_mention")(self.handle_app_mention) |
| 213 | + response = app.dispatch(self.build_app_mention_request()) |
| 214 | + assert response.status == 200 |
| 215 | + assert self.mock_received_requests["/auth.test"] == 1 |
| 216 | + sleep(1) # wait a bit after auto ack() |
| 217 | + assert self.mock_received_requests["/chat.postMessage"] == 1 |
| 218 | + |
| 219 | + def test_installation_store_bot_only_oauth_settings_conflicts(self): |
| 220 | + app = App( |
| 221 | + client=self.web_client, |
| 222 | + signing_secret=self.signing_secret, |
| 223 | + installation_store_bot_only=True, |
| 224 | + oauth_settings=self.oauth_settings, |
| 225 | + ) |
| 226 | + |
| 227 | + app.event("app_mention")(self.handle_app_mention) |
| 228 | + response = app.dispatch(self.build_app_mention_request()) |
| 229 | + assert response.status == 200 |
| 230 | + assert self.mock_received_requests["/auth.test"] == 1 |
| 231 | + sleep(1) # wait a bit after auto ack() |
| 232 | + assert self.mock_received_requests["/chat.postMessage"] == 1 |
| 233 | + |
| 234 | + def test_installation_store_bot_only_oauth_flow(self): |
| 235 | + app = App( |
| 236 | + client=self.web_client, |
| 237 | + signing_secret=self.signing_secret, |
| 238 | + oauth_flow=OAuthFlow(settings=self.oauth_settings_bot_only), |
| 239 | + ) |
| 240 | + |
| 241 | + app.event("app_mention")(self.handle_app_mention) |
| 242 | + response = app.dispatch(self.build_app_mention_request()) |
| 243 | + assert response.status == 200 |
| 244 | + assert self.mock_received_requests["/auth.test"] == 1 |
| 245 | + sleep(1) # wait a bit after auto ack() |
| 246 | + assert self.mock_received_requests["/chat.postMessage"] == 1 |
| 247 | + |
| 248 | + def test_installation_store_bot_only_oauth_flow_conflicts(self): |
| 249 | + app = App( |
| 250 | + client=self.web_client, |
| 251 | + signing_secret=self.signing_secret, |
| 252 | + installation_store_bot_only=True, |
| 253 | + oauth_flow=OAuthFlow(settings=self.oauth_settings), |
| 254 | + ) |
| 255 | + |
| 256 | + app.event("app_mention")(self.handle_app_mention) |
| 257 | + response = app.dispatch(self.build_app_mention_request()) |
| 258 | + assert response.status == 200 |
| 259 | + assert self.mock_received_requests["/auth.test"] == 1 |
| 260 | + sleep(1) # wait a bit after auto ack() |
| 261 | + assert self.mock_received_requests["/chat.postMessage"] == 1 |
0 commit comments