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,236 @@
|
|
|
1
|
+
"""Reverse lookup and reference-change broadcast.
|
|
2
|
+
|
|
3
|
+
`referrers` hands the target and the caller's access predicate to the
|
|
4
|
+
repository's reverse index, and a save announces only the targets it added
|
|
5
|
+
or removed - routed on the source, never on the target.
|
|
6
|
+
"""
|
|
7
|
+
from unittest.mock import patch
|
|
8
|
+
|
|
9
|
+
from asgiref.sync import async_to_sync
|
|
10
|
+
from django.test import TransactionTestCase
|
|
11
|
+
|
|
12
|
+
from graphcore.domain.entity.Entity import Entity
|
|
13
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
14
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
15
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
16
|
+
from graphcore.runtime import django_runtime
|
|
17
|
+
from graphcore.types.identity import Principal
|
|
18
|
+
from graphcore.types.query import Equal
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _RefScope(Entity):
|
|
22
|
+
@classmethod
|
|
23
|
+
def get_entity_type_id(cls) -> str:
|
|
24
|
+
return "graphcore._RefScope"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
28
|
+
TEST_REGISTRY.entities.register_entity(_RefScope, app_label='graphcore')
|
|
29
|
+
TEST_REGISTRY.policy.define(_RefScope, shareable=True)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class RecordingBroadcaster:
|
|
33
|
+
def __init__(self, inner):
|
|
34
|
+
self.inner = inner
|
|
35
|
+
self.calls = 0
|
|
36
|
+
self.reference_changes = []
|
|
37
|
+
|
|
38
|
+
def __getattr__(self, name):
|
|
39
|
+
return getattr(self.inner, name)
|
|
40
|
+
|
|
41
|
+
async def publish_reference_changes(self, source_entity, added, removed):
|
|
42
|
+
if added or removed:
|
|
43
|
+
self.calls += 1
|
|
44
|
+
self.reference_changes += [(source_entity.entity_id, t, 'added') for t in added]
|
|
45
|
+
self.reference_changes += [(source_entity.entity_id, t, 'removed') for t in removed]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ReferrersTestCase(TransactionTestCase):
|
|
49
|
+
OWNER = 'referrers-owner'
|
|
50
|
+
VIEWER = 'referrers-viewer'
|
|
51
|
+
|
|
52
|
+
def setUp(self):
|
|
53
|
+
self.service = django_runtime(TEST_REGISTRY).entity_service
|
|
54
|
+
self.broadcaster = RecordingBroadcaster(self.service.broadcaster)
|
|
55
|
+
self.service.broadcaster = self.broadcaster
|
|
56
|
+
|
|
57
|
+
async def _save(self, entity, owner=None):
|
|
58
|
+
with kernel_internal_context():
|
|
59
|
+
return await self.service.save_entity(
|
|
60
|
+
entity, Principal(principal_entity_id=owner or self.OWNER),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
async def _source_referencing(self, target_id, owner=None):
|
|
64
|
+
source = self.service.create(_RefScope)
|
|
65
|
+
source.set_attribute('spec', target_id)
|
|
66
|
+
return await self._save(source, owner=owner)
|
|
67
|
+
|
|
68
|
+
async def _grant_viewer(self, resource_id):
|
|
69
|
+
grant = self.service.create(Grant)
|
|
70
|
+
grant.subject_id = self.VIEWER
|
|
71
|
+
grant.role_name = 'viewer'
|
|
72
|
+
grant.parent_id = resource_id
|
|
73
|
+
await self._save(grant)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class ReverseLookupTests(ReferrersTestCase):
|
|
77
|
+
def test_referrers_returns_only_sources_the_principal_can_read(self):
|
|
78
|
+
async def _run():
|
|
79
|
+
target = await self._save(self.service.create(_RefScope))
|
|
80
|
+
readable = await self._source_referencing(target.entity_id)
|
|
81
|
+
await self._source_referencing(target.entity_id)
|
|
82
|
+
await self._grant_viewer(readable.entity_id)
|
|
83
|
+
|
|
84
|
+
page = await self.service.referrers(
|
|
85
|
+
target.entity_id, Principal(principal_entity_id=self.VIEWER),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
self.assertEqual([e.entity_id for e in page.entities], [readable.entity_id])
|
|
89
|
+
|
|
90
|
+
async_to_sync(_run)()
|
|
91
|
+
|
|
92
|
+
def test_referrers_finds_every_source_for_the_owner(self):
|
|
93
|
+
async def _run():
|
|
94
|
+
target = await self._save(self.service.create(_RefScope))
|
|
95
|
+
sources = {
|
|
96
|
+
(await self._source_referencing(target.entity_id)).entity_id for _ in range(2)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
page = await self.service.referrers(
|
|
100
|
+
target.entity_id, Principal(principal_entity_id=self.OWNER),
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
self.assertEqual({e.entity_id for e in page.entities}, sources)
|
|
104
|
+
|
|
105
|
+
async_to_sync(_run)()
|
|
106
|
+
|
|
107
|
+
def test_where_narrows_referrers_and_access_still_applies(self):
|
|
108
|
+
async def _run():
|
|
109
|
+
target = await self._save(self.service.create(_RefScope))
|
|
110
|
+
readable = await self._source_referencing(target.entity_id)
|
|
111
|
+
await self._source_referencing(target.entity_id)
|
|
112
|
+
await self._grant_viewer(readable.entity_id)
|
|
113
|
+
plain = self.service.create(Entity)
|
|
114
|
+
plain.set_attribute('spec', target.entity_id)
|
|
115
|
+
await self._save(plain)
|
|
116
|
+
only_scopes = Equal('entity_type_id', TEST_REGISTRY.entities.get_entity_type_id(_RefScope))
|
|
117
|
+
|
|
118
|
+
owner_page = await self.service.referrers(
|
|
119
|
+
target.entity_id, Principal(principal_entity_id=self.OWNER), where=only_scopes,
|
|
120
|
+
)
|
|
121
|
+
viewer_page = await self.service.referrers(
|
|
122
|
+
target.entity_id, Principal(principal_entity_id=self.VIEWER), where=only_scopes,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
self.assertEqual(len(owner_page.entities), 2)
|
|
126
|
+
self.assertNotIn(plain.entity_id, [e.entity_id for e in owner_page.entities])
|
|
127
|
+
self.assertEqual([e.entity_id for e in viewer_page.entities], [readable.entity_id])
|
|
128
|
+
|
|
129
|
+
async_to_sync(_run)()
|
|
130
|
+
|
|
131
|
+
def test_a_child_is_a_referrer_of_its_parent(self):
|
|
132
|
+
async def _run():
|
|
133
|
+
parent = await self._save(self.service.create(_RefScope))
|
|
134
|
+
child = self.service.create(_RefScope)
|
|
135
|
+
child.set_parent(parent.entity_id)
|
|
136
|
+
await self._save(child)
|
|
137
|
+
|
|
138
|
+
page = await self.service.referrers(
|
|
139
|
+
parent.entity_id, Principal(principal_entity_id=self.OWNER),
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
self.assertEqual([e.entity_id for e in page.entities], [child.entity_id])
|
|
143
|
+
|
|
144
|
+
async_to_sync(_run)()
|
|
145
|
+
|
|
146
|
+
def test_referrers_goes_to_the_reverse_index_not_a_listing(self):
|
|
147
|
+
async def _run():
|
|
148
|
+
target = await self._save(self.service.create(_RefScope))
|
|
149
|
+
repository = self.service.repository
|
|
150
|
+
|
|
151
|
+
with patch.object(repository, 'referrers', wraps=repository.referrers) as referrers, \
|
|
152
|
+
patch.object(repository, 'list_entities', side_effect=AssertionError):
|
|
153
|
+
await self.service.referrers(
|
|
154
|
+
target.entity_id, Principal(principal_entity_id=self.OWNER), limit=5,
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
(target_id, predicate, page), _ = referrers.call_args
|
|
158
|
+
self.assertEqual((target_id, page.limit, page.cursor), (target.entity_id, 5, None))
|
|
159
|
+
self.assertIsNotNone(predicate)
|
|
160
|
+
|
|
161
|
+
async_to_sync(_run)()
|
|
162
|
+
|
|
163
|
+
def test_an_unreferenced_target_has_no_referrers(self):
|
|
164
|
+
async def _run():
|
|
165
|
+
target = await self._save(self.service.create(_RefScope))
|
|
166
|
+
|
|
167
|
+
page = await self.service.referrers(
|
|
168
|
+
target.entity_id, Principal(principal_entity_id=self.OWNER),
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
self.assertEqual(page.entities, [])
|
|
172
|
+
|
|
173
|
+
async_to_sync(_run)()
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class ReferenceBroadcastTests(ReferrersTestCase):
|
|
177
|
+
def test_a_new_reference_is_announced_once(self):
|
|
178
|
+
async def _run():
|
|
179
|
+
target = await self._save(self.service.create(_RefScope))
|
|
180
|
+
self.broadcaster.reference_changes.clear()
|
|
181
|
+
|
|
182
|
+
source = await self._source_referencing(target.entity_id)
|
|
183
|
+
|
|
184
|
+
self.assertEqual(
|
|
185
|
+
self.broadcaster.reference_changes,
|
|
186
|
+
[(source.entity_id, target.entity_id, 'added')],
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
async_to_sync(_run)()
|
|
190
|
+
|
|
191
|
+
def test_an_unchanged_reference_announces_nothing(self):
|
|
192
|
+
async def _run():
|
|
193
|
+
target = await self._save(self.service.create(_RefScope))
|
|
194
|
+
source = await self._source_referencing(target.entity_id)
|
|
195
|
+
self.broadcaster.reference_changes.clear()
|
|
196
|
+
|
|
197
|
+
source.set_attribute('name', 'edited')
|
|
198
|
+
await self._save(source)
|
|
199
|
+
|
|
200
|
+
self.assertEqual(self.broadcaster.reference_changes, [])
|
|
201
|
+
|
|
202
|
+
async_to_sync(_run)()
|
|
203
|
+
|
|
204
|
+
def test_a_save_changing_several_references_broadcasts_once(self):
|
|
205
|
+
async def _run():
|
|
206
|
+
targets = [await self._save(self.service.create(_RefScope)) for _ in range(3)]
|
|
207
|
+
self.broadcaster.calls = 0
|
|
208
|
+
self.broadcaster.reference_changes.clear()
|
|
209
|
+
|
|
210
|
+
source = self.service.create(_RefScope)
|
|
211
|
+
source.set_attribute('specs', [t.entity_id for t in targets])
|
|
212
|
+
await self._save(source)
|
|
213
|
+
|
|
214
|
+
self.assertEqual(self.broadcaster.calls, 1)
|
|
215
|
+
self.assertEqual(self.broadcaster.reference_changes, [
|
|
216
|
+
(source.entity_id, target_id, 'added')
|
|
217
|
+
for target_id in sorted(t.entity_id for t in targets)
|
|
218
|
+
])
|
|
219
|
+
|
|
220
|
+
async_to_sync(_run)()
|
|
221
|
+
|
|
222
|
+
def test_dropping_a_reference_announces_its_removal(self):
|
|
223
|
+
async def _run():
|
|
224
|
+
target = await self._save(self.service.create(_RefScope))
|
|
225
|
+
source = await self._source_referencing(target.entity_id)
|
|
226
|
+
self.broadcaster.reference_changes.clear()
|
|
227
|
+
|
|
228
|
+
source.remove_attribute('spec')
|
|
229
|
+
await self._save(source)
|
|
230
|
+
|
|
231
|
+
self.assertEqual(
|
|
232
|
+
self.broadcaster.reference_changes,
|
|
233
|
+
[(source.entity_id, target.entity_id, 'removed')],
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
async_to_sync(_run)()
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""`EntityService` is the only component that reads an identifier's backend
|
|
2
|
+
segment, and the only one that knows a gateway exists. The repository is
|
|
3
|
+
never asked for a remote identifier, and the local AccessPort never decides
|
|
4
|
+
about one."""
|
|
5
|
+
from asgiref.sync import async_to_sync
|
|
6
|
+
from django.test import SimpleTestCase
|
|
7
|
+
|
|
8
|
+
from graphcore.auth.authz.adapter.default_access_adapter import DefaultAccessAdapter
|
|
9
|
+
from graphcore.broadcast.channels import ChannelsBroadcaster
|
|
10
|
+
from graphcore.domain.entity.Entity import Entity
|
|
11
|
+
from graphcore.errors import RemoteUnavailable
|
|
12
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
13
|
+
from graphcore.gateway.remote import NoRemoteGateway
|
|
14
|
+
from graphcore.persistence.django.cache import DjangoCache
|
|
15
|
+
from graphcore.persistence.tests.memory import MemoryEntityRepository
|
|
16
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
17
|
+
from graphcore.scheduling.tests.memory import InMemoryScheduler
|
|
18
|
+
from graphcore.runtime import build_runtime
|
|
19
|
+
from graphcore.tests.ids import fixture_id
|
|
20
|
+
from graphcore.types.entities import Tombstone
|
|
21
|
+
from graphcore.types.entity_id import new_entity_id
|
|
22
|
+
from graphcore.types.identity import Principal
|
|
23
|
+
|
|
24
|
+
FOREIGN_BACKEND = '00000000zz'
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class FakeGateway(NoRemoteGateway):
|
|
28
|
+
def __init__(self):
|
|
29
|
+
self.calls = []
|
|
30
|
+
|
|
31
|
+
async def get_entities(self, entity_ids, principal):
|
|
32
|
+
self.calls.append(list(entity_ids))
|
|
33
|
+
return await super().get_entities(entity_ids, principal)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class WatchfulRepository(MemoryEntityRepository):
|
|
37
|
+
"""Fails loudly if anything asks it about an identifier it cannot own."""
|
|
38
|
+
|
|
39
|
+
def _assert_local(self, entity_ids):
|
|
40
|
+
foreign = [eid for eid in entity_ids if FOREIGN_BACKEND in str(eid)]
|
|
41
|
+
assert not foreign, f"repository was asked for remote ids: {foreign}"
|
|
42
|
+
|
|
43
|
+
async def load_many(self, entity_ids):
|
|
44
|
+
self._assert_local(entity_ids)
|
|
45
|
+
return await super().load_many(entity_ids)
|
|
46
|
+
|
|
47
|
+
async def versions(self, entity_ids):
|
|
48
|
+
self._assert_local(entity_ids)
|
|
49
|
+
return await super().versions(entity_ids)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class RoutingTestCase(SimpleTestCase):
|
|
53
|
+
OWNER = 'routing-owner'
|
|
54
|
+
|
|
55
|
+
def setUp(self):
|
|
56
|
+
repository = WatchfulRepository(DEFAULT_REGISTRY.entities)
|
|
57
|
+
cache = DjangoCache(entity_ttl=900)
|
|
58
|
+
access = DefaultAccessAdapter(DEFAULT_REGISTRY, repository, cache)
|
|
59
|
+
self.gateway = FakeGateway()
|
|
60
|
+
self.service = build_runtime(
|
|
61
|
+
DEFAULT_REGISTRY, repository, cache, access,
|
|
62
|
+
ChannelsBroadcaster(access, DEFAULT_REGISTRY, None), InMemoryScheduler(),
|
|
63
|
+
self.gateway, 100,
|
|
64
|
+
).entity_service
|
|
65
|
+
self.service.clear_all_entities()
|
|
66
|
+
self.principal = Principal(principal_entity_id=self.OWNER, token='a-subject-token')
|
|
67
|
+
self.remote_id = new_entity_id(FOREIGN_BACKEND)
|
|
68
|
+
|
|
69
|
+
def local_entity(self):
|
|
70
|
+
async def _save():
|
|
71
|
+
entity = self.service.create(Entity)
|
|
72
|
+
entity.owner = self.OWNER
|
|
73
|
+
with kernel_internal_context():
|
|
74
|
+
return await self.service.save_entity(entity, self.principal)
|
|
75
|
+
|
|
76
|
+
return async_to_sync(_save)()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class GetEntitiesRoutingTests(RoutingTestCase):
|
|
80
|
+
def test_a_mixed_batch_splits_by_minting_deployment(self):
|
|
81
|
+
local = self.local_entity()
|
|
82
|
+
|
|
83
|
+
results = async_to_sync(self.service.get_entities)(
|
|
84
|
+
[local.entity_id, self.remote_id], self.principal,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
self.assertEqual(self.gateway.calls, [[self.remote_id]])
|
|
88
|
+
self.assertEqual(results[local.entity_id].entity_id, local.entity_id)
|
|
89
|
+
self.assertEqual(results[self.remote_id], Tombstone(self.remote_id, 'unreachable'))
|
|
90
|
+
|
|
91
|
+
def test_an_all_local_batch_never_reaches_the_gateway(self):
|
|
92
|
+
local = self.local_entity()
|
|
93
|
+
|
|
94
|
+
async_to_sync(self.service.get_entities)([local.entity_id], self.principal)
|
|
95
|
+
|
|
96
|
+
self.assertEqual(self.gateway.calls, [])
|
|
97
|
+
|
|
98
|
+
def test_a_non_identifier_stays_on_the_local_path(self):
|
|
99
|
+
results = async_to_sync(self.service.get_entities)(['*'], self.principal)
|
|
100
|
+
|
|
101
|
+
self.assertEqual(self.gateway.calls, [])
|
|
102
|
+
self.assertEqual(results['*'], Tombstone('*', 'missing'))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class GetEntityRoutingTests(RoutingTestCase):
|
|
106
|
+
def test_a_remote_identifier_raises_remote_unavailable(self):
|
|
107
|
+
with self.assertRaises(RemoteUnavailable) as ctx:
|
|
108
|
+
async_to_sync(self.service.get_entity)(self.remote_id, self.principal)
|
|
109
|
+
|
|
110
|
+
self.assertEqual(ctx.exception.cause, 'unreachable')
|
|
111
|
+
self.assertEqual(self.gateway.calls, [[self.remote_id]])
|
|
112
|
+
|
|
113
|
+
def test_a_principal_without_a_subject_token_cannot_delegate(self):
|
|
114
|
+
identity_only = Principal(principal_entity_id=self.OWNER)
|
|
115
|
+
|
|
116
|
+
with self.assertRaises(RemoteUnavailable) as ctx:
|
|
117
|
+
async_to_sync(self.service.get_entity)(self.remote_id, identity_only)
|
|
118
|
+
|
|
119
|
+
self.assertEqual(ctx.exception.cause, 'undelegated')
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""`dependent_on`: deleting a local entity deletes the entities whose
|
|
2
|
+
dependent field names it (docs/ENTITY_REFERENCE_TDD.md, Dependent references)."""
|
|
3
|
+
from typing import ClassVar, Optional
|
|
4
|
+
from unittest.mock import AsyncMock
|
|
5
|
+
|
|
6
|
+
from django.test import SimpleTestCase
|
|
7
|
+
|
|
8
|
+
from graphcore.auth.authz.adapter.default_access_adapter import DefaultAccessAdapter
|
|
9
|
+
from graphcore.broadcast.channels import ChannelsBroadcaster
|
|
10
|
+
from graphcore.domain.entity.Entity import Entity
|
|
11
|
+
from graphcore.domain.strategy.BaseStrategy import DeleteEntity
|
|
12
|
+
from graphcore.execution.strategy_context import kernel_internal_context, strategy_context
|
|
13
|
+
from graphcore.gateway.remote import NoRemoteGateway
|
|
14
|
+
from graphcore.graph.entity_cache import NullEntityCache
|
|
15
|
+
from graphcore.persistence.tests.memory import MemoryEntityRepository
|
|
16
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
17
|
+
from graphcore.runtime import build_runtime
|
|
18
|
+
from graphcore.scheduling.tests.memory import InMemoryScheduler
|
|
19
|
+
from graphcore.types.identity import Principal
|
|
20
|
+
|
|
21
|
+
OWNER = Principal(principal_entity_id='dependent-owner')
|
|
22
|
+
STRANGER = Principal(principal_entity_id='dependent-stranger')
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _Target(Entity):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _Dependent(Entity):
|
|
30
|
+
dependent_on: ClassVar[tuple[str, ...]] = ('target',)
|
|
31
|
+
|
|
32
|
+
def __init__(self, entity_id: str):
|
|
33
|
+
super().__init__(entity_id)
|
|
34
|
+
self.target: Optional[str] = None
|
|
35
|
+
|
|
36
|
+
def to_dict(self) -> dict:
|
|
37
|
+
return {**super().to_dict(), 'target': self.target}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class _SubDependent(_Dependent):
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
45
|
+
for _cls in (_Target, _Dependent, _SubDependent):
|
|
46
|
+
TEST_REGISTRY.entities.register_entity(_cls, app_label='graphcore')
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _service():
|
|
50
|
+
repository = MemoryEntityRepository(TEST_REGISTRY.entities)
|
|
51
|
+
cache = NullEntityCache()
|
|
52
|
+
access = DefaultAccessAdapter(TEST_REGISTRY, repository, cache)
|
|
53
|
+
service = build_runtime(
|
|
54
|
+
TEST_REGISTRY, repository, cache, access, ChannelsBroadcaster(access, TEST_REGISTRY, None),
|
|
55
|
+
InMemoryScheduler(), NoRemoteGateway(), 100,
|
|
56
|
+
).entity_service
|
|
57
|
+
service.broadcaster = AsyncMock()
|
|
58
|
+
return service
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class DependentOnTests(SimpleTestCase):
|
|
62
|
+
def setUp(self):
|
|
63
|
+
self.service = _service()
|
|
64
|
+
|
|
65
|
+
async def _save(self, entity, principal=OWNER, parent=None):
|
|
66
|
+
if parent is not None:
|
|
67
|
+
entity.set_parent(parent.entity_id)
|
|
68
|
+
with kernel_internal_context():
|
|
69
|
+
return await self.service.save_entity(entity, principal)
|
|
70
|
+
|
|
71
|
+
async def _dependent(self, target_id, cls=_Dependent, principal=OWNER, parent=None):
|
|
72
|
+
dependent = self.service.create(cls)
|
|
73
|
+
dependent.target = target_id
|
|
74
|
+
return await self._save(dependent, principal, parent)
|
|
75
|
+
|
|
76
|
+
async def _delete(self, entity_id, principal=OWNER):
|
|
77
|
+
with strategy_context(DeleteEntity):
|
|
78
|
+
return set(await self.service.delete_entity_tree(entity_id, principal))
|
|
79
|
+
|
|
80
|
+
async def test_deleting_the_target_deletes_the_dependent_and_its_children(self):
|
|
81
|
+
target = await self._save(self.service.create(_Target))
|
|
82
|
+
dependent = await self._dependent(target.entity_id)
|
|
83
|
+
child = await self._save(self.service.create(Entity), parent=dependent)
|
|
84
|
+
|
|
85
|
+
deleted = await self._delete(target.entity_id)
|
|
86
|
+
|
|
87
|
+
self.assertEqual(deleted, {target.entity_id, dependent.entity_id, child.entity_id})
|
|
88
|
+
|
|
89
|
+
async def test_rounds_chain_through_dependents_of_dependents(self):
|
|
90
|
+
target = await self._save(self.service.create(_Target))
|
|
91
|
+
first = await self._dependent(target.entity_id)
|
|
92
|
+
second = await self._dependent(first.entity_id, cls=_SubDependent)
|
|
93
|
+
|
|
94
|
+
deleted = await self._delete(target.entity_id)
|
|
95
|
+
|
|
96
|
+
self.assertEqual(deleted, {target.entity_id, first.entity_id, second.entity_id})
|
|
97
|
+
|
|
98
|
+
async def test_a_dependent_of_a_descendant_is_deleted(self):
|
|
99
|
+
root = await self._save(self.service.create(_Target))
|
|
100
|
+
inner = await self._save(self.service.create(_Target), parent=root)
|
|
101
|
+
dependent = await self._dependent(inner.entity_id)
|
|
102
|
+
|
|
103
|
+
deleted = await self._delete(root.entity_id)
|
|
104
|
+
|
|
105
|
+
self.assertIn(dependent.entity_id, deleted)
|
|
106
|
+
|
|
107
|
+
async def test_deleting_the_dependent_leaves_the_target(self):
|
|
108
|
+
target = await self._save(self.service.create(_Target))
|
|
109
|
+
dependent = await self._dependent(target.entity_id)
|
|
110
|
+
|
|
111
|
+
deleted = await self._delete(dependent.entity_id)
|
|
112
|
+
|
|
113
|
+
self.assertEqual(deleted, {dependent.entity_id})
|
|
114
|
+
self.assertIsNotNone(await self.service.repository.load(target.entity_id))
|
|
115
|
+
|
|
116
|
+
async def test_a_remote_target_never_matches(self):
|
|
117
|
+
target = await self._save(self.service.create(_Target))
|
|
118
|
+
remote = await self._dependent('gc://00000000zz/remotetarget')
|
|
119
|
+
|
|
120
|
+
await self._delete(target.entity_id)
|
|
121
|
+
|
|
122
|
+
self.assertIsNotNone(await self.service.repository.load(remote.entity_id))
|
|
123
|
+
|
|
124
|
+
async def test_a_dependent_the_deleter_cannot_read_is_still_deleted(self):
|
|
125
|
+
target = await self._save(self.service.create(_Target))
|
|
126
|
+
hidden = await self._dependent(target.entity_id, principal=STRANGER)
|
|
127
|
+
self.assertFalse(await self.service._can_read(OWNER, hidden))
|
|
128
|
+
|
|
129
|
+
deleted = await self._delete(target.entity_id)
|
|
130
|
+
|
|
131
|
+
self.assertIn(hidden.entity_id, deleted)
|
|
132
|
+
|
|
133
|
+
async def test_the_dependent_parent_is_republished(self):
|
|
134
|
+
target = await self._save(self.service.create(_Target))
|
|
135
|
+
holder = await self._save(self.service.create(Entity))
|
|
136
|
+
await self._dependent(target.entity_id, parent=holder)
|
|
137
|
+
|
|
138
|
+
await self._delete(target.entity_id)
|
|
139
|
+
|
|
140
|
+
published = [c.args[0].entity_id for c in self.service.broadcaster.publish_save.await_args_list]
|
|
141
|
+
self.assertIn(holder.entity_id, published)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Rule 7 (docs/ARCHITECTURE_PRINCIPLES.md): the engine names no concrete
|
|
2
|
+
entity type."""
|
|
3
|
+
import ast
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from django.test import SimpleTestCase
|
|
7
|
+
|
|
8
|
+
ENGINE = Path(__file__).resolve().parent.parent / 'EntityService.py'
|
|
9
|
+
KERNEL_TYPES = {'Grant', 'Group', 'User', 'Deployment', 'StrategyRequest'}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class EngineIsTypeAgnosticTests(SimpleTestCase):
|
|
13
|
+
def test_entity_service_references_no_concrete_entity_type(self):
|
|
14
|
+
tree = ast.parse(ENGINE.read_text())
|
|
15
|
+
named = set()
|
|
16
|
+
for node in ast.walk(tree):
|
|
17
|
+
if isinstance(node, ast.Name):
|
|
18
|
+
named.add(node.id)
|
|
19
|
+
elif isinstance(node, ast.Attribute):
|
|
20
|
+
named.add(node.attr)
|
|
21
|
+
elif isinstance(node, ast.alias):
|
|
22
|
+
named.add(node.name.rsplit('.', 1)[-1])
|
|
23
|
+
elif isinstance(node, ast.ImportFrom) and node.module:
|
|
24
|
+
named.update(node.module.split('.'))
|
|
25
|
+
self.assertEqual(named & KERNEL_TYPES, set())
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""The engine reads each type's read permission and asks the AccessPort for
|
|
2
|
+
exactly that perm - see docs/AUTHORIZATION_TECHNICAL_TDD.md (Inheritance)."""
|
|
3
|
+
from asgiref.sync import async_to_sync
|
|
4
|
+
from django.test import TestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.domain.entity.Entity import Entity
|
|
7
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
8
|
+
from graphcore.errors import Forbidden
|
|
9
|
+
from graphcore.persistence.tests.fixtures import make, persist
|
|
10
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
11
|
+
from graphcore.runtime import django_runtime
|
|
12
|
+
from graphcore.types.access import ManageAccess, Read
|
|
13
|
+
from graphcore.types.entities import EntityFilter, Tombstone
|
|
14
|
+
from graphcore.types.identity import Principal
|
|
15
|
+
|
|
16
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class _ReadScope(Entity):
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
TEST_REGISTRY.entities.register_entity(_ReadScope, app_label='graphcore')
|
|
24
|
+
TEST_REGISTRY.policy.define(_ReadScope, shareable=True)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class EngineResolvesReadPermissionTests(TestCase):
|
|
28
|
+
def setUp(self):
|
|
29
|
+
self.runtime = django_runtime(TEST_REGISTRY)
|
|
30
|
+
self.service = self.runtime.entity_service
|
|
31
|
+
self.alice = Principal(principal_entity_id='alice')
|
|
32
|
+
self.bob = Principal(principal_entity_id='bob')
|
|
33
|
+
|
|
34
|
+
scope = make(_ReadScope, TEST_REGISTRY)
|
|
35
|
+
scope.owner = 'alice'
|
|
36
|
+
persist(scope, TEST_REGISTRY)
|
|
37
|
+
self.scope = scope.entity_id
|
|
38
|
+
grant = make(Grant, TEST_REGISTRY)
|
|
39
|
+
grant.parent_id, grant.subject_id, grant.role_name = self.scope, 'bob', 'viewer'
|
|
40
|
+
persist(grant, TEST_REGISTRY)
|
|
41
|
+
self.grant = grant.entity_id
|
|
42
|
+
|
|
43
|
+
self.asked = []
|
|
44
|
+
adapter = self.service.access_adapter
|
|
45
|
+
allows, read_filters = adapter.allows, adapter.read_filters
|
|
46
|
+
|
|
47
|
+
async def recording_allows(principal, perm, entity_id):
|
|
48
|
+
self.asked.append(perm)
|
|
49
|
+
return await allows(principal, perm, entity_id)
|
|
50
|
+
|
|
51
|
+
async def recording_read_filters(principal, perms):
|
|
52
|
+
perms = list(perms)
|
|
53
|
+
self.asked.extend(perms)
|
|
54
|
+
return await read_filters(principal, perms)
|
|
55
|
+
|
|
56
|
+
adapter.allows, adapter.read_filters = recording_allows, recording_read_filters
|
|
57
|
+
self.addCleanup(self._restore, adapter)
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def _restore(adapter):
|
|
61
|
+
del adapter.allows
|
|
62
|
+
del adapter.read_filters
|
|
63
|
+
|
|
64
|
+
def test_get_entity_asks_for_the_grants_read_permission(self):
|
|
65
|
+
with self.assertRaises(Forbidden):
|
|
66
|
+
async_to_sync(self.service.get_entity)(self.grant, self.bob)
|
|
67
|
+
self.assertEqual(self.asked, [ManageAccess()])
|
|
68
|
+
|
|
69
|
+
self.assertIsInstance(async_to_sync(self.service.get_entity)(self.grant, self.alice), Grant)
|
|
70
|
+
|
|
71
|
+
def test_get_entities_tombstones_a_grant_for_a_viewer(self):
|
|
72
|
+
result = async_to_sync(self.service.get_entities)([self.scope, self.grant], self.bob)
|
|
73
|
+
self.assertEqual(result[self.scope].entity_id, self.scope)
|
|
74
|
+
self.assertIsInstance(result[self.grant], Tombstone)
|
|
75
|
+
self.assertEqual(result[self.grant].cause, 'forbidden')
|
|
76
|
+
self.assertIn(ManageAccess(), self.asked)
|
|
77
|
+
self.assertIn(Read(), self.asked)
|
|
78
|
+
|
|
79
|
+
self.assertIsInstance(
|
|
80
|
+
async_to_sync(self.service.get_entities)([self.grant], self.alice)[self.grant], Grant,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def test_list_entities_excludes_a_grant_for_a_viewer(self):
|
|
84
|
+
grant_type = TEST_REGISTRY.entities.get_entity_type_id(Grant)
|
|
85
|
+
|
|
86
|
+
def listed(principal):
|
|
87
|
+
page = async_to_sync(self.service.list_entities)(
|
|
88
|
+
principal, EntityFilter(type_ids=frozenset({grant_type}), parent_id=self.scope),
|
|
89
|
+
)
|
|
90
|
+
return {entity.entity_id for entity in page.entities}
|
|
91
|
+
|
|
92
|
+
self.assertEqual(listed(self.bob), set())
|
|
93
|
+
self.assertEqual(listed(self.alice), {self.grant})
|