Skip to content

Commit

Permalink
Extend user types (#376)
Browse files Browse the repository at this point in the history
* feat: Add `unregistered` and `guest` user types

* feat: Add processing of unsupported user types

* chore: Bump project version to 0.49.0
  • Loading branch information
nidemidovich committed Aug 12, 2022
1 parent 6d4c550 commit 0090c57
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pybotx/client/chats_api/chat_info.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from datetime import datetime as dt
from typing import List, Literal, Optional
from typing import Any, Dict, List, Literal, Optional, Union
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.common import ChatNotFoundError
from pybotx.logger import logger
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel
from pybotx.models.chats import ChatInfo, ChatInfoMember
from pybotx.models.enums import (
Expand Down Expand Up @@ -35,7 +36,7 @@ class BotXAPIChatInfoResult(VerifiedPayloadBaseModel):
description: Optional[str] = None
group_chat_id: UUID
inserted_at: dt
members: List[BotXAPIChatInfoMember]
members: List[Union[BotXAPIChatInfoMember, Dict[str, Any]]] # noqa: WPS234
name: str
shared_history: bool

Expand All @@ -45,13 +46,17 @@ class BotXAPIChatInfoResponsePayload(VerifiedPayloadBaseModel):
result: BotXAPIChatInfoResult

def to_domain(self) -> ChatInfo:
if any(isinstance(member, dict) for member in self.result.members):
logger.warning("One or more unsupported user types skipped")

members = [
ChatInfoMember(
is_admin=member.admin,
huid=member.user_huid,
kind=convert_user_kind_to_domain(member.user_kind),
)
for member in self.result.members
if isinstance(member, BotXAPIChatInfoMember)
]

return ChatInfo(
Expand Down
6 changes: 6 additions & 0 deletions pybotx/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class UserKinds(AutoName):
RTS_USER = auto()
CTS_USER = auto()
BOT = auto()
UNREGISTERED = auto()
GUEST = auto()


class AttachmentTypes(AutoName):
Expand Down Expand Up @@ -106,6 +108,8 @@ class APIUserKinds(StrEnum):
USER = "user"
CTS_USER = "cts_user"
BOTX = "botx"
UNREGISTERED = "unregistered"
GUEST = "guest"


class APIAttachmentTypes(StrEnum):
Expand Down Expand Up @@ -159,6 +163,8 @@ def convert_user_kind_to_domain(user_kind: APIUserKinds) -> UserKinds:
APIUserKinds.USER: UserKinds.RTS_USER,
APIUserKinds.CTS_USER: UserKinds.CTS_USER,
APIUserKinds.BOTX: UserKinds.BOT,
APIUserKinds.UNREGISTERED: UserKinds.UNREGISTERED,
APIUserKinds.GUEST: UserKinds.GUEST,
}

converted_type = user_kinds_mapping.get(user_kind)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pybotx"
version = "0.48.0"
version = "0.49.0"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <[email protected]>",
Expand Down
83 changes: 83 additions & 0 deletions tests/client/chats_api/test_chat_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,86 @@ async def test__chat_info__succeed(
)

assert endpoint.called


async def test__chat_info__skipped_members(
respx_mock: MockRouter,
host: str,
bot_id: UUID,
datetime_formatter: Callable[[str], dt],
bot_account: BotAccountWithSecret,
loguru_caplog: pytest.LogCaptureFixture,
) -> None:
# - Arrange -
endpoint = respx_mock.get(
f"https://{host}/api/v3/botx/chats/info",
headers={"Authorization": "Bearer token"},
params={"group_chat_id": "054af49e-5e18-4dca-ad73-4f96b6de63fa"},
).mock(
return_value=httpx.Response(
HTTPStatus.OK,
json={
"status": "ok",
"result": {
"chat_type": "group_chat",
"creator": "6fafda2c-6505-57a5-a088-25ea5d1d0364",
"description": None,
"group_chat_id": "054af49e-5e18-4dca-ad73-4f96b6de63fa",
"inserted_at": "2019-08-29T11:22:48.358586Z",
"members": [
{
"admin": True,
"user_huid": "6fafda2c-6505-57a5-a088-25ea5d1d0364",
"user_kind": "user",
},
{
"admin": False,
"user_huid": "705df263-6bfd-536a-9d51-13524afaab5c",
"user_kind": "botx",
},
{
"admin": False,
"user_huid": "0843a8a8-6d56-4ce6-92aa-13dc36bd9ede",
"user_kind": "unsupported_user_type",
},
],
"name": "Group Chat Example",
"shared_history": False,
},
},
),
)

built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account])

# - Act -
async with lifespan_wrapper(built_bot) as bot:
chat_info = await bot.chat_info(
bot_id=bot_id,
chat_id=UUID("054af49e-5e18-4dca-ad73-4f96b6de63fa"),
)

# - Assert -
assert chat_info == ChatInfo(
chat_type=ChatTypes.GROUP_CHAT,
creator_id=UUID("6fafda2c-6505-57a5-a088-25ea5d1d0364"),
description=None,
chat_id=UUID("054af49e-5e18-4dca-ad73-4f96b6de63fa"),
created_at=datetime_formatter("2019-08-29T11:22:48.358586Z"),
members=[
ChatInfoMember(
is_admin=True,
huid=UUID("6fafda2c-6505-57a5-a088-25ea5d1d0364"),
kind=UserKinds.RTS_USER,
),
ChatInfoMember(
is_admin=False,
huid=UUID("705df263-6bfd-536a-9d51-13524afaab5c"),
kind=UserKinds.BOT,
),
],
name="Group Chat Example",
shared_history=False,
)
assert "One or more unsupported user types skipped" in loguru_caplog.text
assert endpoint.called

0 comments on commit 0090c57

Please sign in to comment.