Skip to content

Commit 3095063

Browse files
jlujan-invitaeseratch
authored andcommitted
Add tests for chalice local lazy through LocalLambdaClient
1 parent 86522d0 commit 3095063

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

slack_bolt/adapter/aws_lambda/chalice_handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ def invoke(
2828
self,
2929
FunctionName: str = None,
3030
InvocationType: str = "Event",
31-
Payload: str = None,
31+
Payload: str = "{}",
3232
) -> InvokeResponse:
33-
if Payload is None:
34-
Payload = "{}"
3533
scoped = self._config.scope(self._config.chalice_stage, FunctionName)
3634
lambda_context = LambdaContext(
3735
FunctionName, memory_size=scoped.lambda_memory_size

tests/adapter_tests/aws/test_aws_chalice.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import json
2+
import os
23
from time import time
34
from typing import Dict, Any
45
from urllib.parse import quote
6+
from unittest import mock
7+
import logging
58

69
from chalice import Chalice, Response
710
from chalice.app import Request
811
from chalice.config import Config
912
from chalice.local import LocalGateway
13+
from chalice.test import Client
1014
from slack_sdk.signature import SignatureVerifier
1115
from slack_sdk.web import WebClient
1216

@@ -265,6 +269,67 @@ def say_it(say):
265269
assert_auth_test_count(self, 1)
266270
assert self.mock_received_requests["/chat.postMessage"] == 1
267271

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+
268333
def test_oauth(self):
269334
app = App(
270335
client=self.web_client,

0 commit comments

Comments
 (0)