11import os
2+ from tests .helpers import async_test
23import unittest
4+ import time
35
46from integration_tests .env_variable_names import (
57 SLACK_SDK_TEST_INCOMING_WEBHOOK_URL ,
68 SLACK_SDK_TEST_INCOMING_WEBHOOK_CHANNEL_NAME ,
79 SLACK_SDK_TEST_BOT_TOKEN ,
810)
9- from integration_tests .helpers import async_test
1011from slack_sdk .web .async_client import AsyncWebClient
1112from slack_sdk .webhook .async_client import AsyncWebhookClient
1213from slack_sdk .models .attachments import Attachment , AttachmentField
1617
1718
1819class TestAsyncWebhook (unittest .TestCase ):
19- def setUp (self ):
20- pass
20+ @async_test
21+ async def setUp (self ):
22+ if not hasattr (self , "channel_id" ):
23+ token = os .environ [SLACK_SDK_TEST_BOT_TOKEN ]
24+ channel_name = os .environ [
25+ SLACK_SDK_TEST_INCOMING_WEBHOOK_CHANNEL_NAME
26+ ].replace ("#" , "" )
27+ client = AsyncWebClient (token = token )
28+ self .channel_id = None
29+ async for resp in await client .conversations_list (limit = 10 ):
30+ for c in resp ["channels" ]:
31+ if c ["name" ] == channel_name :
32+ self .channel_id = c ["id" ]
33+ break
34+ if self .channel_id is not None :
35+ break
2136
2237 def tearDown (self ):
2338 pass
@@ -31,24 +46,55 @@ async def test_webhook(self):
3146 self .assertEqual ("ok" , response .body )
3247
3348 token = os .environ [SLACK_SDK_TEST_BOT_TOKEN ]
34- channel_name = os .environ [SLACK_SDK_TEST_INCOMING_WEBHOOK_CHANNEL_NAME ].replace (
35- "#" , ""
36- )
3749 client = AsyncWebClient (token = token )
38- channel_id = None
39- async for resp in await client .conversations_list (limit = 10 ):
40- for c in resp ["channels" ]:
41- if c ["name" ] == channel_name :
42- channel_id = c ["id" ]
43- break
44- if channel_id is not None :
45- break
46-
47- history = await client .conversations_history (channel = channel_id , limit = 1 )
50+ history = await client .conversations_history (channel = self .channel_id , limit = 1 )
4851 self .assertIsNotNone (history )
4952 actual_text = history ["messages" ][0 ]["text" ]
5053 self .assertEqual ("Hello!" , actual_text )
5154
55+ @async_test
56+ async def test_with_unfurls_off (self ):
57+ url = os .environ [SLACK_SDK_TEST_INCOMING_WEBHOOK_URL ]
58+ token = os .environ [SLACK_SDK_TEST_BOT_TOKEN ]
59+ webhook = AsyncWebhookClient (url )
60+ client = AsyncWebClient (token = token )
61+ # send message that does not unfurl
62+ response = await webhook .send (
63+ text = "<https://imgs.xkcd.com/comics/desert_golfing_2x.png|Desert Golfing>" ,
64+ unfurl_links = False ,
65+ unfurl_media = False ,
66+ )
67+ self .assertEqual (200 , response .status_code )
68+ self .assertEqual ("ok" , response .body )
69+ # wait to allow Slack API to edit message with attachments
70+ time .sleep (2 )
71+ history = await client .conversations_history (channel = self .channel_id , limit = 1 )
72+ self .assertIsNotNone (history )
73+ self .assertTrue ("attachments" not in history ["messages" ][0 ])
74+
75+ @async_test
76+ async def test_with_unfurls_on (self ):
77+ # Slack API rate limits unfurls of unique links so test will
78+ # fail when repeated. For testing, either use a different URL
79+ # for text option or delete existing attachments in webhook channel.
80+ url = os .environ [SLACK_SDK_TEST_INCOMING_WEBHOOK_URL ]
81+ token = os .environ [SLACK_SDK_TEST_BOT_TOKEN ]
82+ webhook = AsyncWebhookClient (url )
83+ client = AsyncWebClient (token = token )
84+ # send message that does unfurl
85+ response = await webhook .send (
86+ text = "<https://imgs.xkcd.com/comics/red_spiders_small.jpg|Spiders>" ,
87+ unfurl_links = True ,
88+ unfurl_media = True ,
89+ )
90+ self .assertEqual (200 , response .status_code )
91+ self .assertEqual ("ok" , response .body )
92+ # wait to allow Slack API to edit message with attachments
93+ time .sleep (2 )
94+ history = await client .conversations_history (channel = self .channel_id , limit = 1 )
95+ self .assertIsNotNone (history )
96+ self .assertTrue ("attachments" in history ["messages" ][0 ])
97+
5298 @async_test
5399 async def test_with_blocks (self ):
54100 url = os .environ [SLACK_SDK_TEST_INCOMING_WEBHOOK_URL ]
0 commit comments