-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kirill Sokolov
committed
Aug 16, 2024
1 parent
e5237e5
commit cb67858
Showing
8 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from dataclasses import dataclass | ||
from typing import Dict, Any, Literal | ||
from uuid import UUID | ||
|
||
from pydantic import Field | ||
|
||
from pybotx.models.bot_account import BotAccount | ||
from pybotx.models.api_base import VerifiedPayloadBaseModel | ||
from pybotx.models.base_command import BotCommandBase, BaseBotAPIContext, BotAPIBaseCommand, \ | ||
BotAPIBaseSystemEventPayload | ||
from pybotx.models.enums import BotAPISystemEventTypes | ||
|
||
|
||
@dataclass | ||
class EventDelete(BotCommandBase): | ||
"""Event `system:event_delete`. | ||
Attributes: | ||
sync_id: ID of the deleted message. | ||
""" | ||
|
||
sync_id: UUID | ||
|
||
|
||
class BotAPIEventDeleteData(VerifiedPayloadBaseModel): | ||
sync_id: UUID | ||
|
||
|
||
class BotAPIEventDeletePayload(BotAPIBaseSystemEventPayload): | ||
body: Literal[BotAPISystemEventTypes.EVENT_DELETE] | ||
data: BotAPIEventDeleteData | ||
|
||
|
||
class BotAPIEventDelete(BotAPIBaseCommand): | ||
payload: BotAPIEventDeletePayload = Field(..., alias="command") | ||
sender: BaseBotAPIContext = Field(..., alias="from") | ||
|
||
def to_domain(self, raw_command: Dict[str, Any]) -> EventDelete: | ||
return EventDelete( | ||
bot=BotAccount( | ||
id=self.bot_id, | ||
host=self.sender.host, | ||
), | ||
sync_id=self.sync_id, | ||
raw_command=raw_command | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from typing import Optional | ||
from uuid import UUID | ||
|
||
import pytest | ||
|
||
from pybotx import ( | ||
Bot, | ||
BotAccountWithSecret, | ||
HandlerCollector, | ||
lifespan_wrapper, | ||
) | ||
from pybotx.models.system_events.event_delete import EventDelete | ||
from pybotx.models.bot_account import BotAccount | ||
|
||
pytestmark = [ | ||
pytest.mark.asyncio, | ||
pytest.mark.mock_authorization, | ||
pytest.mark.usefixtures("respx_mock"), | ||
] | ||
|
||
|
||
async def test__chat_deleted_by_user__succeed( | ||
bot_account: BotAccountWithSecret, | ||
) -> None: | ||
# - Arrange - | ||
payload = { | ||
"sync_id": "a465f0f3-1354-491c-8f11-f400164295cb", | ||
"command": { | ||
"body": "system:event_delete", | ||
"data": { | ||
"sync_id": "6fa5f1e9-1453-0ad7-2d6d-b791467e382a", | ||
}, | ||
"command_type": "system", | ||
"metadata": {}, | ||
}, | ||
"async_files": [], | ||
"attachments": [], | ||
"entities": [], | ||
"from": { | ||
"user_huid": None, | ||
"group_chat_id": None, | ||
"ad_login": None, | ||
"ad_domain": None, | ||
"username": None, | ||
"chat_type": None, | ||
"manufacturer": None, | ||
"device": None, | ||
"device_software": None, | ||
"device_meta": {}, | ||
"platform": None, | ||
"platform_package_id": None, | ||
"is_admin": None, | ||
"is_creator": None, | ||
"app_version": None, | ||
"locale": "en", | ||
"host": "cts.example.com", | ||
}, | ||
"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46", | ||
"proto_version": 4, | ||
"source_sync_id": None, | ||
} | ||
|
||
collector = HandlerCollector() | ||
event_deleted: Optional[EventDelete] = None | ||
|
||
@collector.event_delete | ||
async def event_delete_handler(event: EventDelete, bot: Bot) -> None: | ||
nonlocal event_deleted | ||
event_deleted = event | ||
# Drop `raw_command` from asserting | ||
event_deleted.raw_command = None | ||
|
||
built_bot = Bot(collectors=[collector], bot_accounts=[bot_account]) | ||
|
||
# - Act - | ||
async with lifespan_wrapper(built_bot) as bot: | ||
bot.async_execute_raw_bot_command(payload, verify_request=False) | ||
|
||
# - Assert - | ||
assert event_deleted == EventDelete( | ||
sync_id=UUID("a465f0f3-1354-491c-8f11-f400164295cb"), | ||
bot=BotAccount( | ||
id=UUID("24348246-6791-4ac0-9d86-b948cd6a0e46"), | ||
host="cts.example.com", | ||
), | ||
raw_command=None, | ||
) |