test_data_research_context.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from __future__ import annotations
  2. import pytest
  3. from app.core.mcp.context import ContextService
  4. from app.core.mcp.identity import AgentIdentity
  5. class Repository:
  6. def list_semantic_property_context(self, property_uid):
  7. assert property_uid == "property-1"
  8. return [
  9. {
  10. "path_uid": "path-1",
  11. "property_uid": property_uid,
  12. "ontology_uid": "ontology-1",
  13. "ontology_status": "published",
  14. "business_domain_uid": "sales",
  15. "data_element_uid": "element-1",
  16. "field_uid": "field-1",
  17. "evidence_uid": "evidence-1",
  18. "token": "must-not-leak",
  19. },
  20. {
  21. "path_uid": "path-2",
  22. "property_uid": property_uid,
  23. "ontology_uid": "ontology-2",
  24. "ontology_status": "draft",
  25. "business_domain_uid": "sales",
  26. },
  27. ]
  28. def identity(domains=frozenset({"sales"})):
  29. return AgentIdentity(
  30. subject="agent-viewer",
  31. roles=frozenset({"viewer"}),
  32. business_domains=domains,
  33. environments=frozenset({"test"}),
  34. correlation_id="correlation-1",
  35. )
  36. def test_mcp_context_returns_only_published_secret_free_scoped_semantics():
  37. result = ContextService(Repository()).get_semantic_property_context(
  38. identity(), "property-1", business_domain="sales", limit=10
  39. )
  40. assert result["count"] == 1
  41. assert result["items"][0]["evidence_uid"] == "evidence-1"
  42. assert "must-not-leak" not in repr(result)
  43. assert "token" not in repr(result).lower()
  44. def test_mcp_context_rejects_cross_domain_and_unbounded_limit():
  45. service = ContextService(Repository())
  46. with pytest.raises(PermissionError):
  47. service.get_semantic_property_context(identity(), "property-1", business_domain="hr")
  48. with pytest.raises(ValueError, match="limit"):
  49. service.get_semantic_property_context(identity(), "property-1", business_domain="sales", limit=51)