Skip to content

Commit

Permalink
refactor: Proccessing of unsupported sync source types
Browse files Browse the repository at this point in the history
  • Loading branch information
nidemidovich committed Jan 18, 2023
1 parent 5f69f0a commit 2651f76
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pybotx/client/users_api/user_from_csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Union
from uuid import UUID

from pydantic import Field, validator
Expand All @@ -19,7 +19,7 @@ class BotXAPIUserFromCSVResult(VerifiedPayloadBaseModel):
ad_domain: str = Field(alias="Domain")
email: Optional[str] = Field(alias="AD E-mail")
name: str = Field(alias="Name")
sync_source: APISyncSourceTypes = Field(alias="Sync source")
sync_source: Union[APISyncSourceTypes, str] = Field(alias="Sync source")
active: bool = Field(alias="Active")
user_kind: APIUserKinds = Field(alias="Kind")
company: Optional[str] = Field(alias="Company")
Expand Down
29 changes: 25 additions & 4 deletions pybotx/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class SyncSourceTypes(AutoName):
OPENID = auto()


IncomingChatTypes = Union[ChatTypes, Literal["UNSUPPORTED"]]
UNSUPPORTED = Literal["UNSUPPORTED"]
IncomingChatTypes = Union[ChatTypes, UNSUPPORTED]
IncomingSyncSourceTypes = Union[SyncSourceTypes, UNSUPPORTED]


class StrEnum(str, Enum): # noqa: WPS600 (pydantic needs this inheritance)
Expand Down Expand Up @@ -258,7 +260,7 @@ def convert_chat_type_to_domain(chat_type: APIChatTypes) -> ChatTypes:


@overload
def convert_chat_type_to_domain(chat_type: str) -> Literal["UNSUPPORTED"]:
def convert_chat_type_to_domain(chat_type: str) -> UNSUPPORTED:
... # noqa: WPS428


Expand All @@ -283,18 +285,37 @@ def convert_chat_type_to_domain(
return converted_type


@overload
def convert_sync_source_type_to_domain(
sync_type: APISyncSourceTypes,
) -> SyncSourceTypes:
... # noqa: WPS428


@overload
def convert_sync_source_type_to_domain(sync_type: str) -> UNSUPPORTED:
... # noqa: WPS428


def convert_sync_source_type_to_domain(
sync_type: Union[APISyncSourceTypes, str],
) -> IncomingSyncSourceTypes:
sync_source_types_mapping = {
APISyncSourceTypes.AD: SyncSourceTypes.AD,
APISyncSourceTypes.ADMIN: SyncSourceTypes.ADMIN,
APISyncSourceTypes.EMAIL: SyncSourceTypes.EMAIL,
APISyncSourceTypes.OPENID: SyncSourceTypes.OPENID,
}

converted_type = sync_source_types_mapping.get(sync_type)
converted_type: Optional[IncomingSyncSourceTypes]
try:
converted_type = sync_source_types_mapping.get(APISyncSourceTypes(sync_type))
except ValueError:
converted_type = "UNSUPPORTED"

if converted_type is None:
raise NotImplementedError(f"Unsupported sync source type: {sync_type}")
raise NotImplementedError(
f"Unsupported sync source type: {sync_type}",
) from None

return converted_type
4 changes: 2 additions & 2 deletions pybotx/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Optional
from uuid import UUID

from pybotx.models.enums import SyncSourceTypes, UserKinds
from pybotx.models.enums import IncomingSyncSourceTypes, UserKinds


@dataclass
Expand Down Expand Up @@ -55,7 +55,7 @@ class UserFromCSV:
ad_login: str
ad_domain: str
username: str
sync_source: SyncSourceTypes
sync_source: IncomingSyncSourceTypes
active: bool
user_kind: UserKinds
email: Optional[str] = None
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.52.1"
version = "0.53.0"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <[email protected]>",
Expand Down
16 changes: 15 additions & 1 deletion tests/client/users_api/test_users_as_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ async def test__users_as_csv__succeed(
status_code=HTTPStatus.OK,
content=(
b"HUID,AD Login,Domain,AD E-mail,Name,Sync source,Active,Kind,Company,Department,Position\n"
b"dbc8934f-d0d7-4a9e-89df-d45c137a851c,test_user_17,cts.example.com,,test_user_17,ad,true,cts_user,,,"
b"dbc8934f-d0d7-4a9e-89df-d45c137a851c,test_user_17,cts.example.com,,test_user_17,ad,true,cts_user,,,\n"
b"13a6909c-bce1-4dbf-8359-efb7ef8e5b34,test_user_18,cts.example.com,,test_user_18,unsupported,true,cts_user,,,"
),
),
)
Expand Down Expand Up @@ -103,4 +104,17 @@ async def test__users_as_csv__succeed(
department=None,
position=None,
),
UserFromCSV(
huid=UUID("13a6909c-bce1-4dbf-8359-efb7ef8e5b34"),
ad_login="test_user_18",
ad_domain="cts.example.com",
username="test_user_18",
sync_source="UNSUPPORTED",
active=True,
user_kind=UserKinds.CTS_USER,
email=None,
company=None,
department=None,
position=None,
),
]

0 comments on commit 2651f76

Please sign in to comment.