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:
+132
-137
@@ -1,179 +1,174 @@
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock
|
||||
from unittest.mock import AsyncMock, MagicMock, patch, ANY
|
||||
from app.handlers.greeting import analyze_and_respond
|
||||
from app.handlers.consent import handle_consent_response
|
||||
from app.handlers.contact import handle_manual_contact
|
||||
from app.handlers.inquiry import handle_inquiry
|
||||
from app.handlers.handoff import handle_callback
|
||||
from app.models import BotConversation, BotUser, BotTicket, BotKnowledgeBase
|
||||
|
||||
from app.fsm import BotState, FSM
|
||||
from app.handlers.greeting import handle_start
|
||||
from app.handlers.consent import handle_consent_request, handle_consent_given, handle_consent_refused
|
||||
from app.handlers.main_menu import handle_main_menu
|
||||
from app.settings_cache import SettingsCache
|
||||
|
||||
def make_mock_db():
|
||||
db = AsyncMock()
|
||||
result = MagicMock()
|
||||
result.scalar_one_or_none.return_value = None
|
||||
result.scalars.return_value.all.return_value = []
|
||||
db.execute.return_value = result
|
||||
return db
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_start_new_user(test_session, mock_max_api, bot):
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 12345, "first_name": "Иван", "last_name": "Петров"}
|
||||
async def test_consent_response_yes():
|
||||
mock_max_api = MagicMock()
|
||||
mock_max_api.send_message = AsyncMock()
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_start(bot, user_data, test_session, mock_max_api)
|
||||
with patch("app.handlers.consent.max_api", mock_max_api), \
|
||||
patch("app.handlers.consent.async_session") as mock_session:
|
||||
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
call_args = mock_max_api.send_message_with_keyboard.call_args
|
||||
assert call_args[0][0] == 12345
|
||||
assert FSM.get_state(12345) == BotState.IDLE
|
||||
mock_db = make_mock_db()
|
||||
mock_session.return_value.__aenter__.return_value = mock_db
|
||||
|
||||
mock_user = MagicMock(spec=BotUser)
|
||||
mock_user.consent_given = False
|
||||
mock_user.consent_date = None
|
||||
mock_conv = MagicMock(spec=BotConversation)
|
||||
mock_conv.id = 1
|
||||
|
||||
mock_result = MagicMock()
|
||||
mock_result.scalar_one_or_none.side_effect = [mock_user, mock_conv]
|
||||
mock_db.execute.return_value = mock_result
|
||||
|
||||
await handle_consent_response(12345, 1, "даю согласие")
|
||||
assert mock_max_api.send_message.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_start_returning_user(test_session, mock_max_api, bot):
|
||||
from app.conversation import get_or_create_user, create_conversation
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 67890, "first_name": "Пётр"}
|
||||
async def test_consent_response_no():
|
||||
mock_max_api = MagicMock()
|
||||
mock_max_api.send_message = AsyncMock()
|
||||
|
||||
user = await get_or_create_user(test_session, 67890, "Пётр")
|
||||
await test_session.flush()
|
||||
await create_conversation(test_session, user.id)
|
||||
await test_session.flush()
|
||||
with patch("app.handlers.consent.max_api", mock_max_api), \
|
||||
patch("app.handlers.consent.async_session") as mock_session, \
|
||||
patch("app.handlers.consent.config_reader") as mock_config:
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_start(bot, user_data, test_session, mock_max_api)
|
||||
mock_db = make_mock_db()
|
||||
mock_session.return_value.__aenter__.return_value = mock_db
|
||||
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
assert FSM.get_state(67890) == BotState.IDLE
|
||||
mock_config.get_phones.return_value = [("+7 (861) 203-33-30", "+78612033330")]
|
||||
mock_config.get_site_mail.return_value = "mail@aegisone.ru"
|
||||
|
||||
mock_conv = MagicMock(spec=BotConversation)
|
||||
mock_conv.id = 1
|
||||
mock_result = MagicMock()
|
||||
mock_result.scalar_one_or_none.return_value = mock_conv
|
||||
mock_db.execute.return_value = mock_result
|
||||
|
||||
await handle_consent_response(12345, 1, "нет")
|
||||
assert mock_max_api.send_message.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_consent_request(test_session, mock_max_api, bot):
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 111, "first_name": "Анна"}
|
||||
async def test_manual_contact_valid_phone():
|
||||
mock_max_api = MagicMock()
|
||||
mock_max_api.send_message = AsyncMock()
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_consent_request(bot, user_data, test_session, mock_max_api)
|
||||
with patch("app.handlers.contact.max_api", mock_max_api), \
|
||||
patch("app.handlers.contact.async_session") as mock_session, \
|
||||
patch("app.handlers.contact.handle_contact_received",
|
||||
new=AsyncMock()) as mock_received:
|
||||
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
assert FSM.get_state(111) == BotState.CONSENT_REQUEST
|
||||
mock_db = make_mock_db()
|
||||
mock_session.return_value.__aenter__.return_value = mock_db
|
||||
|
||||
await handle_manual_contact(12345, 1, "+78612033330")
|
||||
assert mock_received.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_consent_given(test_session, mock_max_api, bot):
|
||||
from app.conversation import get_or_create_user
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 222}
|
||||
async def test_manual_contact_invalid():
|
||||
mock_max_api = MagicMock()
|
||||
mock_max_api.send_message = AsyncMock()
|
||||
|
||||
user = await get_or_create_user(test_session, 222)
|
||||
await test_session.flush()
|
||||
with patch("app.handlers.contact.max_api", mock_max_api), \
|
||||
patch("app.handlers.contact.async_session") as mock_session:
|
||||
|
||||
await handle_consent_given(bot, user_data, test_session, mock_max_api)
|
||||
mock_db = make_mock_db()
|
||||
mock_session.return_value.__aenter__.return_value = mock_db
|
||||
|
||||
assert user.consent_given is True
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
assert FSM.get_state(222) == BotState.CONTACT_REQUEST
|
||||
await handle_manual_contact(12345, 1, "просто текст")
|
||||
assert mock_max_api.send_message.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_consent_refused(test_session, mock_max_api, bot):
|
||||
from app.conversation import get_or_create_user
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 333}
|
||||
async def test_callback_consent_yes():
|
||||
mock_max_api = MagicMock()
|
||||
mock_max_api.send_message = AsyncMock()
|
||||
|
||||
user = await get_or_create_user(test_session, 333)
|
||||
await test_session.flush()
|
||||
with patch("app.handlers.handoff.max_api", mock_max_api), \
|
||||
patch("app.handlers.consent.max_api", mock_max_api), \
|
||||
patch("app.handlers.handoff.async_session") as mock_hoff_session, \
|
||||
patch("app.handlers.consent.async_session") as mock_consent_session:
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_consent_refused(bot, user_data, test_session, mock_max_api)
|
||||
mock_db = make_mock_db()
|
||||
mock_hoff_session.return_value.__aenter__.return_value = mock_db
|
||||
mock_consent_session.return_value.__aenter__.return_value = mock_db
|
||||
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
assert FSM.get_state(333) == BotState.IDLE
|
||||
mock_conv = MagicMock(spec=BotConversation)
|
||||
mock_conv.id = 1
|
||||
mock_user = MagicMock(spec=BotUser)
|
||||
mock_user.consent_given = False
|
||||
mock_user.consent_date = None
|
||||
|
||||
mock_result = MagicMock()
|
||||
mock_result.scalar_one_or_none.side_effect = [mock_conv, mock_user, mock_conv]
|
||||
mock_db.execute.return_value = mock_result
|
||||
|
||||
await handle_callback(12345, "consent_yes", {})
|
||||
assert mock_max_api.send_message.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_main_menu_new_inquiry_without_consent(test_session, mock_max_api, bot):
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 444, "payload": "new_inquiry"}
|
||||
async def test_inquiry_handling():
|
||||
mock_max_api = MagicMock()
|
||||
mock_max_api.send_message = AsyncMock()
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_main_menu(bot, user_data, test_session, mock_max_api)
|
||||
mock_email = MagicMock()
|
||||
mock_email.send_ticket_escalation = MagicMock(return_value=True)
|
||||
|
||||
# Should go to consent request
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
with patch("app.handlers.inquiry.max_api", mock_max_api), \
|
||||
patch("app.handlers.inquiry.email_sender", mock_email), \
|
||||
patch("app.handlers.inquiry.async_session") as mock_session, \
|
||||
patch("app.handlers.inquiry.settings_cache") as mock_cache:
|
||||
|
||||
mock_db = make_mock_db()
|
||||
mock_session.return_value.__aenter__.return_value = mock_db
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_main_menu_cancel(test_session, mock_max_api, bot):
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 555, "payload": "cancel"}
|
||||
mock_conv = MagicMock(spec=BotConversation)
|
||||
mock_conv.id = 1
|
||||
mock_conv.intent = "ticket"
|
||||
mock_conv.state = "awaiting_inquiry"
|
||||
mock_conv.inquiry_text = None
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_main_menu(bot, user_data, test_session, mock_max_api)
|
||||
mock_user = MagicMock(spec=BotUser)
|
||||
mock_user.id = 12345
|
||||
mock_user.first_name = "Иван"
|
||||
mock_user.phone = "+78612033330"
|
||||
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
assert FSM.get_state(555) == BotState.IDLE
|
||||
mock_conv_result = MagicMock()
|
||||
mock_conv_result.scalar_one_or_none.return_value = mock_conv
|
||||
mock_user_result = MagicMock()
|
||||
mock_user_result.scalar_one_or_none.return_value = mock_user
|
||||
|
||||
mock_db.execute.side_effect = [
|
||||
mock_conv_result, mock_user_result,
|
||||
mock_conv_result,
|
||||
MagicMock(), MagicMock(), MagicMock(),
|
||||
]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_main_menu_unknown_payload(test_session, mock_max_api, bot):
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 666, "payload": "unknown_command"}
|
||||
mock_cache.get = AsyncMock(side_effect=lambda k, d="": {
|
||||
"support_email": "support@aegisone.ru",
|
||||
"email_subject": "Test subject"
|
||||
}.get(k, d))
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_main_menu(bot, user_data, test_session, mock_max_api)
|
||||
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
assert FSM.get_state(666) == BotState.IDLE
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_main_menu_handoff(test_session, mock_max_api, bot):
|
||||
from app.conversation import get_or_create_user
|
||||
from app.handlers.handoff import handle_handoff_request
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 668, "payload": "handoff"}
|
||||
|
||||
await get_or_create_user(test_session, 668, "Тест")
|
||||
await test_session.flush()
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
# Call directly (goes to out_of_hours if after hours, or handoff template)
|
||||
await handle_handoff_request(bot, user_data, test_session, mock_max_api)
|
||||
|
||||
# Either send_message (handoff) or send_message_with_keyboard (out_of_hours)
|
||||
assert mock_max_api.send_message.called or mock_max_api.send_message_with_keyboard.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_out_of_hours(test_session, mock_max_api, bot):
|
||||
from app.handlers.out_of_hours import handle_out_of_hours
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 777}
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_out_of_hours(bot, user_data, test_session, mock_max_api)
|
||||
|
||||
mock_max_api.send_message_with_keyboard.assert_called_once()
|
||||
assert FSM.get_state(777) == BotState.OUT_OF_HOURS
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_out_of_hours_leave_message(test_session, mock_max_api, bot):
|
||||
from app.handlers.out_of_hours import handle_out_of_hours_callback
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 888, "text": "тестовое обращение"}
|
||||
FSM.set_state(888, BotState.OUT_OF_HOURS)
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_out_of_hours_callback(bot, user_data, test_session, mock_max_api, "leave_message")
|
||||
|
||||
# handle_inquiry completes the flow → ends at CONFIRMATION
|
||||
assert FSM.get_state(888) == BotState.CONFIRMATION
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_out_of_hours_callback_request(test_session, mock_max_api, bot):
|
||||
from app.handlers.out_of_hours import handle_out_of_hours_callback
|
||||
FSM.clear_all()
|
||||
user_data = {"user_id": 999}
|
||||
FSM.set_state(999, BotState.OUT_OF_HOURS)
|
||||
|
||||
await SettingsCache.refresh(test_session)
|
||||
await handle_out_of_hours_callback(bot, user_data, test_session, mock_max_api, "callback")
|
||||
|
||||
mock_max_api.send_message.assert_called_once()
|
||||
assert FSM.get_state(999) == BotState.NAME_REQUEST
|
||||
await handle_inquiry(12345, 1, "Нужна консультация")
|
||||
assert mock_max_api.send_message.called
|
||||
|
||||
Reference in New Issue
Block a user