Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,10 @@ async def search_user_handler(message: IncomingMessage, bot: Bot) -> None:
user_info = await bot.search_user_by_huid(
bot_id=message.bot.id,
huid=message.sender.huid,
trusts_search=True,
)
except UserNotFoundError: # Если пользователь и бот находятся на разных CTS
await bot.answer_message("User not found. Maybe you are on a different cts.")
except UserNotFoundError:
await bot.answer_message("User not found.")
return

await bot.answer_message(f"Your info:\n{dataclasses.asdict(user_info)}\n")
Expand Down
22 changes: 21 additions & 1 deletion pybotx/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1578,11 +1578,15 @@ async def search_user_by_huid(
*,
bot_id: UUID,
huid: UUID,
trusts_search: bool = False,
partial_response: bool = False,
) -> UserFromSearch:
"""Search user by huid for search.

:param bot_id: Bot which should perform the request.
:param huid: User huid.
:param trusts_search: Search users on trusted servers.
:param partial_response: Return local results if trusted server lookup fails.

:return: User information.
"""
Expand All @@ -1592,7 +1596,11 @@ async def search_user_by_huid(
self._httpx_client,
self._bot_accounts_storage,
)
payload = BotXAPISearchUserByHUIDRequestPayload.from_domain(huid=huid)
payload = BotXAPISearchUserByHUIDRequestPayload.from_domain(
huid=huid,
trusts_search=trusts_search,
partial_response=partial_response,
)

botx_api_user_from_search = await method.execute(payload)

Expand All @@ -1604,12 +1612,16 @@ async def search_user_by_ad(
bot_id: UUID,
ad_login: str,
ad_domain: str,
trusts_search: bool = False,
partial_response: bool = False,
) -> UserFromSearch:
"""Search user by AD login and AD domain for search.

:param bot_id: Bot which should perform the request.
:param ad_login: User AD login.
:param ad_domain: User AD domain.
:param trusts_search: Search users on trusted servers.
:param partial_response: Return local results if trusted server lookup fails.

:return: User information.
"""
Expand All @@ -1622,6 +1634,8 @@ async def search_user_by_ad(
payload = BotXAPISearchUserByLoginRequestPayload.from_domain(
ad_login=ad_login,
ad_domain=ad_domain,
trusts_search=trusts_search,
partial_response=partial_response,
)

botx_api_user_from_search = await method.execute(payload)
Expand All @@ -1633,11 +1647,15 @@ async def search_user_by_other_id(
*,
bot_id: UUID,
other_id: str,
trusts_search: bool = False,
partial_response: bool = False,
) -> UserFromSearch:
"""Search user by other identificator for search.

:param bot_id: Bot which should perform the request.
:param other_id: User other identificator.
:param trusts_search: Search users on trusted servers.
:param partial_response: Return local results if trusted server lookup fails.

:return: User information.
"""
Expand All @@ -1649,6 +1667,8 @@ async def search_user_by_other_id(
)
payload = BotXAPISearchUserByOtherIdRequestPayload.from_domain(
other_id=other_id,
trusts_search=trusts_search,
partial_response=partial_response,
)

botx_api_user_from_search = await method.execute(payload)
Expand Down
16 changes: 14 additions & 2 deletions pybotx/client/users_api/search_user_by_huid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@
from pybotx.client.botx_method import response_exception_thrower
from pybotx.client.exceptions.users import UserNotFoundError
from pybotx.client.users_api.user_from_search import BotXAPISearchUserResponsePayload
from pybotx.missing import Missing, Undefined
from pybotx.models.api_base import UnverifiedPayloadBaseModel


class BotXAPISearchUserByHUIDRequestPayload(UnverifiedPayloadBaseModel):
user_huid: UUID
trusts_search: Missing[bool] = Undefined
partial_response: Missing[bool] = Undefined

@classmethod
def from_domain(cls, huid: UUID) -> "BotXAPISearchUserByHUIDRequestPayload":
return cls(user_huid=huid)
def from_domain(
cls,
huid: UUID,
trusts_search: bool = False,
partial_response: bool = False,
) -> "BotXAPISearchUserByHUIDRequestPayload":
return cls(
user_huid=huid,
trusts_search=trusts_search or Undefined,
partial_response=partial_response or Undefined,
)


class SearchUserByHUIDMethod(AuthorizedBotXMethod):
Expand Down
12 changes: 11 additions & 1 deletion pybotx/client/users_api/search_user_by_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@
from pybotx.client.botx_method import response_exception_thrower
from pybotx.client.exceptions.users import UserNotFoundError
from pybotx.client.users_api.user_from_search import BotXAPISearchUserResponsePayload
from pybotx.missing import Missing, Undefined
from pybotx.models.api_base import UnverifiedPayloadBaseModel


class BotXAPISearchUserByLoginRequestPayload(UnverifiedPayloadBaseModel):
ad_login: str
ad_domain: str
trusts_search: Missing[bool] = Undefined
partial_response: Missing[bool] = Undefined

@classmethod
def from_domain(
cls,
ad_login: str,
ad_domain: str,
trusts_search: bool = False,
partial_response: bool = False,
) -> "BotXAPISearchUserByLoginRequestPayload":
return cls(ad_login=ad_login, ad_domain=ad_domain)
return cls(
ad_login=ad_login,
ad_domain=ad_domain,
trusts_search=trusts_search or Undefined,
partial_response=partial_response or Undefined,
)


class SearchUserByLoginMethod(AuthorizedBotXMethod):
Expand Down
16 changes: 14 additions & 2 deletions pybotx/client/users_api/search_user_by_other_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
from pybotx.client.botx_method import response_exception_thrower
from pybotx.client.exceptions.users import UserNotFoundError
from pybotx.client.users_api.user_from_search import BotXAPISearchUserResponsePayload
from pybotx.missing import Missing, Undefined
from pybotx.models.api_base import UnverifiedPayloadBaseModel


class BotXAPISearchUserByOtherIdRequestPayload(UnverifiedPayloadBaseModel):
other_id: str
trusts_search: Missing[bool] = Undefined
partial_response: Missing[bool] = Undefined

@classmethod
def from_domain(cls, other_id: str) -> "BotXAPISearchUserByOtherIdRequestPayload":
return cls(other_id=other_id)
def from_domain(
cls,
other_id: str,
trusts_search: bool = False,
partial_response: bool = False,
) -> "BotXAPISearchUserByOtherIdRequestPayload":
return cls(
other_id=other_id,
trusts_search=trusts_search or Undefined,
partial_response=partial_response or Undefined,
)


class SearchUserByOtherIdMethod(AuthorizedBotXMethod):
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.76.5"
version = "0.76.6"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <nsidnev@ccsteam.ru>",
Expand Down
40 changes: 40 additions & 0 deletions tests/client/users_api/test_search_user_by_huid.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,43 @@ async def test__search_user_by_huid_without_data__succeed(
# - Assert -
assert_deep_equal(user, user_from_search_without_data)
assert endpoint.called


async def test__search_user_by_huid_with_trusts_search__succeed(
respx_mock: MockRouter,
host: str,
bot_id: UUID,
user_from_search_with_data: UserFromSearch,
user_from_search_with_data_json: dict[str, Any],
bot_factory: Any,
) -> None:
# - Arrange -
request = BotXRequest(
method="GET",
path="/api/v3/botx/users/by_huid",
params={
"user_huid": "f837dff4-d3ad-4b8d-a0a3-5c6ca9c747d1",
"trusts_search": True,
"partial_response": True,
},
)
endpoint = mock_botx(
respx_mock,
host,
request,
ok_payload(user_from_search_with_data_json),
HTTPStatus.OK,
)

# - Act -
async with bot_factory() as bot:
user = await bot.search_user_by_huid(
bot_id=bot_id,
huid=UUID("f837dff4-d3ad-4b8d-a0a3-5c6ca9c747d1"),
trusts_search=True,
partial_response=True,
)

# - Assert -
assert_deep_equal(user, user_from_search_with_data)
assert endpoint.called
42 changes: 42 additions & 0 deletions tests/client/users_api/test_search_user_by_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,45 @@ async def test__search_user_by_ad_without_data__succeed(
# - Assert -
assert_deep_equal(user, user_from_search_without_data)
assert endpoint.called


async def test__search_user_by_ad_with_trusts_search__succeed(
respx_mock: MockRouter,
host: str,
bot_id: UUID,
user_from_search_with_data: UserFromSearch,
user_from_search_with_data_json: dict[str, Any],
bot_factory: Any,
) -> None:
# - Arrange -
request = BotXRequest(
method="GET",
path="/api/v3/botx/users/by_login",
params={
"ad_login": "ad_user_login",
"ad_domain": "cts.com",
"trusts_search": True,
"partial_response": True,
},
)
endpoint = mock_botx(
respx_mock,
host,
request,
ok_payload(user_from_search_with_data_json),
HTTPStatus.OK,
)

# - Act -
async with bot_factory() as bot:
user = await bot.search_user_by_ad(
bot_id=bot_id,
ad_login="ad_user_login",
ad_domain="cts.com",
trusts_search=True,
partial_response=True,
)

# - Assert -
assert_deep_equal(user, user_from_search_with_data)
assert endpoint.called
40 changes: 40 additions & 0 deletions tests/client/users_api/test_search_user_by_other_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,43 @@ async def test__search_user_by_other_id_without_data__succeed(
# - Assert -
assert_deep_equal(user, user_from_search_without_data)
assert endpoint.called


async def test__search_user_by_other_id_with_trusts_search__succeed(
respx_mock: MockRouter,
host: str,
bot_id: UUID,
user_from_search_with_data: UserFromSearch,
user_from_search_with_data_json: dict[str, Any],
bot_factory: Any,
) -> None:
# - Arrange -
request = BotXRequest(
method="GET",
path="/api/v3/botx/users/by_other_id",
params={
"other_id": "some_id",
"trusts_search": True,
"partial_response": True,
},
)
endpoint = mock_botx(
respx_mock,
host,
request,
ok_payload(user_from_search_with_data_json),
HTTPStatus.OK,
)

# - Act -
async with bot_factory() as bot:
user = await bot.search_user_by_other_id(
bot_id=bot_id,
other_id="some_id",
trusts_search=True,
partial_response=True,
)

# - Assert -
assert_deep_equal(user, user_from_search_with_data)
assert endpoint.called