forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_blocks.py
More file actions
141 lines (118 loc) · 4.73 KB
/
Copy pathtest_blocks.py
File metadata and controls
141 lines (118 loc) · 4.73 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
import unittest
from slack.errors import SlackObjectFormationError
from slack.web.classes.blocks import (
ActionsBlock,
ContextBlock,
DividerBlock,
ImageBlock,
SectionBlock,
)
from slack.web.classes.elements import ButtonElement, ImageElement, LinkButtonElement
from slack.web.classes.objects import PlainTextObject
from . import STRING_3001_CHARS
class DividerBlockTests(unittest.TestCase):
def test_json(self):
self.assertDictEqual(DividerBlock().to_dict(), {"type": "divider"})
class SectionBlockTests(unittest.TestCase):
def test_json(self):
self.assertDictEqual(
SectionBlock(text="some text", block_id="a_block").to_dict(),
{
"text": {"text": "some text", "type": "mrkdwn", "verbatim": False},
"block_id": "a_block",
"type": "section",
},
)
self.assertDictEqual(
SectionBlock(
text="some text", fields=[f"field{i}" for i in range(5)]
).to_dict(),
{
"text": {"text": "some text", "type": "mrkdwn", "verbatim": False},
"fields": [
{"text": "field0", "type": "mrkdwn", "verbatim": False},
{"text": "field1", "type": "mrkdwn", "verbatim": False},
{"text": "field2", "type": "mrkdwn", "verbatim": False},
{"text": "field3", "type": "mrkdwn", "verbatim": False},
{"text": "field4", "type": "mrkdwn", "verbatim": False},
],
"type": "section",
},
)
button = LinkButtonElement(text="Click me!", url="http://google.com")
self.assertDictEqual(
SectionBlock(text="some text", accessory=button).to_dict(),
{
"text": {"text": "some text", "type": "mrkdwn", "verbatim": False},
"accessory": button.to_dict(),
"type": "section",
},
)
def test_text_or_fields_populated(self):
with self.assertRaises(SlackObjectFormationError):
SectionBlock().to_dict()
def test_fields_length(self):
with self.assertRaises(SlackObjectFormationError):
SectionBlock(fields=[f"field{i}" for i in range(11)]).to_dict()
class ImageBlockTests(unittest.TestCase):
def test_json(self):
self.assertDictEqual(
ImageBlock(
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):
ImageBlock(image_url=STRING_3001_CHARS, alt_text="text").to_dict()
def test_alt_text_length(self):
with self.assertRaises(SlackObjectFormationError):
ImageBlock(
image_url="http://google.com", alt_text=STRING_3001_CHARS
).to_dict()
def test_title_length(self):
with self.assertRaises(SlackObjectFormationError):
ImageBlock(
image_url="http://google.com", alt_text="text", title=STRING_3001_CHARS
).to_dict()
class ActionsBlockTests(unittest.TestCase):
def setUp(self) -> None:
self.elements = [
ButtonElement(text="Click me", action_id="reg_button", value="1"),
LinkButtonElement(text="URL Button", url="http://google.com"),
]
def test_json(self):
self.assertDictEqual(
ActionsBlock(elements=self.elements).to_dict(),
{"elements": [e.to_dict() for e in self.elements], "type": "actions"},
)
def test_elements_length(self):
with self.assertRaises(SlackObjectFormationError):
ActionsBlock(elements=self.elements * 3).to_dict()
class ContextBlockTests(unittest.TestCase):
def setUp(self) -> None:
self.elements = [
ImageElement(image_url="http://google.com", alt_text="google"),
PlainTextObject(text="Just text"),
]
def test_basic_json(self):
d = ContextBlock(elements=self.elements).to_dict()
e = {
"elements": [
{
"type": "image",
"image_url": "http://google.com",
"alt_text": "google",
},
{"type": "plain_text", "emoji": True, "text": "Just text"},
],
"type": "context",
}
self.assertDictEqual(d, e)
def test_elements_length(self):
with self.assertRaises(SlackObjectFormationError):
ContextBlock(elements=self.elements * 6).to_dict()