|
7 | 7 |
|
8 | 8 | import inspect |
9 | 9 | import logging |
| 10 | +import time |
10 | 11 | from unittest.mock import AsyncMock, MagicMock, patch |
11 | 12 |
|
12 | 13 | import pytest |
@@ -215,3 +216,105 @@ async def test_get_backend_auth_headers_returns_headers_on_success(self): |
215 | 216 |
|
216 | 217 | assert "Authorization" in result |
217 | 218 | assert "Bearer" in result["Authorization"] |
| 219 | + |
| 220 | + |
| 221 | +@pytest.mark.parametrize( |
| 222 | + "body", |
| 223 | + [b"", b"not json", b"[1, 2]"], |
| 224 | + ids=["empty", "invalid", "not-an-object"], |
| 225 | +) |
| 226 | +async def test_events_endpoint_rejects_bad_body(bot, aiohttp_client, body): |
| 227 | + """A body that is not a JSON object returns 400, not an unhandled 500.""" |
| 228 | + client = await aiohttp_client(bot) |
| 229 | + |
| 230 | + response = await client.post( |
| 231 | + "/slack/events", data=body, headers={"Content-Type": "application/json"} |
| 232 | + ) |
| 233 | + assert response.status == 400 |
| 234 | + |
| 235 | + |
| 236 | +async def test_url_verification_without_token_returns_500(bot, aiohttp_client): |
| 237 | + """A url_verification payload with no token is refused, not a KeyError.""" |
| 238 | + client = await aiohttp_client(bot) |
| 239 | + |
| 240 | + response = await client.post( |
| 241 | + "/slack/events", json={"type": "url_verification", "challenge": "abc"} |
| 242 | + ) |
| 243 | + assert response.status == 500 |
| 244 | + |
| 245 | + |
| 246 | +async def test_url_verification_with_valid_token(bot, aiohttp_client): |
| 247 | + client = await aiohttp_client(bot) |
| 248 | + |
| 249 | + response = await client.post( |
| 250 | + "/slack/events", |
| 251 | + json={"type": "url_verification", "token": "supersecuretoken", "challenge": "abc"}, |
| 252 | + ) |
| 253 | + assert response.status == 200 |
| 254 | + assert await response.text() == "abc" |
| 255 | + |
| 256 | + |
| 257 | +@pytest.fixture |
| 258 | +async def signed_bot(): |
| 259 | + """A bot whose Slack plugin has a signing_secret set (production mode).""" |
| 260 | + import aiohttp |
| 261 | + |
| 262 | + from pybot._vendor.sirbot import SirBot |
| 263 | + from pybot._vendor.sirbot.plugins.slack import SlackPlugin |
| 264 | + |
| 265 | + b = SirBot() |
| 266 | + b["http_session"] = aiohttp.ClientSession() |
| 267 | + slack = SlackPlugin( |
| 268 | + token="token", |
| 269 | + signing_secret="signing-secret", |
| 270 | + bot_user_id="bot_user_id", |
| 271 | + bot_id="bot_id", |
| 272 | + ) |
| 273 | + endpoints.slack.create_endpoints(slack) |
| 274 | + b.load_plugin(slack) |
| 275 | + await slack._initialize_api(b) |
| 276 | + yield b |
| 277 | + await b["http_session"].close() |
| 278 | + |
| 279 | + |
| 280 | +BAD_SIGNATURE_HEADERS = [ |
| 281 | + pytest.param({}, id="no-headers"), |
| 282 | + pytest.param({"X-Slack-Request-Timestamp": "abc", "X-Slack-Signature": "v0=x"}, id="bad-ts"), |
| 283 | + pytest.param({"X-Slack-Request-Timestamp": str(int(time.time()))}, id="missing-signature"), |
| 284 | + pytest.param({"X-Slack-Signature": "v0=x"}, id="missing-timestamp"), |
| 285 | +] |
| 286 | + |
| 287 | + |
| 288 | +@pytest.mark.parametrize("headers", BAD_SIGNATURE_HEADERS) |
| 289 | +async def test_signed_events_endpoint_rejects_bad_headers(signed_bot, aiohttp_client, headers): |
| 290 | + """Missing or malformed Slack signature headers return 401, not a 500.""" |
| 291 | + client = await aiohttp_client(signed_bot) |
| 292 | + |
| 293 | + response = await client.post("/slack/events", json={}, headers=headers) |
| 294 | + assert response.status == 401 |
| 295 | + |
| 296 | + |
| 297 | +@pytest.mark.parametrize("headers", BAD_SIGNATURE_HEADERS) |
| 298 | +async def test_signed_url_verification_rejects_bad_headers( |
| 299 | + signed_bot, aiohttp_client, headers, caplog |
| 300 | +): |
| 301 | + """url_verification keeps its 500 for a failed signature, without an unhandled exception.""" |
| 302 | + client = await aiohttp_client(signed_bot) |
| 303 | + caplog.set_level(logging.ERROR, logger="aiohttp.server") |
| 304 | + |
| 305 | + response = await client.post( |
| 306 | + "/slack/events", json={"type": "url_verification", "challenge": "abc"}, headers=headers |
| 307 | + ) |
| 308 | + assert response.status == 500 |
| 309 | + # aiohttp logs an unhandled handler exception on "aiohttp.server" with exc_info. |
| 310 | + # Check the record, not the message text, so a reworded message cannot hide it. |
| 311 | + unhandled = [r for r in caplog.records if r.name == "aiohttp.server" and r.exc_info] |
| 312 | + assert not unhandled |
| 313 | + |
| 314 | + |
| 315 | +@pytest.mark.parametrize("path", ["/slack/commands", "/slack/actions"]) |
| 316 | +async def test_signed_commands_and_actions_reject_missing_headers(signed_bot, aiohttp_client, path): |
| 317 | + client = await aiohttp_client(signed_bot) |
| 318 | + |
| 319 | + response = await client.post(path, data={"payload": "{}"}) |
| 320 | + assert response.status == 401 |
0 commit comments