Skip to content

Commit a1feadd

Browse files
committed
Add more unit tests
1 parent 2216935 commit a1feadd

File tree

5 files changed

+244
-2
lines changed

5 files changed

+244
-2
lines changed

tests/slack_bolt/authorization/test_authorize.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from logging import Logger
44
from typing import Optional
55

6+
import pytest
67
from slack_sdk import WebClient
78
from slack_sdk.oauth import InstallationStore
89
from slack_sdk.oauth.installation_store import Bot, Installation
910

1011
from slack_bolt import BoltContext
11-
from slack_bolt.authorization.authorize import InstallationStoreAuthorize
12+
from slack_bolt.authorization.authorize import InstallationStoreAuthorize, Authorize
1213
from tests.mock_web_api_server import (
1314
cleanup_mock_web_api_server,
1415
setup_mock_web_api_server,
@@ -24,6 +25,16 @@ def setup_method(self):
2425
def teardown_method(self):
2526
cleanup_mock_web_api_server(self)
2627

28+
def test_root_class(self):
29+
authorize = Authorize()
30+
with pytest.raises(NotImplementedError):
31+
authorize(
32+
context=BoltContext(),
33+
enterprise_id="E111",
34+
team_id="T111",
35+
user_id="U111",
36+
)
37+
2738
def test_installation_store_legacy(self):
2839
installation_store = LegacyMemoryInstallationStore()
2940
authorize = InstallationStoreAuthorize(

tests/slack_bolt/listener_matcher/test_builtins.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
block_action,
88
action,
99
workflow_step_execute,
10+
event,
11+
shortcut,
1012
)
1113

1214

@@ -52,6 +54,7 @@ def test_block_action(self):
5254
action({"action_id": re.compile("invalid_.+")}).matches(req, resp) is False
5355
)
5456

57+
# block_id + action_id
5558
assert (
5659
action({"action_id": "valid_action_id", "block_id": "b"}).matches(req, resp)
5760
is True
@@ -100,6 +103,26 @@ def test_block_action(self):
100103
is False
101104
)
102105

106+
# with type
107+
assert (
108+
action({"action_id": "valid_action_id", "type": "block_actions"}).matches(
109+
req, resp
110+
)
111+
is True
112+
)
113+
assert (
114+
action(
115+
{"callback_id": "valid_action_id", "type": "interactive_message"}
116+
).matches(req, resp)
117+
is False
118+
)
119+
assert (
120+
action(
121+
{"callback_id": "valid_action_id", "type": "workflow_step_edit"}
122+
).matches(req, resp)
123+
is False
124+
)
125+
103126
def test_workflow_step_execute(self):
104127
payload = {
105128
"team_id": "T111",
@@ -135,3 +158,137 @@ def test_workflow_step_execute(self):
135158

136159
m = workflow_step_execute(re.compile("copy_.+"))
137160
assert m.matches(request, None) == True
161+
162+
def test_events(self):
163+
request = BoltRequest(body=json.dumps(event_payload))
164+
165+
m = event("app_mention")
166+
assert m.matches(request, None)
167+
m = event({"type": "app_mention"})
168+
assert m.matches(request, None)
169+
m = event("message")
170+
assert not m.matches(request, None)
171+
m = event({"type": "message"})
172+
assert not m.matches(request, None)
173+
174+
request = BoltRequest(body=f"payload={quote(json.dumps(shortcut_payload))}")
175+
176+
m = event("app_mention")
177+
assert not m.matches(request, None)
178+
m = event({"type": "app_mention"})
179+
assert not m.matches(request, None)
180+
181+
def test_global_shortcuts(self):
182+
request = BoltRequest(body=f"payload={quote(json.dumps(shortcut_payload))}")
183+
184+
m = shortcut("test-shortcut")
185+
assert m.matches(request, None)
186+
m = shortcut({"callback_id": "test-shortcut", "type": "shortcut"})
187+
assert m.matches(request, None)
188+
189+
m = shortcut("test-shortcut!!!")
190+
assert not m.matches(request, None)
191+
m = shortcut({"callback_id": "test-shortcut", "type": "message_action"})
192+
assert not m.matches(request, None)
193+
m = shortcut({"callback_id": "test-shortcut!!!", "type": "shortcut"})
194+
assert not m.matches(request, None)
195+
196+
def test_message_shortcuts(self):
197+
request = BoltRequest(
198+
body=f"payload={quote(json.dumps(message_shortcut_payload))}"
199+
)
200+
201+
m = shortcut("test-shortcut")
202+
assert m.matches(request, None)
203+
m = shortcut({"callback_id": "test-shortcut", "type": "message_action"})
204+
assert m.matches(request, None)
205+
206+
m = shortcut("test-shortcut!!!")
207+
assert not m.matches(request, None)
208+
m = shortcut({"callback_id": "test-shortcut", "type": "shortcut"})
209+
assert not m.matches(request, None)
210+
m = shortcut({"callback_id": "test-shortcut!!!", "type": "message_action"})
211+
assert not m.matches(request, None)
212+
213+
214+
event_payload = {
215+
"team_id": "T111",
216+
"enterprise_id": "E111",
217+
"api_app_id": "A111",
218+
"event": {
219+
"type": "app_mention",
220+
"text": "<@W111> Hi there!",
221+
"user": "W222",
222+
"ts": "1595926230.009600",
223+
"event_ts": "1595926230.009600",
224+
},
225+
"type": "event_callback",
226+
"event_id": "Ev111",
227+
"event_time": 1595926230,
228+
"authorizations": [
229+
{
230+
"enterprise_id": "E111",
231+
"team_id": "T111",
232+
"user_id": "W111",
233+
"is_bot": True,
234+
"is_enterprise_install": True,
235+
}
236+
],
237+
}
238+
239+
shortcut_payload = {
240+
"type": "shortcut",
241+
"token": "verification_token",
242+
"action_ts": "111.111",
243+
"team": {
244+
"id": "T111",
245+
"domain": "workspace-domain",
246+
"enterprise_id": "E111",
247+
"enterprise_name": "Org Name",
248+
},
249+
"user": {"id": "W111", "username": "primary-owner", "team_id": "T111"},
250+
"callback_id": "test-shortcut",
251+
"trigger_id": "111.111.xxxxxx",
252+
}
253+
254+
255+
message_shortcut_payload = {
256+
"type": "message_action",
257+
"token": "verification_token",
258+
"action_ts": "1583637157.207593",
259+
"team": {
260+
"id": "T111",
261+
"domain": "test-test",
262+
"enterprise_id": "E111",
263+
"enterprise_name": "Org Name",
264+
},
265+
"user": {"id": "W111", "name": "test-test"},
266+
"channel": {"id": "C111", "name": "dev"},
267+
"callback_id": "test-shortcut",
268+
"trigger_id": "111.222.xxx",
269+
"message_ts": "1583636382.000300",
270+
"message": {
271+
"client_msg_id": "zzzz-111-222-xxx-yyy",
272+
"type": "message",
273+
"text": "<@W222> test",
274+
"user": "W111",
275+
"ts": "1583636382.000300",
276+
"team": "T111",
277+
"blocks": [
278+
{
279+
"type": "rich_text",
280+
"block_id": "d7eJ",
281+
"elements": [
282+
{
283+
"type": "rich_text_section",
284+
"elements": [
285+
{"type": "user", "user_id": "U222"},
286+
{"type": "text", "text": " test"},
287+
],
288+
}
289+
],
290+
}
291+
],
292+
},
293+
"response_url": "https://hooks.slack.com/app/T111/111/xxx",
294+
}

tests/slack_bolt/util/__init__.py

Whitespace-only changes.

tests/slack_bolt/util/test_util.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import sys
2+
from typing import Set
3+
4+
import pytest
5+
from slack_sdk.models import JsonObject
6+
7+
from slack_bolt.error import BoltError
8+
from slack_bolt.util.utils import convert_to_dict, get_boot_message
9+
from tests.utils import remove_os_env_temporarily, restore_os_env
10+
11+
12+
class Data:
13+
def __init__(self, name: str):
14+
self.name = name
15+
16+
17+
class SerializableData(JsonObject):
18+
@property
19+
def attributes(self) -> Set[str]:
20+
return {"name"}
21+
22+
def __init__(self, name: str):
23+
self.name = name
24+
25+
26+
class TestUtil:
27+
def setup_method(self):
28+
self.old_os_env = remove_os_env_temporarily()
29+
30+
def teardown_method(self):
31+
restore_os_env(self.old_os_env)
32+
33+
def test_convert_to_dict(self):
34+
assert convert_to_dict({"foo": "bar"}) == {"foo": "bar"}
35+
assert convert_to_dict(SerializableData("baz")) == {"name": "baz"}
36+
37+
def test_convert_to_dict_errors(self):
38+
with pytest.raises(BoltError):
39+
convert_to_dict(None)
40+
with pytest.raises(BoltError):
41+
convert_to_dict(123)
42+
with pytest.raises(BoltError):
43+
convert_to_dict("test")
44+
with pytest.raises(BoltError):
45+
convert_to_dict(Data("baz"))
46+
47+
def test_get_boot_message(self):
48+
assert get_boot_message() == "⚡️ Bolt app is running!"
49+
assert (
50+
get_boot_message(development_server=True)
51+
== "⚡️ Bolt app is running! (development server)"
52+
)
53+
54+
def test_get_boot_message_win32(self):
55+
sys_platform_backup = sys.platform
56+
try:
57+
sys.platform = "win32"
58+
assert get_boot_message() == "Bolt app is running!"
59+
finally:
60+
sys.platform = sys_platform_backup

tests/slack_bolt_async/authorization/test_async_authorize.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
)
1212
from slack_sdk.web.async_client import AsyncWebClient
1313

14-
from slack_bolt.authorization.async_authorize import AsyncInstallationStoreAuthorize
14+
from slack_bolt.authorization.async_authorize import (
15+
AsyncInstallationStoreAuthorize,
16+
AsyncAuthorize,
17+
)
1518
from slack_bolt.context.async_context import AsyncBoltContext
1619
from tests.mock_web_api_server import (
1720
setup_mock_web_api_server,
@@ -38,6 +41,17 @@ def event_loop(self):
3841
finally:
3942
restore_os_env(old_os_env)
4043

44+
@pytest.mark.asyncio
45+
async def test_root_class(self):
46+
authorize = AsyncAuthorize()
47+
with pytest.raises(NotImplementedError):
48+
await authorize(
49+
context=AsyncBoltContext(),
50+
enterprise_id="T111",
51+
team_id="T111",
52+
user_id="U111",
53+
)
54+
4155
@pytest.mark.asyncio
4256
async def test_installation_store_legacy(self):
4357
installation_store = LegacyMemoryInstallationStore()

0 commit comments

Comments
 (0)