Skip to content

Commit

Permalink
feat: method for search user by emails
Browse files Browse the repository at this point in the history
  • Loading branch information
vadikball committed Jul 26, 2023
1 parent ba11bbf commit 7874f23
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pybotx/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@
BotXAPISearchUserByEmailRequestPayload,
SearchUserByEmailMethod,
)
from pybotx.client.users_api.search_user_by_emails import (
BotXAPISearchUserByEmailsRequestPayload,
SearchUserByEmailsMethod,
)
from pybotx.client.users_api.search_user_by_huid import (
BotXAPISearchUserByHUIDRequestPayload,
SearchUserByHUIDMethod,
Expand Down Expand Up @@ -1063,6 +1067,31 @@ async def unpin_message(

await method.execute(payload)

async def search_user_by_emails(
self,
*,
bot_id: UUID,
emails: List[str],
) -> List[UserFromSearch]:
"""Search user by emails for search.
:param bot_id: Bot which should perform the request.
:param emails: User emails.
:return: Search result with user information.
"""

method = SearchUserByEmailsMethod(
bot_id,
self._httpx_client,
self._bot_accounts_storage,
)
payload = BotXAPISearchUserByEmailsRequestPayload.from_domain(emails=emails)

botx_api_users_from_search = await method.execute(payload)

return botx_api_users_from_search.to_domain()

# - Users API -
async def search_user_by_email(
self,
Expand All @@ -1072,6 +1101,8 @@ async def search_user_by_email(
) -> UserFromSearch:
"""Search user by email for search.
DEPRECATED.
:param bot_id: Bot which should perform the request.
:param email: User email.
Expand Down
37 changes: 37 additions & 0 deletions pybotx/client/users_api/search_user_by_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import List

from pybotx.client.authorized_botx_method import AuthorizedBotXMethod
from pybotx.client.users_api.user_from_search import (
BotXAPISearchUserByEmailsResponsePayload,
)
from pybotx.models.api_base import UnverifiedPayloadBaseModel


class BotXAPISearchUserByEmailsRequestPayload(UnverifiedPayloadBaseModel):
emails: List[str]

@classmethod
def from_domain(
cls,
emails: List[str],
) -> "BotXAPISearchUserByEmailsRequestPayload":
return cls(emails=emails)


class SearchUserByEmailsMethod(AuthorizedBotXMethod):
async def execute(
self,
payload: BotXAPISearchUserByEmailsRequestPayload,
) -> BotXAPISearchUserByEmailsResponsePayload:
path = "/api/v3/botx/users/by_email"

response = await self._botx_method_call(
"POST",
self._build_url(path),
json=payload.jsonable_dict(),
)

return self._verify_and_extract_api_model(
BotXAPISearchUserByEmailsResponsePayload,
response,
)
22 changes: 22 additions & 0 deletions pybotx/client/users_api/user_from_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ def to_domain(self) -> UserFromSearch:
other_id=self.result.other_id,
user_kind=convert_user_kind_to_domain(self.result.user_kind),
)


class BotXAPISearchUserByEmailsResponsePayload(VerifiedPayloadBaseModel):
status: Literal["ok"]
result: List[BotXAPISearchUserResult]

def to_domain(self) -> List[UserFromSearch]:
return [
UserFromSearch(
huid=user.user_huid,
ad_login=user.ad_login,
ad_domain=user.ad_domain,
username=user.name,
company=user.company,
company_position=user.company_position,
department=user.department,
emails=user.emails,
other_id=user.other_id,
user_kind=convert_user_kind_to_domain(user.user_kind),
)
for user in self.result
]
82 changes: 82 additions & 0 deletions tests/client/users_api/test_search_user_by_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from http import HTTPStatus
from uuid import UUID

import httpx
import pytest
from respx.router import MockRouter

from pybotx import (
Bot,
BotAccountWithSecret,
HandlerCollector,
UserFromSearch,
lifespan_wrapper,
)
from pybotx.models.enums import UserKinds

pytestmark = [
pytest.mark.asyncio,
pytest.mark.mock_authorization,
pytest.mark.usefixtures("respx_mock"),
]


async def test__search_user_by_email__succeed(
respx_mock: MockRouter,
host: str,
bot_id: UUID,
bot_account: BotAccountWithSecret,
) -> None:
# - Arrange -
user_emails = ["ad_user@cts.com"]

endpoint = respx_mock.post(
f"https://{host}/api/v3/botx/users/by_email",
headers={"Authorization": "Bearer token", "Content-Type": "application/json"},
json={"emails": user_emails},
).mock(
return_value=httpx.Response(
HTTPStatus.OK,
json={
"status": "ok",
"result": [
{
"user_huid": "6fafda2c-6505-57a5-a088-25ea5d1d0364",
"ad_login": "ad_user_login",
"ad_domain": "cts.com",
"name": "Bob",
"company": "Bobs Co",
"company_position": "Director",
"department": "Owners",
"emails": user_emails,
"user_kind": "cts_user",
},
],
},
),
)

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

# - Act -
async with lifespan_wrapper(built_bot) as bot:
users = await bot.search_user_by_emails(
bot_id=bot_id,
emails=user_emails,
)

# - Assert -
assert users[0] == UserFromSearch(
huid=UUID("6fafda2c-6505-57a5-a088-25ea5d1d0364"),
ad_login="ad_user_login",
ad_domain="cts.com",
username="Bob",
company="Bobs Co",
company_position="Director",
department="Owners",
emails=user_emails,
other_id=None,
user_kind=UserKinds.CTS_USER,
)

assert endpoint.called

0 comments on commit 7874f23

Please sign in to comment.