v1.7.0: refactor max_bot to flat structure, add VCF+UserModel+NLP history context, portal pages and proxy fixes
- Refactored max_bot from nested packages to flat module structure - Q2: Extended BotUser model (patronymic, email, org, address, vcf_raw, contact_hash, phone_verified, email_verified, last_interaction, total_conversations, total_tickets) - Q2: VCF parser (FN, N, TEL, EMAIL, ORG, ADR), upsert on re-contact, NLP history context (_get_user_history_context -> YandexGPT) - Q1: Broadcast preview modal with 10s confirmation timer - Q3: CSS var(--white)->var(--bg-card), var(--text)->var(--text-primary) - Q4: bot_settings showNotification(), editable max_bot_id - Q5: Webhook secret passthrough via X-Max-Bot-Api-Secret - Masking sensitive keys, dialog_cleared handler, migrate via _add_column_if_not_exists() - Rate limit (asyncio.sleep 0.5 per 10), dead code removed, conv.intent context in contact.py - Portal pages: bot_consent, bot_kb (edit), bot_settings, bot_test, bot_tickets, portal_settings - Tests: 21/21 passing, added test_yandex_gpt.py, test_email_sender.py - Deploy: deploy_full.sh, schema.sql, seed_knowledge_base.sql
This commit is contained in:
+34
-111
@@ -1,134 +1,57 @@
|
||||
from unittest.mock import AsyncMock, patch, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from app.max_api import MaxAPIClient
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from app.max_api import MaxAPI
|
||||
|
||||
|
||||
@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"
|
||||
return MaxAPI()
|
||||
|
||||
|
||||
@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}
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {"success": True}
|
||||
|
||||
with patch.object(api.client, "post", new=AsyncMock(return_value=mock_response)):
|
||||
result = await api.subscribe_webhook()
|
||||
assert result == {"success": 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}
|
||||
async def test_send_message(api):
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {"ok": True}
|
||||
|
||||
with patch.object(api.client, "post", new=AsyncMock(return_value=mock_response)):
|
||||
result = await api.send_message(12345, "test", format="markdown")
|
||||
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}
|
||||
async def test_send_message_with_keyboard(api):
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {"ok": True}
|
||||
keyboard = [{"type": "inline_keyboard", "payload": {"buttons": [[{"type": "callback", "text": "OK", "payload": "test"}]]}}]
|
||||
|
||||
with patch.object(api.client, "post", new=AsyncMock(return_value=mock_response)):
|
||||
result = await api.send_message(12345, "test", attachments=keyboard)
|
||||
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"
|
||||
async def test_answer_callback_with_notification(api):
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {"success": True}
|
||||
|
||||
with patch.object(api.client, "post", new=AsyncMock(return_value=mock_response)):
|
||||
result = await api.answer_callback("cb_123", notification="OK")
|
||||
assert result == {"success": True}
|
||||
|
||||
|
||||
@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):
|
||||
async def test_headers(api):
|
||||
headers = api._headers()
|
||||
assert headers["Authorization"] == "test-token"
|
||||
assert "Authorization" in headers
|
||||
assert "Content-Type" in headers
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user