Files
site_aegisone/max_bot/tests/test_max_api.py
T

135 lines
4.0 KiB
Python

from unittest.mock import AsyncMock, patch, MagicMock
import pytest
from app.max_api import MaxAPIClient
@pytest.fixture
def api():
return MaxAPIClient(token="test-token")
@pytest.mark.asyncio
async def test_send_message(api):
api._request = AsyncMock(return_value={"ok": True})
result = await api.send_message(user_id=123, text="Hello")
assert result == {"ok": True}
api._request.assert_called_once()
@pytest.mark.asyncio
async def test_send_message_with_chat_id(api):
api._request = AsyncMock(return_value={"ok": True})
result = await api.send_message(chat_id=456, text="Hello")
assert result == {"ok": True}
@pytest.mark.asyncio
async def test_send_message_with_keyboard(api):
api.send_message = AsyncMock(return_value={"ok": True})
buttons = [[{"type": "message", "text": "Test", "payload": "test"}]]
result = await api.send_message_with_keyboard(user_id=123, text="Menu", buttons=buttons)
assert result == {"ok": True}
api.send_message.assert_called_once()
_, kwargs = api.send_message.call_args
assert kwargs["user_id"] == 123
assert kwargs["text"] == "Menu"
assert len(kwargs["attachments"]) == 1
@pytest.mark.asyncio
async def test_get_me(api):
api._request = AsyncMock(return_value={"ok": True, "bot": "test"})
result = await api.get_me()
assert result["bot"] == "test"
@pytest.mark.asyncio
async def test_subscribe_webhook(api):
api._request = AsyncMock(return_value={"ok": True})
result = await api.subscribe_webhook(url="https://example.com/webhook", secret="secret123")
assert result == {"ok": True}
@pytest.mark.asyncio
async def test_answer_callback(api):
api._request = AsyncMock(return_value={"ok": True})
result = await api.answer_callback(callback_id="cb_123", text="Thanks")
assert result == {"ok": True}
@pytest.mark.asyncio
async def test_send_file(api):
api.upload_file = AsyncMock(return_value={"file_id": "file_123"})
api._request = AsyncMock(return_value={"ok": True})
result = await api.send_file(user_id=123, file_path="/tmp/test.pdf", caption="Here")
assert result == {"ok": True}
@pytest.mark.asyncio
async def test_get_file_url(api):
api._request = AsyncMock(return_value={"url": "https://example.com/file.pdf"})
result = await api.get_file_url("file_123")
assert result == "https://example.com/file.pdf"
@pytest.mark.asyncio
async def test_send_contact_request(api):
api.send_message = AsyncMock(return_value={"ok": True})
result = await api.send_contact_request(user_id=123)
assert result == {"ok": True}
@pytest.mark.asyncio
async def test_edit_message(api):
api._request = AsyncMock(return_value={"ok": True})
result = await api.edit_message(message_id=1, text="Updated")
assert result == {"ok": True}
@pytest.mark.asyncio
async def test_get_subscriptions(api):
api._request = AsyncMock(return_value={"ok": True})
result = await api.get_subscriptions()
assert result == {"ok": True}
@pytest.mark.asyncio
async def test_unsubscribe_webhook(api):
api._request = AsyncMock(return_value={"ok": True})
result = await api.unsubscribe_webhook()
assert result == {"ok": True}
def test_verify_webhook_secret_valid(api):
assert api.verify_webhook_secret("secret123", "secret123") is True
def test_verify_webhook_secret_invalid(api):
assert api.verify_webhook_secret("wrong", "secret123") is False
def test_verify_webhook_secret_empty(api):
assert api.verify_webhook_secret("anything", "") is True
def test_verify_contact_hash(api):
result = api.verify_contact_hash("test vcf", "access_token", "some_hash")
assert result is False or result is True
def test_headers(api):
headers = api._headers()
assert headers["Authorization"] == "test-token"
assert headers["Content-Type"] == "application/json"
@pytest.mark.asyncio
async def test_close(api):
session = AsyncMock()
session.closed = False
api._session = session
await api.close()
session.close.assert_awaited_once()