|
1 | 1 | import json |
| 2 | +import os |
2 | 3 | from time import time |
3 | 4 | from typing import Dict, Any |
4 | 5 | from urllib.parse import quote |
| 6 | +from unittest import mock |
| 7 | +import logging |
5 | 8 |
|
6 | 9 | from chalice import Chalice, Response |
7 | 10 | from chalice.app import Request |
8 | 11 | from chalice.config import Config |
9 | 12 | from chalice.local import LocalGateway |
| 13 | +from chalice.test import Client |
10 | 14 | from slack_sdk.signature import SignatureVerifier |
11 | 15 | from slack_sdk.web import WebClient |
12 | 16 |
|
@@ -265,6 +269,67 @@ def say_it(say): |
265 | 269 | assert_auth_test_count(self, 1) |
266 | 270 | assert self.mock_received_requests["/chat.postMessage"] == 1 |
267 | 271 |
|
| 272 | + def test_lazy_listeners_cli(self): |
| 273 | + with mock.patch.dict(os.environ, {"AWS_CHALICE_CLI_MODE": "true"}): |
| 274 | + assert os.environ.get("AWS_CHALICE_CLI_MODE") == "true" |
| 275 | + app = App( |
| 276 | + client=self.web_client, |
| 277 | + signing_secret=self.signing_secret, |
| 278 | + process_before_response=True |
| 279 | + ) |
| 280 | + |
| 281 | + def command_handler(ack): |
| 282 | + ack() |
| 283 | + |
| 284 | + def say_it(say): |
| 285 | + say("Done!") |
| 286 | + |
| 287 | + app.command("/hello-world")(ack=command_handler, lazy=[say_it]) |
| 288 | + |
| 289 | + input = ( |
| 290 | + "token=verification_token" |
| 291 | + "&team_id=T111" |
| 292 | + "&team_domain=test-domain" |
| 293 | + "&channel_id=C111" |
| 294 | + "&channel_name=random" |
| 295 | + "&user_id=W111" |
| 296 | + "&user_name=primary-owner" |
| 297 | + "&command=%2Fhello-world" |
| 298 | + "&text=Hi" |
| 299 | + "&enterprise_id=E111" |
| 300 | + "&enterprise_name=Org+Name" |
| 301 | + "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx" |
| 302 | + "&trigger_id=111.111.xxx" |
| 303 | + ) |
| 304 | + timestamp, body = str(int(time())), input |
| 305 | + |
| 306 | + chalice_app = Chalice(app_name="bolt-python-chalice") |
| 307 | + slack_handler = ChaliceSlackRequestHandler(app=app, chalice=chalice_app) |
| 308 | + |
| 309 | + @chalice_app.route( |
| 310 | + "/slack/events", |
| 311 | + methods=["POST"], |
| 312 | + content_types=["application/x-www-form-urlencoded", "application/json"], |
| 313 | + ) |
| 314 | + def events() -> Response: |
| 315 | + return slack_handler.handle(chalice_app.current_request) |
| 316 | + |
| 317 | + headers = self.build_headers(timestamp, body) |
| 318 | + client = Client(chalice_app, Config()) |
| 319 | + response = client.http.post("/slack/events", headers=headers, body=body) |
| 320 | + # |
| 321 | + # response: Dict[str, Any] = LocalGateway(chalice_app, Config()).handle_request( |
| 322 | + # method="POST", |
| 323 | + # path="/slack/events", |
| 324 | + # body=body, |
| 325 | + # headers=self.build_headers(timestamp, body), |
| 326 | + # ) |
| 327 | + |
| 328 | + # assert response["statusCode"] == 200, f"error: {response['body']}" |
| 329 | + assert response.status_code == 200, f"Failed request: {response.body}" |
| 330 | + assert_auth_test_count(self, 1) |
| 331 | + assert self.mock_received_requests["/chat.postMessage"] == 1 |
| 332 | + |
268 | 333 | def test_oauth(self): |
269 | 334 | app = App( |
270 | 335 | client=self.web_client, |
|
0 commit comments