graphcore-kernel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
from unittest.mock import Mock, patch, MagicMock, AsyncMock
|
|
2
|
+
|
|
3
|
+
from django.test import TestCase
|
|
4
|
+
from asgiref.sync import async_to_sync
|
|
5
|
+
from graphcore.types.identity import Principal
|
|
6
|
+
from graphcore.persistence.django.models import EntityModel
|
|
7
|
+
from graphcore.persistence.tests.memory import MemoryEntityRepository
|
|
8
|
+
from graphcore.domain.entity.Entity import Entity
|
|
9
|
+
from graphcore.errors import ConcurrentModificationError, Forbidden, InvalidRequest, NotFound
|
|
10
|
+
from graphcore.execution.strategy_context import kernel_internal_context, strategy_context
|
|
11
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
12
|
+
from graphcore.scheduling.tests.memory import InMemoryScheduler
|
|
13
|
+
from graphcore.runtime import build_runtime, django_runtime
|
|
14
|
+
from graphcore.auth.authz.adapter.default_access_adapter import DefaultAccessAdapter
|
|
15
|
+
from graphcore.broadcast.channels import ChannelsBroadcaster
|
|
16
|
+
from graphcore.gateway.remote import NoRemoteGateway
|
|
17
|
+
from graphcore.persistence.django.cache import DjangoCache
|
|
18
|
+
from graphcore.types.entities import Tombstone
|
|
19
|
+
from graphcore.tests.ids import fixture_id
|
|
20
|
+
from graphcore.types.entity_id import backend_of, is_entity_id, new_entity_id
|
|
21
|
+
|
|
22
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _principal(entity_id: str) -> Principal:
|
|
26
|
+
"""Test-only shorthand: wrap a raw identity string as an identity-only Principal."""
|
|
27
|
+
return Principal(principal_entity_id=entity_id)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Note(Entity):
|
|
31
|
+
"""Mock entity for testing"""
|
|
32
|
+
|
|
33
|
+
def __init__(self, entity_id=None):
|
|
34
|
+
super().__init__(entity_id)
|
|
35
|
+
self.set_attribute('name', 'Test Entity')
|
|
36
|
+
|
|
37
|
+
def to_client(self):
|
|
38
|
+
return {'id': self.entity_id, 'name': self.get_attribute('name')}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Other(Entity):
|
|
42
|
+
"""A second registered type, for type-filtered traversal."""
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
TEST_REGISTRY.entities.register_entity(Note, app_label='graphcore')
|
|
46
|
+
TEST_REGISTRY.entities.register_entity(Other, app_label='graphcore')
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def in_memory_service():
|
|
50
|
+
"""EntityService over an in-memory repository: real orchestration, real
|
|
51
|
+
access decisions, no SQL."""
|
|
52
|
+
repository = MemoryEntityRepository(TEST_REGISTRY.entities)
|
|
53
|
+
cache = DjangoCache(entity_ttl=900)
|
|
54
|
+
access = DefaultAccessAdapter(TEST_REGISTRY, repository, cache)
|
|
55
|
+
broadcaster = ChannelsBroadcaster(access, TEST_REGISTRY, None)
|
|
56
|
+
return build_runtime(
|
|
57
|
+
TEST_REGISTRY, repository, cache, access, broadcaster, InMemoryScheduler(),
|
|
58
|
+
NoRemoteGateway(), 100,
|
|
59
|
+
).entity_service
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class EntityServiceTestCase(TestCase):
|
|
63
|
+
"""EntityService orchestration over a real (in-memory) repository."""
|
|
64
|
+
TEST_USER_ENTITY_ID = "test-user-entity-id"
|
|
65
|
+
|
|
66
|
+
def setUp(self):
|
|
67
|
+
self.entity_service = in_memory_service()
|
|
68
|
+
self.repository = self.entity_service.repository
|
|
69
|
+
self.test_entity_id = fixture_id(self.entity_service.backend_id)
|
|
70
|
+
|
|
71
|
+
self.entity_service.cache.get = Mock(return_value=None)
|
|
72
|
+
self.entity_service.cache.set = Mock()
|
|
73
|
+
self.entity_service.cache.delete = Mock()
|
|
74
|
+
self.entity_service.cache.get_entity = Mock(return_value=None)
|
|
75
|
+
self.entity_service.cache.set_entity = Mock()
|
|
76
|
+
self.entity_service.cache.delete_entity = Mock()
|
|
77
|
+
self.entity_service.cache.get_entities = Mock(return_value={})
|
|
78
|
+
self.entity_service.cache.set_entities = Mock()
|
|
79
|
+
self.entity_service.cache.delete_entities = Mock()
|
|
80
|
+
self.entity_service.cache.clear_all_entities = Mock()
|
|
81
|
+
|
|
82
|
+
async def _persist(self, entity, owner=None):
|
|
83
|
+
with kernel_internal_context():
|
|
84
|
+
return await self.entity_service.save_entity(entity, _principal(owner or self.TEST_USER_ENTITY_ID))
|
|
85
|
+
|
|
86
|
+
def test_create_mints_a_local_identifier(self):
|
|
87
|
+
entity = self.entity_service.create(Entity)
|
|
88
|
+
|
|
89
|
+
self.assertTrue(is_entity_id(entity.entity_id))
|
|
90
|
+
self.assertEqual(backend_of(entity.entity_id), self.entity_service.backend_id)
|
|
91
|
+
|
|
92
|
+
def test_create_forwards_kwargs_to_the_constructor(self):
|
|
93
|
+
from graphcore.domain.entity.user.User import User
|
|
94
|
+
|
|
95
|
+
entity = self.entity_service.create(User, username='x')
|
|
96
|
+
|
|
97
|
+
self.assertEqual(entity.username, 'x')
|
|
98
|
+
|
|
99
|
+
def test_create_mints_a_distinct_id_each_time(self):
|
|
100
|
+
self.assertNotEqual(
|
|
101
|
+
self.entity_service.create(Entity).entity_id,
|
|
102
|
+
self.entity_service.create(Entity).entity_id,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
async def test_get_entity_reads_storage_not_the_payload_cache(self):
|
|
106
|
+
entity = await self._persist(Note(self.test_entity_id))
|
|
107
|
+
entity.set_attribute('name', 'Updated')
|
|
108
|
+
await self._persist(entity)
|
|
109
|
+
stale = Note(self.test_entity_id)
|
|
110
|
+
stale.version = 1
|
|
111
|
+
self.entity_service.cache.get_entity = Mock(return_value=stale)
|
|
112
|
+
|
|
113
|
+
result = await self.entity_service.get_entity(self.test_entity_id, _principal(self.TEST_USER_ENTITY_ID))
|
|
114
|
+
|
|
115
|
+
self.assertEqual((result.version, result.get_attribute('name')), (2, 'Updated'))
|
|
116
|
+
self.entity_service.cache.get_entity.assert_not_called()
|
|
117
|
+
self.entity_service.cache.set_entity.assert_not_called()
|
|
118
|
+
|
|
119
|
+
async def test_get_entity_from_storage(self):
|
|
120
|
+
await self._persist(Note(self.test_entity_id))
|
|
121
|
+
|
|
122
|
+
result = await self.entity_service.get_entity(self.test_entity_id, _principal(self.TEST_USER_ENTITY_ID))
|
|
123
|
+
|
|
124
|
+
self.assertEqual(result.entity_id, self.test_entity_id)
|
|
125
|
+
|
|
126
|
+
async def test_get_entity_not_found(self):
|
|
127
|
+
with self.assertRaises(NotFound) as context:
|
|
128
|
+
await self.entity_service.get_entity(self.test_entity_id, _principal(self.TEST_USER_ENTITY_ID))
|
|
129
|
+
|
|
130
|
+
self.assertIn('not found', str(context.exception))
|
|
131
|
+
|
|
132
|
+
async def test_a_supplied_existing_is_used_only_at_the_entitys_version(self):
|
|
133
|
+
entity = await self._persist(Note(self.test_entity_id))
|
|
134
|
+
stored = await self.repository.load(self.test_entity_id)
|
|
135
|
+
load = AsyncMock(wraps=self.repository.load)
|
|
136
|
+
self.repository.load = load
|
|
137
|
+
|
|
138
|
+
entity.set_attribute('name', 'second')
|
|
139
|
+
with kernel_internal_context():
|
|
140
|
+
await self.entity_service.save_entity(entity, _principal(self.TEST_USER_ENTITY_ID), existing=stored)
|
|
141
|
+
load.assert_not_called()
|
|
142
|
+
|
|
143
|
+
entity.set_attribute('name', 'third')
|
|
144
|
+
with kernel_internal_context():
|
|
145
|
+
saved = await self.entity_service.save_entity(
|
|
146
|
+
entity, _principal(self.TEST_USER_ENTITY_ID), existing=stored,
|
|
147
|
+
)
|
|
148
|
+
load.assert_called_once_with(self.test_entity_id)
|
|
149
|
+
self.assertEqual((saved.version, saved.get_attribute('name')), (3, 'third'))
|
|
150
|
+
|
|
151
|
+
async def test_save_entity_new(self):
|
|
152
|
+
result = await self._persist(Note(self.test_entity_id))
|
|
153
|
+
|
|
154
|
+
self.assertEqual(result.version, 1)
|
|
155
|
+
self.assertEqual(result.owner, self.TEST_USER_ENTITY_ID)
|
|
156
|
+
self.entity_service.cache.delete_entity.assert_called_once_with(self.test_entity_id)
|
|
157
|
+
self.entity_service.cache.set_entity.assert_not_called()
|
|
158
|
+
|
|
159
|
+
async def test_save_entity_update_increments_version_in_memory(self):
|
|
160
|
+
entity = await self._persist(Note(self.test_entity_id))
|
|
161
|
+
entity.set_attribute('name', 'Updated')
|
|
162
|
+
|
|
163
|
+
result = await self._persist(entity)
|
|
164
|
+
|
|
165
|
+
self.assertEqual(result.version, 2)
|
|
166
|
+
self.assertEqual(entity.version, 2)
|
|
167
|
+
self.assertEqual(
|
|
168
|
+
(await self.repository.load(self.test_entity_id)).get_attribute('name'), 'Updated',
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
async def test_save_entity_concurrent_modification(self):
|
|
172
|
+
entity = await self._persist(Note(self.test_entity_id))
|
|
173
|
+
entity.version = 0
|
|
174
|
+
|
|
175
|
+
with self.assertRaises(ConcurrentModificationError) as context:
|
|
176
|
+
await self._persist(entity)
|
|
177
|
+
|
|
178
|
+
self.assertIn('version mismatch', str(context.exception).lower())
|
|
179
|
+
self.assertIn('expected 0', str(context.exception))
|
|
180
|
+
self.assertIn('found 1', str(context.exception))
|
|
181
|
+
|
|
182
|
+
async def test_failed_save_broadcasts_nothing(self):
|
|
183
|
+
entity = await self._persist(Note(self.test_entity_id))
|
|
184
|
+
entity.version = 0
|
|
185
|
+
broadcaster = AsyncMock()
|
|
186
|
+
self.entity_service.broadcaster = broadcaster
|
|
187
|
+
|
|
188
|
+
with self.assertRaises(ConcurrentModificationError):
|
|
189
|
+
await self._persist(entity)
|
|
190
|
+
|
|
191
|
+
broadcaster.publish_save.assert_not_called()
|
|
192
|
+
broadcaster.publish_access_change.assert_not_called()
|
|
193
|
+
|
|
194
|
+
async def test_save_entity_hook_order(self):
|
|
195
|
+
"""persist -> cache -> entity_saved -> publish_save."""
|
|
196
|
+
calls = []
|
|
197
|
+
self.entity_service.cache.delete_entity = Mock(
|
|
198
|
+
side_effect=lambda *a, **k: calls.append('cache')
|
|
199
|
+
)
|
|
200
|
+
adapter = MagicMock()
|
|
201
|
+
|
|
202
|
+
async def entity_saved(entity, previous):
|
|
203
|
+
calls.append('entity_saved')
|
|
204
|
+
self.assertEqual(entity.version, 1)
|
|
205
|
+
self.assertIsNone(previous)
|
|
206
|
+
return None
|
|
207
|
+
|
|
208
|
+
adapter.entity_saved = entity_saved
|
|
209
|
+
adapter.allows = AsyncMock(return_value=True)
|
|
210
|
+
self.entity_service.access_adapter = adapter
|
|
211
|
+
|
|
212
|
+
broadcaster = AsyncMock()
|
|
213
|
+
broadcaster.publish_save = AsyncMock(side_effect=lambda *a: calls.append('publish'))
|
|
214
|
+
self.entity_service.broadcaster = broadcaster
|
|
215
|
+
|
|
216
|
+
await self._persist(Note(self.test_entity_id))
|
|
217
|
+
|
|
218
|
+
self.assertEqual(calls, ['cache', 'entity_saved', 'publish'])
|
|
219
|
+
|
|
220
|
+
async def test_save_entity_passes_the_previous_entity_to_the_access_hook(self):
|
|
221
|
+
entity = await self._persist(Note(self.test_entity_id))
|
|
222
|
+
entity.set_attribute('name', 'Second')
|
|
223
|
+
|
|
224
|
+
seen = {}
|
|
225
|
+
adapter = MagicMock()
|
|
226
|
+
|
|
227
|
+
async def entity_saved(saved, previous):
|
|
228
|
+
seen['previous'] = previous
|
|
229
|
+
return None
|
|
230
|
+
|
|
231
|
+
adapter.entity_saved = entity_saved
|
|
232
|
+
adapter.allows = AsyncMock(return_value=True)
|
|
233
|
+
self.entity_service.access_adapter = adapter
|
|
234
|
+
self.entity_service.broadcaster = AsyncMock()
|
|
235
|
+
|
|
236
|
+
await self._persist(entity)
|
|
237
|
+
|
|
238
|
+
self.assertEqual(seen['previous'].get_attribute('name'), 'Test Entity')
|
|
239
|
+
|
|
240
|
+
async def test_save_entity_republishes_touched_parents(self):
|
|
241
|
+
parent = await self._persist(self.entity_service.create(Note))
|
|
242
|
+
child = self.entity_service.create(Note)
|
|
243
|
+
child.set_parent(parent.entity_id)
|
|
244
|
+
|
|
245
|
+
broadcaster = AsyncMock()
|
|
246
|
+
self.entity_service.broadcaster = broadcaster
|
|
247
|
+
await self._persist(child)
|
|
248
|
+
|
|
249
|
+
republished = [call.args[0].entity_id for call in broadcaster.publish_save.await_args_list]
|
|
250
|
+
self.assertEqual(republished, [child.entity_id, parent.entity_id])
|
|
251
|
+
|
|
252
|
+
async def test_reparenting_invalidates_the_moved_subtree_cache(self):
|
|
253
|
+
"""`ancestor_ids` rides in the cached payload but a move never bumps
|
|
254
|
+
a descendant's version, so only an explicit invalidation stops the
|
|
255
|
+
pre-move ancestry being served forever."""
|
|
256
|
+
old_parent = await self._persist(self.entity_service.create(Note))
|
|
257
|
+
new_parent = await self._persist(self.entity_service.create(Note))
|
|
258
|
+
child = self.entity_service.create(Note)
|
|
259
|
+
child.set_parent(old_parent.entity_id)
|
|
260
|
+
await self._persist(child)
|
|
261
|
+
grandchild = self.entity_service.create(Note)
|
|
262
|
+
grandchild.set_parent(child.entity_id)
|
|
263
|
+
await self._persist(grandchild)
|
|
264
|
+
|
|
265
|
+
self.entity_service.cache.delete_entities.reset_mock()
|
|
266
|
+
self.entity_service.broadcaster = AsyncMock()
|
|
267
|
+
child.set_parent(new_parent.entity_id)
|
|
268
|
+
await self._persist(child)
|
|
269
|
+
|
|
270
|
+
invalidated = {
|
|
271
|
+
eid for call in self.entity_service.cache.delete_entities.call_args_list
|
|
272
|
+
for eid in call.args[0]
|
|
273
|
+
}
|
|
274
|
+
self.assertIn(grandchild.entity_id, invalidated)
|
|
275
|
+
self.assertEqual(
|
|
276
|
+
(await self.repository.load(grandchild.entity_id)).ancestor_ids,
|
|
277
|
+
(new_parent.entity_id, child.entity_id),
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
async def test_deleted_bag_value_does_not_delete_entity(self):
|
|
281
|
+
entity = Note(self.test_entity_id)
|
|
282
|
+
entity.set_attribute('deleted', True)
|
|
283
|
+
with patch.object(self.entity_service, 'delete_entity_tree', new_callable=AsyncMock) as mock_delete:
|
|
284
|
+
with kernel_internal_context():
|
|
285
|
+
await self.entity_service.save_entity(entity, _principal(self.TEST_USER_ENTITY_ID))
|
|
286
|
+
mock_delete.assert_not_called()
|
|
287
|
+
loaded = await self.repository.load(entity.entity_id)
|
|
288
|
+
self.assertTrue(loaded.get_attribute('deleted'))
|
|
289
|
+
|
|
290
|
+
async def test_delete_entities_single_entity(self):
|
|
291
|
+
await self._persist(Note(self.test_entity_id))
|
|
292
|
+
self.entity_service.broadcaster = AsyncMock()
|
|
293
|
+
|
|
294
|
+
result = await self.entity_service.delete_entities(
|
|
295
|
+
[self.test_entity_id], principal=_principal(self.TEST_USER_ENTITY_ID)
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
self.entity_service.cache.delete_entities.assert_called_once_with([self.test_entity_id])
|
|
299
|
+
self.assertEqual(result, [self.test_entity_id])
|
|
300
|
+
self.assertIsNone(await self.repository.load(self.test_entity_id))
|
|
301
|
+
|
|
302
|
+
async def test_delete_entity_tree_removes_the_whole_subtree(self):
|
|
303
|
+
root = await self._persist(self.entity_service.create(Note))
|
|
304
|
+
child = self.entity_service.create(Note)
|
|
305
|
+
child.set_parent(root.entity_id)
|
|
306
|
+
await self._persist(child)
|
|
307
|
+
grandchild = self.entity_service.create(Note)
|
|
308
|
+
grandchild.set_parent(child.entity_id)
|
|
309
|
+
await self._persist(grandchild)
|
|
310
|
+
self.entity_service.broadcaster = AsyncMock()
|
|
311
|
+
|
|
312
|
+
with kernel_internal_context():
|
|
313
|
+
deleted = await self.entity_service.delete_entity_tree(
|
|
314
|
+
root.entity_id, Principal(principal_entity_id=self.TEST_USER_ENTITY_ID),
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
self.assertEqual(
|
|
318
|
+
set(deleted), {root.entity_id, child.entity_id, grandchild.entity_id},
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
async def test_the_scheduler_hears_every_committed_save_and_delete(self):
|
|
322
|
+
scheduler = self.entity_service.scheduler = AsyncMock()
|
|
323
|
+
self.entity_service.broadcaster = AsyncMock()
|
|
324
|
+
root = await self._persist(self.entity_service.create(Note))
|
|
325
|
+
child = self.entity_service.create(Note)
|
|
326
|
+
child.set_parent(root.entity_id)
|
|
327
|
+
await self._persist(child)
|
|
328
|
+
|
|
329
|
+
saved = [call.args[0].entity_id for call in scheduler.entity_saved.await_args_list]
|
|
330
|
+
self.assertEqual(saved, [root.entity_id, child.entity_id])
|
|
331
|
+
self.assertIsNone(scheduler.entity_saved.await_args_list[0].args[1])
|
|
332
|
+
|
|
333
|
+
with kernel_internal_context():
|
|
334
|
+
await self.entity_service.delete_entity_tree(
|
|
335
|
+
root.entity_id, Principal(principal_entity_id=self.TEST_USER_ENTITY_ID),
|
|
336
|
+
)
|
|
337
|
+
self.assertEqual(set(scheduler.entities_deleted.await_args.args[0]), {root.entity_id, child.entity_id})
|
|
338
|
+
|
|
339
|
+
async def test_a_scheduler_failure_does_not_fail_the_write(self):
|
|
340
|
+
self.entity_service.broadcaster = AsyncMock()
|
|
341
|
+
self.entity_service.scheduler.entity_saved = AsyncMock(side_effect=RuntimeError('down'))
|
|
342
|
+
self.entity_service.scheduler.entities_deleted = AsyncMock(side_effect=RuntimeError('down'))
|
|
343
|
+
root = await self._persist(self.entity_service.create(Note))
|
|
344
|
+
|
|
345
|
+
with kernel_internal_context():
|
|
346
|
+
deleted = await self.entity_service.delete_entity_tree(
|
|
347
|
+
root.entity_id, Principal(principal_entity_id=self.TEST_USER_ENTITY_ID),
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
self.assertEqual(deleted, [root.entity_id])
|
|
351
|
+
|
|
352
|
+
async def test_delete_publishes_tombstones_before_access_changes(self):
|
|
353
|
+
"""A deleted scope's own grant rows would otherwise drop the
|
|
354
|
+
grantee's channel membership before the scope's tombstone arrived."""
|
|
355
|
+
entity = await self._persist(Note(self.test_entity_id))
|
|
356
|
+
calls = []
|
|
357
|
+
broadcaster = AsyncMock()
|
|
358
|
+
broadcaster.publish_delete = AsyncMock(side_effect=lambda *a: calls.append('tombstone'))
|
|
359
|
+
broadcaster.publish_access_change = AsyncMock(side_effect=lambda *a: calls.append('access'))
|
|
360
|
+
self.entity_service.broadcaster = broadcaster
|
|
361
|
+
|
|
362
|
+
adapter = MagicMock()
|
|
363
|
+
|
|
364
|
+
async def prepare_delete(entity_ids):
|
|
365
|
+
calls.append('prepare')
|
|
366
|
+
return 'plan'
|
|
367
|
+
|
|
368
|
+
async def entities_deleted(plan):
|
|
369
|
+
calls.append(f'hook:{plan}')
|
|
370
|
+
return [object()]
|
|
371
|
+
|
|
372
|
+
adapter.prepare_delete = prepare_delete
|
|
373
|
+
adapter.entities_deleted = entities_deleted
|
|
374
|
+
self.entity_service.access_adapter = adapter
|
|
375
|
+
|
|
376
|
+
await self.entity_service.delete_entities([entity.entity_id], _principal(self.TEST_USER_ENTITY_ID))
|
|
377
|
+
|
|
378
|
+
self.assertEqual(calls, ['prepare', 'tombstone', 'hook:plan', 'access'])
|
|
379
|
+
|
|
380
|
+
async def test_delete_publishes_a_tombstone_carrying_its_ancestry(self):
|
|
381
|
+
root = await self._persist(self.entity_service.create(Note))
|
|
382
|
+
child = self.entity_service.create(Note)
|
|
383
|
+
child.set_parent(root.entity_id)
|
|
384
|
+
await self._persist(child)
|
|
385
|
+
broadcaster = AsyncMock()
|
|
386
|
+
self.entity_service.broadcaster = broadcaster
|
|
387
|
+
|
|
388
|
+
await self.entity_service.delete_entities([child.entity_id], _principal(self.TEST_USER_ENTITY_ID))
|
|
389
|
+
|
|
390
|
+
deleted_row = broadcaster.publish_delete.await_args.args[0]
|
|
391
|
+
self.assertEqual(deleted_row.entity_id, child.entity_id)
|
|
392
|
+
self.assertEqual(deleted_row.ancestor_ids, (root.entity_id,))
|
|
393
|
+
|
|
394
|
+
async def test_delete_entities_requires_principal(self):
|
|
395
|
+
"""delete_entities has no unauthenticated/unauthorized path."""
|
|
396
|
+
with self.assertRaises(TypeError):
|
|
397
|
+
await self.entity_service.delete_entities([self.test_entity_id])
|
|
398
|
+
|
|
399
|
+
async def test_delete_entities_rejects_unauthorized_user(self):
|
|
400
|
+
"""A user without delete access cannot delete via the authorized
|
|
401
|
+
entry point (delete_entity_tree), and the row survives. `delete_entities`
|
|
402
|
+
itself is an internal, unchecked bookkeeping path called only after
|
|
403
|
+
that authorization has already happened."""
|
|
404
|
+
from graphcore.domain.strategy.BaseStrategy import DeleteEntity
|
|
405
|
+
|
|
406
|
+
await self._persist(Note(self.test_entity_id))
|
|
407
|
+
|
|
408
|
+
with self.assertRaises(Forbidden):
|
|
409
|
+
with strategy_context(DeleteEntity):
|
|
410
|
+
await self.entity_service.delete_entity_tree(
|
|
411
|
+
self.test_entity_id, Principal(principal_entity_id='someone-else')
|
|
412
|
+
)
|
|
413
|
+
|
|
414
|
+
self.assertTrue(await self.entity_service.entity_exists_in_db(self.test_entity_id))
|
|
415
|
+
|
|
416
|
+
async def test_entity_exists_in_db(self):
|
|
417
|
+
await self._persist(Note(self.test_entity_id))
|
|
418
|
+
|
|
419
|
+
self.assertTrue(await self.entity_service.entity_exists_in_db(self.test_entity_id))
|
|
420
|
+
self.assertFalse(await self.entity_service.entity_exists_in_db(fixture_id(self.entity_service.backend_id)))
|
|
421
|
+
|
|
422
|
+
async def test_load_from_db(self):
|
|
423
|
+
await self._persist(Note(self.test_entity_id))
|
|
424
|
+
|
|
425
|
+
self.assertIsNotNone(await self.entity_service.load_from_db(self.test_entity_id))
|
|
426
|
+
self.assertIsNone(await self.entity_service.load_from_db(fixture_id(self.entity_service.backend_id)))
|
|
427
|
+
|
|
428
|
+
def test_load_from_cache(self):
|
|
429
|
+
entity = Note(self.test_entity_id)
|
|
430
|
+
self.entity_service.cache.get_entity = Mock(return_value=entity)
|
|
431
|
+
|
|
432
|
+
self.assertEqual(self.entity_service.load_from_cache(self.test_entity_id), entity)
|
|
433
|
+
|
|
434
|
+
def test_load_entities_from_cache(self):
|
|
435
|
+
entity_ids = [fixture_id(self.entity_service.backend_id), fixture_id(self.entity_service.backend_id)]
|
|
436
|
+
entities = {eid: Note(eid) for eid in entity_ids}
|
|
437
|
+
self.entity_service.cache.get_entities = Mock(return_value=entities)
|
|
438
|
+
|
|
439
|
+
self.assertEqual(self.entity_service.load_entities_from_cache(entity_ids), entities)
|
|
440
|
+
|
|
441
|
+
def test_clear_entities(self):
|
|
442
|
+
entity_ids = [fixture_id(self.entity_service.backend_id), fixture_id(self.entity_service.backend_id)]
|
|
443
|
+
|
|
444
|
+
self.entity_service.clear_entities(entity_ids)
|
|
445
|
+
|
|
446
|
+
self.entity_service.cache.delete_entities.assert_called_once_with(entity_ids)
|
|
447
|
+
|
|
448
|
+
def test_clear_all_entities(self):
|
|
449
|
+
self.entity_service.clear_all_entities()
|
|
450
|
+
|
|
451
|
+
self.entity_service.cache.clear_all_entities.assert_called_once()
|
|
452
|
+
|
|
453
|
+
async def test_get_children_ids_by_type(self):
|
|
454
|
+
"""Test get_children_ids_by_type filters children by class name"""
|
|
455
|
+
parent_id = fixture_id(self.entity_service.backend_id)
|
|
456
|
+
parent_entity = Note(parent_id)
|
|
457
|
+
child1_id = fixture_id(self.entity_service.backend_id)
|
|
458
|
+
child2_id = fixture_id(self.entity_service.backend_id)
|
|
459
|
+
child3_id = fixture_id(self.entity_service.backend_id)
|
|
460
|
+
|
|
461
|
+
parent_entity._children_ids = tuple([child1_id, child2_id, child3_id])
|
|
462
|
+
|
|
463
|
+
# Create mock children of different types
|
|
464
|
+
child1 = Note(child1_id)
|
|
465
|
+
child2 = Note(child2_id)
|
|
466
|
+
|
|
467
|
+
child3 = Other(child3_id)
|
|
468
|
+
|
|
469
|
+
children_by_id = {child1_id: child1, child2_id: child2, child3_id: child3}
|
|
470
|
+
|
|
471
|
+
async def mock_get_entity(entity_id, principal_entity_id):
|
|
472
|
+
return parent_entity
|
|
473
|
+
|
|
474
|
+
async def mock_get_entities(entity_ids, principal_entity_id):
|
|
475
|
+
return {eid: children_by_id[eid] for eid in entity_ids if eid in children_by_id}
|
|
476
|
+
|
|
477
|
+
with patch.object(self.entity_service, 'get_entity', side_effect=mock_get_entity), \
|
|
478
|
+
patch.object(self.entity_service, 'get_entities', side_effect=mock_get_entities):
|
|
479
|
+
result = await self.entity_service.get_children_ids_by_type(
|
|
480
|
+
parent_id, 'graphcore.Note', _principal(self.TEST_USER_ENTITY_ID)
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
self.assertEqual(len(result), 2)
|
|
484
|
+
self.assertIn(child1_id, result)
|
|
485
|
+
self.assertIn(child2_id, result)
|
|
486
|
+
self.assertNotIn(child3_id, result)
|
|
487
|
+
|
|
488
|
+
async def test_get_children_ids_by_type_handles_not_found(self):
|
|
489
|
+
"""Test get_children_ids_by_type handles missing children gracefully"""
|
|
490
|
+
parent_id = fixture_id(self.entity_service.backend_id)
|
|
491
|
+
parent_entity = Note(parent_id)
|
|
492
|
+
valid_child_id = fixture_id(self.entity_service.backend_id)
|
|
493
|
+
invalid_child_id = fixture_id(self.entity_service.backend_id)
|
|
494
|
+
|
|
495
|
+
parent_entity._children_ids = tuple([valid_child_id, invalid_child_id])
|
|
496
|
+
|
|
497
|
+
valid_child = Note(valid_child_id)
|
|
498
|
+
|
|
499
|
+
async def mock_get_entity(entity_id, principal_entity_id):
|
|
500
|
+
return parent_entity
|
|
501
|
+
|
|
502
|
+
async def mock_get_entities(entity_ids, principal_entity_id):
|
|
503
|
+
# invalid_child_id does not exist - get_entities simply omits it.
|
|
504
|
+
return {eid: valid_child for eid in entity_ids if eid == valid_child_id}
|
|
505
|
+
|
|
506
|
+
with patch.object(self.entity_service, 'get_entity', side_effect=mock_get_entity), \
|
|
507
|
+
patch.object(self.entity_service, 'get_entities', side_effect=mock_get_entities):
|
|
508
|
+
result = await self.entity_service.get_children_ids_by_type(
|
|
509
|
+
parent_id, 'graphcore.Note', _principal(self.TEST_USER_ENTITY_ID)
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
# Should only return the valid child
|
|
513
|
+
self.assertEqual(len(result), 1)
|
|
514
|
+
self.assertIn(valid_child_id, result)
|
|
515
|
+
|
|
516
|
+
async def test_recurse_children_requires_principal(self):
|
|
517
|
+
"""recurse_children has no unfiltered mode - a principal is mandatory."""
|
|
518
|
+
with self.assertRaises(TypeError):
|
|
519
|
+
await self.entity_service.recurse_children(fixture_id(self.entity_service.backend_id))
|
|
520
|
+
|
|
521
|
+
async def test_recurse_children(self):
|
|
522
|
+
"""Test recurse_children recursively gets all children"""
|
|
523
|
+
# Create a tree: parent -> child1 -> grandchild1
|
|
524
|
+
# -> child2
|
|
525
|
+
parent_id = fixture_id(self.entity_service.backend_id)
|
|
526
|
+
child1_id = fixture_id(self.entity_service.backend_id)
|
|
527
|
+
child2_id = fixture_id(self.entity_service.backend_id)
|
|
528
|
+
grandchild1_id = fixture_id(self.entity_service.backend_id)
|
|
529
|
+
|
|
530
|
+
parent = Note(parent_id)
|
|
531
|
+
parent._children_ids = tuple([child1_id, child2_id])
|
|
532
|
+
|
|
533
|
+
child1 = Note(child1_id)
|
|
534
|
+
child1._children_ids = tuple([grandchild1_id])
|
|
535
|
+
|
|
536
|
+
child2 = Note(child2_id)
|
|
537
|
+
child2._children_ids = tuple([])
|
|
538
|
+
|
|
539
|
+
grandchild1 = Note(grandchild1_id)
|
|
540
|
+
grandchild1._children_ids = tuple([])
|
|
541
|
+
|
|
542
|
+
entities = {
|
|
543
|
+
parent_id: parent,
|
|
544
|
+
child1_id: child1,
|
|
545
|
+
child2_id: child2,
|
|
546
|
+
grandchild1_id: grandchild1
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
async def mock_get_entities(entity_ids, principal_entity_id=None):
|
|
550
|
+
return {eid: entities[eid] for eid in entity_ids if eid in entities}
|
|
551
|
+
|
|
552
|
+
with patch.object(self.entity_service, 'get_entities', side_effect=mock_get_entities):
|
|
553
|
+
result = await self.entity_service.recurse_children(parent_id, principal=_principal(self.TEST_USER_ENTITY_ID))
|
|
554
|
+
|
|
555
|
+
# Should return all entities in the tree
|
|
556
|
+
self.assertEqual(len(result), 4)
|
|
557
|
+
result_ids = [e.entity_id for e in result]
|
|
558
|
+
self.assertIn(parent_id, result_ids)
|
|
559
|
+
self.assertIn(child1_id, result_ids)
|
|
560
|
+
self.assertIn(child2_id, result_ids)
|
|
561
|
+
self.assertIn(grandchild1_id, result_ids)
|
|
562
|
+
|
|
563
|
+
async def test_recurse_children_with_type_filter(self):
|
|
564
|
+
"""Test recurse_children filters by entity_type_id"""
|
|
565
|
+
parent_id = fixture_id(self.entity_service.backend_id)
|
|
566
|
+
child1_id = fixture_id(self.entity_service.backend_id)
|
|
567
|
+
child2_id = fixture_id(self.entity_service.backend_id)
|
|
568
|
+
|
|
569
|
+
parent = Note(parent_id)
|
|
570
|
+
parent._children_ids = tuple([child1_id, child2_id])
|
|
571
|
+
|
|
572
|
+
child1 = Note(child1_id)
|
|
573
|
+
child1._children_ids = tuple([])
|
|
574
|
+
|
|
575
|
+
child2 = Other(child2_id)
|
|
576
|
+
child2._children_ids = tuple([])
|
|
577
|
+
|
|
578
|
+
entities = {
|
|
579
|
+
parent_id: parent,
|
|
580
|
+
child1_id: child1,
|
|
581
|
+
child2_id: child2
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
async def mock_get_entities(entity_ids, principal_entity_id=None):
|
|
585
|
+
return {eid: entities[eid] for eid in entity_ids if eid in entities}
|
|
586
|
+
|
|
587
|
+
with patch.object(self.entity_service, 'get_entities', side_effect=mock_get_entities):
|
|
588
|
+
result = await self.entity_service.recurse_children(parent_id, entity_type_id='graphcore.Note', principal=_principal(self.TEST_USER_ENTITY_ID))
|
|
589
|
+
|
|
590
|
+
# Should only return Note instances
|
|
591
|
+
self.assertEqual(len(result), 2)
|
|
592
|
+
result_ids = [e.entity_id for e in result]
|
|
593
|
+
self.assertIn(parent_id, result_ids)
|
|
594
|
+
self.assertIn(child1_id, result_ids)
|
|
595
|
+
self.assertNotIn(child2_id, result_ids)
|
|
596
|
+
|
|
597
|
+
async def test_recurse_children_prevents_circular_references(self):
|
|
598
|
+
"""Test recurse_children detects and prevents circular references"""
|
|
599
|
+
parent_id = fixture_id(self.entity_service.backend_id)
|
|
600
|
+
child_id = fixture_id(self.entity_service.backend_id)
|
|
601
|
+
|
|
602
|
+
# Create circular reference: parent -> child -> parent
|
|
603
|
+
parent = Note(parent_id)
|
|
604
|
+
parent._children_ids = tuple([child_id])
|
|
605
|
+
|
|
606
|
+
child = Note(child_id)
|
|
607
|
+
child._children_ids = tuple([parent_id]) # Circular reference
|
|
608
|
+
|
|
609
|
+
entities = {
|
|
610
|
+
parent_id: parent,
|
|
611
|
+
child_id: child
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
async def mock_get_entities(entity_ids, principal_entity_id=None):
|
|
615
|
+
return {eid: entities[eid] for eid in entity_ids if eid in entities}
|
|
616
|
+
|
|
617
|
+
with patch.object(self.entity_service, 'get_entities', side_effect=mock_get_entities):
|
|
618
|
+
result = await self.entity_service.recurse_children(parent_id, principal=_principal(self.TEST_USER_ENTITY_ID))
|
|
619
|
+
|
|
620
|
+
# Should handle circular reference without infinite loop
|
|
621
|
+
# Should visit each entity only once
|
|
622
|
+
self.assertEqual(len(result), 2)
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
class TestEntityServicePermissions(TestCase):
|
|
626
|
+
"""Test permission checks in EntityService against the real TDD
|
|
627
|
+
authorization model (owner-implicit-admin + strategy context), not the
|
|
628
|
+
legacy ACL fields, which the adapter no longer consults for decisions -
|
|
629
|
+
see docs/AUTHORIZATION_TECHNICAL_TDD.md (Enforcement)."""
|
|
630
|
+
TEST_USER_ENTITY_ID = "test-user-entity-id"
|
|
631
|
+
|
|
632
|
+
def setUp(self):
|
|
633
|
+
self.entity_service = django_runtime(TEST_REGISTRY).entity_service
|
|
634
|
+
self.user1 = "user-uuid-1"
|
|
635
|
+
self.user2 = "user-uuid-2"
|
|
636
|
+
|
|
637
|
+
async def _create_owned_entity(self, owner):
|
|
638
|
+
entity = self.entity_service.create(Note)
|
|
639
|
+
with kernel_internal_context():
|
|
640
|
+
return await self.entity_service.save_entity(entity, _principal(owner))
|
|
641
|
+
|
|
642
|
+
async def test_get_entity_succeeds_with_read_permission(self):
|
|
643
|
+
"""The owner has implicit Admin (which includes Read) on their entity."""
|
|
644
|
+
entity = await self._create_owned_entity(self.user1)
|
|
645
|
+
result = await self.entity_service.get_entity(entity.entity_id, _principal(self.user1))
|
|
646
|
+
self.assertEqual(result.entity_id, entity.entity_id)
|
|
647
|
+
|
|
648
|
+
async def test_get_entity_raises_permission_error_without_read(self):
|
|
649
|
+
"""A user with no grant and no ownership cannot read the entity."""
|
|
650
|
+
entity = await self._create_owned_entity(self.user1)
|
|
651
|
+
with self.assertRaises(Forbidden):
|
|
652
|
+
await self.entity_service.get_entity(entity.entity_id, _principal(self.user2))
|
|
653
|
+
|
|
654
|
+
async def test_save_new_entity_automatically_grants_write_to_creator(self):
|
|
655
|
+
"""A new root entity is stamped with the creator as owner, who then
|
|
656
|
+
has implicit Admin (superset of Write) on it."""
|
|
657
|
+
entity = self.entity_service.create(Note)
|
|
658
|
+
with kernel_internal_context():
|
|
659
|
+
result = await self.entity_service.save_entity(entity, _principal(self.user1))
|
|
660
|
+
|
|
661
|
+
self.assertEqual(result.entity_id, entity.entity_id)
|
|
662
|
+
self.assertEqual(result.owner, self.user1)
|
|
663
|
+
|
|
664
|
+
async def test_save_new_entity_ignores_caller_supplied_owner(self):
|
|
665
|
+
entity = self.entity_service.create(Note)
|
|
666
|
+
entity.owner = self.user2
|
|
667
|
+
|
|
668
|
+
with kernel_internal_context():
|
|
669
|
+
result = await self.entity_service.save_entity(entity, _principal(self.user1))
|
|
670
|
+
|
|
671
|
+
self.assertEqual(result.owner, self.user1)
|
|
672
|
+
|
|
673
|
+
async def test_save_new_entity_succeeds_for_any_user(self):
|
|
674
|
+
"""Any user can create a new root entity via a kernel-internal context
|
|
675
|
+
and becomes its owner."""
|
|
676
|
+
entity = self.entity_service.create(Note)
|
|
677
|
+
with kernel_internal_context():
|
|
678
|
+
result = await self.entity_service.save_entity(entity, _principal(self.user2))
|
|
679
|
+
|
|
680
|
+
self.assertEqual(result.entity_id, entity.entity_id)
|
|
681
|
+
self.assertEqual(result.owner, self.user2)
|
|
682
|
+
|
|
683
|
+
async def test_save_existing_entity_fails_when_user_not_in_write_list(self):
|
|
684
|
+
"""A user who is neither owner nor grantee cannot update the entity
|
|
685
|
+
through a real strategy invocation (kernel_internal_context is for
|
|
686
|
+
the executor's own bookkeeping, and bypasses this check entirely)."""
|
|
687
|
+
from graphcore.domain.strategy.BaseStrategy import SetAttributes
|
|
688
|
+
|
|
689
|
+
entity = await self._create_owned_entity(self.user1)
|
|
690
|
+
entity.set_attribute('name', 'Hijacked')
|
|
691
|
+
|
|
692
|
+
with self.assertRaises(Forbidden) as context:
|
|
693
|
+
with strategy_context(SetAttributes):
|
|
694
|
+
await self.entity_service.save_entity(entity, _principal(self.user2))
|
|
695
|
+
|
|
696
|
+
self.assertIn(self.user2, str(context.exception))
|
|
697
|
+
|
|
698
|
+
async def test_save_existing_entity_succeeds_when_user_in_write_list(self):
|
|
699
|
+
"""The owner can update their own entity."""
|
|
700
|
+
from graphcore.domain.strategy.BaseStrategy import SetAttributes
|
|
701
|
+
|
|
702
|
+
entity = await self._create_owned_entity(self.user1)
|
|
703
|
+
entity.set_attribute('name', 'Updated Name')
|
|
704
|
+
|
|
705
|
+
with strategy_context(SetAttributes):
|
|
706
|
+
result = await self.entity_service.save_entity(entity, _principal(self.user1))
|
|
707
|
+
|
|
708
|
+
self.assertEqual(result.entity_id, entity.entity_id)
|
|
709
|
+
self.assertEqual(result.get_attribute('name'), 'Updated Name')
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
class TestA7CacheCoherence(TestCase):
|
|
713
|
+
"""Integration tests for A7: batched traversal, DB-sourced ACL reads,
|
|
714
|
+
write-path cache invalidation, and group cache busting - exercised
|
|
715
|
+
against the real (LocMemCache) cache and the real test database rather
|
|
716
|
+
than mocks, since these are exactly the layers being coordinated."""
|
|
717
|
+
|
|
718
|
+
TEST_USER_ENTITY_ID = "a7-owner"
|
|
719
|
+
OTHER_USER_ENTITY_ID = "a7-other"
|
|
720
|
+
|
|
721
|
+
def setUp(self):
|
|
722
|
+
self.entity_service = django_runtime(TEST_REGISTRY).entity_service
|
|
723
|
+
self.entity_service.cache.cache.clear()
|
|
724
|
+
self.entity_service.channel_layer = None
|
|
725
|
+
|
|
726
|
+
async def test_revoking_read_access_takes_effect_despite_stale_cache(self):
|
|
727
|
+
"""A pre-revocation blob sitting in cache must not be served once the
|
|
728
|
+
grant that gave the requester read access has been revoked."""
|
|
729
|
+
from graphcore.persistence.django.models import GrantModel
|
|
730
|
+
|
|
731
|
+
entity_id = fixture_id(self.entity_service.backend_id)
|
|
732
|
+
await EntityModel.objects.acreate(
|
|
733
|
+
entity_id=entity_id,
|
|
734
|
+
|
|
735
|
+
entity_type_id='graphcore.Note',
|
|
736
|
+
owner=self.TEST_USER_ENTITY_ID,
|
|
737
|
+
version=1,
|
|
738
|
+
)
|
|
739
|
+
grant = await GrantModel.objects.acreate(
|
|
740
|
+
entity_type_id='graphcore.Grant',
|
|
741
|
+
|
|
742
|
+
subject_id=self.OTHER_USER_ENTITY_ID,
|
|
743
|
+
role_name='viewer',
|
|
744
|
+
parent_id=entity_id,
|
|
745
|
+
)
|
|
746
|
+
|
|
747
|
+
stale_entity = Note(entity_id)
|
|
748
|
+
stale_entity.version = 1
|
|
749
|
+
self.entity_service.cache.set_entity(entity_id, stale_entity)
|
|
750
|
+
|
|
751
|
+
# Revoke read access directly at the DB layer, simulating a write
|
|
752
|
+
# made through a different process the cache never saw.
|
|
753
|
+
await GrantModel.objects.filter(entity_id=grant.entity_id).adelete()
|
|
754
|
+
|
|
755
|
+
with self.assertRaises(Forbidden):
|
|
756
|
+
await self.entity_service.get_entity(entity_id, _principal(self.OTHER_USER_ENTITY_ID))
|
|
757
|
+
|
|
758
|
+
async def test_get_entities_batch_issues_one_acl_query_not_n(self):
|
|
759
|
+
"""A batch of N ids costs one readability decision regardless of N."""
|
|
760
|
+
from graphcore.auth.authz.adapter.default_access_adapter import DefaultAccessAdapter
|
|
761
|
+
|
|
762
|
+
entity_ids = [fixture_id(self.entity_service.backend_id) for _ in range(5)]
|
|
763
|
+
for entity_id in entity_ids:
|
|
764
|
+
await EntityModel.objects.acreate(
|
|
765
|
+
entity_id=entity_id,
|
|
766
|
+
|
|
767
|
+
entity_type_id='graphcore.Note',
|
|
768
|
+
owner=self.TEST_USER_ENTITY_ID,
|
|
769
|
+
version=1,
|
|
770
|
+
)
|
|
771
|
+
entity = Note(entity_id)
|
|
772
|
+
entity.version = 1
|
|
773
|
+
entity.owner = self.TEST_USER_ENTITY_ID
|
|
774
|
+
self.entity_service.cache.set_entity(entity_id, entity)
|
|
775
|
+
|
|
776
|
+
calls = []
|
|
777
|
+
original = DefaultAccessAdapter.read_filters
|
|
778
|
+
|
|
779
|
+
async def spy(self_adapter, principal, perms):
|
|
780
|
+
calls.append(list(perms))
|
|
781
|
+
return await original(self_adapter, principal, perms)
|
|
782
|
+
|
|
783
|
+
with patch.object(DefaultAccessAdapter, 'read_filters', spy):
|
|
784
|
+
result = await self.entity_service.get_entities(entity_ids, _principal(self.TEST_USER_ENTITY_ID))
|
|
785
|
+
|
|
786
|
+
self.assertEqual(len(result), 5)
|
|
787
|
+
self.assertEqual(len(calls), 1)
|
|
788
|
+
|
|
789
|
+
async def test_get_entities_nonexistent_id_returns_missing_tombstone(self):
|
|
790
|
+
"""A batch containing an ID with no DB row gets a `missing` tombstone
|
|
791
|
+
in the result dict, not an omitted key."""
|
|
792
|
+
entity_id = fixture_id(self.entity_service.backend_id)
|
|
793
|
+
missing_id = fixture_id(self.entity_service.backend_id)
|
|
794
|
+
await EntityModel.objects.acreate(
|
|
795
|
+
entity_id=entity_id,
|
|
796
|
+
|
|
797
|
+
entity_type_id='graphcore.Note',
|
|
798
|
+
owner=self.TEST_USER_ENTITY_ID,
|
|
799
|
+
version=1,
|
|
800
|
+
)
|
|
801
|
+
|
|
802
|
+
result = await self.entity_service.get_entities([entity_id, missing_id], _principal(self.TEST_USER_ENTITY_ID))
|
|
803
|
+
|
|
804
|
+
self.assertEqual(result[missing_id], Tombstone(entity_id=missing_id, cause='missing'))
|
|
805
|
+
|
|
806
|
+
async def test_recurse_children_batches_get_entities_per_level(self):
|
|
807
|
+
"""recurse_children issues one get_entities call per tree level, not
|
|
808
|
+
one lookup per node - the call count must not grow with breadth."""
|
|
809
|
+
parent_id = fixture_id(self.entity_service.backend_id)
|
|
810
|
+
child_ids = [fixture_id(self.entity_service.backend_id) for _ in range(8)]
|
|
811
|
+
|
|
812
|
+
entities = {parent_id: Note(parent_id)}
|
|
813
|
+
entities[parent_id]._children_ids = tuple(child_ids)
|
|
814
|
+
for cid in child_ids:
|
|
815
|
+
entities[cid] = Note(cid)
|
|
816
|
+
entities[cid]._children_ids = tuple([])
|
|
817
|
+
|
|
818
|
+
calls = []
|
|
819
|
+
|
|
820
|
+
async def fake_get_entities(entity_ids, principal_entity_id=None):
|
|
821
|
+
calls.append(list(entity_ids))
|
|
822
|
+
return {eid: entities[eid] for eid in entity_ids if eid in entities}
|
|
823
|
+
|
|
824
|
+
with patch.object(self.entity_service, 'get_entities', side_effect=fake_get_entities):
|
|
825
|
+
result = await self.entity_service.recurse_children(parent_id, principal=_principal(self.TEST_USER_ENTITY_ID))
|
|
826
|
+
|
|
827
|
+
# One call for the root, one batched call for all 8 children - two
|
|
828
|
+
# total regardless of how wide the tree is, not 1 + 8.
|
|
829
|
+
self.assertEqual(len(calls), 2)
|
|
830
|
+
self.assertEqual(len(result), 9)
|
|
831
|
+
|
|
832
|
+
async def _save(self, entity):
|
|
833
|
+
with kernel_internal_context():
|
|
834
|
+
return await self.entity_service.save_entity(entity, _principal(self.TEST_USER_ENTITY_ID))
|
|
835
|
+
|
|
836
|
+
async def test_a_save_is_read_over_the_payload_cached_before_it(self):
|
|
837
|
+
note = await self._save(self.entity_service.create(Note))
|
|
838
|
+
principal = _principal(self.TEST_USER_ENTITY_ID)
|
|
839
|
+
await self.entity_service.get_entities([note.entity_id], principal)
|
|
840
|
+
self.assertEqual(self.entity_service.cache.get_entity(note.entity_id).version, 1)
|
|
841
|
+
|
|
842
|
+
note.set_attribute('name', 'renamed')
|
|
843
|
+
await self._save(note)
|
|
844
|
+
|
|
845
|
+
read = (await self.entity_service.get_entities([note.entity_id], principal))[note.entity_id]
|
|
846
|
+
self.assertEqual((read.version, read.get_attribute('name')), (2, 'renamed'))
|
|
847
|
+
|
|
848
|
+
async def test_a_create_drops_a_payload_cached_under_its_id(self):
|
|
849
|
+
entity_id = fixture_id(self.entity_service.backend_id)
|
|
850
|
+
gone = Note(entity_id)
|
|
851
|
+
gone.version = 1
|
|
852
|
+
gone.set_attribute('marker', 'deleted')
|
|
853
|
+
self.entity_service.cache.set_entity(entity_id, gone)
|
|
854
|
+
|
|
855
|
+
await self._save(Note(entity_id))
|
|
856
|
+
|
|
857
|
+
read = (await self.entity_service.get_entities([entity_id], _principal(self.TEST_USER_ENTITY_ID)))[entity_id]
|
|
858
|
+
self.assertFalse(read.has_attribute('marker'))
|
|
859
|
+
|
|
860
|
+
def test_no_system_or_sudo_escape_hatch(self):
|
|
861
|
+
"""No read method on EntityService accepts a system/sudo bypass argument."""
|
|
862
|
+
import inspect
|
|
863
|
+
for name in ('recurse_children', 'get_entity', 'get_entities'):
|
|
864
|
+
sig = inspect.signature(getattr(self.entity_service, name))
|
|
865
|
+
for param_name in sig.parameters:
|
|
866
|
+
self.assertNotIn('system', param_name.lower())
|
|
867
|
+
self.assertNotIn('sudo', param_name.lower())
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
class ContainmentLocalityTests(TestCase):
|
|
872
|
+
"""Containment never crosses a deployment boundary: access is derived
|
|
873
|
+
from the containment chain, which a deployment computes only over the
|
|
874
|
+
entities it owns."""
|
|
875
|
+
|
|
876
|
+
OWNER = 'locality-owner'
|
|
877
|
+
FOREIGN_BACKEND = '00000000zz'
|
|
878
|
+
|
|
879
|
+
def setUp(self):
|
|
880
|
+
self.entity_service = in_memory_service()
|
|
881
|
+
|
|
882
|
+
async def _root(self):
|
|
883
|
+
root = self.entity_service.create(Note)
|
|
884
|
+
with kernel_internal_context():
|
|
885
|
+
return await self.entity_service.save_entity(root, _principal(self.OWNER))
|
|
886
|
+
|
|
887
|
+
async def test_a_foreign_parent_is_refused_and_writes_nothing(self):
|
|
888
|
+
child = self.entity_service.create(Note)
|
|
889
|
+
child.parent_id = new_entity_id(self.FOREIGN_BACKEND)
|
|
890
|
+
|
|
891
|
+
with self.assertRaises(InvalidRequest) as ctx:
|
|
892
|
+
with kernel_internal_context():
|
|
893
|
+
await self.entity_service.save_entity(child, _principal(self.OWNER))
|
|
894
|
+
|
|
895
|
+
self.assertIn('does not cross deployments', str(ctx.exception))
|
|
896
|
+
self.assertIsNone(await self.entity_service.load_from_db(child.entity_id))
|
|
897
|
+
|
|
898
|
+
async def test_a_local_parent_saves_normally(self):
|
|
899
|
+
root = await self._root()
|
|
900
|
+
child = self.entity_service.create(Note)
|
|
901
|
+
child.set_parent(root.entity_id)
|
|
902
|
+
|
|
903
|
+
with kernel_internal_context():
|
|
904
|
+
await self.entity_service.save_entity(child, _principal(self.OWNER))
|
|
905
|
+
|
|
906
|
+
self.assertEqual(
|
|
907
|
+
(await self.entity_service.load_from_db(child.entity_id)).parent_id, root.entity_id,
|
|
908
|
+
)
|