|
1 | 1 | import asyncio |
2 | 2 | import json |
| 3 | +import re |
3 | 4 | from random import random |
4 | 5 | from time import time |
5 | 6 |
|
@@ -400,6 +401,166 @@ async def handler2(say: AsyncSay): |
400 | 401 | await asyncio.sleep(1) # wait a bit after auto ack() |
401 | 402 | assert self.mock_received_requests["/chat.postMessage"] == 2 |
402 | 403 |
|
| 404 | + message_file_share_body = { |
| 405 | + "token": "verification-token", |
| 406 | + "team_id": "T111", |
| 407 | + "api_app_id": "A111", |
| 408 | + "event": { |
| 409 | + "type": "message", |
| 410 | + "text": "Here is your file!", |
| 411 | + "files": [ |
| 412 | + { |
| 413 | + "id": "F111", |
| 414 | + "created": 1610493713, |
| 415 | + "timestamp": 1610493713, |
| 416 | + "name": "test.png", |
| 417 | + "title": "test.png", |
| 418 | + "mimetype": "image/png", |
| 419 | + "filetype": "png", |
| 420 | + "pretty_type": "PNG", |
| 421 | + "user": "U111", |
| 422 | + "editable": False, |
| 423 | + "size": 42706, |
| 424 | + "mode": "hosted", |
| 425 | + "is_external": False, |
| 426 | + "external_type": "", |
| 427 | + "is_public": False, |
| 428 | + "public_url_shared": False, |
| 429 | + "display_as_bot": False, |
| 430 | + "username": "", |
| 431 | + "url_private": "https://files.slack.com/files-pri/T111-F111/test.png", |
| 432 | + "url_private_download": "https://files.slack.com/files-pri/T111-F111/download/test.png", |
| 433 | + "thumb_64": "https://files.slack.com/files-tmb/T111-F111-8d3f9a6d4b/test_64.png", |
| 434 | + "thumb_80": "https://files.slack.com/files-tmb/T111-F111-8d3f9a6d4b/test_80.png", |
| 435 | + "thumb_360": "https://files.slack.com/files-tmb/T111-F111-8d3f9a6d4b/test_360.png", |
| 436 | + "thumb_360_w": 358, |
| 437 | + "thumb_360_h": 360, |
| 438 | + "thumb_480": "https://files.slack.com/files-tmb/T111-F111-8d3f9a6d4b/test_480.png", |
| 439 | + "thumb_480_w": 477, |
| 440 | + "thumb_480_h": 480, |
| 441 | + "thumb_160": "https://files.slack.com/files-tmb/T111-F111-8d3f9a6d4b/test_160.png", |
| 442 | + "thumb_720": "https://files.slack.com/files-tmb/T111-F111-8d3f9a6d4b/test_720.png", |
| 443 | + "thumb_720_w": 716, |
| 444 | + "thumb_720_h": 720, |
| 445 | + "original_w": 736, |
| 446 | + "original_h": 740, |
| 447 | + "thumb_tiny": "xxx", |
| 448 | + "permalink": "https://xxx.slack.com/files/U111/F111/test.png", |
| 449 | + "permalink_public": "https://slack-files.com/T111-F111-3e534ef8ca", |
| 450 | + "has_rich_preview": False, |
| 451 | + } |
| 452 | + ], |
| 453 | + "upload": False, |
| 454 | + "blocks": [ |
| 455 | + { |
| 456 | + "type": "rich_text", |
| 457 | + "block_id": "gvM", |
| 458 | + "elements": [ |
| 459 | + { |
| 460 | + "type": "rich_text_section", |
| 461 | + "elements": [ |
| 462 | + {"type": "text", "text": "Here is your file!"} |
| 463 | + ], |
| 464 | + } |
| 465 | + ], |
| 466 | + } |
| 467 | + ], |
| 468 | + "user": "U111", |
| 469 | + "display_as_bot": False, |
| 470 | + "ts": "1610493715.001000", |
| 471 | + "channel": "G111", |
| 472 | + "subtype": "file_share", |
| 473 | + "event_ts": "1610493715.001000", |
| 474 | + "channel_type": "group", |
| 475 | + }, |
| 476 | + "type": "event_callback", |
| 477 | + "event_id": "Ev111", |
| 478 | + "event_time": 1610493715, |
| 479 | + "authorizations": [ |
| 480 | + { |
| 481 | + "enterprise_id": None, |
| 482 | + "team_id": "T111", |
| 483 | + "user_id": "U111", |
| 484 | + "is_bot": True, |
| 485 | + "is_enterprise_install": False, |
| 486 | + } |
| 487 | + ], |
| 488 | + "is_ext_shared_channel": False, |
| 489 | + "event_context": "1-message-T111-G111", |
| 490 | + } |
| 491 | + |
| 492 | + @pytest.mark.asyncio |
| 493 | + async def test_message_subtypes_0(self): |
| 494 | + app = AsyncApp(client=self.web_client, signing_secret=self.signing_secret) |
| 495 | + app._client = AsyncWebClient( |
| 496 | + token="uninstalled-revoked", base_url=self.mock_api_server_base_url |
| 497 | + ) |
| 498 | + |
| 499 | + @app.event({"type": "message", "subtype": "file_share"}) |
| 500 | + async def handler1(event): |
| 501 | + assert event["subtype"] == "file_share" |
| 502 | + |
| 503 | + timestamp, body = str(int(time())), json.dumps(self.message_file_share_body) |
| 504 | + request: AsyncBoltRequest = AsyncBoltRequest( |
| 505 | + body=body, headers=self.build_headers(timestamp, body) |
| 506 | + ) |
| 507 | + response = await app.async_dispatch(request) |
| 508 | + assert response.status == 200 |
| 509 | + |
| 510 | + @pytest.mark.asyncio |
| 511 | + async def test_message_subtypes_1(self): |
| 512 | + app = AsyncApp(client=self.web_client, signing_secret=self.signing_secret) |
| 513 | + app._client = AsyncWebClient( |
| 514 | + token="uninstalled-revoked", base_url=self.mock_api_server_base_url |
| 515 | + ) |
| 516 | + |
| 517 | + @app.event({"type": "message", "subtype": re.compile("file_.+")}) |
| 518 | + async def handler1(event): |
| 519 | + assert event["subtype"] == "file_share" |
| 520 | + |
| 521 | + timestamp, body = str(int(time())), json.dumps(self.message_file_share_body) |
| 522 | + request: AsyncBoltRequest = AsyncBoltRequest( |
| 523 | + body=body, headers=self.build_headers(timestamp, body) |
| 524 | + ) |
| 525 | + response = await app.async_dispatch(request) |
| 526 | + assert response.status == 200 |
| 527 | + |
| 528 | + @pytest.mark.asyncio |
| 529 | + async def test_message_subtypes_2(self): |
| 530 | + app = AsyncApp(client=self.web_client, signing_secret=self.signing_secret) |
| 531 | + app._client = AsyncWebClient( |
| 532 | + token="uninstalled-revoked", base_url=self.mock_api_server_base_url |
| 533 | + ) |
| 534 | + |
| 535 | + @app.event({"type": "message", "subtype": ["file_share"]}) |
| 536 | + async def handler1(event): |
| 537 | + assert event["subtype"] == "file_share" |
| 538 | + |
| 539 | + timestamp, body = str(int(time())), json.dumps(self.message_file_share_body) |
| 540 | + request: AsyncBoltRequest = AsyncBoltRequest( |
| 541 | + body=body, headers=self.build_headers(timestamp, body) |
| 542 | + ) |
| 543 | + response = await app.async_dispatch(request) |
| 544 | + assert response.status == 200 |
| 545 | + |
| 546 | + @pytest.mark.asyncio |
| 547 | + async def test_message_subtypes_3(self): |
| 548 | + app = AsyncApp(client=self.web_client, signing_secret=self.signing_secret) |
| 549 | + app._client = AsyncWebClient( |
| 550 | + token="uninstalled-revoked", base_url=self.mock_api_server_base_url |
| 551 | + ) |
| 552 | + |
| 553 | + @app.event("message") |
| 554 | + async def handler1(event): |
| 555 | + assert event["subtype"] == "file_share" |
| 556 | + |
| 557 | + timestamp, body = str(int(time())), json.dumps(self.message_file_share_body) |
| 558 | + request: AsyncBoltRequest = AsyncBoltRequest( |
| 559 | + body=body, headers=self.build_headers(timestamp, body) |
| 560 | + ) |
| 561 | + response = await app.async_dispatch(request) |
| 562 | + assert response.status == 200 |
| 563 | + |
403 | 564 |
|
404 | 565 | app_mention_body = { |
405 | 566 | "token": "verification_token", |
|
0 commit comments