forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_async_webhook.py
193 lines (174 loc) · 6.84 KB
/
test_async_webhook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from tests.helpers import async_test
import unittest
from slack.web.classes.attachments import Attachment, AttachmentField
from slack.web.classes.blocks import SectionBlock, ImageBlock
from slack.webhook import AsyncWebhookClient, WebhookResponse
from tests.webhook.mock_web_api_server import (
cleanup_mock_web_api_server,
setup_mock_web_api_server,
)
class TestAsyncWebhook(unittest.TestCase):
def setUp(self):
setup_mock_web_api_server(self)
def tearDown(self):
cleanup_mock_web_api_server(self)
@async_test
async def test_send(self):
client = AsyncWebhookClient("http://localhost:8888")
resp: WebhookResponse = await client.send(text="hello!")
self.assertEqual(200, resp.status_code)
self.assertEqual("ok", resp.body)
resp = await client.send(text="hello!", response_type="in_channel")
self.assertEqual("ok", resp.body)
@async_test
async def test_send_with_url_unfurl_opts_issue_1045(self):
client = AsyncWebhookClient("http://localhost:8888")
resp: WebhookResponse = await client.send(
text="<https://imgs.xkcd.com/comics/1991_and_2021_2x.png|XKCD>",
unfurl_links=False,
unfurl_media=False,
)
self.assertEqual(200, resp.status_code)
self.assertEqual("ok", resp.body)
@async_test
async def test_send_blocks(self):
client = AsyncWebhookClient("http://localhost:8888")
resp = await client.send(
text="hello!",
response_type="ephemeral",
blocks=[
SectionBlock(text="Some text"),
ImageBlock(image_url="image.jpg", alt_text="an image"),
],
)
self.assertEqual("ok", resp.body)
resp = await client.send(
text="hello!",
response_type="ephemeral",
blocks=[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>",
},
},
{"type": "divider"},
{
"type": "section",
"text": {"type": "mrkdwn", "text": "Pick a date for the deadline."},
"accessory": {
"type": "datepicker",
"initial_date": "1990-04-28",
"placeholder": {
"type": "plain_text",
"text": "Select a date",
},
},
},
],
)
self.assertEqual("ok", resp.body)
resp = await client.send(
text="hello!",
response_type="ephemeral",
blocks=[
SectionBlock(text="Some text"),
ImageBlock(image_url="image.jpg", alt_text="an image"),
],
)
self.assertEqual("ok", resp.body)
@async_test
async def test_send_attachments(self):
client = AsyncWebhookClient("http://localhost:8888")
resp = await client.send(
text="hello!",
response_type="ephemeral",
attachments=[
{
"color": "#f2c744",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>",
},
},
{"type": "divider"},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Pick a date for the deadline.",
},
"accessory": {
"type": "datepicker",
"initial_date": "1990-04-28",
"placeholder": {
"type": "plain_text",
"text": "Select a date",
},
},
},
],
}
],
)
self.assertEqual("ok", resp.body)
resp = await client.send(
text="hello!",
response_type="ephemeral",
attachments=[
Attachment(
text="attachment text",
title="Attachment",
fallback="fallback_text",
pretext="some_pretext",
title_link="link in title",
fields=[
AttachmentField(
title=f"field_{i}_title", value=f"field_{i}_value"
)
for i in range(5)
],
color="#FFFF00",
author_name="John Doe",
author_link="http://johndoeisthebest.com",
author_icon="http://johndoeisthebest.com/avatar.jpg",
thumb_url="thumbnail URL",
footer="and a footer",
footer_icon="link to footer icon",
ts=123456789,
markdown_in=["fields"],
)
],
)
self.assertEqual("ok", resp.body)
@async_test
async def test_send_dict(self):
client = AsyncWebhookClient("http://localhost:8888")
resp: WebhookResponse = await client.send_dict({"text": "hello!"})
self.assertEqual(200, resp.status_code)
self.assertEqual("ok", resp.body)
@async_test
async def test_timeout_issue_712(self):
client = AsyncWebhookClient(url="http://localhost:8888/timeout", timeout=1)
with self.assertRaises(Exception):
await client.send_dict({"text": "hello!"})
@async_test
async def test_proxy_issue_714(self):
client = AsyncWebhookClient(
url="http://localhost:8888", proxy="http://invalid-host:9999"
)
with self.assertRaises(Exception):
await client.send_dict({"text": "hello!"})
@async_test
async def test_user_agent_customization_issue_769(self):
client = AsyncWebhookClient(
url="http://localhost:8888/user-agent-this_is-test",
user_agent_prefix="this_is",
user_agent_suffix="test",
)
resp = await client.send_dict({"text": "hi!"})
self.assertEqual(resp.body, "ok")