fix(webhooks): wire receiver to vector sync queue and fix registered URI

The /webhooks/nextcloud endpoint was a no-op stub that logged the
payload and returned 200 OK; webhook deletions never reached Qdrant.
Compounding that, _get_webhook_uri() registered the docker-compose
internal hostname (http://mcp:8000) with Nextcloud whenever
/.dockerenv existed — including ECS Fargate — so cloud deployments
were registering a URL NC could not resolve.

- New vector/webhook_parser.py extracts a DocumentTask from
  NodeCreatedEvent / NodeWrittenEvent / BeforeNodeDeletedEvent
  payloads scoped to */files/Notes/*.md (matching the registered
  preset filters).
- New vector/webhook_receiver.py pushes that task onto the same
  send-stream the scanner uses (app.state.document_send_stream),
  with 503 when sync is not running so NC retries delivery.
- _get_webhook_uri() now prefers NEXTCLOUD_MCP_SERVER_URL over the
  /.dockerenv branch, so the explicit public URL set on cloud tasks
  wins; docker-compose dev still falls back to the internal name when
  no public URL is configured.

Calendar / Tables event parsing is intentionally out of scope here.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-04-30 03:13:50 +02:00
co-authored by Claude Opus 4.7
parent 111ae988f9
commit 2e2a098bee
7 changed files with 624 additions and 48 deletions
+144
View File
@@ -0,0 +1,144 @@
"""Unit tests for the ``/webhooks/nextcloud`` HTTP receiver.
Builds a minimal Starlette app around ``handle_nextcloud_webhook`` so we can
drive it with ``TestClient`` without standing up the full FastMCP server.
"""
import anyio
import pytest
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.testclient import TestClient
from nextcloud_mcp_server.vector.webhook_receiver import handle_nextcloud_webhook
pytestmark = pytest.mark.unit
def _make_app(send_stream=None) -> Starlette:
app = Starlette(
routes=[
Route("/webhooks/nextcloud", handle_nextcloud_webhook, methods=["POST"])
]
)
app.state.document_send_stream = send_stream
return app
_NOTE_CREATED = {
"user": {"uid": "admin", "displayName": "admin"},
"time": 1762850245,
"event": {
"class": "OCP\\Files\\Events\\Node\\NodeCreatedEvent",
"node": {
"id": 437,
"path": "/admin/files/Notes/Webhooks/Webhook Test Note.md",
},
},
}
_NOTE_DELETED = {
"user": {"uid": "alice"},
"time": 1762851093,
"event": {
"class": "OCP\\Files\\Events\\Node\\BeforeNodeDeletedEvent",
"node": {"id": 99, "path": "/alice/files/Notes/foo.md"},
},
}
def test_index_event_queues_task_and_returns_200():
send_stream, receive_stream = anyio.create_memory_object_stream(max_buffer_size=4)
app = _make_app(send_stream=send_stream)
with TestClient(app) as client:
response = client.post("/webhooks/nextcloud", json=_NOTE_CREATED)
assert response.status_code == 200
assert response.json()["status"] == "queued"
assert response.json()["operation"] == "index"
assert response.json()["doc_id"] == "437"
task = receive_stream.receive_nowait()
assert task.user_id == "admin"
assert task.doc_id == "437"
assert task.operation == "index"
assert task.doc_type == "note"
def test_delete_event_queues_delete_task():
send_stream, receive_stream = anyio.create_memory_object_stream(max_buffer_size=4)
app = _make_app(send_stream=send_stream)
with TestClient(app) as client:
response = client.post("/webhooks/nextcloud", json=_NOTE_DELETED)
assert response.status_code == 200
assert response.json()["operation"] == "delete"
task = receive_stream.receive_nowait()
assert task.operation == "delete"
assert task.doc_id == "99"
assert task.user_id == "alice"
def test_unsupported_event_is_ignored():
send_stream, receive_stream = anyio.create_memory_object_stream(max_buffer_size=4)
app = _make_app(send_stream=send_stream)
payload = {
"user": {"uid": "admin"},
"time": 1,
"event": {
"class": "OCP\\Calendar\\Events\\CalendarObjectCreatedEvent",
"objectData": {"id": 7},
},
}
with TestClient(app) as client:
response = client.post("/webhooks/nextcloud", json=payload)
assert response.status_code == 200
assert response.json()["status"] == "ignored"
with pytest.raises(anyio.WouldBlock):
receive_stream.receive_nowait()
def test_invalid_json_returns_400():
app = _make_app(send_stream=None)
with TestClient(app) as client:
response = client.post(
"/webhooks/nextcloud",
content=b"not json",
headers={"content-type": "application/json"},
)
assert response.status_code == 400
assert response.json()["status"] == "error"
def test_returns_503_when_send_stream_not_wired():
"""Vector sync not running → tell NC to retry instead of dropping the
event."""
app = _make_app(send_stream=None)
with TestClient(app) as client:
response = client.post("/webhooks/nextcloud", json=_NOTE_CREATED)
assert response.status_code == 503
assert response.json()["status"] == "unavailable"
def test_returns_500_when_stream_is_closed():
send_stream, receive_stream = anyio.create_memory_object_stream(max_buffer_size=1)
receive_stream.close() # close receiver → send raises BrokenResourceError
app = _make_app(send_stream=send_stream)
with TestClient(app) as client:
response = client.post("/webhooks/nextcloud", json=_NOTE_CREATED)
assert response.status_code == 500
assert response.json()["status"] == "error"
+188
View File
@@ -0,0 +1,188 @@
"""Unit tests for the Nextcloud webhook payload parser.
Payload examples are taken from real Nextcloud captures recorded in
``webhook-testing-findings.md``.
"""
import pytest
from nextcloud_mcp_server.vector.webhook_parser import extract_document_task
@pytest.mark.unit
def test_node_created_event_returns_index_task():
payload = {
"user": {"uid": "admin", "displayName": "admin"},
"time": 1762850245,
"event": {
"class": "OCP\\Files\\Events\\Node\\NodeCreatedEvent",
"node": {
"id": 437,
"path": "/admin/files/Notes/Webhooks/Webhook Test Note.md",
},
},
}
task = extract_document_task(payload)
assert task is not None
assert task.user_id == "admin"
assert task.doc_id == "437"
assert task.doc_type == "note"
assert task.operation == "index"
assert task.modified_at == 1762850245
@pytest.mark.unit
def test_node_written_event_returns_index_task():
payload = {
"user": {"uid": "admin", "displayName": "admin"},
"time": 1762850960,
"event": {
"class": "OCP\\Files\\Events\\Node\\NodeWrittenEvent",
"node": {
"id": 437,
"path": "/admin/files/Notes/Webhooks/Webhook Test Note.md",
},
},
}
task = extract_document_task(payload)
assert task is not None
assert task.operation == "index"
assert task.doc_id == "437"
@pytest.mark.unit
def test_before_node_deleted_event_returns_delete_task():
payload = {
"user": {"uid": "alice", "displayName": "Alice"},
"time": 1762851093,
"event": {
"class": "OCP\\Files\\Events\\Node\\BeforeNodeDeletedEvent",
"node": {
"id": 437,
"path": "/alice/files/Notes/Webhooks/Webhook Test Note.md",
},
},
}
task = extract_document_task(payload)
assert task is not None
assert task.user_id == "alice"
assert task.operation == "delete"
assert task.doc_id == "437"
assert task.doc_type == "note"
@pytest.mark.unit
def test_node_id_is_normalized_to_string():
"""NC sends node.id as int; we always emit str (per ADR-010 §A.3)."""
payload = {
"user": {"uid": "admin"},
"time": 1,
"event": {
"class": "OCP\\Files\\Events\\Node\\NodeCreatedEvent",
"node": {"id": 437, "path": "/admin/files/Notes/foo.md"},
},
}
task = extract_document_task(payload)
assert task is not None
assert isinstance(task.doc_id, str)
assert task.doc_id == "437"
@pytest.mark.unit
def test_path_outside_notes_returns_none():
payload = {
"user": {"uid": "admin"},
"time": 1,
"event": {
"class": "OCP\\Files\\Events\\Node\\NodeCreatedEvent",
"node": {"id": 1, "path": "/admin/files/Documents/foo.md"},
},
}
assert extract_document_task(payload) is None
@pytest.mark.unit
def test_non_markdown_inside_notes_returns_none():
payload = {
"user": {"uid": "admin"},
"time": 1,
"event": {
"class": "OCP\\Files\\Events\\Node\\NodeCreatedEvent",
"node": {"id": 1, "path": "/admin/files/Notes/image.png"},
},
}
assert extract_document_task(payload) is None
@pytest.mark.unit
def test_parent_folder_event_returns_none():
"""Creating a note fires events for the parent folder too — ignore those."""
payload = {
"user": {"uid": "admin"},
"time": 1,
"event": {
"class": "OCP\\Files\\Events\\Node\\NodeCreatedEvent",
"node": {"id": 100, "path": "/admin/files/Notes/Webhooks"},
},
}
assert extract_document_task(payload) is None
@pytest.mark.unit
def test_unknown_event_class_returns_none():
payload = {
"user": {"uid": "admin"},
"time": 1,
"event": {
"class": "OCP\\Calendar\\Events\\CalendarObjectCreatedEvent",
"objectData": {"id": 7, "uri": "x.ics"},
},
}
assert extract_document_task(payload) is None
@pytest.mark.unit
def test_node_deleted_event_without_id_returns_none():
"""``NodeDeletedEvent`` (no node.id) is not the registered event, but if
one ever leaks through we ignore it rather than guess at the doc_id —
the polling scanner will catch up via its grace period."""
payload = {
"user": {"uid": "admin"},
"time": 1,
"event": {
"class": "OCP\\Files\\Events\\Node\\BeforeNodeDeletedEvent",
"node": {"path": "/admin/files/Notes/foo.md"},
},
}
assert extract_document_task(payload) is None
@pytest.mark.unit
def test_missing_user_field_returns_none():
payload = {
"time": 1,
"event": {
"class": "OCP\\Files\\Events\\Node\\NodeCreatedEvent",
"node": {"id": 1, "path": "/admin/files/Notes/foo.md"},
},
}
assert extract_document_task(payload) is None
@pytest.mark.unit
def test_empty_payload_returns_none():
assert extract_document_task({}) is None
+95
View File
@@ -0,0 +1,95 @@
"""Unit tests for ``_get_webhook_uri`` priority order.
Cloud deployments register the webhook URI returned by this function with
Nextcloud. ECS Fargate also exposes ``/.dockerenv``, so an explicit public
URL must win over the docker auto-detection branch.
"""
import pytest
from nextcloud_mcp_server.auth.webhook_routes import _get_webhook_uri
ENV_VARS = (
"WEBHOOK_INTERNAL_URL",
"NEXTCLOUD_MCP_SERVER_URL",
"NEXTCLOUD_MCP_SERVICE_NAME",
"NEXTCLOUD_MCP_PORT",
"DOCKER_CONTAINER",
)
@pytest.fixture(autouse=True)
def _clean_env(monkeypatch):
for name in ENV_VARS:
monkeypatch.delenv(name, raising=False)
def _no_docker_markers(monkeypatch):
monkeypatch.setattr(
"nextcloud_mcp_server.auth.webhook_routes.os.path.exists",
lambda _path: False,
)
def _docker_markers(monkeypatch):
monkeypatch.setattr(
"nextcloud_mcp_server.auth.webhook_routes.os.path.exists",
lambda path: path == "/.dockerenv",
)
@pytest.mark.unit
def test_webhook_internal_url_wins_over_everything(monkeypatch):
monkeypatch.setenv("WEBHOOK_INTERNAL_URL", "https://internal.example.com")
monkeypatch.setenv("NEXTCLOUD_MCP_SERVER_URL", "https://public.example.com")
_docker_markers(monkeypatch)
assert _get_webhook_uri() == "https://internal.example.com/webhooks/nextcloud"
@pytest.mark.unit
def test_public_url_wins_over_docker_detection(monkeypatch):
"""The bug-fix case: ECS containers have /.dockerenv but a public URL is
set. Docker auto-detection must NOT clobber the explicit public URL."""
monkeypatch.setenv(
"NEXTCLOUD_MCP_SERVER_URL", "https://holy-bluegill.astrolabecloud.com"
)
_docker_markers(monkeypatch)
assert (
_get_webhook_uri()
== "https://holy-bluegill.astrolabecloud.com/webhooks/nextcloud"
)
@pytest.mark.unit
def test_docker_detection_used_when_no_public_url(monkeypatch):
"""docker-compose dev: no public URL set, /.dockerenv exists → use the
docker-compose service name."""
_docker_markers(monkeypatch)
assert _get_webhook_uri() == "http://mcp:8000/webhooks/nextcloud"
@pytest.mark.unit
def test_docker_detection_honors_service_name_and_port_overrides(monkeypatch):
monkeypatch.setenv("NEXTCLOUD_MCP_SERVICE_NAME", "mcp-login-flow")
monkeypatch.setenv("NEXTCLOUD_MCP_PORT", "8004")
_docker_markers(monkeypatch)
assert _get_webhook_uri() == "http://mcp-login-flow:8004/webhooks/nextcloud"
@pytest.mark.unit
def test_docker_container_env_var_triggers_docker_branch(monkeypatch):
monkeypatch.setenv("DOCKER_CONTAINER", "true")
_no_docker_markers(monkeypatch)
assert _get_webhook_uri() == "http://mcp:8000/webhooks/nextcloud"
@pytest.mark.unit
def test_localhost_fallback_when_nothing_set(monkeypatch):
_no_docker_markers(monkeypatch)
assert _get_webhook_uri() == "http://localhost:8000/webhooks/nextcloud"