-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add send_smartapp_manifest method (#461)
- Loading branch information
Showing
5 changed files
with
246 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
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
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,63 @@ | ||
from typing import Literal | ||
|
||
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod | ||
from pybotx.models.api_base import VerifiedPayloadBaseModel | ||
from pybotx.models.enums import SmartappManifestWebLayoutChoices as WebLayoutChoices | ||
|
||
|
||
class SmartappManifestWebParams(VerifiedPayloadBaseModel): | ||
default_layout: WebLayoutChoices = WebLayoutChoices.minimal | ||
expanded_layout: WebLayoutChoices = WebLayoutChoices.half | ||
always_pinned: bool = False | ||
|
||
|
||
class SmartappManifest(VerifiedPayloadBaseModel): | ||
web: SmartappManifestWebParams | ||
|
||
|
||
class BotXAPISmartAppManifestRequestPayload(VerifiedPayloadBaseModel): | ||
manifest: SmartappManifest | ||
|
||
@classmethod | ||
def from_domain( | ||
cls, | ||
web_default_layout: WebLayoutChoices = WebLayoutChoices.minimal, | ||
web_expanded_layout: WebLayoutChoices = WebLayoutChoices.half, | ||
web_always_pinned: bool = False, | ||
) -> "BotXAPISmartAppManifestRequestPayload": | ||
return cls( | ||
manifest=SmartappManifest( | ||
web=SmartappManifestWebParams( | ||
default_layout=web_default_layout, | ||
expanded_layout=web_expanded_layout, | ||
always_pinned=web_always_pinned, | ||
), | ||
), | ||
) | ||
|
||
|
||
class BotXAPISmartAppManifestResponsePayload(VerifiedPayloadBaseModel): | ||
status: Literal["ok"] | ||
result: SmartappManifest | ||
|
||
def to_domain(self) -> SmartappManifest: | ||
return self.result | ||
|
||
|
||
class SmartAppManifestMethod(AuthorizedBotXMethod): | ||
async def execute( | ||
self, | ||
payload: BotXAPISmartAppManifestRequestPayload, | ||
) -> BotXAPISmartAppManifestResponsePayload: | ||
path = "/api/v1/botx/smartapps/manifest" | ||
|
||
response = await self._botx_method_call( | ||
"POST", | ||
self._build_url(path), | ||
json=payload.jsonable_dict(), | ||
) | ||
|
||
return self._verify_and_extract_api_model( | ||
BotXAPISmartAppManifestResponsePayload, | ||
response, | ||
) |
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
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,131 @@ | ||
from http import HTTPStatus | ||
from uuid import UUID | ||
|
||
import httpx | ||
import pytest | ||
from respx.router import MockRouter | ||
|
||
from pybotx import ( | ||
Bot, | ||
BotAccountWithSecret, | ||
HandlerCollector, | ||
SmartappManifest, | ||
SmartappManifestWebLayoutChoices, | ||
SmartappManifestWebParams, | ||
lifespan_wrapper, | ||
) | ||
|
||
pytestmark = [ | ||
pytest.mark.asyncio, | ||
pytest.mark.mock_authorization, | ||
pytest.mark.usefixtures("respx_mock"), | ||
] | ||
|
||
|
||
async def test__send_smartapp_manifest__all_params_provided__succeed( | ||
respx_mock: MockRouter, | ||
host: str, | ||
bot_id: UUID, | ||
bot_account: BotAccountWithSecret, | ||
) -> None: | ||
# - Arrange - | ||
endpoint = respx_mock.post( | ||
f"https://{host}/api/v1/botx/smartapps/manifest", | ||
headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, | ||
json={ | ||
"manifest": { | ||
"web": { | ||
"always_pinned": True, | ||
"default_layout": "full", | ||
"expanded_layout": "full", | ||
}, | ||
}, | ||
}, | ||
).mock( | ||
return_value=httpx.Response( | ||
HTTPStatus.ACCEPTED, | ||
json={ | ||
"result": { | ||
"web": { | ||
"always_pinned": True, | ||
"default_layout": "full", | ||
"expanded_layout": "full", | ||
}, | ||
}, | ||
"status": "ok", | ||
}, | ||
), | ||
) | ||
|
||
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) | ||
|
||
# - Act - | ||
async with lifespan_wrapper(built_bot) as bot: | ||
smartapp_manifest = await bot.send_smartapp_manifest( | ||
bot_id=bot_id, | ||
web_default_layout=SmartappManifestWebLayoutChoices.full, | ||
web_expanded_layout=SmartappManifestWebLayoutChoices.full, | ||
web_always_pinned=True, | ||
) | ||
|
||
# - Assert - | ||
assert endpoint.called | ||
assert smartapp_manifest == SmartappManifest( | ||
web=SmartappManifestWebParams( | ||
default_layout=SmartappManifestWebLayoutChoices.full, | ||
expanded_layout=SmartappManifestWebLayoutChoices.full, | ||
always_pinned=True, | ||
), | ||
) | ||
|
||
|
||
async def test__send_smartapp_manifest__only_default_params_provided__succeed( | ||
respx_mock: MockRouter, | ||
host: str, | ||
bot_id: UUID, | ||
bot_account: BotAccountWithSecret, | ||
) -> None: | ||
# - Arrange - | ||
endpoint = respx_mock.post( | ||
f"https://{host}/api/v1/botx/smartapps/manifest", | ||
headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, | ||
json={ | ||
"manifest": { | ||
"web": { | ||
"always_pinned": False, | ||
"default_layout": "minimal", | ||
"expanded_layout": "half", | ||
}, | ||
}, | ||
}, | ||
).mock( | ||
return_value=httpx.Response( | ||
HTTPStatus.ACCEPTED, | ||
json={ | ||
"result": { | ||
"web": { | ||
"always_pinned": False, | ||
"default_layout": "minimal", | ||
"expanded_layout": "half", | ||
}, | ||
}, | ||
"status": "ok", | ||
}, | ||
), | ||
) | ||
|
||
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) | ||
|
||
# - Act - | ||
async with lifespan_wrapper(built_bot) as bot: | ||
smartapp_manifest = await bot.send_smartapp_manifest(bot_id=bot_id) | ||
|
||
# - Assert - | ||
assert endpoint.called | ||
assert smartapp_manifest == SmartappManifest( | ||
web=SmartappManifestWebParams( | ||
default_layout=SmartappManifestWebLayoutChoices.minimal, | ||
expanded_layout=SmartappManifestWebLayoutChoices.half, | ||
always_pinned=False, | ||
), | ||
) |