90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from adapter.max.api_client import MaxApiError, MaxBotApi
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_updates_returns_marker_and_updates():
|
|
api = MaxBotApi("token-x", base_url="http://max.test")
|
|
try:
|
|
api._client.request = AsyncMock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"updates": [{"update_type": "message_created", "timestamp": 1}],
|
|
"marker": 7,
|
|
},
|
|
)
|
|
)
|
|
updates, marker = await api.get_updates(types=["message_created"])
|
|
assert len(updates) == 1
|
|
assert updates[0]["update_type"] == "message_created"
|
|
assert marker == 7
|
|
|
|
_, kwargs = api._client.request.call_args
|
|
assert kwargs["params"]["types"] == "message_created"
|
|
finally:
|
|
await api.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_updates_non_dict_body():
|
|
api = MaxBotApi("token-x", base_url="http://max.test")
|
|
try:
|
|
api._client.request = AsyncMock(return_value=httpx.Response(200, text="oops"))
|
|
updates, marker = await api.get_updates()
|
|
assert updates == []
|
|
assert marker is None
|
|
finally:
|
|
await api.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_http_error_raises_max_api_error():
|
|
api = MaxBotApi("token-x", base_url="http://max.test")
|
|
try:
|
|
api._client.request = AsyncMock(
|
|
return_value=httpx.Response(401, json={"code": "verify.token", "message": "bad"})
|
|
)
|
|
with pytest.raises(MaxApiError) as ei:
|
|
await api.get_me()
|
|
assert ei.value.status == 401
|
|
assert "bad" in str(ei.value).lower() or ei.value.payload
|
|
finally:
|
|
await api.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_message_to_chat_posts_json_body():
|
|
api = MaxBotApi("token-x", base_url="http://max.test")
|
|
try:
|
|
api._client.request = AsyncMock(return_value=httpx.Response(200, json={"message": {}}))
|
|
|
|
await api.send_message_to_chat(12345, text="hi", attachments=None, fmt=None)
|
|
|
|
args, kw = api._client.request.call_args
|
|
assert args[0] == "POST"
|
|
assert args[1] == "/messages"
|
|
assert kw["params"]["chat_id"] == 12345
|
|
assert kw["json"] == {"text": "hi"}
|
|
finally:
|
|
await api.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_download_file_uses_get():
|
|
api = MaxBotApi("token-x", base_url="http://max.test")
|
|
try:
|
|
api._client.get = AsyncMock(return_value=httpx.Response(200, content=b"\xff\xd8"))
|
|
|
|
buf = await api.download_file("https://files.example/bin")
|
|
|
|
assert buf == b"\xff\xd8"
|
|
api._client.get.assert_awaited_once()
|
|
finally:
|
|
await api.aclose()
|