- Type-annotate _wrap_contact_field signature; drop stale "url" mention from its docstring (url is handled by the list-coercion helper, not this one). - Split the shape-coercion helper so comma-splitting only applies to categories: _as_str_list (no split) for org/nickname/url, _split_categories (comma split) for CATEGORIES. Fixes the case where organization="Smith, Jones & Associates" was mangled into a two- component ORG. - Share _normalize_contact_data between create and update so _merge_vcard_properties only sees canonical keys; add URL handlers in both update branches so the primary update path no longer drops URL silently. - Annotate the Contact(**kwargs) type:ignore with the reason (pythonvCard4 typeshed doesn't accept **dict[str, Any]). - Add tests/unit/client/test_contacts.py (pure unit, no HTTP) covering the #716 round-trip, comma-in-org regression, invalid-bday warning, tel/phone precedence, categories string-vs-list behaviour, and direct _normalize_contact_data cases. - Extend the MCP workflow test with an update-with-url step asserting the URL handler in _merge_vcard_properties. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
112 lines
4.4 KiB
Python
112 lines
4.4 KiB
Python
"""Integration tests for Contacts MCP tools."""
|
|
|
|
import logging
|
|
import uuid
|
|
|
|
import pytest
|
|
from mcp import ClientSession
|
|
|
|
from nextcloud_mcp_server.client import NextcloudClient
|
|
|
|
logger = logging.getLogger(__name__)
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
async def test_mcp_contacts_workflow(
|
|
nc_mcp_client: ClientSession, nc_client: NextcloudClient
|
|
):
|
|
"""Test complete Contacts workflow via MCP tools with verification via NextcloudClient."""
|
|
|
|
addressbook_name = f"mcp-test-addressbook-{uuid.uuid4().hex[:8]}"
|
|
unique_suffix = uuid.uuid4().hex[:8]
|
|
contact_uid = f"mcp-contact-{unique_suffix}"
|
|
contact_data = {
|
|
"fn": f"MCP Contact {unique_suffix}",
|
|
"email": f"mcp.contact.{unique_suffix}@example.com",
|
|
"tel": "1234567890",
|
|
# Regression for issue #716 — these were silently dropped before
|
|
"organization": "MCP Test Corp",
|
|
"note": f"Created by test {unique_suffix}",
|
|
}
|
|
|
|
try:
|
|
# 1. Create address book via MCP
|
|
logger.info(f"Creating address book via MCP: {addressbook_name}")
|
|
create_ab_result = await nc_mcp_client.call_tool(
|
|
"nc_contacts_create_addressbook",
|
|
{"name": addressbook_name, "display_name": f"MCP Test {addressbook_name}"},
|
|
)
|
|
assert create_ab_result.isError is False
|
|
|
|
# 2. Verify address book creation
|
|
addressbooks = await nc_client.contacts.list_addressbooks()
|
|
assert any(ab["name"] == addressbook_name for ab in addressbooks)
|
|
|
|
# 3. Create contact via MCP
|
|
logger.info(f"Creating contact in {addressbook_name} via MCP")
|
|
create_c_result = await nc_mcp_client.call_tool(
|
|
"nc_contacts_create_contact",
|
|
{
|
|
"addressbook": addressbook_name,
|
|
"uid": contact_uid,
|
|
"contact_data": contact_data,
|
|
},
|
|
)
|
|
assert create_c_result.isError is False
|
|
|
|
# 4. Verify contact creation (and that all fields — #716 — actually persisted)
|
|
contacts = await nc_client.contacts.list_contacts(addressbook=addressbook_name)
|
|
created = next((c for c in contacts if c["vcard_id"] == contact_uid), None)
|
|
assert created is not None
|
|
raw_vcard = created.get("addressdata", "")
|
|
assert "ORG:MCP Test Corp" in raw_vcard
|
|
assert f"NOTE:Created by test {unique_suffix}" in raw_vcard
|
|
|
|
# 4b. Update with a URL — regression guard for PR #719 review:
|
|
# _merge_vcard_properties previously had no URL handler, silently dropping it.
|
|
update_result = await nc_mcp_client.call_tool(
|
|
"nc_contacts_update_contact",
|
|
{
|
|
"addressbook": addressbook_name,
|
|
"uid": contact_uid,
|
|
"contact_data": {"url": "https://mcp-test.example.com"},
|
|
},
|
|
)
|
|
assert update_result.isError is False
|
|
contacts = await nc_client.contacts.list_contacts(addressbook=addressbook_name)
|
|
updated = next(c for c in contacts if c["vcard_id"] == contact_uid)
|
|
updated_vcard = updated.get("addressdata", "")
|
|
assert "mcp-test.example.com" in updated_vcard
|
|
# Prior properties must not have been clobbered by the merge.
|
|
assert "ORG:MCP Test Corp" in updated_vcard
|
|
|
|
# 5. Delete contact via MCP
|
|
logger.info(f"Deleting contact {contact_uid} via MCP")
|
|
delete_c_result = await nc_mcp_client.call_tool(
|
|
"nc_contacts_delete_contact",
|
|
{"addressbook": addressbook_name, "uid": contact_uid},
|
|
)
|
|
assert delete_c_result.isError is False
|
|
|
|
# 6. Verify contact deletion
|
|
contacts = await nc_client.contacts.list_contacts(addressbook=addressbook_name)
|
|
assert not any(c["vcard_id"] == contact_uid for c in contacts)
|
|
|
|
# 7. Delete address book via MCP
|
|
logger.info(f"Deleting address book {addressbook_name} via MCP")
|
|
delete_ab_result = await nc_mcp_client.call_tool(
|
|
"nc_contacts_delete_addressbook", {"name": addressbook_name}
|
|
)
|
|
assert delete_ab_result.isError is False
|
|
|
|
# 8. Verify address book deletion
|
|
addressbooks = await nc_client.contacts.list_addressbooks()
|
|
assert not any(ab["name"] == addressbook_name for ab in addressbooks)
|
|
|
|
finally:
|
|
# Cleanup in case of failure
|
|
try:
|
|
await nc_client.contacts.delete_addressbook(name=addressbook_name)
|
|
except Exception:
|
|
pass
|