-
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
5 changed files
with
130 additions
and
0 deletions.
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
Empty file.
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,43 @@ | ||
from typing import List, Literal | ||
from uuid import UUID | ||
|
||
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod | ||
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel | ||
|
||
|
||
class BotXAPIAddBotFunctionRequestPayload(UnverifiedPayloadBaseModel): | ||
group_chat_id: UUID | ||
user_huids: List[UUID] | ||
bot_function: str | ||
|
||
@classmethod | ||
def from_domain( | ||
cls, | ||
chat_id: UUID, | ||
huids: List[UUID], | ||
bot_function: str, | ||
) -> "BotXAPIAddBotFunctionRequestPayload": | ||
return cls(group_chat_id=chat_id, user_huids=huids, bot_function=bot_function) | ||
|
||
|
||
class BotXAPIAddBotFunctionResponsePayload(VerifiedPayloadBaseModel): | ||
status: Literal["ok"] | ||
result: bool | ||
|
||
|
||
class AddBotFunctionMethod(AuthorizedBotXMethod): | ||
async def execute( | ||
self, | ||
payload: BotXAPIAddBotFunctionRequestPayload, | ||
) -> None: | ||
path = "/api/v3/botx/metrics/bot_function" | ||
|
||
response = await self._botx_method_call( | ||
"POST", | ||
self._build_url(path), | ||
json=payload.jsonable_dict(), | ||
) | ||
self._verify_and_extract_api_model( | ||
BotXAPIAddBotFunctionResponsePayload, | ||
response, | ||
) |
Empty file.
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,54 @@ | ||
from http import HTTPStatus | ||
from uuid import UUID | ||
|
||
import httpx | ||
import pytest | ||
from respx import MockRouter | ||
|
||
from pybotx import Bot, BotAccountWithSecret, HandlerCollector, lifespan_wrapper | ||
|
||
pytestmark = [ | ||
pytest.mark.asyncio, | ||
pytest.mark.mock_authorization, | ||
pytest.mark.usefixtures("respx_mock"), | ||
] | ||
|
||
|
||
async def test__collect_bot_function_metric__success( | ||
respx_mock: MockRouter, | ||
host: str, | ||
bot_id: UUID, | ||
bot_account: BotAccountWithSecret, | ||
) -> None: | ||
# - Arrange - | ||
endpoint = respx_mock.post( | ||
f"https://{host}/api/v3/botx/metrics/bot_function", | ||
headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, | ||
json={ | ||
"user_huids": ["33bd8924-da34-4615-b8ea-f8f7139bf4ef"], | ||
"group_chat_id": "3c11c28e-fbe1-4080-86e8-58e03c217dae", | ||
"bot_function": "email_sent", | ||
}, | ||
).mock( | ||
return_value=httpx.Response( | ||
HTTPStatus.ACCEPTED, | ||
json={ | ||
"status": "ok", | ||
"result": True, | ||
}, | ||
), | ||
) | ||
|
||
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) | ||
|
||
# - Act - | ||
async with lifespan_wrapper(built_bot) as bot: | ||
await bot.collect_metric( | ||
bot_id=bot_id, | ||
bot_function="email_sent", | ||
huids=[UUID("33bd8924-da34-4615-b8ea-f8f7139bf4ef")], | ||
chat_id=UUID("3c11c28e-fbe1-4080-86e8-58e03c217dae"), | ||
) | ||
|
||
# - Assert - | ||
assert endpoint.called |