forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_elements.py
More file actions
237 lines (210 loc) · 8.02 KB
/
Copy pathtest_elements.py
File metadata and controls
237 lines (210 loc) · 8.02 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import unittest
from slack.errors import SlackObjectFormationError
from slack.web.classes.elements import (
ButtonElement,
ChannelSelectElement,
ConversationSelectElement,
ExternalDataSelectElement,
ImageElement,
LinkButtonElement,
SelectElement,
UserSelectElement,
)
from slack.web.classes.objects import ConfirmObject, Option
from . import STRING_3001_CHARS, STRING_301_CHARS
class InteractiveElementTests(unittest.TestCase):
def test_action_id(self):
with self.assertRaises(SlackObjectFormationError):
ButtonElement(
text="click me!", action_id=STRING_301_CHARS, value="clickable button"
).to_dict()
class ButtonElementTests(unittest.TestCase):
def test_json(self):
self.assertDictEqual(
ButtonElement(
text="button text", action_id="some_button", value="button_123"
).to_dict(),
{
"text": {"emoji": True, "text": "button text", "type": "plain_text"},
"action_id": "some_button",
"value": "button_123",
"type": "button",
},
)
confirm = ConfirmObject(title="really?", text="are you sure?")
self.assertDictEqual(
ButtonElement(
text="button text",
action_id="some_button",
value="button_123",
style="primary",
confirm=confirm,
).to_dict(),
{
"text": {"emoji": True, "text": "button text", "type": "plain_text"},
"action_id": "some_button",
"value": "button_123",
"type": "button",
"style": "primary",
"confirm": confirm.to_dict(),
},
)
def test_text_length(self):
with self.assertRaises(SlackObjectFormationError):
ButtonElement(
text=STRING_301_CHARS, action_id="button", value="click_me"
).to_dict()
def test_value_length(self):
with self.assertRaises(SlackObjectFormationError):
ButtonElement(
text="Button", action_id="button", value=STRING_301_CHARS
).to_dict()
def test_invalid_style(self):
with self.assertRaises(SlackObjectFormationError):
ButtonElement(
text="Button", action_id="button", value="button", style="invalid"
).to_dict()
class LinkButtonElementTests(unittest.TestCase):
def test_json(self):
button = LinkButtonElement(text="button text", url="http://google.com")
self.assertDictEqual(
button.to_dict(),
{
"text": {"emoji": True, "text": "button text", "type": "plain_text"},
"url": "http://google.com",
"type": "button",
"value": "",
"action_id": button.action_id,
},
)
def test_url_length(self):
with self.assertRaises(SlackObjectFormationError):
LinkButtonElement(text="Button", url=STRING_3001_CHARS).to_dict()
class ImageElementTests(unittest.TestCase):
def test_json(self):
self.assertDictEqual(
ImageElement(
image_url="http://google.com", alt_text="not really an image"
).to_dict(),
{
"image_url": "http://google.com",
"alt_text": "not really an image",
"type": "image",
},
)
def test_image_url_length(self):
with self.assertRaises(SlackObjectFormationError):
ImageElement(image_url=STRING_3001_CHARS, alt_text="text").to_dict()
def test_alt_text_length(self):
with self.assertRaises(SlackObjectFormationError):
ImageElement(
image_url="http://google.com", alt_text=STRING_3001_CHARS
).to_dict()
class SelectElementTests(unittest.TestCase):
option_one = Option.from_single_value("one")
option_two = Option.from_single_value("two")
options = [option_one, option_two, Option.from_single_value("three")]
def test_json(self):
self.maxDiff = None
self.assertDictEqual(
SelectElement(
placeholder="selectedValue",
action_id="dropdown",
options=self.options,
initial_option=self.option_two,
).to_dict(),
{
"placeholder": {
"emoji": True,
"text": "selectedValue",
"type": "plain_text",
},
"action_id": "dropdown",
"options": [o.to_dict("block") for o in self.options],
"initial_option": self.option_two.to_dict(),
"type": "static_select",
},
)
self.assertDictEqual(
SelectElement(
placeholder="selectedValue",
action_id="dropdown",
options=self.options,
confirm=ConfirmObject(title="title", text="text"),
).to_dict(),
{
"placeholder": {
"emoji": True,
"text": "selectedValue",
"type": "plain_text",
},
"action_id": "dropdown",
"options": [o.to_dict("block") for o in self.options],
"confirm": ConfirmObject(title="title", text="text").to_dict("block"),
"type": "static_select",
},
)
def test_options_length(self):
with self.assertRaises(SlackObjectFormationError):
SelectElement(
placeholder="select",
action_id="selector",
options=[self.option_one] * 101,
).to_dict()
class ExternalDropdownElementTests(unittest.TestCase):
def test_json(self):
self.assertDictEqual(
ExternalDataSelectElement(
placeholder="selectedValue", action_id="dropdown", min_query_length=5
).to_dict(),
{
"placeholder": {
"emoji": True,
"text": "selectedValue",
"type": "plain_text",
},
"action_id": "dropdown",
"min_query_length": 5,
"type": "external_select",
},
)
self.assertDictEqual(
ExternalDataSelectElement(
placeholder="selectedValue",
action_id="dropdown",
confirm=ConfirmObject(title="title", text="text"),
).to_dict(),
{
"placeholder": {
"emoji": True,
"text": "selectedValue",
"type": "plain_text",
},
"action_id": "dropdown",
"confirm": ConfirmObject(title="title", text="text").to_dict("block"),
"type": "external_select",
},
)
class DynamicDropdownTests(unittest.TestCase):
dynamic_types = {UserSelectElement, ConversationSelectElement, ChannelSelectElement}
def test_json(self):
for dropdown_type in self.dynamic_types:
with self.subTest(dropdown_type=dropdown_type):
self.assertDictEqual(
dropdown_type(
placeholder="abc",
action_id="dropdown",
# somewhat silly abuse of kwargs ahead:
**{f"initial_{dropdown_type.initial_object_type}": "def"},
).to_dict(),
{
"placeholder": {
"emoji": True,
"text": "abc",
"type": "plain_text",
},
"action_id": "dropdown",
f"initial_{dropdown_type.initial_object_type}": "def",
"type": f"{dropdown_type.initial_object_type}s_select",
},
)