-
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
Showing
6 changed files
with
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Literal | ||
from uuid import UUID | ||
|
||
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod | ||
from pybotx.client.botx_method import response_exception_thrower | ||
from pybotx.client.exceptions.message import MessageNotFoundError | ||
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel | ||
|
||
|
||
class BotXAPIDeleteEventRequestPayload(UnverifiedPayloadBaseModel): | ||
sync_id: UUID | ||
|
||
@classmethod | ||
def from_domain( | ||
cls, | ||
sync_id: UUID, | ||
) -> "BotXAPIDeleteEventRequestPayload": | ||
return cls(sync_id=sync_id) | ||
|
||
|
||
class BotXAPIDeleteEventResponsePayload(VerifiedPayloadBaseModel): | ||
status: Literal["ok"] | ||
result: str | ||
|
||
|
||
class DeleteEventMethod(AuthorizedBotXMethod): | ||
status_handlers = { | ||
**AuthorizedBotXMethod.status_handlers, | ||
404: response_exception_thrower(MessageNotFoundError), | ||
} | ||
|
||
async def execute( | ||
self, | ||
payload: BotXAPIDeleteEventRequestPayload, | ||
) -> BotXAPIDeleteEventResponsePayload: | ||
path = "/api/v3/botx/events/delete_event" | ||
|
||
response = await self._botx_method_call( | ||
"POST", | ||
self._build_url(path), | ||
json=payload.jsonable_dict(), | ||
) | ||
|
||
return self._verify_and_extract_api_model( | ||
BotXAPIDeleteEventResponsePayload, | ||
response, | ||
) |
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,5 @@ | ||
from pybotx.client.exceptions.base import BaseClientError | ||
|
||
|
||
class MessageNotFoundError(BaseClientError): | ||
"""Message not found.""" |
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,96 @@ | ||
from http import HTTPStatus | ||
from uuid import UUID | ||
|
||
import httpx | ||
import pytest | ||
from respx.router import MockRouter | ||
|
||
from pybotx import ( | ||
Bot, | ||
BotAccountWithSecret, | ||
HandlerCollector, | ||
MessageNotFoundError, | ||
lifespan_wrapper, | ||
) | ||
|
||
pytestmark = [ | ||
pytest.mark.asyncio, | ||
pytest.mark.mock_authorization, | ||
pytest.mark.usefixtures("respx_mock"), | ||
] | ||
|
||
|
||
async def test__delete_message__succeed( | ||
respx_mock: MockRouter, | ||
host: str, | ||
bot_id: UUID, | ||
bot_account: BotAccountWithSecret, | ||
) -> None: | ||
# - Arrange - | ||
endpoint = respx_mock.post( | ||
f"https://{host}/api/v3/botx/events/delete_event", | ||
headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, | ||
json={ | ||
"sync_id": "8ba66c5b-40bf-5c77-911d-519cb4e382e9", | ||
}, | ||
).mock( | ||
return_value=httpx.Response( | ||
HTTPStatus.ACCEPTED, | ||
json={ | ||
"status": "ok", | ||
"result": "event_deleted", | ||
}, | ||
), | ||
) | ||
|
||
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) | ||
|
||
# - Act - | ||
async with lifespan_wrapper(built_bot) as bot: | ||
await bot.delete_message( | ||
bot_id=bot_id, | ||
sync_id=UUID("8ba66c5b-40bf-5c77-911d-519cb4e382e9"), | ||
) | ||
|
||
# - Assert - | ||
assert endpoint.called | ||
|
||
|
||
async def test__delete_message__message_not_found_error_raised( | ||
respx_mock: MockRouter, | ||
host: str, | ||
bot_id: UUID, | ||
bot_account: BotAccountWithSecret, | ||
) -> None: | ||
# - Arrange - | ||
endpoint = respx_mock.post( | ||
f"https://{host}/api/v3/botx/events/delete_event", | ||
headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, | ||
json={ | ||
"sync_id": "fe1f285c-073e-4231-b190-2959f28168cc", | ||
}, | ||
).mock( | ||
return_value=httpx.Response( | ||
HTTPStatus.NOT_FOUND, | ||
json={ | ||
"status": "error", | ||
"reason": "sync_id_not_found", | ||
"errors": [], | ||
"error_data": {}, | ||
}, | ||
), | ||
) | ||
|
||
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) | ||
|
||
# - Act - | ||
async with lifespan_wrapper(built_bot) as bot: | ||
with pytest.raises(MessageNotFoundError) as exc: | ||
await bot.delete_message( | ||
bot_id=bot_id, | ||
sync_id=UUID("fe1f285c-073e-4231-b190-2959f28168cc"), | ||
) | ||
|
||
# - Assert - | ||
assert "sync_id_not_found" in str(exc.value) | ||
assert endpoint.called |