forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_webhook.py
More file actions
179 lines (160 loc) · 6.36 KB
/
Copy pathtest_webhook.py
File metadata and controls
179 lines (160 loc) · 6.36 KB
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
import unittest
import socket
import urllib
from slack.web.classes.attachments import Attachment, AttachmentField
from slack.web.classes.blocks import SectionBlock, ImageBlock
from slack.webhook import WebhookClient, WebhookResponse
from tests.webhook.mock_web_api_server import (
cleanup_mock_web_api_server,
setup_mock_web_api_server,
)
class TestWebhook(unittest.TestCase):
def setUp(self):
setup_mock_web_api_server(self)
def tearDown(self):
cleanup_mock_web_api_server(self)
def test_send(self):
client = WebhookClient("http://localhost:8888")
resp: WebhookResponse = client.send(text="hello!")
self.assertEqual(200, resp.status_code)
self.assertEqual("ok", resp.body)
resp = client.send(text="hello!", response_type="in_channel")
self.assertEqual("ok", resp.body)
def test_send_blocks(self):
client = WebhookClient("http://localhost:8888")
resp = 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 = 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 = 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)
def test_send_attachments(self):
client = WebhookClient("http://localhost:8888")
resp = 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 = 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)
def test_send_dict(self):
client = WebhookClient("http://localhost:8888")
resp: WebhookResponse = client.send_dict({"text": "hello!"})
self.assertEqual(200, resp.status_code)
self.assertEqual("ok", resp.body)
def test_timeout_issue_712(self):
client = WebhookClient(url="http://localhost:8888/timeout", timeout=1)
with self.assertRaises(socket.timeout):
client.send_dict({"text": "hello!"})
def test_error_response(self):
client = WebhookClient(url="http://localhost:8888/error")
resp: WebhookResponse = client.send_dict({"text": "hello!"})
self.assertEqual(500, resp.status_code)
self.assertTrue(resp.body.startswith("<!DOCTYPE html>"))
def test_proxy_issue_714(self):
client = WebhookClient(
url="http://localhost:8888", proxy="http://invalid-host:9999"
)
with self.assertRaises(urllib.error.URLError):
client.send_dict({"text": "hello!"})
def test_user_agent_customization_issue_769(self):
client = WebhookClient(
url="http://localhost:8888/user-agent-this_is-test",
user_agent_prefix="this_is",
user_agent_suffix="test",
)
resp = client.send_dict({"text": "hi!"})
self.assertEqual(resp.body, "ok")