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,109 @@
|
|
|
1
|
+
"""GraphAPI.changed_entities - the resync read, against stored data."""
|
|
2
|
+
from asgiref.sync import async_to_sync
|
|
3
|
+
from django.test import SimpleTestCase, TransactionTestCase
|
|
4
|
+
|
|
5
|
+
from graphcore.domain.entity.Entity import Entity
|
|
6
|
+
from graphcore.errors import InvalidRequest
|
|
7
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
8
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
9
|
+
from graphcore.runtime import django_runtime
|
|
10
|
+
from graphcore.tests.ids import fixture_id
|
|
11
|
+
from graphcore.types.entities import Tombstone, client_view
|
|
12
|
+
from graphcore.types.identity import Principal
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class _ResyncScope(Entity):
|
|
16
|
+
@classmethod
|
|
17
|
+
def get_entity_type_id(cls) -> str:
|
|
18
|
+
return "graphcore._ResyncScope"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
22
|
+
TEST_REGISTRY.entities.register_entity(_ResyncScope, app_label='graphcore')
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ClientViewTests(SimpleTestCase):
|
|
26
|
+
def test_the_etag_covers_every_field_and_ignores_key_order(self):
|
|
27
|
+
entity_id = fixture_id()
|
|
28
|
+
entity = _ResyncScope(entity_id)
|
|
29
|
+
etag = client_view(entity)['etag']
|
|
30
|
+
|
|
31
|
+
self.assertEqual(client_view(_ResyncScope(entity_id))['etag'], etag)
|
|
32
|
+
entity._children_ids = (fixture_id(),)
|
|
33
|
+
self.assertNotEqual(client_view(entity)['etag'], etag)
|
|
34
|
+
|
|
35
|
+
def test_tombstones_carry_one_too(self):
|
|
36
|
+
view = client_view(Tombstone('gc://a/1', 'forbidden'))
|
|
37
|
+
self.assertEqual((view['forbidden'], len(view['etag'])), (True, 16))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ChangedEntitiesTests(TransactionTestCase):
|
|
41
|
+
OWNER = 'resync-owner'
|
|
42
|
+
STRANGER = 'resync-stranger'
|
|
43
|
+
|
|
44
|
+
def setUp(self):
|
|
45
|
+
self.api = django_runtime(TEST_REGISTRY).graph_api
|
|
46
|
+
self.owner = Principal(principal_entity_id=self.OWNER)
|
|
47
|
+
self.scope = async_to_sync(self._save)(self.OWNER)
|
|
48
|
+
self.child = async_to_sync(self._save)(self.OWNER, parent_id=self.scope.entity_id)
|
|
49
|
+
self.unreadable = async_to_sync(self._save)(self.STRANGER)
|
|
50
|
+
|
|
51
|
+
async def _save(self, owner: str, parent_id: str = None):
|
|
52
|
+
entity = self.api.entity_service.create(_ResyncScope)
|
|
53
|
+
entity.owner = owner
|
|
54
|
+
if parent_id:
|
|
55
|
+
entity.set_parent(parent_id)
|
|
56
|
+
with kernel_internal_context():
|
|
57
|
+
return await self.api.entity_service.save_entity(entity, Principal(principal_entity_id=owner))
|
|
58
|
+
|
|
59
|
+
def _held(self, *entity_ids) -> dict:
|
|
60
|
+
current = async_to_sync(self.api.get_entities)(self.owner, list(entity_ids))
|
|
61
|
+
return {eid: client_view(current[eid])['etag'] for eid in entity_ids}
|
|
62
|
+
|
|
63
|
+
def _changed(self, held: dict) -> dict:
|
|
64
|
+
return async_to_sync(self.api.changed_entities)(self.owner, held)
|
|
65
|
+
|
|
66
|
+
def test_what_the_client_holds_unchanged_is_left_out(self):
|
|
67
|
+
self.assertEqual(self._changed(self._held(self.scope.entity_id, self.child.entity_id)), {})
|
|
68
|
+
|
|
69
|
+
def test_a_newer_version_is_returned(self):
|
|
70
|
+
held = self._held(self.scope.entity_id, self.child.entity_id)
|
|
71
|
+
async_to_sync(self._resave)(self.child.entity_id)
|
|
72
|
+
|
|
73
|
+
self.assertEqual(set(self._changed(held)), {self.child.entity_id})
|
|
74
|
+
|
|
75
|
+
async def _resave(self, entity_id: str):
|
|
76
|
+
entity = await self.api.entity_service.get_entity(entity_id, self.owner)
|
|
77
|
+
entity.set_attribute('name', 'renamed')
|
|
78
|
+
with kernel_internal_context():
|
|
79
|
+
await self.api.entity_service.save_entity(entity, self.owner)
|
|
80
|
+
|
|
81
|
+
def test_a_containment_change_without_a_version_bump_is_returned(self):
|
|
82
|
+
held = self._held(self.scope.entity_id)
|
|
83
|
+
async_to_sync(self._save)(self.OWNER, parent_id=self.scope.entity_id)
|
|
84
|
+
|
|
85
|
+
changed = self._changed(held)
|
|
86
|
+
|
|
87
|
+
self.assertEqual(changed[self.scope.entity_id].version, self.scope.version)
|
|
88
|
+
self.assertEqual(len(changed[self.scope.entity_id].children_ids), 2)
|
|
89
|
+
|
|
90
|
+
def test_a_missing_entity_comes_back_deleted(self):
|
|
91
|
+
gone = self.api.entity_service.create(_ResyncScope).entity_id
|
|
92
|
+
|
|
93
|
+
changed = self._changed({gone: 'stale'})
|
|
94
|
+
|
|
95
|
+
self.assertEqual(changed[gone].to_client(), {'id': gone, 'entity_id': gone, 'deleted': True})
|
|
96
|
+
|
|
97
|
+
def test_an_unreadable_entity_comes_back_forbidden_once(self):
|
|
98
|
+
held = {self.unreadable.entity_id: 'stale'}
|
|
99
|
+
forbidden = client_view(self._changed(held)[self.unreadable.entity_id])
|
|
100
|
+
self.assertTrue(forbidden['forbidden'])
|
|
101
|
+
self.assertEqual(self._changed({self.unreadable.entity_id: forbidden['etag']}), {})
|
|
102
|
+
|
|
103
|
+
def test_an_unknown_etag_is_always_returned(self):
|
|
104
|
+
self.assertEqual(set(self._changed({self.child.entity_id: ''})), {self.child.entity_id})
|
|
105
|
+
|
|
106
|
+
def test_the_batch_size_is_enforced(self):
|
|
107
|
+
held = {f'gc://x/{i}': '' for i in range(self.api.max_batch_size + 1)}
|
|
108
|
+
with self.assertRaises(InvalidRequest):
|
|
109
|
+
self._changed(held)
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"""GraphAPI unit tests: tombstones, batch caps, payload building, outcomes."""
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from unittest.mock import AsyncMock, MagicMock, patch
|
|
4
|
+
|
|
5
|
+
from asgiref.sync import async_to_sync
|
|
6
|
+
from django.test import TestCase
|
|
7
|
+
|
|
8
|
+
from graphcore.api import GraphAPI
|
|
9
|
+
from graphcore.types.identity import Principal
|
|
10
|
+
from graphcore.types.api import StrategyOutcome, StrategyRequestPayload
|
|
11
|
+
from graphcore.types.entities import EntityPage, Tombstone
|
|
12
|
+
from graphcore.types.access import Perm, Read, Role
|
|
13
|
+
from graphcore.auth.authz.defaults import DEFAULT_ROLES
|
|
14
|
+
from graphcore.domain.entity.Entity import Entity
|
|
15
|
+
from graphcore.domain.entity.user.User import User
|
|
16
|
+
from graphcore.errors import (
|
|
17
|
+
ConcurrentModificationError, Forbidden, InvalidRequest, NotFound, RemoteUnavailable,
|
|
18
|
+
)
|
|
19
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
20
|
+
|
|
21
|
+
class GraphAPITombstoneTests(TestCase):
|
|
22
|
+
def setUp(self):
|
|
23
|
+
self.principal = Principal(principal_entity_id='user-1')
|
|
24
|
+
self.api = GraphAPI(
|
|
25
|
+
registry=MagicMock(),
|
|
26
|
+
entity_service=MagicMock(),
|
|
27
|
+
strategy_executor=MagicMock(),
|
|
28
|
+
max_batch_size=100,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
def test_get_entity_missing_returns_tombstone(self):
|
|
32
|
+
self.api.entity_service.get_entity = AsyncMock(side_effect=NotFound('not found'))
|
|
33
|
+
result = async_to_sync(self.api.get_entity)(self.principal, 'missing-id')
|
|
34
|
+
self.assertIsInstance(result, Tombstone)
|
|
35
|
+
self.assertEqual(result.cause, 'missing')
|
|
36
|
+
self.assertEqual(result.entity_id, 'missing-id')
|
|
37
|
+
|
|
38
|
+
def test_get_entity_unreachable_returns_tombstone(self):
|
|
39
|
+
self.api.entity_service.get_entity = AsyncMock(
|
|
40
|
+
side_effect=RemoteUnavailable('no peer', cause='unreachable')
|
|
41
|
+
)
|
|
42
|
+
result = async_to_sync(self.api.get_entity)(self.principal, 'gc://other/x')
|
|
43
|
+
self.assertIsInstance(result, Tombstone)
|
|
44
|
+
self.assertEqual(result.cause, 'unreachable')
|
|
45
|
+
|
|
46
|
+
def test_get_entity_undelegated_returns_tombstone(self):
|
|
47
|
+
self.api.entity_service.get_entity = AsyncMock(
|
|
48
|
+
side_effect=RemoteUnavailable('no token', cause='undelegated')
|
|
49
|
+
)
|
|
50
|
+
self.assertEqual(
|
|
51
|
+
async_to_sync(self.api.get_entity)(self.principal, 'gc://other/x').cause,
|
|
52
|
+
'undelegated',
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def test_get_entity_forbidden_returns_tombstone(self):
|
|
56
|
+
self.api.entity_service.get_entity = AsyncMock(side_effect=Forbidden('denied'))
|
|
57
|
+
result = async_to_sync(self.api.get_entity)(self.principal, 'secret-id')
|
|
58
|
+
self.assertIsInstance(result, Tombstone)
|
|
59
|
+
self.assertEqual(result.cause, 'forbidden')
|
|
60
|
+
self.assertEqual(
|
|
61
|
+
result.to_dict(),
|
|
62
|
+
{'id': 'secret-id', 'entity_id': 'secret-id', 'forbidden': True},
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class GraphAPIBatchCapTests(TestCase):
|
|
67
|
+
def setUp(self):
|
|
68
|
+
self.principal = Principal(principal_entity_id='user-1')
|
|
69
|
+
self.api = GraphAPI(
|
|
70
|
+
registry=MagicMock(),
|
|
71
|
+
entity_service=MagicMock(),
|
|
72
|
+
strategy_executor=MagicMock(),
|
|
73
|
+
max_batch_size=2,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
def test_get_entities_over_cap_raises(self):
|
|
77
|
+
with self.assertRaises(InvalidRequest):
|
|
78
|
+
async_to_sync(self.api.get_entities)(self.principal, ['a', 'b', 'c'])
|
|
79
|
+
|
|
80
|
+
def test_referrers_limit_is_clamped_to_the_cap(self):
|
|
81
|
+
self.api.entity_service.referrers = AsyncMock(return_value=EntityPage([], None))
|
|
82
|
+
|
|
83
|
+
async_to_sync(self.api.get_referrers)(self.principal, 'target-1', limit=50)
|
|
84
|
+
|
|
85
|
+
self.assertEqual(self.api.entity_service.referrers.await_args.kwargs['limit'], 2)
|
|
86
|
+
|
|
87
|
+
def test_execute_strategies_over_cap_raises(self):
|
|
88
|
+
payloads = [
|
|
89
|
+
StrategyRequestPayload(strategy_name='X', target_entity_id=f't-{i}')
|
|
90
|
+
for i in range(3)
|
|
91
|
+
]
|
|
92
|
+
with self.assertRaises(InvalidRequest):
|
|
93
|
+
async_to_sync(self.api.execute_strategies)(self.principal, payloads)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class GraphAPIPayloadTests(TestCase):
|
|
97
|
+
def test_client_entity_id_ignored(self):
|
|
98
|
+
payload = StrategyRequestPayload.from_json({
|
|
99
|
+
'entity_id': 'client-supplied-id',
|
|
100
|
+
'strategy_name': 'SetAttributes',
|
|
101
|
+
'target_entity_id': 'target-1',
|
|
102
|
+
'param_config': {'a': 1},
|
|
103
|
+
})
|
|
104
|
+
self.assertFalse(hasattr(payload, 'entity_id'))
|
|
105
|
+
self.assertEqual(payload.strategy_name, 'SetAttributes')
|
|
106
|
+
self.assertEqual(payload.target_entity_id, 'target-1')
|
|
107
|
+
|
|
108
|
+
def test_unknown_keys_dropped(self):
|
|
109
|
+
payload = StrategyRequestPayload.from_json({
|
|
110
|
+
'strategy_name': 'X',
|
|
111
|
+
'target_entity_ids': ['a', 'b'],
|
|
112
|
+
'principal_entity_id': 'spoofed',
|
|
113
|
+
'client_request_id': 'c-1',
|
|
114
|
+
})
|
|
115
|
+
self.assertEqual(payload.client_request_id, 'c-1')
|
|
116
|
+
self.assertFalse(hasattr(payload, 'target_entity_ids'))
|
|
117
|
+
self.assertFalse(hasattr(payload, 'principal_entity_id'))
|
|
118
|
+
|
|
119
|
+
def test_missing_strategy_name_raises(self):
|
|
120
|
+
with self.assertRaises(InvalidRequest):
|
|
121
|
+
StrategyRequestPayload.from_json({'target_entity_id': 't'})
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class _DescribeEntitiesScope(Entity):
|
|
125
|
+
@classmethod
|
|
126
|
+
def get_entity_type_id(cls) -> str:
|
|
127
|
+
return "graphcore._DescribeEntitiesScope"
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@dataclass(frozen=True)
|
|
131
|
+
class _Publish(Perm):
|
|
132
|
+
pass
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
_PUBLISHER = Role('publisher', perms=frozenset({Read(), _Publish()}))
|
|
136
|
+
|
|
137
|
+
_DESCRIBE_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
138
|
+
_DESCRIBE_REGISTRY.entities.register_entity(_DescribeEntitiesScope, app_label='graphcore')
|
|
139
|
+
_DESCRIBE_REGISTRY.policy.define(
|
|
140
|
+
_DescribeEntitiesScope, shareable=True, roles=[*DEFAULT_ROLES, _PUBLISHER],
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class GraphAPIDescribeEntitiesTests(TestCase):
|
|
145
|
+
def setUp(self):
|
|
146
|
+
self.api = GraphAPI(
|
|
147
|
+
registry=_DESCRIBE_REGISTRY,
|
|
148
|
+
entity_service=MagicMock(),
|
|
149
|
+
strategy_executor=MagicMock(),
|
|
150
|
+
max_batch_size=100,
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
def test_type_with_extra_role_lists_all_four(self):
|
|
154
|
+
directory = self.api.describe_entities()
|
|
155
|
+
entity_info = directory[_DescribeEntitiesScope.get_entity_type_id()]
|
|
156
|
+
role_names = {role['name'] for role in entity_info['roles']}
|
|
157
|
+
self.assertEqual(role_names, {'viewer', 'editor', 'admin', 'publisher'})
|
|
158
|
+
|
|
159
|
+
def test_roles_carry_descriptions_and_permission_details(self):
|
|
160
|
+
directory = self.api.describe_entities()
|
|
161
|
+
roles = {role['name']: role for role in directory[_DescribeEntitiesScope.get_entity_type_id()]['roles']}
|
|
162
|
+
self.assertEqual(roles['viewer']['description'], 'Can view')
|
|
163
|
+
self.assertEqual(roles['viewer']['permissions'], ['Read'])
|
|
164
|
+
self.assertEqual(roles['viewer']['permission_details'], [{'name': 'Read', 'description': 'View the entity'}])
|
|
165
|
+
|
|
166
|
+
def test_user_entity_has_public_read_by_default(self):
|
|
167
|
+
directory = self.api.describe_entities()
|
|
168
|
+
entity_info = directory[User.get_entity_type_id()]
|
|
169
|
+
self.assertEqual(entity_info['public_permissions'], ['Read'])
|
|
170
|
+
|
|
171
|
+
def test_undeclared_type_has_no_public_permissions(self):
|
|
172
|
+
directory = self.api.describe_entities()
|
|
173
|
+
entity_info = directory[_DescribeEntitiesScope.get_entity_type_id()]
|
|
174
|
+
self.assertEqual(entity_info['public_permissions'], [])
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class GraphAPIOutcomeTests(TestCase):
|
|
178
|
+
def setUp(self):
|
|
179
|
+
self.principal = Principal(principal_entity_id='user-1')
|
|
180
|
+
self.api = GraphAPI(
|
|
181
|
+
registry=MagicMock(),
|
|
182
|
+
entity_service=MagicMock(),
|
|
183
|
+
strategy_executor=MagicMock(),
|
|
184
|
+
max_batch_size=100,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
def _request(self, **fields):
|
|
188
|
+
defaults = dict(entity_id='request-1', status='pending', target_entity_id='t-1', ret_val={}, error=None)
|
|
189
|
+
return MagicMock(**{**defaults, **fields})
|
|
190
|
+
|
|
191
|
+
def test_the_wire_payload_reaches_the_executor_unchanged(self):
|
|
192
|
+
payload = StrategyRequestPayload(
|
|
193
|
+
strategy_name='X', target_entity_id='t-1', param_config={'x': 2},
|
|
194
|
+
callbacks=[{'strategy_name': 'Y', 'target_entity_id': 't-2'}], client_request_id='c1',
|
|
195
|
+
)
|
|
196
|
+
self.api.strategy_executor.submit = AsyncMock(return_value=(self._request(), None))
|
|
197
|
+
|
|
198
|
+
async_to_sync(self.api.execute_strategies)(self.principal, [payload], callback_channel='chan')
|
|
199
|
+
|
|
200
|
+
call = self.api.strategy_executor.submit.await_args
|
|
201
|
+
self.assertIs(call.args[0], payload)
|
|
202
|
+
self.assertIs(call.args[1], self.principal)
|
|
203
|
+
self.assertEqual(call.kwargs['callback_channel'], 'chan')
|
|
204
|
+
|
|
205
|
+
def test_outcome_carries_the_request_and_its_status(self):
|
|
206
|
+
self.api.strategy_executor.submit = AsyncMock(return_value=(self._request(), None))
|
|
207
|
+
|
|
208
|
+
outcomes = async_to_sync(self.api.execute_strategies)(
|
|
209
|
+
self.principal,
|
|
210
|
+
[StrategyRequestPayload(strategy_name='X', target_entity_id='t-1', client_request_id='c1')],
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
self.assertEqual(outcomes[0].strategy_request_id, 'request-1')
|
|
214
|
+
self.assertEqual(outcomes[0].status, 'pending')
|
|
215
|
+
self.assertEqual(outcomes[0].client_request_id, 'c1')
|
|
216
|
+
|
|
217
|
+
def test_a_refusal_is_a_failed_outcome_without_a_request(self):
|
|
218
|
+
for error in (Forbidden('Permission denied'),
|
|
219
|
+
ConcurrentModificationError('version mismatch', entity_id='e-1')):
|
|
220
|
+
self.api.strategy_executor.submit = AsyncMock(side_effect=error)
|
|
221
|
+
outcomes = async_to_sync(self.api.execute_strategies)(
|
|
222
|
+
self.principal, [StrategyRequestPayload(strategy_name='X', target_entity_id='t-1')],
|
|
223
|
+
)
|
|
224
|
+
self.assertEqual(outcomes[0].status, 'failed')
|
|
225
|
+
self.assertEqual(outcomes[0].error, str(error))
|
|
226
|
+
self.assertIsNone(outcomes[0].strategy_request_id)
|
|
227
|
+
|
|
228
|
+
def test_an_unexpected_error_is_not_leaked(self):
|
|
229
|
+
self.api.strategy_executor.submit = AsyncMock(side_effect=RuntimeError('secret detail'))
|
|
230
|
+
outcomes = async_to_sync(self.api.execute_strategies)(
|
|
231
|
+
self.principal, [StrategyRequestPayload(strategy_name='X', target_entity_id='t-1')],
|
|
232
|
+
)
|
|
233
|
+
self.assertEqual((outcomes[0].status, outcomes[0].error), ('failed', 'Internal server error'))
|
|
234
|
+
|
|
235
|
+
def test_awaited_outcomes_report_the_terminal_request(self):
|
|
236
|
+
self.api.strategy_executor.submit = AsyncMock(side_effect=[
|
|
237
|
+
(self._request(status='succeeded', ret_val={'ok': True}), MagicMock()),
|
|
238
|
+
(self._request(entity_id='request-2', status='failed', error='boom'), None),
|
|
239
|
+
])
|
|
240
|
+
outcomes = async_to_sync(self.api.execute_strategies)(
|
|
241
|
+
self.principal,
|
|
242
|
+
[StrategyRequestPayload(strategy_name='X', target_entity_id='t-1')] * 2,
|
|
243
|
+
wait=True,
|
|
244
|
+
)
|
|
245
|
+
self.assertEqual(outcomes[0].to_dict(), {
|
|
246
|
+
'status': 'succeeded', 'strategy_request_id': 'request-1', 'target_entity_id': 't-1',
|
|
247
|
+
'ret_val': {'ok': True},
|
|
248
|
+
})
|
|
249
|
+
self.assertEqual(outcomes[1].to_dict()['status'], 'failed')
|
|
250
|
+
self.assertEqual(outcomes[1].to_dict()['error'], 'boom')
|
|
251
|
+
self.assertTrue(self.api.strategy_executor.submit.await_args.kwargs['wait'])
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
class GraphAPIAgentReadTests(TestCase):
|
|
255
|
+
def setUp(self):
|
|
256
|
+
self.api = GraphAPI(
|
|
257
|
+
registry=DEFAULT_REGISTRY, entity_service=MagicMock(),
|
|
258
|
+
strategy_executor=MagicMock(), max_batch_size=100,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
def test_deployment_strategies_match_the_allowlist(self):
|
|
262
|
+
from graphcore.domain.entity.deployment.Deployment import Deployment
|
|
263
|
+
|
|
264
|
+
allowed = DEFAULT_REGISTRY.policy.allowed_strategies(Deployment)
|
|
265
|
+
names = {s['name'] for s in self.api.deployment_strategies()}
|
|
266
|
+
self.assertEqual(names, {cls.__name__ for cls in allowed})
|
|
267
|
+
|
|
268
|
+
def test_type_strategies_respect_a_closed_allowlist(self):
|
|
269
|
+
allowed = DEFAULT_REGISTRY.policy.allowed_strategies(User)
|
|
270
|
+
names = {s['name'] for s in self.api.type_strategies('graphcore.User')}
|
|
271
|
+
if allowed is not None:
|
|
272
|
+
self.assertEqual(names, {cls.__name__ for cls in allowed})
|
|
273
|
+
else:
|
|
274
|
+
self.assertTrue(names)
|
|
275
|
+
|
|
276
|
+
def test_type_strategies_unknown_type(self):
|
|
277
|
+
with self.assertRaises(NotFound):
|
|
278
|
+
self.api.type_strategies('nope.Missing')
|
|
279
|
+
|
|
280
|
+
def test_list_children_defaults_to_the_types_the_parent_may_contain(self):
|
|
281
|
+
from graphcore.types.entities import EntityFilter
|
|
282
|
+
|
|
283
|
+
parent = Entity('gc://' + 'a' * 10 + '/' + 'b' * 12)
|
|
284
|
+
self.api.entity_service.get_entity = AsyncMock(return_value=parent)
|
|
285
|
+
self.api.list_entities = AsyncMock(return_value=EntityPage([], None))
|
|
286
|
+
async_to_sync(self.api.list_children)(Principal(principal_entity_id='u'), parent.entity_id)
|
|
287
|
+
entity_filter = self.api.list_entities.await_args.args[1]
|
|
288
|
+
self.assertIsInstance(entity_filter, EntityFilter)
|
|
289
|
+
self.assertEqual(entity_filter.parent_id, parent.entity_id)
|
|
290
|
+
self.assertIn('graphcore.Entity', entity_filter.type_ids)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""GraphAPI.get_access - integration-style, against the real
|
|
2
|
+
EntityService/StrategyExecutorService (no direct model writes)."""
|
|
3
|
+
from asgiref.sync import async_to_sync
|
|
4
|
+
from django.test import TransactionTestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.types.identity import Principal
|
|
7
|
+
from graphcore.types.api import AccessSummary
|
|
8
|
+
from graphcore.domain.entity.Entity import Entity
|
|
9
|
+
from graphcore.domain.entity.user.User import User
|
|
10
|
+
from graphcore.errors import Forbidden
|
|
11
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
12
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
13
|
+
from graphcore.runtime import django_runtime
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class _AccessTestScope(Entity):
|
|
17
|
+
@classmethod
|
|
18
|
+
def get_entity_type_id(cls) -> str:
|
|
19
|
+
return "graphcore._AccessTestScope"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
23
|
+
TEST_REGISTRY.entities.register_entity(_AccessTestScope, app_label='graphcore')
|
|
24
|
+
TEST_REGISTRY.policy.define(_AccessTestScope, shareable=True)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class GraphAPIGetAccessTests(TransactionTestCase):
|
|
28
|
+
OWNER = 'access-owner'
|
|
29
|
+
STRANGER = 'access-stranger'
|
|
30
|
+
|
|
31
|
+
def setUp(self):
|
|
32
|
+
self.api = django_runtime(TEST_REGISTRY).graph_api
|
|
33
|
+
self.owner_principal = Principal(principal_entity_id=self.OWNER)
|
|
34
|
+
self.stranger_principal = Principal(principal_entity_id=self.STRANGER)
|
|
35
|
+
|
|
36
|
+
self.scope = async_to_sync(self._create_scope)()
|
|
37
|
+
self.grantee = async_to_sync(self._create_user)('grantee')
|
|
38
|
+
self.VIEWER = self.grantee.entity_id
|
|
39
|
+
self.viewer_principal = Principal(principal_entity_id=self.VIEWER)
|
|
40
|
+
async_to_sync(self.api.grant)(
|
|
41
|
+
self.owner_principal, self.scope.entity_id, self.VIEWER, 'viewer',
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
async def _create_scope(self):
|
|
45
|
+
scope = self.api.entity_service.create(_AccessTestScope)
|
|
46
|
+
scope.owner = self.OWNER
|
|
47
|
+
with kernel_internal_context():
|
|
48
|
+
return await self.api.entity_service.save_entity(scope, self.owner_principal)
|
|
49
|
+
|
|
50
|
+
async def _create_user(self, username: str):
|
|
51
|
+
user = self.api.entity_service.create(User)
|
|
52
|
+
user.username = username
|
|
53
|
+
with kernel_internal_context():
|
|
54
|
+
return await self.api.entity_service.save_entity(user, self.owner_principal)
|
|
55
|
+
|
|
56
|
+
def test_owner_can_manage_and_sees_owner_and_direct_grants(self):
|
|
57
|
+
summary = async_to_sync(self.api.get_access)(self.owner_principal, self.scope.entity_id)
|
|
58
|
+
|
|
59
|
+
self.assertIsInstance(summary, AccessSummary)
|
|
60
|
+
self.assertEqual(summary.resource_id, self.scope.entity_id)
|
|
61
|
+
self.assertTrue(summary.can_manage)
|
|
62
|
+
provenances = {(g.subject_id, g.provenance) for g in summary.grants}
|
|
63
|
+
self.assertIn((self.OWNER, 'owner'), provenances)
|
|
64
|
+
self.assertIn((self.VIEWER, 'direct'), provenances)
|
|
65
|
+
direct = next(g for g in summary.grants if g.subject_id == self.VIEWER)
|
|
66
|
+
self.assertIsNotNone(direct.grant_entity_id)
|
|
67
|
+
self.assertEqual(direct.role.name, 'viewer')
|
|
68
|
+
|
|
69
|
+
def test_viewer_sees_no_grants(self):
|
|
70
|
+
summary = async_to_sync(self.api.get_access)(self.viewer_principal, self.scope.entity_id)
|
|
71
|
+
|
|
72
|
+
self.assertFalse(summary.can_manage)
|
|
73
|
+
self.assertEqual(summary.grants, [])
|
|
74
|
+
|
|
75
|
+
def test_list_grants_requires_manage_access(self):
|
|
76
|
+
with self.assertRaises(Forbidden):
|
|
77
|
+
async_to_sync(self.api.list_grants)(self.viewer_principal, self.scope.entity_id)
|
|
78
|
+
grants = async_to_sync(self.api.list_grants)(self.owner_principal, self.scope.entity_id)
|
|
79
|
+
self.assertIn(self.VIEWER, {g.subject_id for g in grants})
|
|
80
|
+
|
|
81
|
+
def test_non_reader_is_forbidden(self):
|
|
82
|
+
with self.assertRaises(Forbidden):
|
|
83
|
+
async_to_sync(self.api.get_access)(self.stranger_principal, self.scope.entity_id)
|
|
84
|
+
|
|
85
|
+
def test_to_dict_shape(self):
|
|
86
|
+
summary = async_to_sync(self.api.get_access)(self.owner_principal, self.scope.entity_id)
|
|
87
|
+
data = summary.to_dict()
|
|
88
|
+
|
|
89
|
+
self.assertEqual(data['resource_id'], self.scope.entity_id)
|
|
90
|
+
self.assertTrue(data['can_manage'])
|
|
91
|
+
direct = next(g for g in data['grants'] if g['subject_id'] == self.VIEWER)
|
|
92
|
+
self.assertEqual(direct['role'], 'viewer')
|
|
93
|
+
self.assertEqual(direct['provenance'], 'direct')
|
|
94
|
+
self.assertIsNotNone(direct['grant_entity_id'])
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""GraphAPI.grant/revoke/list_grants/available_roles - integration-style,
|
|
2
|
+
against the real EntityService/StrategyExecutorService (no direct model
|
|
3
|
+
writes) since these are thin sugar over the grant strategies and adapter."""
|
|
4
|
+
from asgiref.sync import async_to_sync
|
|
5
|
+
from django.test import TransactionTestCase
|
|
6
|
+
|
|
7
|
+
from graphcore.auth.authz.defaults import ADMIN, EDITOR, VIEWER
|
|
8
|
+
from graphcore.types.identity import Principal
|
|
9
|
+
from graphcore.domain.entity.Entity import Entity
|
|
10
|
+
from graphcore.domain.entity.user.User import User
|
|
11
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
12
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
13
|
+
from graphcore.runtime import django_runtime
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class _ApiTestScope(Entity):
|
|
17
|
+
@classmethod
|
|
18
|
+
def get_entity_type_id(cls) -> str:
|
|
19
|
+
return "graphcore._ApiTestScope"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
|
|
23
|
+
TEST_REGISTRY.entities.register_entity(_ApiTestScope, app_label='graphcore')
|
|
24
|
+
TEST_REGISTRY.policy.define(_ApiTestScope, shareable=True)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class GraphAPIGrantTests(TransactionTestCase):
|
|
28
|
+
OWNER = 'graph-api-owner'
|
|
29
|
+
|
|
30
|
+
def setUp(self):
|
|
31
|
+
self.api = django_runtime(TEST_REGISTRY).graph_api
|
|
32
|
+
self.principal = Principal(principal_entity_id=self.OWNER)
|
|
33
|
+
|
|
34
|
+
self.scope = async_to_sync(self._create_scope)()
|
|
35
|
+
self.user = async_to_sync(self._create_user)()
|
|
36
|
+
|
|
37
|
+
async def _create_scope(self):
|
|
38
|
+
scope = self.api.entity_service.create(_ApiTestScope)
|
|
39
|
+
scope.owner = self.OWNER
|
|
40
|
+
with kernel_internal_context():
|
|
41
|
+
return await self.api.entity_service.save_entity(scope, self.principal)
|
|
42
|
+
|
|
43
|
+
async def _create_user(self):
|
|
44
|
+
user = self.api.entity_service.create(User)
|
|
45
|
+
user.username = 'grantee'
|
|
46
|
+
with kernel_internal_context():
|
|
47
|
+
return await self.api.entity_service.save_entity(user, self.principal)
|
|
48
|
+
|
|
49
|
+
def test_grant_then_revoke_round_trip(self):
|
|
50
|
+
grant_id = async_to_sync(self.api.grant)(
|
|
51
|
+
self.principal, self.scope.entity_id, self.user.entity_id, 'viewer',
|
|
52
|
+
)
|
|
53
|
+
self.assertIsNotNone(grant_id)
|
|
54
|
+
|
|
55
|
+
grants = async_to_sync(self.api.list_grants)(self.principal, self.scope.entity_id)
|
|
56
|
+
subjects = {g.subject_id: g.role for g in grants if g.provenance == 'direct'}
|
|
57
|
+
self.assertEqual(subjects.get(self.user.entity_id), VIEWER)
|
|
58
|
+
|
|
59
|
+
async_to_sync(self.api.revoke)(self.principal, self.scope.entity_id, self.user.entity_id)
|
|
60
|
+
|
|
61
|
+
grants_after = async_to_sync(self.api.list_grants)(self.principal, self.scope.entity_id)
|
|
62
|
+
subjects_after = {g.subject_id for g in grants_after if g.provenance == 'direct'}
|
|
63
|
+
self.assertNotIn(self.user.entity_id, subjects_after)
|
|
64
|
+
|
|
65
|
+
def test_grant_twice_replaces_role(self):
|
|
66
|
+
async_to_sync(self.api.grant)(
|
|
67
|
+
self.principal, self.scope.entity_id, self.user.entity_id, 'viewer',
|
|
68
|
+
)
|
|
69
|
+
async_to_sync(self.api.grant)(
|
|
70
|
+
self.principal, self.scope.entity_id, self.user.entity_id, 'editor',
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
grants = async_to_sync(self.api.list_grants)(self.principal, self.scope.entity_id)
|
|
74
|
+
direct = [g for g in grants if g.provenance == 'direct' and g.subject_id == self.user.entity_id]
|
|
75
|
+
self.assertEqual(len(direct), 1)
|
|
76
|
+
self.assertEqual(direct[0].role, EDITOR)
|
|
77
|
+
|
|
78
|
+
def test_list_grants_includes_owner_provenance(self):
|
|
79
|
+
grants = async_to_sync(self.api.list_grants)(self.principal, self.scope.entity_id)
|
|
80
|
+
owner_entries = [g for g in grants if g.provenance == 'owner']
|
|
81
|
+
self.assertEqual(len(owner_entries), 1)
|
|
82
|
+
self.assertEqual(owner_entries[0].subject_id, self.OWNER)
|
|
83
|
+
self.assertEqual(owner_entries[0].role, ADMIN)
|
|
84
|
+
|
|
85
|
+
def test_available_roles_includes_the_three_defaults(self):
|
|
86
|
+
roles = async_to_sync(self.api.available_roles)(self.scope.entity_id)
|
|
87
|
+
self.assertEqual(set(roles), {VIEWER, EDITOR, ADMIN})
|
|
88
|
+
|
|
89
|
+
def test_revoke_with_no_existing_grant_is_a_no_op(self):
|
|
90
|
+
async_to_sync(self.api.revoke)(self.principal, self.scope.entity_id, self.user.entity_id)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from unittest import TestCase
|
|
2
|
+
|
|
3
|
+
from graphcore.api.predicate_codec import decode_predicate, encode_predicate
|
|
4
|
+
from graphcore.errors import InvalidRequest
|
|
5
|
+
from graphcore.types.query import AllOf, AnyOf, ContainsAny, ContainsValue, Equal, InValues
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class RoundTripTests(TestCase):
|
|
9
|
+
def assert_round_trips(self, predicate):
|
|
10
|
+
self.assertEqual(decode_predicate(encode_predicate(predicate)), predicate)
|
|
11
|
+
|
|
12
|
+
def test_each_node_round_trips(self):
|
|
13
|
+
for predicate in (
|
|
14
|
+
Equal('slot', 'entity'),
|
|
15
|
+
InValues('renders', {'gc://a/1', 'gc://a/2'}),
|
|
16
|
+
ContainsValue('member_ids', 'gc://a/3'),
|
|
17
|
+
ContainsAny('ancestor_ids', {'gc://a/4'}),
|
|
18
|
+
AllOf((Equal('a', 1),)),
|
|
19
|
+
AnyOf((Equal('a', True),)),
|
|
20
|
+
):
|
|
21
|
+
with self.subTest(predicate=predicate):
|
|
22
|
+
self.assert_round_trips(predicate)
|
|
23
|
+
|
|
24
|
+
def test_nested_branches_round_trip(self):
|
|
25
|
+
self.assert_round_trips(AllOf((
|
|
26
|
+
Equal('entity_type_id', 'graphcore.View'),
|
|
27
|
+
AnyOf((InValues('address', {'/a', '/b'}), Equal('renders', None))),
|
|
28
|
+
)))
|
|
29
|
+
|
|
30
|
+
def test_null_in_equal_is_an_explicit_null_check(self):
|
|
31
|
+
self.assertEqual(
|
|
32
|
+
decode_predicate({'equal': {'field': 'path', 'value': None}}), Equal('path', None),
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def test_decodes_the_documented_example(self):
|
|
36
|
+
self.assertEqual(
|
|
37
|
+
decode_predicate({'all_of': [
|
|
38
|
+
{'equal': {'field': 'slot', 'value': 'entity'}},
|
|
39
|
+
{'in_values': {'field': 'renders', 'values': ['gc://k7m2x9qp4n/3fz8a1c0w4ne']}},
|
|
40
|
+
]}),
|
|
41
|
+
AllOf((Equal('slot', 'entity'), InValues('renders', {'gc://k7m2x9qp4n/3fz8a1c0w4ne'}))),
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class RefusalTests(TestCase):
|
|
46
|
+
def assert_refused(self, obj):
|
|
47
|
+
with self.assertRaises(InvalidRequest):
|
|
48
|
+
decode_predicate(obj)
|
|
49
|
+
|
|
50
|
+
def test_unknown_node(self):
|
|
51
|
+
self.assert_refused({'not_in': {'field': 'a', 'values': [1]}})
|
|
52
|
+
|
|
53
|
+
def test_two_keys_in_one_node(self):
|
|
54
|
+
self.assert_refused({
|
|
55
|
+
'equal': {'field': 'a', 'value': 1},
|
|
56
|
+
'in_values': {'field': 'b', 'values': [1]},
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
def test_non_scalar_value(self):
|
|
60
|
+
self.assert_refused({'equal': {'field': 'a', 'value': {'nested': 1}}})
|
|
61
|
+
self.assert_refused({'in_values': {'field': 'a', 'values': [[1]]}})
|
|
62
|
+
|
|
63
|
+
def test_malformed_bodies(self):
|
|
64
|
+
self.assert_refused({'equal': {'field': 'a'}})
|
|
65
|
+
self.assert_refused({'equal': {'field': '', 'value': 1}})
|
|
66
|
+
self.assert_refused({'in_values': {'field': 'a', 'values': 'abc'}})
|
|
67
|
+
self.assert_refused({'all_of': {'equal': {'field': 'a', 'value': 1}}})
|
|
68
|
+
self.assert_refused(['equal'])
|
|
69
|
+
|
|
70
|
+
def test_depth_nine(self):
|
|
71
|
+
node = {'equal': {'field': 'a', 'value': 1}}
|
|
72
|
+
for _ in range(7):
|
|
73
|
+
node = {'all_of': [node]}
|
|
74
|
+
decode_predicate(node)
|
|
75
|
+
self.assert_refused({'all_of': [node]})
|
|
76
|
+
|
|
77
|
+
def test_more_than_a_thousand_values(self):
|
|
78
|
+
decode_predicate({'in_values': {'field': 'a', 'values': list(range(1000))}})
|
|
79
|
+
self.assert_refused({'in_values': {'field': 'a', 'values': list(range(1001))}})
|
|
80
|
+
self.assert_refused({'any_of': [
|
|
81
|
+
{'in_values': {'field': 'a', 'values': list(range(1000))}},
|
|
82
|
+
{'equal': {'field': 'a', 'value': 1}},
|
|
83
|
+
]})
|