-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathtest_events_agent.py
More file actions
231 lines (193 loc) · 7.51 KB
/
test_events_agent.py
File metadata and controls
231 lines (193 loc) · 7.51 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
import asyncio
import json
import pytest
from slack_sdk.web.async_client import AsyncWebClient
from slack_bolt.agent.async_agent import AsyncBoltAgent
from slack_bolt.app.async_app import AsyncApp
from slack_bolt.context.async_context import AsyncBoltContext
from slack_bolt.request.async_request import AsyncBoltRequest
from slack_bolt.warning import ExperimentalWarning
from tests.mock_web_api_server import (
cleanup_mock_web_api_server_async,
setup_mock_web_api_server_async,
)
from tests.utils import remove_os_env_temporarily, restore_os_env
class TestAsyncEventsAgent:
valid_token = "xoxb-valid"
mock_api_server_base_url = "http://localhost:8888"
web_client = AsyncWebClient(
token=valid_token,
base_url=mock_api_server_base_url,
)
@pytest.fixture(scope="function", autouse=True)
def setup_teardown(self):
old_os_env = remove_os_env_temporarily()
setup_mock_web_api_server_async(self)
try:
yield
finally:
cleanup_mock_web_api_server_async(self)
restore_os_env(old_os_env)
@pytest.mark.asyncio
async def test_agent_injected_for_app_mention(self):
app = AsyncApp(client=self.web_client)
state = {"called": False}
async def assert_target_called():
count = 0
while state["called"] is False and count < 20:
await asyncio.sleep(0.1)
count += 1
assert state["called"] is True
state["called"] = False
@app.event("app_mention")
async def handle_mention(agent: AsyncBoltAgent, context: AsyncBoltContext):
assert agent is not None
assert isinstance(agent, AsyncBoltAgent)
assert context.channel_id == "C111"
state["called"] = True
request = AsyncBoltRequest(body=app_mention_event_body, mode="socket_mode")
response = await app.async_dispatch(request)
assert response.status == 200
await assert_target_called()
@pytest.mark.asyncio
async def test_agent_available_in_action_listener(self):
app = AsyncApp(client=self.web_client)
state = {"called": False}
async def assert_target_called():
count = 0
while state["called"] is False and count < 20:
await asyncio.sleep(0.1)
count += 1
assert state["called"] is True
state["called"] = False
@app.action("test_action")
async def handle_action(ack, agent: AsyncBoltAgent):
await ack()
assert agent is not None
assert isinstance(agent, AsyncBoltAgent)
state["called"] = True
request = AsyncBoltRequest(body=json.dumps(action_event_body), mode="socket_mode")
response = await app.async_dispatch(request)
assert response.status == 200
await assert_target_called()
@pytest.mark.asyncio
async def test_agent_thread_ts_from_event_in_thread(self):
"""Agent gets thread_ts from event when in a thread."""
app = AsyncApp(client=self.web_client)
state = {"thread_ts": None}
async def assert_target_called():
count = 0
while state["thread_ts"] is None and count < 20:
await asyncio.sleep(0.1)
count += 1
assert state["thread_ts"] is not None
@app.event("app_mention")
async def handle_mention(agent: AsyncBoltAgent):
state["thread_ts"] = agent._thread_ts
request = AsyncBoltRequest(body=app_mention_in_thread_body, mode="socket_mode")
response = await app.async_dispatch(request)
assert response.status == 200
await assert_target_called()
# Should use event.thread_ts (the thread root), not event.ts
assert state["thread_ts"] == "1111111111.111111"
@pytest.mark.asyncio
async def test_agent_thread_ts_falls_back_to_ts(self):
"""Agent falls back to event.ts when not in a thread."""
app = AsyncApp(client=self.web_client)
state = {"thread_ts": None}
async def assert_target_called():
count = 0
while state["thread_ts"] is None and count < 20:
await asyncio.sleep(0.1)
count += 1
assert state["thread_ts"] is not None
@app.event("app_mention")
async def handle_mention(agent: AsyncBoltAgent):
state["thread_ts"] = agent._thread_ts
request = AsyncBoltRequest(body=app_mention_event_body, mode="socket_mode")
response = await app.async_dispatch(request)
assert response.status == 200
await assert_target_called()
# Should fall back to event.ts since no thread_ts
assert state["thread_ts"] == "1234567890.123456"
@pytest.mark.asyncio
async def test_agent_kwarg_emits_experimental_warning(self):
app = AsyncApp(client=self.web_client)
state = {"called": False}
async def assert_target_called():
count = 0
while state["called"] is False and count < 20:
await asyncio.sleep(0.1)
count += 1
assert state["called"] is True
state["called"] = False
@app.event("app_mention")
async def handle_mention(agent: AsyncBoltAgent):
state["called"] = True
request = AsyncBoltRequest(body=app_mention_event_body, mode="socket_mode")
with pytest.warns(ExperimentalWarning, match="agent listener argument is experimental"):
response = await app.async_dispatch(request)
assert response.status == 200
await assert_target_called()
# ---- Test event bodies ----
def build_payload(event: dict) -> dict:
return {
"token": "verification_token",
"team_id": "T111",
"enterprise_id": "E111",
"api_app_id": "A111",
"event": event,
"type": "event_callback",
"event_id": "Ev111",
"event_time": 1599616881,
"authorizations": [
{
"enterprise_id": "E111",
"team_id": "T111",
"user_id": "W111",
"is_bot": True,
"is_enterprise_install": False,
}
],
}
app_mention_event_body = build_payload(
{
"type": "app_mention",
"user": "W222",
"text": "<@W111> hello",
"ts": "1234567890.123456",
"channel": "C111",
"event_ts": "1234567890.123456",
}
)
app_mention_in_thread_body = build_payload(
{
"type": "app_mention",
"user": "W222",
"text": "<@W111> hello in thread",
"ts": "2222222222.222222",
"thread_ts": "1111111111.111111", # Thread root timestamp
"channel": "C111",
"event_ts": "2222222222.222222",
}
)
action_event_body = {
"type": "block_actions",
"user": {"id": "W222", "username": "test_user", "name": "test_user", "team_id": "T111"},
"api_app_id": "A111",
"token": "verification_token",
"container": {"type": "message", "message_ts": "1234567890.123456", "channel_id": "C111", "is_ephemeral": False},
"channel": {"id": "C111", "name": "test-channel"},
"team": {"id": "T111", "domain": "test"},
"enterprise": {"id": "E111", "name": "test"},
"trigger_id": "111.222.xxx",
"actions": [
{
"type": "button",
"block_id": "b",
"action_id": "test_action",
"text": {"type": "plain_text", "text": "Button"},
"action_ts": "1234567890.123456",
}
],
}