test_wp10_tenant_boundaries.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from __future__ import annotations
  2. import pytest
  3. from app.core.system.tenant_context import DeliveryMode, TenantScope
  4. from app.core.system.tenant_resources import (
  5. TenantResourceError,
  6. build_tenant_namespace,
  7. build_tenant_resource_manifest,
  8. )
  9. @pytest.mark.parametrize(
  10. "resource",
  11. ["object", "graph", "cache", "index", "keys", "connectors", "models", "plugins", "backup", "audit_export"],
  12. )
  13. def test_tenant_namespaces_are_resource_specific_and_cannot_cross_scope(resource: str):
  14. alpha = TenantScope("tenant-alpha", DeliveryMode.SHARED_CONTROL_PLANE)
  15. beta = TenantScope("tenant-beta", DeliveryMode.SHARED_CONTROL_PLANE)
  16. alpha_namespace = build_tenant_namespace(alpha, resource, "daily-export")
  17. beta_namespace = build_tenant_namespace(beta, resource, "daily-export")
  18. assert alpha_namespace != beta_namespace
  19. assert alpha_namespace.startswith("tenant-alpha/")
  20. assert beta_namespace.startswith("tenant-beta/")
  21. @pytest.mark.parametrize(
  22. "value",
  23. [
  24. "../tenant-beta",
  25. "tenant-alpha/../tenant-beta",
  26. "https://tenant-beta.example/object",
  27. "tenant-аlpha", # Cyrillic a, not ASCII a.
  28. "tenant-alpha\\tenant-beta",
  29. ],
  30. )
  31. def test_namespace_contract_rejects_escape_url_and_unicode_homograph(value: str):
  32. scope = TenantScope("tenant-alpha", DeliveryMode.SHARED_CONTROL_PLANE)
  33. with pytest.raises(TenantResourceError, match="tenant_resource_invalid"):
  34. build_tenant_namespace(scope, "object", value)
  35. def test_manifest_is_hash_only_and_provider_is_default_disabled():
  36. scope = TenantScope("tenant-alpha", DeliveryMode.SHARED_CONTROL_PLANE)
  37. manifest = build_tenant_resource_manifest(
  38. scope,
  39. resource="backup",
  40. name="backup-001",
  41. digest="a" * 64,
  42. )
  43. assert manifest["tenant_id"] == "tenant-alpha"
  44. assert manifest["provider_enabled"] is False
  45. assert "secret" not in manifest
  46. assert manifest["digest"] == "sha256:" + "a" * 64