77from slack_sdk .signature import SignatureVerifier
88from slack_sdk .web .async_client import AsyncWebClient
99
10+ from slack_bolt import BoltResponse
1011from slack_bolt .app .async_app import AsyncApp
1112from slack_bolt .authorization import AuthorizeResult
1213from slack_bolt .request .async_request import AsyncBoltRequest
14+ from slack_bolt .request .payload_utils import is_event
1315from tests .mock_web_api_server import (
1416 setup_mock_web_api_server ,
1517 cleanup_mock_web_api_server ,
@@ -84,8 +86,37 @@ def build_headers(self, timestamp: str, body: str):
8486 "x-slack-request-timestamp" : [timestamp ],
8587 }
8688
87- def build_valid_request (self ) -> AsyncBoltRequest :
89+ def build_block_actions_request (self ) -> AsyncBoltRequest :
8890 timestamp = str (int (time ()))
91+ return AsyncBoltRequest (body = block_actions_raw_body , headers = self .build_headers (timestamp , block_actions_raw_body ))
92+
93+ def build_message_changed_event_request (self ) -> AsyncBoltRequest :
94+ timestamp = str (int (time ()))
95+ raw_body = json .dumps (
96+ {
97+ "token" : "verification_token" ,
98+ "team_id" : "T111" ,
99+ "enterprise_id" : "E111" ,
100+ "api_app_id" : "A111" ,
101+ "event" : {
102+ "type" : "message" ,
103+ "subtype" : "message_changed" ,
104+ "channel" : "C2147483705" ,
105+ "ts" : "1358878755.000001" ,
106+ "message" : {
107+ "type" : "message" ,
108+ "user" : "U2147483697" ,
109+ "text" : "Hello, world!" ,
110+ "ts" : "1355517523.000005" ,
111+ "edited" : {"user" : "U2147483697" , "ts" : "1358878755.000001" },
112+ },
113+ },
114+ "type" : "event_callback" ,
115+ "event_id" : "Ev111" ,
116+ "event_time" : 1599616881 ,
117+ "authed_users" : ["W111" ],
118+ }
119+ )
89120 return AsyncBoltRequest (body = raw_body , headers = self .build_headers (timestamp , raw_body ))
90121
91122 @pytest .mark .asyncio
@@ -97,7 +128,7 @@ async def test_success(self):
97128 )
98129 app .action ("a" )(simple_listener )
99130
100- request = self .build_valid_request ()
131+ request = self .build_block_actions_request ()
101132 response = await app .async_dispatch (request )
102133 assert response .status == 200
103134 assert response .body == ""
@@ -112,7 +143,7 @@ async def test_failure(self):
112143 )
113144 app .block_action ("a" )(simple_listener )
114145
115- request = self .build_valid_request ()
146+ request = self .build_block_actions_request ()
116147 response = await app .async_dispatch (request )
117148 assert response .status == 200
118149 assert response .body == ""
@@ -127,7 +158,7 @@ async def test_bot_context_attributes(self):
127158 )
128159 app .action ("a" )(assert_bot_context_attributes )
129160
130- request = self .build_valid_request ()
161+ request = self .build_block_actions_request ()
131162 response = await app .async_dispatch (request )
132163 assert response .status == 200
133164 assert response .body == ""
@@ -142,14 +173,41 @@ async def test_user_context_attributes(self):
142173 )
143174 app .action ("a" )(assert_user_context_attributes )
144175
145- request = self .build_valid_request ()
176+ request = self .build_block_actions_request ()
146177 response = await app .async_dispatch (request )
147178 assert response .status == 200
148179 assert response .body == ""
149180 await assert_auth_test_count_async (self , 1 )
150181
182+ @pytest .mark .asyncio
183+ async def test_user_context_attributes (self ):
184+ async def skip_message_changed_events (body : dict , payload : dict , next_ ):
185+ if is_event (body ) and payload .get ("type" ) == "message" and payload .get ("subtype" ) == "message_changed" :
186+ return BoltResponse (status = 200 , body = "as expected" )
187+ await next_ ()
188+
189+ app = AsyncApp (
190+ client = self .web_client ,
191+ before_authorize = skip_message_changed_events ,
192+ authorize = user_authorize ,
193+ signing_secret = self .signing_secret ,
194+ )
195+ app .action ("a" )(assert_user_context_attributes )
196+
197+ request = self .build_block_actions_request ()
198+ response = await app .async_dispatch (request )
199+ assert response .status == 200
200+ assert response .body == ""
201+ await assert_auth_test_count_async (self , 1 )
202+
203+ request = self .build_message_changed_event_request ()
204+ response = await app .async_dispatch (request )
205+ assert response .status == 200
206+ assert response .body == "as expected"
207+ await assert_auth_test_count_async (self , 1 ) # should be skipped
208+
151209
152- body = {
210+ block_actions_body = {
153211 "type" : "block_actions" ,
154212 "user" : {
155213 "id" : "W99999" ,
@@ -186,7 +244,7 @@ async def test_user_context_attributes(self):
186244 ],
187245}
188246
189- raw_body = f"payload={ quote (json .dumps (body ))} "
247+ block_actions_raw_body = f"payload={ quote (json .dumps (block_actions_body ))} "
190248
191249
192250async def simple_listener (ack , body , payload , action ):
0 commit comments