Skip to content

Commit 27a01c1

Browse files
authored
Fix #639 is_enterprise_install does not exist in context object (#640)
* Fix #639 is_enterprise_install does not exist in context object * Remove unused imports
1 parent 319d000 commit 27a01c1

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

slack_bolt/request/async_internals.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from slack_bolt.context.async_context import AsyncBoltContext
44
from slack_bolt.request.internals import (
55
extract_enterprise_id,
6+
extract_is_enterprise_install,
67
extract_team_id,
78
extract_user_id,
89
extract_channel_id,
@@ -14,6 +15,7 @@ def build_async_context(
1415
context: AsyncBoltContext,
1516
body: Dict[str, Any],
1617
) -> AsyncBoltContext:
18+
context["is_enterprise_install"] = extract_is_enterprise_install(body)
1719
enterprise_id = extract_enterprise_id(body)
1820
if enterprise_id:
1921
context["enterprise_id"] = enterprise_id

slack_bolt/request/internals.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def parse_body(body: str, content_type: Optional[str]) -> Dict[str, Any]:
4747

4848

4949
def extract_is_enterprise_install(payload: Dict[str, Any]) -> Optional[bool]:
50+
if payload.get("authorizations") is not None and len(payload["authorizations"]) > 0:
51+
# To make Events API handling functioning also for shared channels,
52+
# we should use .authorizations[0].is_enterprise_install over .is_enterprise_install
53+
return extract_is_enterprise_install(payload["authorizations"][0])
5054
if "is_enterprise_install" in payload:
5155
is_enterprise_install = payload.get("is_enterprise_install")
5256
return is_enterprise_install is not None and (

tests/scenario_tests/test_events.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from slack_sdk.signature import SignatureVerifier
77
from slack_sdk.web import WebClient
88

9-
from slack_bolt import App, BoltRequest, Say
9+
from slack_bolt import App, BoltRequest, Say, BoltContext
1010
from tests.mock_web_api_server import (
1111
setup_mock_web_api_server,
1212
cleanup_mock_web_api_server,
@@ -541,3 +541,50 @@ def handle():
541541
app.event({"type": "message.channels"})(handle)
542542
with pytest.raises(ValueError):
543543
app.event({"type": re.compile("message\\..*")})(handle)
544+
545+
def test_context_generation(self):
546+
body = {
547+
"token": "verification-token",
548+
"enterprise_id": "E222", # intentionally inconsistent for testing
549+
"team_id": "T222", # intentionally inconsistent for testing
550+
"api_app_id": "A111",
551+
"event": {
552+
"type": "member_left_channel",
553+
"user": "W111",
554+
"channel": "C111",
555+
"channel_type": "C",
556+
"team": "T111",
557+
},
558+
"type": "event_callback",
559+
"event_id": "Ev111",
560+
"event_time": 1610493715,
561+
"authorizations": [
562+
{
563+
"enterprise_id": "E333",
564+
"user_id": "W222",
565+
"is_bot": True,
566+
"is_enterprise_install": True,
567+
}
568+
],
569+
"is_ext_shared_channel": False,
570+
"event_context": "1-message-T111-G111",
571+
}
572+
app = App(
573+
client=self.web_client,
574+
signing_secret=self.signing_secret,
575+
process_before_response=True,
576+
)
577+
578+
@app.event("member_left_channel")
579+
def handle(context: BoltContext):
580+
assert context.enterprise_id == "E333"
581+
assert context.team_id is None
582+
assert context.is_enterprise_install is True
583+
assert context.user_id == "W111"
584+
585+
timestamp, json_body = str(int(time())), json.dumps(body)
586+
request: BoltRequest = BoltRequest(
587+
body=json_body, headers=self.build_headers(timestamp, json_body)
588+
)
589+
response = app.dispatch(request)
590+
assert response.status == 200

tests/scenario_tests_async/test_events.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from slack_sdk.web.async_client import AsyncWebClient
1010

1111
from slack_bolt.app.async_app import AsyncApp
12+
from slack_bolt.context.async_context import AsyncBoltContext
1213
from slack_bolt.context.say.async_say import AsyncSay
1314
from slack_bolt.request.async_request import AsyncBoltRequest
1415
from tests.mock_web_api_server import (
@@ -552,6 +553,54 @@ async def handle():
552553
with pytest.raises(ValueError):
553554
app.event({"type": re.compile("message\\..*")})(handle)
554555

556+
@pytest.mark.asyncio
557+
async def test_context_generation(self):
558+
body = {
559+
"token": "verification-token",
560+
"enterprise_id": "E222", # intentionally inconsistent for testing
561+
"team_id": "T222", # intentionally inconsistent for testing
562+
"api_app_id": "A111",
563+
"event": {
564+
"type": "member_left_channel",
565+
"user": "W111",
566+
"channel": "C111",
567+
"channel_type": "C",
568+
"team": "T111",
569+
},
570+
"type": "event_callback",
571+
"event_id": "Ev111",
572+
"event_time": 1610493715,
573+
"authorizations": [
574+
{
575+
"enterprise_id": "E333",
576+
"user_id": "W222",
577+
"is_bot": True,
578+
"is_enterprise_install": True,
579+
}
580+
],
581+
"is_ext_shared_channel": False,
582+
"event_context": "1-message-T111-G111",
583+
}
584+
app = AsyncApp(
585+
client=self.web_client,
586+
signing_secret=self.signing_secret,
587+
process_before_response=True,
588+
)
589+
590+
@app.event("member_left_channel")
591+
async def handle(context: AsyncBoltContext):
592+
assert context.enterprise_id == "E333"
593+
assert context.team_id is None
594+
assert context.is_enterprise_install is True
595+
assert context.user_id == "W111"
596+
597+
timestamp, json_body = str(int(time())), json.dumps(body)
598+
request: AsyncBoltRequest = AsyncBoltRequest(
599+
body=json_body, headers=self.build_headers(timestamp, json_body)
600+
)
601+
response = await app.async_dispatch(request)
602+
assert response.status == 200
603+
555604

556605
app_mention_body = {
557606
"token": "verification_token",

0 commit comments

Comments
 (0)