Skip to content

Commit ebc67bb

Browse files
Add unit tests
1 parent 807e53b commit ebc67bb

File tree

6 files changed

+193
-5
lines changed

6 files changed

+193
-5
lines changed

tests/scenario_tests/test_events_assistant.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,17 @@ def start_thread(say: Say, set_suggested_prompts: SetSuggestedPrompts, set_statu
192192
say("Hi, how can I help you today?")
193193
state["called"] = True
194194

195-
@app.event("message")
196-
def handle_message(say: Say, context: BoltContext, body: dict):
197-
if context.get("set_status") is not None:
198-
assert say.thread_ts == context.thread_ts
195+
@app.message()
196+
def handle_user_message(say: Say, set_status: SetStatus, context: BoltContext):
197+
assert context.channel_id == "D111"
198+
assert context.thread_ts == "1726133698.626339"
199+
assert say.thread_ts == context.thread_ts
200+
try:
201+
set_status("is typing...")
202+
say("Here you are!")
199203
state["called"] = True
204+
except Exception as e:
205+
say(f"Oops, something went wrong (error: {e}")
200206

201207
request = BoltRequest(body=thread_started_event_body, mode="socket_mode")
202208
response = app.dispatch(request)

tests/scenario_tests_async/test_events_assistant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async def start_thread(
153153
await say("Hi, how can I help you today?")
154154
state["called"] = True
155155

156-
@app.event("message")
156+
@app.message()
157157
async def handle_message(say: AsyncSay, context: AsyncBoltContext):
158158
if context.get("set_status") is not None:
159159
assert say.thread_ts == context.thread_ts

tests/slack_bolt/middleware/attaching_agent_kwargs/__init__.py

Whitespace-only changes.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from slack_sdk import WebClient
2+
3+
from slack_bolt.middleware.attaching_agent_kwargs import AttachingAgentKwargs
4+
from slack_bolt.request import BoltRequest
5+
from slack_bolt.response import BoltResponse
6+
from tests.scenario_tests.test_events_assistant import (
7+
thread_started_event_body,
8+
user_message_event_body,
9+
channel_user_message_event_body,
10+
)
11+
12+
13+
def next():
14+
return BoltResponse(status=200)
15+
16+
17+
AGENT_KWARGS = ("say", "set_status", "set_title", "set_suggested_prompts", "get_thread_context", "save_thread_context")
18+
19+
20+
class TestAttachingAgentKwargs:
21+
def test_assistant_event_attaches_kwargs(self):
22+
middleware = AttachingAgentKwargs()
23+
req = BoltRequest(body=thread_started_event_body, mode="socket_mode")
24+
req.context["client"] = WebClient(token="xoxb-test")
25+
resp = BoltResponse(status=404)
26+
27+
resp = middleware.process(req=req, resp=resp, next=next)
28+
29+
assert resp.status == 200
30+
for key in AGENT_KWARGS:
31+
assert key in req.context, f"{key} should be set on context"
32+
assert req.context["say"].thread_ts == "1726133698.626339"
33+
34+
def test_user_message_assistant_event_attaches_kwargs(self):
35+
middleware = AttachingAgentKwargs()
36+
req = BoltRequest(body=user_message_event_body, mode="socket_mode")
37+
req.context["client"] = WebClient(token="xoxb-test")
38+
resp = BoltResponse(status=404)
39+
40+
resp = middleware.process(req=req, resp=resp, next=next)
41+
42+
assert resp.status == 200
43+
for key in AGENT_KWARGS:
44+
assert key in req.context, f"{key} should be set on context"
45+
assert req.context["say"].thread_ts == "1726133698.626339"
46+
47+
def test_non_assistant_event_does_not_attach_kwargs(self):
48+
middleware = AttachingAgentKwargs()
49+
req = BoltRequest(body=channel_user_message_event_body, mode="socket_mode")
50+
req.context["client"] = WebClient(token="xoxb-test")
51+
resp = BoltResponse(status=404)
52+
53+
resp = middleware.process(req=req, resp=resp, next=next)
54+
55+
assert resp.status == 200
56+
for key in AGENT_KWARGS:
57+
assert key not in req.context, f"{key} should not be set on context"
58+
59+
def test_non_event_body_does_not_attach_kwargs(self):
60+
middleware = AttachingAgentKwargs()
61+
req = BoltRequest(body="payload={}", headers={})
62+
resp = BoltResponse(status=404)
63+
64+
resp = middleware.process(req=req, resp=resp, next=next)
65+
66+
assert resp.status == 200
67+
for key in AGENT_KWARGS:
68+
assert key not in req.context, f"{key} should not be set on context"
69+
70+
def test_next_always_called(self):
71+
middleware = AttachingAgentKwargs()
72+
call_state = {"called": False}
73+
74+
def tracking_next():
75+
call_state["called"] = True
76+
return BoltResponse(status=200)
77+
78+
# With assistant event
79+
req = BoltRequest(body=thread_started_event_body, mode="socket_mode")
80+
req.context["client"] = WebClient(token="xoxb-test")
81+
middleware.process(req=req, resp=BoltResponse(status=404), next=tracking_next)
82+
assert call_state["called"] is True
83+
84+
# With non-event body
85+
call_state["called"] = False
86+
req = BoltRequest(body="payload={}", headers={})
87+
middleware.process(req=req, resp=BoltResponse(status=404), next=tracking_next)
88+
assert call_state["called"] is True

tests/slack_bolt_async/middleware/attaching_agent_kwargs/__init__.py

Whitespace-only changes.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import pytest
2+
from slack_sdk.web.async_client import AsyncWebClient
3+
4+
from slack_bolt.middleware.attaching_agent_kwargs.async_attaching_agent_kwargs import AsyncAttachingAgentKwargs
5+
from slack_bolt.request.async_request import AsyncBoltRequest
6+
from slack_bolt.response import BoltResponse
7+
from tests.scenario_tests_async.test_events_assistant import (
8+
thread_started_event_body,
9+
user_message_event_body,
10+
channel_user_message_event_body,
11+
)
12+
13+
14+
async def next():
15+
return BoltResponse(status=200)
16+
17+
18+
AGENT_KWARGS = ("say", "set_status", "set_title", "set_suggested_prompts", "get_thread_context", "save_thread_context")
19+
20+
21+
class TestAsyncAttachingAgentKwargs:
22+
@pytest.mark.asyncio
23+
async def test_assistant_event_attaches_kwargs(self):
24+
middleware = AsyncAttachingAgentKwargs()
25+
req = AsyncBoltRequest(body=thread_started_event_body, mode="socket_mode")
26+
req.context["client"] = AsyncWebClient(token="xoxb-test")
27+
resp = BoltResponse(status=404)
28+
29+
resp = await middleware.async_process(req=req, resp=resp, next=next)
30+
31+
assert resp.status == 200
32+
for key in AGENT_KWARGS:
33+
assert key in req.context, f"{key} should be set on context"
34+
assert req.context["say"].thread_ts == "1726133698.626339"
35+
36+
@pytest.mark.asyncio
37+
async def test_user_message_assistant_event_attaches_kwargs(self):
38+
middleware = AsyncAttachingAgentKwargs()
39+
req = AsyncBoltRequest(body=user_message_event_body, mode="socket_mode")
40+
req.context["client"] = AsyncWebClient(token="xoxb-test")
41+
resp = BoltResponse(status=404)
42+
43+
resp = await middleware.async_process(req=req, resp=resp, next=next)
44+
45+
assert resp.status == 200
46+
for key in AGENT_KWARGS:
47+
assert key in req.context, f"{key} should be set on context"
48+
assert req.context["say"].thread_ts == "1726133698.626339"
49+
50+
@pytest.mark.asyncio
51+
async def test_non_assistant_event_does_not_attach_kwargs(self):
52+
middleware = AsyncAttachingAgentKwargs()
53+
req = AsyncBoltRequest(body=channel_user_message_event_body, mode="socket_mode")
54+
req.context["client"] = AsyncWebClient(token="xoxb-test")
55+
resp = BoltResponse(status=404)
56+
57+
resp = await middleware.async_process(req=req, resp=resp, next=next)
58+
59+
assert resp.status == 200
60+
for key in AGENT_KWARGS:
61+
assert key not in req.context, f"{key} should not be set on context"
62+
63+
@pytest.mark.asyncio
64+
async def test_non_event_body_does_not_attach_kwargs(self):
65+
middleware = AsyncAttachingAgentKwargs()
66+
req = AsyncBoltRequest(body="payload={}", headers={})
67+
resp = BoltResponse(status=404)
68+
69+
resp = await middleware.async_process(req=req, resp=resp, next=next)
70+
71+
assert resp.status == 200
72+
for key in AGENT_KWARGS:
73+
assert key not in req.context, f"{key} should not be set on context"
74+
75+
@pytest.mark.asyncio
76+
async def test_next_always_called(self):
77+
middleware = AsyncAttachingAgentKwargs()
78+
call_state = {"called": False}
79+
80+
async def tracking_next():
81+
call_state["called"] = True
82+
return BoltResponse(status=200)
83+
84+
# With assistant event
85+
req = AsyncBoltRequest(body=thread_started_event_body, mode="socket_mode")
86+
req.context["client"] = AsyncWebClient(token="xoxb-test")
87+
await middleware.async_process(req=req, resp=BoltResponse(status=404), next=tracking_next)
88+
assert call_state["called"] is True
89+
90+
# With non-event body
91+
call_state["called"] = False
92+
req = AsyncBoltRequest(body="payload={}", headers={})
93+
await middleware.async_process(req=req, resp=BoltResponse(status=404), next=tracking_next)
94+
assert call_state["called"] is True

0 commit comments

Comments
 (0)