1+ import asyncio
12import json
23import re
34from time import time
45from urllib .parse import quote
56
7+ import pytest
68from slack_sdk .signature import SignatureVerifier
79from slack_sdk .web .async_client import AsyncWebClient
810from sanic import Sanic
@@ -25,13 +27,17 @@ class TestSanic:
2527 signature_verifier = SignatureVerifier (signing_secret )
2628 web_client = AsyncWebClient (token = valid_token , base_url = mock_api_server_base_url ,)
2729
28- def setup_method (self ):
29- self .old_os_env = remove_os_env_temporarily ()
30- setup_mock_web_api_server (self )
31-
32- def teardown_method (self ):
33- cleanup_mock_web_api_server (self )
34- restore_os_env (self .old_os_env )
30+ @pytest .fixture
31+ def event_loop (self ):
32+ old_os_env = remove_os_env_temporarily ()
33+ try :
34+ setup_mock_web_api_server (self )
35+ loop = asyncio .get_event_loop ()
36+ yield loop
37+ loop .close ()
38+ cleanup_mock_web_api_server (self )
39+ finally :
40+ restore_os_env (old_os_env )
3541
3642 def generate_signature (self , body : str , timestamp : str ):
3743 return self .signature_verifier .generate_signature (
@@ -50,7 +56,8 @@ def build_headers(self, timestamp: str, body: str):
5056 "x-slack-request-timestamp" : timestamp ,
5157 }
5258
53- def test_events (self ):
59+ @pytest .mark .asyncio
60+ async def test_events (self ):
5461 app = AsyncApp (client = self .web_client , signing_secret = self .signing_secret ,)
5562
5663 async def event_handler ():
@@ -87,13 +94,14 @@ async def event_handler():
8794 async def endpoint (req : Request ):
8895 return await app_handler .handle (req )
8996
90- _ , response = api .test_client .post (
91- uri = "/slack/events" , data = body , headers = self .build_headers (timestamp , body ),
97+ _ , response = await api .asgi_client .post (
98+ url = "/slack/events" , data = body , headers = self .build_headers (timestamp , body ),
9299 )
93100 assert response .status_code == 200
94101 assert self .mock_received_requests ["/auth.test" ] == 1
95102
96- def test_shortcuts (self ):
103+ @pytest .mark .asyncio
104+ async def test_shortcuts (self ):
97105 app = AsyncApp (client = self .web_client , signing_secret = self .signing_secret ,)
98106
99107 async def shortcut_handler (ack ):
@@ -125,13 +133,14 @@ async def shortcut_handler(ack):
125133 async def endpoint (req : Request ):
126134 return await app_handler .handle (req )
127135
128- _ , response = api .test_client .post (
129- "/slack/events" , data = body , headers = self .build_headers (timestamp , body ),
136+ _ , response = await api .asgi_client .post (
137+ url = "/slack/events" , data = body , headers = self .build_headers (timestamp , body ),
130138 )
131139 assert response .status_code == 200
132140 assert self .mock_received_requests ["/auth.test" ] == 1
133141
134- def test_commands (self ):
142+ @pytest .mark .asyncio
143+ async def test_commands (self ):
135144 app = AsyncApp (client = self .web_client , signing_secret = self .signing_secret ,)
136145
137146 async def command_handler (ack ):
@@ -163,13 +172,14 @@ async def command_handler(ack):
163172 async def endpoint (req : Request ):
164173 return await app_handler .handle (req )
165174
166- _ , response = api .test_client .post (
167- "/slack/events" , data = body , headers = self .build_headers (timestamp , body ),
175+ _ , response = await api .asgi_client .post (
176+ url = "/slack/events" , data = body , headers = self .build_headers (timestamp , body ),
168177 )
169178 assert response .status_code == 200
170179 assert self .mock_received_requests ["/auth.test" ] == 1
171180
172- def test_oauth (self ):
181+ @pytest .mark .asyncio
182+ async def test_oauth (self ):
173183 app = AsyncApp (
174184 client = self .web_client ,
175185 signing_secret = self .signing_secret ,
@@ -186,7 +196,9 @@ def test_oauth(self):
186196 async def endpoint (req : Request ):
187197 return await app_handler .handle (req )
188198
189- _ , response = api .test_client .get ("/slack/install" , allow_redirects = False )
199+ _ , response = await api .asgi_client .get (
200+ url = "/slack/install" , allow_redirects = False
201+ )
190202 assert response .status_code == 302
191203 assert re .match (
192204 "https://slack.com/oauth/v2/authorize\\ ?state=[^&]+&client_id=111.111&scope=chat:write,commands&user_scope=" ,
0 commit comments