|
| 1 | +"""Integration tests for Contacts MCP tools.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import uuid |
| 5 | + |
| 6 | +import pytest |
| 7 | +from mcp import ClientSession |
| 8 | + |
| 9 | +from nextcloud_mcp_server.client import NextcloudClient |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | +pytestmark = pytest.mark.integration |
| 13 | + |
| 14 | + |
| 15 | +async def test_mcp_contacts_workflow( |
| 16 | + nc_mcp_client: ClientSession, nc_client: NextcloudClient |
| 17 | +): |
| 18 | + """Test complete Contacts workflow via MCP tools with verification via NextcloudClient.""" |
| 19 | + |
| 20 | + addressbook_name = f"mcp-test-addressbook-{uuid.uuid4().hex[:8]}" |
| 21 | + unique_suffix = uuid.uuid4().hex[:8] |
| 22 | + contact_uid = f"mcp-contact-{unique_suffix}" |
| 23 | + contact_data = { |
| 24 | + "fn": f"MCP Contact {unique_suffix}", |
| 25 | + "email": f"mcp.contact.{unique_suffix}@example.com", |
| 26 | + "tel": "1234567890", |
| 27 | + } |
| 28 | + |
| 29 | + try: |
| 30 | + # 1. Create address book via MCP |
| 31 | + logger.info(f"Creating address book via MCP: {addressbook_name}") |
| 32 | + create_ab_result = await nc_mcp_client.call_tool( |
| 33 | + "nc_contacts_create_addressbook", |
| 34 | + {"name": addressbook_name, "display_name": f"MCP Test {addressbook_name}"}, |
| 35 | + ) |
| 36 | + assert create_ab_result.isError is False |
| 37 | + |
| 38 | + # 2. Verify address book creation |
| 39 | + addressbooks = await nc_client.contacts.list_addressbooks() |
| 40 | + assert any(ab["name"] == addressbook_name for ab in addressbooks) |
| 41 | + |
| 42 | + # 3. Create contact via MCP |
| 43 | + logger.info(f"Creating contact in {addressbook_name} via MCP") |
| 44 | + create_c_result = await nc_mcp_client.call_tool( |
| 45 | + "nc_contacts_create_contact", |
| 46 | + { |
| 47 | + "addressbook": addressbook_name, |
| 48 | + "uid": contact_uid, |
| 49 | + "contact_data": contact_data, |
| 50 | + }, |
| 51 | + ) |
| 52 | + assert create_c_result.isError is False |
| 53 | + |
| 54 | + # 4. Verify contact creation |
| 55 | + contacts = await nc_client.contacts.list_contacts(addressbook=addressbook_name) |
| 56 | + assert any(c["vcard_id"] == contact_uid for c in contacts) |
| 57 | + |
| 58 | + # 5. Delete contact via MCP |
| 59 | + logger.info(f"Deleting contact {contact_uid} via MCP") |
| 60 | + delete_c_result = await nc_mcp_client.call_tool( |
| 61 | + "nc_contacts_delete_contact", |
| 62 | + {"addressbook": addressbook_name, "uid": contact_uid}, |
| 63 | + ) |
| 64 | + assert delete_c_result.isError is False |
| 65 | + |
| 66 | + # 6. Verify contact deletion |
| 67 | + contacts = await nc_client.contacts.list_contacts(addressbook=addressbook_name) |
| 68 | + assert not any(c["vcard_id"] == contact_uid for c in contacts) |
| 69 | + |
| 70 | + # 7. Delete address book via MCP |
| 71 | + logger.info(f"Deleting address book {addressbook_name} via MCP") |
| 72 | + delete_ab_result = await nc_mcp_client.call_tool( |
| 73 | + "nc_contacts_delete_addressbook", {"name": addressbook_name} |
| 74 | + ) |
| 75 | + assert delete_ab_result.isError is False |
| 76 | + |
| 77 | + # 8. Verify address book deletion |
| 78 | + addressbooks = await nc_client.contacts.list_addressbooks() |
| 79 | + assert not any(ab["name"] == addressbook_name for ab in addressbooks) |
| 80 | + |
| 81 | + finally: |
| 82 | + # Cleanup in case of failure |
| 83 | + try: |
| 84 | + await nc_client.contacts.delete_addressbook(name=addressbook_name) |
| 85 | + except Exception: |
| 86 | + pass |
0 commit comments