forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_actions.py
More file actions
181 lines (157 loc) · 5.87 KB
/
Copy pathtest_actions.py
File metadata and controls
181 lines (157 loc) · 5.87 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
import unittest
from slack.errors import SlackObjectFormationError
from slack.web.classes.actions import (
ActionButton,
ActionChannelSelector,
ActionConversationSelector,
ActionExternalSelector,
ActionLinkButton,
ActionStaticSelector,
ActionUserSelector,
)
from slack.web.classes.objects import ConfirmObject, Option, OptionGroup
from tests.web.classes import STRING_3001_CHARS
class ButtonTests(unittest.TestCase):
def test_json(self):
self.assertDictEqual(
ActionButton(name="button_1", text="Click me!", value="btn_1").to_dict(),
{
"name": "button_1",
"text": "Click me!",
"value": "btn_1",
"type": "button",
},
)
confirm = ConfirmObject(title="confirm_title", text="confirm_text")
self.assertDictEqual(
ActionButton(
name="button_1",
text="Click me!",
value="btn_1",
confirm=confirm,
style="danger",
).to_dict(),
{
"name": "button_1",
"text": "Click me!",
"value": "btn_1",
"type": "button",
"confirm": confirm.to_dict("action"),
"style": "danger",
},
)
def test_value_length(self):
with self.assertRaises(SlackObjectFormationError):
ActionButton(
name="button_1", text="Click me!", value=STRING_3001_CHARS
).to_dict()
def test_style_validator(self):
b = ActionButton(name="button_1", text="Click me!", value="btn_1")
with self.assertRaises(SlackObjectFormationError):
b.style = "abcdefg"
b.to_dict()
b.style = "primary"
b.to_dict()
class LinkButtonTests(unittest.TestCase):
def test_json(self):
self.assertDictEqual(
ActionLinkButton(text="Click me!", url="http://google.com").to_dict(),
{"url": "http://google.com", "text": "Click me!", "type": "button"},
)
class StaticActionSelectorTests(unittest.TestCase):
def setUp(self) -> None:
self.options = [
Option.from_single_value("one"),
Option.from_single_value("two"),
Option.from_single_value("three"),
]
self.option_group = [OptionGroup(label="group_1", options=self.options)]
def test_json(self):
self.assertDictEqual(
ActionStaticSelector(
name="select_1", text="selector_1", options=self.options
).to_dict(),
{
"name": "select_1",
"text": "selector_1",
"options": [o.to_dict("action") for o in self.options],
"type": "select",
"data_source": "static",
},
)
self.assertDictEqual(
ActionStaticSelector(
name="select_1", text="selector_1", options=self.option_group
).to_dict(),
{
"name": "select_1",
"text": "selector_1",
"option_groups": [o.to_dict("action") for o in self.option_group],
"type": "select",
"data_source": "static",
},
)
def test_options_length(self):
with self.assertRaises(SlackObjectFormationError):
ActionStaticSelector(
name="select_1", text="selector_1", options=self.options * 34
).to_dict()
class DynamicActionSelectorTests(unittest.TestCase):
selectors = {ActionUserSelector, ActionChannelSelector, ActionConversationSelector}
def setUp(self) -> None:
self.selected_opt = Option.from_single_value("U12345")
def test_json(self):
for component in self.selectors:
with self.subTest(msg=f"{component} json formation test"):
self.assertDictEqual(
component(name="select_1", text="selector_1").to_dict(),
{
"name": "select_1",
"text": "selector_1",
"type": "select",
"data_source": component.data_source,
},
)
self.assertDictEqual(
component(
name="select_1",
text="selector_1",
# next line is a little silly, but so is writing the test
# three times
**{f"selected_{component.data_source[:-1]}": self.selected_opt},
).to_dict(),
{
"name": "select_1",
"text": "selector_1",
"type": "select",
"data_source": component.data_source,
"selected_options": [self.selected_opt.to_dict("action")],
},
)
class ExternalActionSelectorTests(unittest.TestCase):
def test_json(self):
option = Option.from_single_value("one")
self.assertDictEqual(
ActionExternalSelector(
name="select_1", text="selector_1", min_query_length=3
).to_dict(),
{
"name": "select_1",
"text": "selector_1",
"min_query_length": 3,
"type": "select",
"data_source": "external",
},
)
self.assertDictEqual(
ActionExternalSelector(
name="select_1", text="selector_1", selected_option=option
).to_dict(),
{
"name": "select_1",
"text": "selector_1",
"selected_options": [option.to_dict("action")],
"type": "select",
"data_source": "external",
},
)