Files
mcp-nextcloud/tests/unit/vector/test_scanner_app_gating.py
T
Chris CoutinhoandClaude Opus 4.8 2e609cbea7 fix(vector): gate scanner app polls on per-user enabled apps
The vector-sync scanner polled every indexed app (Notes, Files, News,
Deck) for every provisioned user on each scan cycle. When a user lacks
an app, its REST API returns 404; these were caught (indexing
continued) but flooded tenant logs with repeated 404s, scaling with
users x disabled-apps x scan-frequency and masking real failures.

Add NextcloudClient.get_enabled_apps(), which reads the per-user
/ocs/v2.php/core/navigation/apps endpoint (respects group
restrictions). Chosen over /cloud/capabilities because the News app
advertises no capability and never appears there.

scan_user_documents now resolves the enabled-app set once per cycle and
skips the Notes/News/Deck scans for apps the user lacks. Files stays
unconditional (core Tags API, not a 404 source). Detection failures
fall back to scanning every app (prior behaviour), so a transient
nav-endpoint blip never silently halts indexing; the per-app 404 guards
remain as the safety net.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-07 19:57:00 +02:00

60 lines
2.1 KiB
Python

"""Unit tests for the vector scanner's enabled-app gating helper.
``scan_user_documents`` skips polling apps the user doesn't have enabled (those
polls 404 and flood tenant logs). ``_get_enabled_apps_or_none`` resolves the
enabled-app set, returning ``None`` on any failure so the caller falls back to
scanning every app (the prior behaviour) rather than silently halting indexing.
"""
from unittest.mock import AsyncMock
import pytest
from httpx import HTTPStatusError, Request, Response
from nextcloud_mcp_server.vector.scanner import _get_enabled_apps_or_none
pytestmark = pytest.mark.unit
async def test_returns_enabled_set_on_success():
nc_client = AsyncMock()
nc_client.get_enabled_apps = AsyncMock(return_value={"files", "notes"})
result = await _get_enabled_apps_or_none(nc_client, "alice", scan_id=1234)
assert result == {"files", "notes"}
async def test_returns_none_when_detection_raises(caplog):
nc_client = AsyncMock()
request = Request("GET", "http://nc.test/ocs/v2.php/core/navigation/apps")
nc_client.get_enabled_apps = AsyncMock(
side_effect=HTTPStatusError(
"boom", request=request, response=Response(503, request=request)
)
)
import logging
caplog.set_level(logging.WARNING, logger="nextcloud_mcp_server.vector.scanner")
result = await _get_enabled_apps_or_none(nc_client, "alice", scan_id=1234)
# None signals scan-all fallback; the inline gate treats `None` as
# "every app enabled" so indexing never silently stops.
assert result is None
assert "scanning all apps" in caplog.text
def test_none_set_enables_every_app():
"""The gate predicate used in scan_user_documents: a None set means
detection failed, so every app must be scanned (back-compat)."""
def app_enabled(app_id: str, enabled: set[str] | None) -> bool:
return enabled is None or app_id in enabled
assert app_enabled("news", None) is True
assert app_enabled("deck", None) is True
# And a concrete set gates precisely.
assert app_enabled("news", {"notes"}) is False
assert app_enabled("notes", {"notes"}) is True