File size: 61,663 Bytes
cc036ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 | """
End-to-End Tests for MCP Tools
This test suite validates MCP (Model Context Protocol) tool implementations
using real PostgreSQL database and mocked external services.
Tests cover 8 major MCP tool categories:
- CRM (contacts, leads, pipeline)
- Tasks (projects, task management)
- Tickets (support tickets)
- Knowledge (document ingestion, graph queries)
- Canvas (presentations, UI components)
- Finance (metrics, invoices)
- WhatsApp (messaging, templates)
- Shopify (products, inventory, orders)
All tests use real PostgreSQL database operations to validate actual tool behavior,
not mocked database calls. External APIs (WhatsApp, Shopify) are mocked with httpx.
"""
import pytest
import asyncio
import uuid
from datetime import datetime, timedelta
from typing import Dict, Any, List
from sqlalchemy.orm import Session
from sqlalchemy import text
# Ensure backend is in path
import sys
from pathlib import Path
backend_dir = Path(__file__).parent.parent.parent
sys.path.insert(0, str(backend_dir))
# =============================================================================
# Test Constants and Helpers
# =============================================================================
TEST_WORKSPACE_ID = "e2e-test-workspace"
TEST_AGENT_ID = "e2e-test-agent"
TEST_USER_ID = "e2e-test-user"
def assert_success_response(response: Dict[str, Any], operation_name: str):
"""Helper to assert MCP operation succeeded."""
assert isinstance(response, dict), f"{operation_name}: Response should be a dict"
assert "error" not in response or response.get("error") is None, \
f"{operation_name}: Got error - {response.get('error')}"
assert response.get("success") is not False, \
f"{operation_name}: Operation failed"
def assert_database_record(db: Session, table_name: str, filters: Dict[str, Any], should_exist: bool = True):
"""Helper to verify database record exists."""
query = text(f"SELECT * FROM {table_name} WHERE " + " AND ".join([f"{k} = :{k}" for k in filters.keys()]))
result = db.execute(query, filters).fetchone()
if should_exist:
assert result is not None, f"Record not found in {table_name} with filters {filters}"
else:
assert result is None, f"Unexpected record found in {table_name} with filters {filters}"
return result
# =============================================================================
# CRM Tool E2E Tests
# =============================================================================
class TestMCPToolsCRM:
"""E2E tests for MCP CRM tools (contacts, leads, pipeline)."""
@pytest.mark.asyncio
async def test_mcp_search_contacts_empty(self, mcp_service, e2e_postgres_db):
"""Test searching contacts when database is empty."""
response = await mcp_service.call_tool(
"search_contacts",
{"query": "test", "platform": "salesforce"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Should return empty results or success with no matches
assert isinstance(response, dict)
assert response.get("results") == [] or response.get("contacts") == []
@pytest.mark.asyncio
async def test_mcp_create_crm_lead_basic(self, mcp_service, e2e_postgres_db, crm_contact_factory):
"""Test creating a basic CRM lead via MCP tool."""
lead_data = crm_contact_factory(
first_name="John",
last_name="Doe",
email="john.doe@example.com",
company="Acme Corp",
status="lead"
)
response = await mcp_service.call_tool(
"create_crm_lead",
{
"platform": "salesforce",
**lead_data
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Verify response
assert_success_response(response, "create_crm_lead")
assert "id" in response or "lead_id" in response or response.get("success") is True
# Note: Actual database verification depends on integration implementation
# Some tools may write to external CRM APIs, not local database
# For E2E tests with real services, we verify the tool executed successfully
@pytest.mark.asyncio
async def test_mcp_create_crm_lead_with_company(self, mcp_service, e2e_postgres_db):
"""Test creating CRM lead with company information."""
lead_data = {
"platform": "hubspot",
"first_name": "Jane",
"last_name": "Smith",
"email": f"jane.smith.{uuid.uuid4()}@example.com",
"company": "Tech Startup Inc",
"status": "prospect"
}
response = await mcp_service.call_tool(
"create_crm_lead",
lead_data,
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "create_crm_lead_with_company")
@pytest.mark.asyncio
async def test_mcp_crm_contact_lifecycle(self, mcp_service, e2e_postgres_db, crm_contact_factory):
"""Test full CRM contact lifecycle: create → search → update → delete."""
# Create
contact_data = crm_contact_factory(
first_name="Lifecycle",
last_name="Test",
email=f"lifecycle.{uuid.uuid4()}@example.com"
)
create_response = await mcp_service.call_tool(
"create_crm_lead",
{"platform": "salesforce", **contact_data},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(create_response, "CRM lifecycle create")
# Search
search_response = await mcp_service.call_tool(
"search_contacts",
{"query": "Lifecycle", "platform": "salesforce"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(search_response, dict)
# Update (if update implemented)
if create_response.get("id"):
update_response = await mcp_service.call_tool(
"update_crm_lead",
{
"platform": "salesforce",
"id": create_response["id"],
"data": {"status": "qualified", "phone": "+15551234567"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Update might not be implemented in all integrations
assert isinstance(update_response, dict)
@pytest.mark.asyncio
async def test_mcp_get_sales_pipeline(self, mcp_service, e2e_postgres_db):
"""Test fetching sales pipeline data."""
response = await mcp_service.call_tool(
"get_sales_pipeline",
{"platform": "salesforce", "status": "open"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Should return pipeline data (may be empty)
assert isinstance(response, dict)
assert "pipeline" in response or "deals" in response or "results" in response or isinstance(response.get("value"), (list, dict))
# =============================================================================
# Tasks Tool E2E Tests
# =============================================================================
class TestMCPToolsTasks:
"""E2E tests for MCP task management tools."""
@pytest.mark.asyncio
async def test_mcp_list_projects(self, mcp_service, e2e_postgres_db):
"""Test listing projects from project management platforms."""
response = await mcp_service.call_tool(
"list_projects",
{"platform": "asana"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Should return list of projects (may be empty)
assert isinstance(response, dict)
assert "projects" in response or "results" in response or isinstance(response.get("value"), list)
@pytest.mark.asyncio
async def test_mcp_create_task(self, mcp_service, e2e_postgres_db, task_factory):
"""Test creating a task via MCP tool."""
task_data = task_factory(
title=f"E2E Test Task {uuid.uuid4()}",
description="This is an E2E test task",
status="todo",
priority="high",
due_date=(datetime.now() + timedelta(days=7)).isoformat()
)
response = await mcp_service.call_tool(
"create_task",
{
"platform": "asana",
"project": "E2E Test Project",
**task_data
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "create_task")
assert "id" in response or "task_id" in response or response.get("success") is True
@pytest.mark.asyncio
async def test_mcp_get_tasks_with_filters(self, mcp_service, e2e_postgres_db):
"""Test fetching tasks with filters."""
response = await mcp_service.call_tool(
"get_tasks",
{
"platform": "jira",
"assignee": "test-user",
"status": "todo"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
assert "tasks" in response or "results" in response or isinstance(response.get("value"), list)
@pytest.mark.asyncio
async def test_mcp_update_task(self, mcp_service, e2e_postgres_db, task_factory):
"""Test updating a task via MCP tool."""
# First create a task
create_response = await mcp_service.call_tool(
"create_task",
{
"platform": "linear",
"project": "E2E Test",
"title": f"Update Test {uuid.uuid4()}",
"status": "todo"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Then update it
if create_response.get("id"):
update_response = await mcp_service.call_tool(
"update_task",
{
"platform": "linear",
"id": create_response["id"],
"data": {"status": "done", "priority": "urgent"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(update_response, dict)
@pytest.mark.asyncio
async def test_mcp_task_workflow(self, mcp_service, e2e_postgres_db):
"""Test complete task workflow: create → assign → complete → query."""
# Create
task_title = f"Workflow Test {uuid.uuid4()}"
create_response = await mcp_service.call_tool(
"create_task",
{
"platform": "monday",
"project": "Workflow Test Project",
"title": task_title,
"description": "Testing complete task workflow"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Assign (update with assignee)
if create_response.get("id"):
assign_response = await mcp_service.call_tool(
"update_task",
{
"platform": "monday",
"id": create_response["id"],
"data": {"assignee": "test-user-123"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Complete
complete_response = await mcp_service.call_tool(
"update_task",
{
"platform": "monday",
"id": create_response["id"],
"data": {"status": "done"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
@pytest.mark.asyncio
async def test_mcp_search_tasks(self, mcp_service, e2e_postgres_db):
"""Test searching for tasks by query."""
response = await mcp_service.call_tool(
"search_tasks",
{"query": "urgent", "platform": "jira"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
assert "tasks" in response or "results" in response or isinstance(response.get("value"), list)
# =============================================================================
# Tickets Tool E2E Tests
# =============================================================================
class TestMCPToolsTickets:
"""E2E tests for MCP support ticket tools."""
@pytest.mark.asyncio
async def test_mcp_create_support_ticket(self, mcp_service, e2e_postgres_db, ticket_factory):
"""Test creating a support ticket."""
ticket_data = ticket_factory(
subject=f"E2E Test Issue {uuid.uuid4()}",
description="Customer unable to access account",
priority="high",
status="open"
)
response = await mcp_service.call_tool(
"create_support_ticket",
{
"platform": "zendesk",
**ticket_data
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "create_support_ticket")
assert "id" in response or "ticket_id" in response or response.get("success") is True
@pytest.mark.asyncio
async def test_mcp_create_ticket_with_priority(self, mcp_service, e2e_postgres_db):
"""Test creating ticket with different priority levels."""
for priority in ["low", "normal", "high", "urgent"]:
response = await mcp_service.call_tool(
"create_support_ticket",
{
"platform": "freshdesk",
"subject": f"Priority Test - {priority}",
"description": f"Testing {priority} priority ticket",
"priority": priority
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_update_support_ticket(self, mcp_service, e2e_postgres_db, ticket_factory):
"""Test updating support ticket status and adding comments."""
# Create ticket first
create_response = await mcp_service.call_tool(
"create_support_ticket",
{
"platform": "intercom",
"subject": f"Update Test {uuid.uuid4()}",
"description": "Test ticket for updates",
"priority": "normal"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Update with status change and comment
if create_response.get("id"):
update_response = await mcp_service.call_tool(
"update_support_ticket",
{
"platform": "intercom",
"id": create_response["id"],
"data": {
"status": "resolved",
"priority": "high",
"comment": "Issue resolved by support team"
}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(update_response, dict)
@pytest.mark.asyncio
async def test_mcp_ticket_resolution_workflow(self, mcp_service, e2e_postgres_db):
"""Test ticket resolution workflow: create → comment → resolve → verify."""
# Create
subject = f"Resolution Test {uuid.uuid4()}"
create_response = await mcp_service.call_tool(
"create_support_ticket",
{
"platform": "zendesk",
"subject": subject,
"description": "Customer billing inquiry",
"priority": "normal"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Add comment
if create_response.get("id"):
comment_response = await mcp_service.call_tool(
"update_support_ticket",
{
"platform": "zendesk",
"id": create_response["id"],
"data": {"comment": "Investigating billing discrepancy"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Resolve
resolve_response = await mcp_service.call_tool(
"update_support_ticket",
{
"platform": "zendesk",
"id": create_response["id"],
"data": {"status": "solved", "comment": "Billing discrepancy resolved"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# =============================================================================
# Knowledge Tool E2E Tests
# =============================================================================
class TestMCPToolsKnowledge:
"""E2E tests for MCP knowledge and memory tools."""
@pytest.mark.asyncio
async def test_mcp_ingest_knowledge_from_text(self, mcp_service, e2e_postgres_db):
"""Test ingesting knowledge from text."""
test_text = """
Atom is an AI-powered automation platform that helps businesses streamline workflows.
It uses multi-agent systems with different maturity levels: STUDENT, INTERN, SUPERVISED, and AUTONOMOUS.
The platform integrates with popular tools like Slack, Salesforce, and Jira.
"""
response = await mcp_service.call_tool(
"ingest_knowledge_from_text",
{
"text": test_text,
"doc_id": f"e2e-test-doc-{uuid.uuid4()}",
"source": "e2e_test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "ingest_knowledge_from_text")
# Knowledge ingestion may return entities extracted, relationships found
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_ingest_knowledge_structured_data(self, mcp_service, e2e_postgres_db):
"""Test ingesting knowledge with structured business data."""
business_text = """
Q4 2025 Revenue: $1.2M, up 15% from Q3
Active customers: 1,250
Churn rate: 2.3% (down from 3.1%)
Top product: Atom Enterprise Edition
"""
response = await mcp_service.call_tool(
"ingest_knowledge_from_text",
{
"text": business_text,
"doc_id": f"q4-report-{uuid.uuid4()}",
"source": "quarterly_report"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should extract metrics like revenue, customer count, churn rate
@pytest.mark.asyncio
async def test_mcp_query_knowledge_graph(self, mcp_service, e2e_postgres_db):
"""Test querying knowledge graph."""
# First ingest some knowledge
await mcp_service.call_tool(
"ingest_knowledge_from_text",
{
"text": "Atom uses PostgreSQL for data storage and Redis for caching. The platform supports multi-tenant workspaces.",
"doc_id": f"tech-doc-{uuid.uuid4()}",
"source": "e2e_test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Then query
response = await mcp_service.call_tool(
"query_knowledge_graph",
{
"query": "What database does Atom use?",
"mode": "local"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
assert "results" in response or "answer" in response or "entities" in response
@pytest.mark.asyncio
async def test_mcp_query_knowledge_global_mode(self, mcp_service, e2e_postgres_db):
"""Test querying knowledge graph in global mode (high-level summaries)."""
response = await mcp_service.call_tool(
"query_knowledge_graph",
{
"query": "Summarize Atom's architecture",
"mode": "global"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_save_business_fact(self, mcp_service, e2e_postgres_db):
"""Test saving a verifiable business fact with citations."""
response = await mcp_service.call_tool(
"save_business_fact",
{
"fact": "Atom supports 4 agent maturity levels: STUDENT, INTERN, SUPERVISED, AUTONOMOUS",
"citations": ["/docs/AGENT_MATURITY.md", "/docs/GOVERNANCE.md"],
"reason": "Core architecture principle",
"source": "e2e_test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "save_business_fact")
@pytest.mark.asyncio
async def test_mcp_save_multiple_facts_and_query(self, mcp_service, e2e_postgres_db):
"""Test saving multiple facts and then querying them."""
facts = [
{
"fact": "Atom uses FastAPI for the backend REST API",
"citations": ["/docs/ARCHITECTURE.md"],
"reason": "Technology stack documentation"
},
{
"fact": "MCP stands for Model Context Protocol",
"citations": ["/docs/MCP.md"],
"reason": "Terminology clarification"
}
]
for fact_data in facts:
response = await mcp_service.call_tool(
"save_business_fact",
{**fact_data, "source": "e2e_test"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "save_business_fact")
# =============================================================================
# Canvas Tool E2E Tests
# =============================================================================
class TestMCPToolsCanvas:
"""E2E tests for MCP canvas presentation tools."""
@pytest.mark.asyncio
async def test_mcp_canvas_present_chart(self, mcp_service, e2e_postgres_db, canvas_data_factory):
"""Test presenting a chart via canvas tool."""
chart_data = canvas_data_factory["create_chart_data"](chart_type="line")
response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "present",
"component": "chart",
"data": chart_data,
"title": "E2E Test Chart"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "canvas_tool present chart")
assert "canvas_id" in response or "id" in response or response.get("success") is True
@pytest.mark.asyncio
async def test_mcp_canvas_present_form(self, mcp_service, e2e_postgres_db, canvas_data_factory):
"""Test presenting a form via canvas tool."""
form_data = canvas_data_factory["create_form_data"]()
response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "present",
"component": "form",
"data": form_data,
"title": "E2E Test Form"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "canvas_tool present form")
@pytest.mark.asyncio
async def test_mcp_canvas_update(self, mcp_service, e2e_postgres_db, canvas_data_factory):
"""Test updating a canvas presentation."""
# Present initial canvas
chart_data = canvas_data_factory["create_chart_data"]("bar")
present_response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "present",
"component": "chart",
"data": chart_data,
"title": "Original Chart"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Update if we got a canvas ID
if present_response.get("canvas_id") or present_response.get("id"):
canvas_id = present_response.get("canvas_id") or present_response.get("id")
updated_data = chart_data.copy()
updated_data["title"] = "Updated Chart"
update_response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "update",
"canvas_id": canvas_id,
"data": updated_data
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(update_response, dict)
@pytest.mark.asyncio
async def test_mcp_canvas_close(self, mcp_service, e2e_postgres_db, canvas_data_factory):
"""Test closing a canvas presentation."""
# Present canvas first
form_data = canvas_data_factory["create_form_data"]()
present_response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "present",
"component": "form",
"data": form_data,
"title": "Temporary Form"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Close if we got a canvas ID
if present_response.get("canvas_id") or present_response.get("id"):
canvas_id = present_response.get("canvas_id") or present_response.get("id")
close_response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "close",
"canvas_id": canvas_id
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(close_response, dict)
@pytest.mark.asyncio
async def test_mcp_canvas_governance_checks(self, mcp_service, e2e_postgres_db, test_agents):
"""Test that canvas tool respects agent governance (maturity levels)."""
chart_data = {"type": "line", "title": "Governance Test", "data": {"labels": ["A", "B"], "datasets": [{"data": [1, 2]}]}}
# Test with different agent maturity levels
for maturity, agent in test_agents.items():
response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "present",
"component": "chart",
"data": chart_data,
"title": f"Governance Test - {maturity}"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": agent.id, "user_id": TEST_USER_ID}
)
# All maturity levels should be able to present basic canvas
assert isinstance(response, dict)
# =============================================================================
# Finance Tool E2E Tests
# =============================================================================
class TestMCPToolsFinance:
"""E2E tests for MCP finance tools."""
@pytest.mark.asyncio
async def test_mcp_query_financial_metrics(self, mcp_service, e2e_postgres_db):
"""Test querying financial metrics."""
response = await mcp_service.call_tool(
"query_financial_metrics",
{
"metrics": ["revenue", "expenses", "profit"],
"period": "2025-01"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Should return metrics (may be empty/mocked data)
assert isinstance(response, dict)
assert "metrics" in response or "results" in response or "data" in response
@pytest.mark.asyncio
async def test_mcp_list_finance_invoices(self, mcp_service, e2e_postgres_db):
"""Test listing finance invoices."""
response = await mcp_service.call_tool(
"list_finance_invoices",
{
"status": "pending",
"limit": 10
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
assert "invoices" in response or "results" in response or isinstance(response.get("value"), list)
@pytest.mark.asyncio
async def test_mcp_create_invoice(self, mcp_service, e2e_postgres_db, finance_data_factory):
"""Test creating an invoice."""
invoice_data = finance_data_factory(
customer_id=f"cust-{uuid.uuid4().hex[:8]}",
amount=1500.00,
currency="USD",
description="Professional Services - January 2025",
status="pending"
)
response = await mcp_service.call_tool(
"create_invoice",
invoice_data,
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert_success_response(response, "create_invoice")
assert "id" in response or "invoice_id" in response or response.get("success") is True
# =============================================================================
# WhatsApp Tool E2E Tests
# =============================================================================
class TestMCPToolsWhatsApp:
"""E2E tests for MCP WhatsApp tools (with mocked external API)."""
@pytest.mark.asyncio
async def test_mcp_whatsapp_send_message(self, mcp_service, e2e_postgres_db):
"""Test sending WhatsApp message (will be mocked/hitl gated)."""
response = await mcp_service.call_tool(
"whatsapp_send_message",
{
"to": "+15551234567",
"message": "Test message from E2E test suite",
"template_name": None
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# May be HITL intercepted or return success
assert isinstance(response, dict)
# HITL will return intervention request, or success if agent has permission
@pytest.mark.asyncio
async def test_mcp_whatsapp_send_template(self, mcp_service, e2e_postgres_db):
"""Test sending WhatsApp template message."""
response = await mcp_service.call_tool(
"whatsapp_send_template",
{
"to": "+15551234567",
"template_name": "order_confirmation",
"template_data": {"order_id": "12345", "amount": "$99.99"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_whatsapp_list_templates(self, mcp_service, e2e_postgres_db):
"""Test listing WhatsApp message templates."""
response = await mcp_service.call_tool(
"whatsapp_list_templates",
{"limit": 20},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
assert "templates" in response or isinstance(response.get("value"), list)
# =============================================================================
# Shopify Tool E2E Tests
# =============================================================================
class TestMCPToolsShopify:
"""E2E tests for MCP Shopify tools (with mocked external API)."""
@pytest.mark.asyncio
async def test_mcp_shopify_list_products(self, mcp_service, e2e_postgres_db):
"""Test listing Shopify products."""
response = await mcp_service.call_tool(
"shopify_get_products",
{"limit": 10},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
assert "products" in response or "results" in response or isinstance(response.get("value"), list)
@pytest.mark.asyncio
async def test_mcp_shopify_create_product(self, mcp_service, e2e_postgres_db):
"""Test creating a Shopify product."""
response = await mcp_service.call_tool(
"shopify_create_product",
{
"title": f"E2E Test Product {uuid.uuid4()}",
"description": "Test product for E2E testing",
"price": 29.99,
"inventory_quantity": 100,
"sku": f"E2E-{uuid.uuid4().hex[:8].upper()}"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_shopify_update_inventory(self, mcp_service, e2e_postgres_db):
"""Test updating Shopify product inventory."""
response = await mcp_service.call_tool(
"shopify_update_inventory",
{
"product_id": f"prod_{uuid.uuid4().hex[:8]}",
"inventory_quantity": 50,
"location_id": "default"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_shopify_get_orders(self, mcp_service, e2e_postgres_db):
"""Test fetching Shopify orders."""
response = await mcp_service.call_tool(
"shopify_get_orders",
{
"status": "open",
"limit": 10
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
assert "orders" in response or "results" in response or isinstance(response.get("value"), list)
# =============================================================================
# Additional Tool Tests
# =============================================================================
class TestMCPToolsAdditional:
"""E2E tests for additional MCP tools."""
@pytest.mark.asyncio
async def test_mcp_send_email(self, mcp_service, e2e_postgres_db):
"""Test sending email (should be HITL gated)."""
response = await mcp_service.call_tool(
"send_email",
{
"platform": "gmail",
"to": "test@example.com",
"subject": "E2E Test Email",
"body": "This is a test email from E2E test suite"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Should be HITL intercepted for non-autonomous agents
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_search_emails(self, mcp_service, e2e_postgres_db):
"""Test searching emails."""
response = await mcp_service.call_tool(
"search_emails",
{
"query": "urgent",
"platform": "gmail",
"limit": 10
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_send_message(self, mcp_service, e2e_postgres_db):
"""Test sending message to communication platform."""
response = await mcp_service.call_tool(
"send_message",
{
"platform": "slack",
"target": "#general",
"message": "Test message from E2E test suite"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_post_channel_message(self, mcp_service, e2e_postgres_db):
"""Test posting high-visibility channel message."""
response = await mcp_service.call_tool(
"post_channel_message",
{
"platform": "slack",
"channel": "#announcements",
"message": "Important announcement from E2E test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_reconcile_inventory(self, mcp_service, e2e_postgres_db):
"""Test inventory reconciliation tool."""
response = await mcp_service.call_tool(
"reconcile_inventory",
{
"skus": "SKU-001,SKU-002,SKU-003"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# =============================================================================
# Error Handling Edge Case Tests
# =============================================================================
class TestMCPToolsErrorHandling:
"""E2E tests for MCP tool error handling."""
@pytest.mark.asyncio
async def test_mcp_crm_invalid_email(self, mcp_service, e2e_postgres_db):
"""Test creating CRM contact with invalid email."""
response = await mcp_service.call_tool(
"create_crm_lead",
{
"platform": "salesforce",
"first_name": "Invalid",
"last_name": "Email",
"email": "not-an-email", # Invalid format
"company": "Test Corp"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Should handle gracefully - either reject or return error
assert isinstance(response, dict)
# May have validation error or proceed (depends on integration)
@pytest.mark.asyncio
async def test_mcp_task_invalid_status(self, mcp_service, e2e_postgres_db):
"""Test updating task with invalid status."""
response = await mcp_service.call_tool(
"update_task",
{
"platform": "jira",
"id": "fake-task-id",
"data": {"status": "invalid_status_value_12345"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should return error or handle gracefully
@pytest.mark.asyncio
async def test_mcp_knowledge_empty_ingest(self, mcp_service, e2e_postgres_db):
"""Test ingesting empty knowledge text."""
response = await mcp_service.call_tool(
"ingest_knowledge_from_text",
{
"text": "", # Empty text
"doc_id": f"empty-test-{uuid.uuid4()}",
"source": "e2e_test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should handle empty input gracefully
@pytest.mark.asyncio
async def test_mcp_canvas_invalid_component(self, mcp_service, e2e_postgres_db):
"""Test creating canvas with invalid component type."""
response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "present",
"component": "nonexistent_component_type",
"data": {"invalid": "data"},
"title": "Invalid Component Test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should return error or handle gracefully
@pytest.mark.asyncio
async def test_mcp_missing_required_parameters(self, mcp_service, e2e_postgres_db):
"""Test calling tools with missing required parameters."""
response = await mcp_service.call_tool(
"create_task",
{
# Missing required 'project' and 'title' parameters
"platform": "asana"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should return validation error
@pytest.mark.asyncio
async def test_mcp_unknown_tool_name(self, mcp_service, e2e_postgres_db):
"""Test calling non-existent tool."""
response = await mcp_service.call_tool(
"nonexistent_tool_name_xyz",
{"any": "parameter"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
assert "error" in response or response.get("success") is False
@pytest.mark.asyncio
async def test_mcp_invoice_negative_amount(self, mcp_service, e2e_postgres_db):
"""Test creating invoice with negative amount."""
response = await mcp_service.call_tool(
"create_invoice",
{
"customer_id": f"cust-{uuid.uuid4().hex[:8]}",
"amount": -100.00, # Negative amount
"currency": "USD",
"description": "Invalid negative invoice"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should reject or handle negative amount
@pytest.mark.asyncio
async def test_mcp_support_ticket_missing_subject(self, mcp_service, e2e_postgres_db):
"""Test creating support ticket without required subject."""
response = await mcp_service.call_tool(
"create_support_ticket",
{
"platform": "zendesk",
"description": "Ticket without subject"
# Missing required 'subject' field
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# =============================================================================
# Concurrency Edge Case Tests
# =============================================================================
class TestMCPToolsConcurrency:
"""E2E tests for MCP tool concurrent operations."""
@pytest.mark.asyncio
async def test_mcp_concurrent_task_creation(self, mcp_service, e2e_postgres_db):
"""Test creating 10 tasks concurrently."""
task_count = 10
tasks = []
# Create concurrent tasks
for i in range(task_count):
task = mcp_service.call_tool(
"create_task",
{
"platform": "asana",
"project": "Concurrency Test",
"title": f"Concurrent Task {i} - {uuid.uuid4()}",
"description": "Testing concurrent task creation"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
tasks.append(task)
# Wait for all to complete
results = await asyncio.gather(*tasks, return_exceptions=True)
# All should return dict responses (no exceptions)
for i, result in enumerate(results):
assert not isinstance(result, Exception), f"Task {i} raised exception: {result}"
assert isinstance(result, dict), f"Task {i} returned non-dict: {type(result)}"
@pytest.mark.asyncio
async def test_mcp_concurrent_knowledge_ingest(self, mcp_service, e2e_postgres_db):
"""Test ingesting multiple knowledge documents concurrently."""
doc_count = 5
docs = []
for i in range(doc_count):
doc = mcp_service.call_tool(
"ingest_knowledge_from_text",
{
"text": f"Concurrent test document {i}: This is test content for document {i}.",
"doc_id": f"concurrent-doc-{i}-{uuid.uuid4()}",
"source": "concurrency_test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
docs.append(doc)
results = await asyncio.gather(*docs, return_exceptions=True)
for i, result in enumerate(results):
assert not isinstance(result, Exception), f"Doc {i} raised exception: {result}"
assert isinstance(result, dict)
@pytest.mark.asyncio
async def test_mcp_concurrent_canvas_operations(self, mcp_service, e2e_postgres_db):
"""Test multiple canvas presentations concurrently."""
canvas_count = 5
canvases = []
for i in range(canvas_count):
canvas = mcp_service.call_tool(
"canvas_tool",
{
"action": "present",
"component": "chart",
"data": {
"type": "line",
"title": f"Concurrent Chart {i}",
"data": {
"labels": ["A", "B", "C"],
"datasets": [{"data": [i, i*2, i*3]}]
}
}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
canvases.append(canvas)
results = await asyncio.gather(*canvases, return_exceptions=True)
for i, result in enumerate(results):
assert not isinstance(result, Exception), f"Canvas {i} raised exception: {result}"
assert isinstance(result, dict)
@pytest.mark.asyncio
async def test_mcp_concurrent_contact_search(self, mcp_service, e2e_postgres_db):
"""Test concurrent contact searches."""
search_count = 8
searches = []
queries = ["test", "john", "jane", "acme", "tech", "startup", "customer", "lead"]
for query in queries:
search = mcp_service.call_tool(
"search_contacts",
{"query": query, "platform": "salesforce"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
searches.append(search)
results = await asyncio.gather(*searches, return_exceptions=True)
for i, result in enumerate(results):
assert not isinstance(result, Exception), f"Search {i} raised exception: {result}"
assert isinstance(result, dict)
# =============================================================================
# Performance Edge Case Tests
# =============================================================================
class TestMCPToolsPerformance:
"""E2E tests for MCP tool performance with large datasets."""
@pytest.mark.asyncio
async def test_mcp_bulk_task_creation(self, mcp_service, e2e_postgres_db, performance_monitor):
"""Test creating 100 tasks and measure performance."""
task_count = 100
performance_monitor.start_timer("bulk_task_creation")
tasks = []
for i in range(task_count):
task = mcp_service.call_tool(
"create_task",
{
"platform": "asana",
"project": "Bulk Test Project",
"title": f"Bulk Task {i:03d} - {uuid.uuid4()}",
"description": f"Bulk test task number {i}"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
tasks.append(task)
results = await asyncio.gather(*tasks, return_exceptions=True)
duration = performance_monitor.stop_timer("bulk_task_creation")
# Verify all succeeded
success_count = sum(1 for r in results if isinstance(r, dict))
assert success_count == task_count, f"Only {success_count}/{task_count} tasks succeeded"
# Performance assertion: should complete in reasonable time
# This is a soft assertion - adjust threshold based on environment
print(f"\nBulk task creation ({task_count} tasks): {duration:.2f}ms")
@pytest.mark.asyncio
async def test_mcp_large_knowledge_ingest(self, mcp_service, e2e_postgres_db, performance_monitor):
"""Test ingesting large document (10K characters)."""
# Generate 10K character document
large_text = " ".join([f"Word{i} " for i in range(5000)]) # ~10K chars
performance_monitor.start_timer("large_knowledge_ingest")
response = await mcp_service.call_tool(
"ingest_knowledge_from_text",
{
"text": large_text,
"doc_id": f"large-doc-{uuid.uuid4()}",
"source": "performance_test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
duration = performance_monitor.stop_timer("large_knowledge_ingest")
assert isinstance(response, dict)
print(f"\nLarge knowledge ingest (10K chars): {duration:.2f}ms")
@pytest.mark.asyncio
async def test_mcp_complex_canvas_render(self, mcp_service, e2e_postgres_db, performance_monitor):
"""Test rendering canvas with many components."""
# Create canvas with complex nested data
complex_data = {
"type": "line",
"title": "Performance Test Chart",
"data": {
"labels": [f"Label{i}" for i in range(50)],
"datasets": [
{
"label": f"Dataset{j}",
"data": [i * j for i in range(50)],
"borderColor": f"rgb({j*20}, 100, 200)"
}
for j in range(10)
]
}
}
performance_monitor.start_timer("complex_canvas_render")
response = await mcp_service.call_tool(
"canvas_tool",
{
"action": "present",
"component": "chart",
"data": complex_data,
"title": "Complex Performance Test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
duration = performance_monitor.stop_timer("complex_canvas_render")
assert isinstance(response, dict)
print(f"\nComplex canvas render (50 labels, 10 datasets): {duration:.2f}ms")
@pytest.mark.asyncio
async def test_mcp_rapid_sequential_calls(self, mcp_service, e2e_postgres_db, performance_monitor):
"""Test 50 rapid sequential tool calls."""
call_count = 50
performance_monitor.start_timer("rapid_sequential_calls")
for i in range(call_count):
response = await mcp_service.call_tool(
"search_contacts",
{"query": f"test{i}", "platform": "salesforce"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
duration = performance_monitor.stop_timer("rapid_sequential_calls")
avg_time = duration / call_count
print(f"\nRapid sequential calls ({call_count} calls): {duration:.2f}ms total, {avg_time:.2f}ms avg")
# =============================================================================
# Data Validation Edge Case Tests
# =============================================================================
class TestMCPToolsDataValidation:
"""E2E tests for MCP tool data validation."""
@pytest.mark.asyncio
async def test_mcp_required_field_validation(self, mcp_service, e2e_postgres_db):
"""Test validation of required fields across tools."""
# Missing required fields should be handled gracefully
# Task without title
task_response = await mcp_service.call_tool(
"create_task",
{
"platform": "asana",
"project": "Test Project"
# Missing: title (required)
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(task_response, dict)
# CRM lead without email
crm_response = await mcp_service.call_tool(
"create_crm_lead",
{
"platform": "salesforce",
"first_name": "No",
"last_name": "Email"
# Missing: email (typically required)
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(crm_response, dict)
@pytest.mark.asyncio
async def test_mcp_foreign_key_constraints(self, mcp_service, e2e_postgres_db):
"""Test operations with invalid foreign key references."""
# Update non-existent task
response = await mcp_service.call_tool(
"update_task",
{
"platform": "jira",
"id": "non-existent-task-id-12345",
"data": {"status": "done"}
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should handle gracefully (error or not-found response)
@pytest.mark.asyncio
async def test_mcp_unique_constraints(self, mcp_service, e2e_postgres_db):
"""Test handling of unique constraint violations."""
# Some systems enforce unique emails, IDs, etc.
unique_id = f"unique-{uuid.uuid4()}"
# Create first contact
response1 = await mcp_service.call_tool(
"create_crm_lead",
{
"platform": "salesforce",
"first_name": "Unique",
"last_name": "Test",
"email": f"{unique_id}@example.com",
"company": "Unique Test Corp"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Try to create duplicate (if unique constraint exists)
response2 = await mcp_service.call_tool(
"create_crm_lead",
{
"platform": "salesforce",
"first_name": "Duplicate",
"last_name": "Test",
"email": f"{unique_id}@example.com", # Same email
"company": "Duplicate Corp"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
# Both should return dict responses
assert isinstance(response1, dict)
assert isinstance(response2, dict)
@pytest.mark.asyncio
async def test_mcp_data_type_validation(self, mcp_service, e2e_postgres_db):
"""Test handling of incorrect data types."""
# Pass string where number expected
response = await mcp_service.call_tool(
"create_invoice",
{
"customer_id": f"cust-{uuid.uuid4().hex[:8]}",
"amount": "not-a-number", # Should be number
"currency": "USD",
"description": "Type validation test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
@pytest.mark.asyncio
async def test_mcp_string_length_validation(self, mcp_service, e2e_postgres_db):
"""Test handling of excessively long strings."""
very_long_string = "x" * 10000 # 10K character string
response = await mcp_service.call_tool(
"create_task",
{
"platform": "asana",
"project": "Test",
"title": very_long_string, # Excessively long title
"description": "Testing length validation"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should truncate or reject
@pytest.mark.asyncio
async def test_mcp_special_characters_handling(self, mcp_service, e2e_postgres_db):
"""Test handling of special characters and unicode."""
special_text = "Test with émojis 🎉 spëcial çhars & symbols <script>alert('xss')</script>"
response = await mcp_service.call_tool(
"ingest_knowledge_from_text",
{
"text": special_text,
"doc_id": f"special-chars-{uuid.uuid4()}",
"source": "security_test"
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should handle/sanitize special characters safely
@pytest.mark.asyncio
async def test_mcp_null_value_handling(self, mcp_service, e2e_postgres_db):
"""Test handling of null/None values in parameters."""
response = await mcp_service.call_tool(
"create_crm_lead",
{
"platform": "hubspot",
"first_name": "Test",
"last_name": None, # Explicit None value
"email": f"null-test-{uuid.uuid4()}@example.com",
"company": None
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should handle None values gracefully
@pytest.mark.asyncio
async def test_mcp_sql_injection_prevention(self, mcp_service, e2e_postgres_db):
"""Test that SQL injection attempts are handled safely."""
malicious_input = "'; DROP TABLE contacts; --"
response = await mcp_service.call_tool(
"search_contacts",
{"query": malicious_input, "platform": "salesforce"},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should sanitize input and not cause SQL errors
@pytest.mark.asyncio
async def test_mcp_malformed_json(self, mcp_service, e2e_postgres_db):
"""Test handling of malformed JSON in data parameters."""
response = await mcp_service.call_tool(
"update_task",
{
"platform": "jira",
"id": "test-id",
"data": "{invalid json}" # Malformed JSON string
},
context={"workspace_id": TEST_WORKSPACE_ID, "agent_id": TEST_AGENT_ID, "user_id": TEST_USER_ID}
)
assert isinstance(response, dict)
# Should handle parse errors gracefully
|