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,245 @@
|
|
|
1
|
+
"""Tests for GrantStrategies (CreateGrant, ChangeGrantRole, RevokeGrant)."""
|
|
2
|
+
from graphcore.domain.entity.Entity import Entity
|
|
3
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
4
|
+
from graphcore.domain.entity.group.Group import Group
|
|
5
|
+
from graphcore.domain.strategy.tests.BaseStrategyTest import BaseStrategyTest
|
|
6
|
+
from graphcore.errors import InvalidRequest
|
|
7
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class _TestScope(Entity):
|
|
11
|
+
"""Minimal shareable entity for testing - test modules are skipped by
|
|
12
|
+
discovery, so register the flag directly instead of via the decorator."""
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def get_entity_type_id(cls) -> str:
|
|
16
|
+
return "graphcore._TestScope"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class _TestServiceAccount(Entity):
|
|
20
|
+
"""Minimal principal=True, non-User type - stands in for a future
|
|
21
|
+
service-account-shaped principal to prove _validate_subject accepts any
|
|
22
|
+
principal=True type, not just User specifically."""
|
|
23
|
+
|
|
24
|
+
@classmethod
|
|
25
|
+
def get_entity_type_id(cls) -> str:
|
|
26
|
+
return "graphcore._TestServiceAccount"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class GrantStrategiesTest(BaseStrategyTest):
|
|
30
|
+
def setUp(self):
|
|
31
|
+
super().setUp()
|
|
32
|
+
self.registry.entities.register_entity(_TestScope, app_label='graphcore')
|
|
33
|
+
self.registry.policy.define(_TestScope, shareable=True)
|
|
34
|
+
self.registry.entities.register_entity(
|
|
35
|
+
_TestServiceAccount, app_label='graphcore', principal=True
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
async def _create_scope(self):
|
|
39
|
+
scope = self.entity_service.create(_TestScope)
|
|
40
|
+
scope.owner = self.TEST_USER_ENTITY_ID
|
|
41
|
+
with kernel_internal_context():
|
|
42
|
+
scope = await self.entity_service.save_entity(scope, self.test_principal)
|
|
43
|
+
self.entities_created.append(scope.entity_id)
|
|
44
|
+
return scope
|
|
45
|
+
|
|
46
|
+
async def _create_user(self, user_id_suffix='1'):
|
|
47
|
+
from graphcore.domain.entity.user.User import User
|
|
48
|
+
user = self.entity_service.create(User)
|
|
49
|
+
user.username = f'user-{user_id_suffix}'
|
|
50
|
+
with kernel_internal_context():
|
|
51
|
+
user = await self.entity_service.save_entity(user, self.test_principal)
|
|
52
|
+
self.entities_created.append(user.entity_id)
|
|
53
|
+
return user
|
|
54
|
+
|
|
55
|
+
# --- create/replace/revoke round trip ---
|
|
56
|
+
|
|
57
|
+
async def test_create_grant_creates_a_grant_entity_child_of_the_resource(self):
|
|
58
|
+
scope = await self._create_scope()
|
|
59
|
+
user = await self._create_user()
|
|
60
|
+
|
|
61
|
+
req = self.create_strategy_request(
|
|
62
|
+
'CreateGrant', scope.entity_id,
|
|
63
|
+
{'subject_id': user.entity_id, 'role_name': 'viewer'},
|
|
64
|
+
)
|
|
65
|
+
result = await self.execute_strategy(req)
|
|
66
|
+
self.assert_strategy_success(result)
|
|
67
|
+
|
|
68
|
+
grant_id = result.ret_val['grant_entity_id']
|
|
69
|
+
grant = await self.entity_service.get_entity(grant_id, self.test_principal)
|
|
70
|
+
self.assertIsInstance(grant, Grant)
|
|
71
|
+
self.assertEqual(grant.subject_id, user.entity_id)
|
|
72
|
+
self.assertEqual(grant.role_name, 'viewer')
|
|
73
|
+
|
|
74
|
+
refreshed_scope = await self.entity_service.get_entity(scope.entity_id, self.test_principal)
|
|
75
|
+
self.assertIn(grant_id, refreshed_scope.children_ids)
|
|
76
|
+
|
|
77
|
+
async def test_regrant_replaces_the_existing_grant_role_in_place(self):
|
|
78
|
+
scope = await self._create_scope()
|
|
79
|
+
user = await self._create_user()
|
|
80
|
+
|
|
81
|
+
req1 = self.create_strategy_request(
|
|
82
|
+
'CreateGrant', scope.entity_id,
|
|
83
|
+
{'subject_id': user.entity_id, 'role_name': 'viewer'},
|
|
84
|
+
)
|
|
85
|
+
result1 = await self.execute_strategy(req1)
|
|
86
|
+
first_grant_id = result1.ret_val['grant_entity_id']
|
|
87
|
+
|
|
88
|
+
req2 = self.create_strategy_request(
|
|
89
|
+
'CreateGrant', scope.entity_id,
|
|
90
|
+
{'subject_id': user.entity_id, 'role_name': 'editor'},
|
|
91
|
+
)
|
|
92
|
+
result2 = await self.execute_strategy(req2)
|
|
93
|
+
second_grant_id = result2.ret_val['grant_entity_id']
|
|
94
|
+
|
|
95
|
+
self.assertEqual(first_grant_id, second_grant_id)
|
|
96
|
+
grant = await self.entity_service.get_entity(second_grant_id, self.test_principal)
|
|
97
|
+
self.assertEqual(grant.role_name, 'editor')
|
|
98
|
+
|
|
99
|
+
refreshed_scope = await self.entity_service.get_entity(scope.entity_id, self.test_principal)
|
|
100
|
+
grant_children = [
|
|
101
|
+
cid for cid in refreshed_scope.children_ids if cid == second_grant_id
|
|
102
|
+
]
|
|
103
|
+
self.assertEqual(len(grant_children), 1)
|
|
104
|
+
|
|
105
|
+
async def test_revoke_deletes_the_grant_and_detaches_it_from_the_resource(self):
|
|
106
|
+
scope = await self._create_scope()
|
|
107
|
+
user = await self._create_user()
|
|
108
|
+
|
|
109
|
+
create_req = self.create_strategy_request(
|
|
110
|
+
'CreateGrant', scope.entity_id,
|
|
111
|
+
{'subject_id': user.entity_id, 'role_name': 'viewer'},
|
|
112
|
+
)
|
|
113
|
+
create_result = await self.execute_strategy(create_req)
|
|
114
|
+
grant_id = create_result.ret_val['grant_entity_id']
|
|
115
|
+
|
|
116
|
+
revoke_req = self.create_strategy_request('RevokeGrant', grant_id)
|
|
117
|
+
revoke_result = await self.execute_strategy(revoke_req)
|
|
118
|
+
self.assert_strategy_success(revoke_result)
|
|
119
|
+
|
|
120
|
+
from graphcore.errors import NotFound
|
|
121
|
+
with self.assertRaises(NotFound):
|
|
122
|
+
await self.entity_service.get_entity(grant_id, self.test_principal)
|
|
123
|
+
|
|
124
|
+
refreshed_scope = await self.entity_service.get_entity(scope.entity_id, self.test_principal)
|
|
125
|
+
self.assertNotIn(grant_id, refreshed_scope.children_ids)
|
|
126
|
+
|
|
127
|
+
async def test_change_grant_role_updates_the_role(self):
|
|
128
|
+
scope = await self._create_scope()
|
|
129
|
+
user = await self._create_user()
|
|
130
|
+
|
|
131
|
+
create_req = self.create_strategy_request(
|
|
132
|
+
'CreateGrant', scope.entity_id,
|
|
133
|
+
{'subject_id': user.entity_id, 'role_name': 'viewer'},
|
|
134
|
+
)
|
|
135
|
+
create_result = await self.execute_strategy(create_req)
|
|
136
|
+
grant_id = create_result.ret_val['grant_entity_id']
|
|
137
|
+
|
|
138
|
+
change_req = self.create_strategy_request(
|
|
139
|
+
'ChangeGrantRole', grant_id, {'role_name': 'admin'},
|
|
140
|
+
)
|
|
141
|
+
change_result = await self.execute_strategy(change_req)
|
|
142
|
+
self.assertEqual(change_result.ret_val['role_name'], 'admin')
|
|
143
|
+
|
|
144
|
+
# --- validation failures ---
|
|
145
|
+
|
|
146
|
+
async def test_unknown_subject_is_rejected(self):
|
|
147
|
+
scope = await self._create_scope()
|
|
148
|
+
req = self.create_strategy_request(
|
|
149
|
+
'CreateGrant', scope.entity_id,
|
|
150
|
+
{'subject_id': 'does-not-exist', 'role_name': 'viewer'},
|
|
151
|
+
)
|
|
152
|
+
with self.assertRaises(InvalidRequest):
|
|
153
|
+
await self.execute_strategy(req)
|
|
154
|
+
|
|
155
|
+
async def test_non_group_non_user_entity_as_subject_is_rejected(self):
|
|
156
|
+
scope = await self._create_scope()
|
|
157
|
+
other_scope = await self._create_scope()
|
|
158
|
+
req = self.create_strategy_request(
|
|
159
|
+
'CreateGrant', scope.entity_id,
|
|
160
|
+
{'subject_id': other_scope.entity_id, 'role_name': 'viewer'},
|
|
161
|
+
)
|
|
162
|
+
with self.assertRaises(InvalidRequest):
|
|
163
|
+
await self.execute_strategy(req)
|
|
164
|
+
|
|
165
|
+
async def test_non_user_principal_type_is_accepted_as_subject(self):
|
|
166
|
+
"""_validate_subject accepts any principal=True type, not just
|
|
167
|
+
User by name - the extensibility point future non-human
|
|
168
|
+
principal kinds (e.g. a service account) would register through."""
|
|
169
|
+
scope = await self._create_scope()
|
|
170
|
+
service_account = self.entity_service.create(_TestServiceAccount)
|
|
171
|
+
with kernel_internal_context():
|
|
172
|
+
service_account = await self.entity_service.save_entity(
|
|
173
|
+
service_account, self.test_principal
|
|
174
|
+
)
|
|
175
|
+
self.entities_created.append(service_account.entity_id)
|
|
176
|
+
|
|
177
|
+
req = self.create_strategy_request(
|
|
178
|
+
'CreateGrant', scope.entity_id,
|
|
179
|
+
{'subject_id': service_account.entity_id, 'role_name': 'editor'},
|
|
180
|
+
)
|
|
181
|
+
result = await self.execute_strategy(req)
|
|
182
|
+
self.assert_strategy_success(result)
|
|
183
|
+
|
|
184
|
+
grant_id = result.ret_val['grant_entity_id']
|
|
185
|
+
grant = await self.entity_service.get_entity(grant_id, self.test_principal)
|
|
186
|
+
self.assertEqual(grant.subject_id, service_account.entity_id)
|
|
187
|
+
self.assertEqual(grant.role_name, 'editor')
|
|
188
|
+
|
|
189
|
+
async def test_group_entity_is_accepted_as_subject(self):
|
|
190
|
+
scope = await self._create_scope()
|
|
191
|
+
group = self.entity_service.create(Group)
|
|
192
|
+
group.name = 'Engineering'
|
|
193
|
+
with kernel_internal_context():
|
|
194
|
+
group = await self.entity_service.save_entity(group, self.test_principal)
|
|
195
|
+
self.entities_created.append(group.entity_id)
|
|
196
|
+
|
|
197
|
+
req = self.create_strategy_request(
|
|
198
|
+
'CreateGrant', scope.entity_id,
|
|
199
|
+
{'subject_id': group.entity_id, 'role_name': 'editor'},
|
|
200
|
+
)
|
|
201
|
+
result = await self.execute_strategy(req)
|
|
202
|
+
self.assert_strategy_success(result)
|
|
203
|
+
|
|
204
|
+
async def test_unregistered_role_is_rejected(self):
|
|
205
|
+
scope = await self._create_scope()
|
|
206
|
+
user = await self._create_user()
|
|
207
|
+
req = self.create_strategy_request(
|
|
208
|
+
'CreateGrant', scope.entity_id,
|
|
209
|
+
{'subject_id': user.entity_id, 'role_name': 'not-a-role'},
|
|
210
|
+
)
|
|
211
|
+
with self.assertRaises(InvalidRequest):
|
|
212
|
+
await self.execute_strategy(req)
|
|
213
|
+
|
|
214
|
+
async def test_non_shareable_resource_is_rejected(self):
|
|
215
|
+
non_shareable = self.entity_service.create(Entity)
|
|
216
|
+
with kernel_internal_context():
|
|
217
|
+
non_shareable = await self.entity_service.save_entity(non_shareable, self.test_principal)
|
|
218
|
+
self.entities_created.append(non_shareable.entity_id)
|
|
219
|
+
user = await self._create_user()
|
|
220
|
+
|
|
221
|
+
req = self.create_strategy_request(
|
|
222
|
+
'CreateGrant', non_shareable.entity_id,
|
|
223
|
+
{'subject_id': user.entity_id, 'role_name': 'viewer'},
|
|
224
|
+
)
|
|
225
|
+
with self.assertRaises(InvalidRequest):
|
|
226
|
+
await self.execute_strategy(req)
|
|
227
|
+
|
|
228
|
+
async def test_one_grant_per_resource_subject_pair_after_regrant(self):
|
|
229
|
+
scope = await self._create_scope()
|
|
230
|
+
user = await self._create_user()
|
|
231
|
+
|
|
232
|
+
for role_name in ('viewer', 'editor', 'admin'):
|
|
233
|
+
req = self.create_strategy_request(
|
|
234
|
+
'CreateGrant', scope.entity_id,
|
|
235
|
+
{'subject_id': user.entity_id, 'role_name': role_name},
|
|
236
|
+
)
|
|
237
|
+
await self.execute_strategy(req)
|
|
238
|
+
|
|
239
|
+
refreshed_scope = await self.entity_service.get_entity(scope.entity_id, self.test_principal)
|
|
240
|
+
grant_children_for_subject = 0
|
|
241
|
+
for child_id in refreshed_scope.children_ids:
|
|
242
|
+
child = await self.entity_service.get_entity(child_id, self.test_principal)
|
|
243
|
+
if isinstance(child, Grant) and child.subject_id == user.entity_id:
|
|
244
|
+
grant_children_for_subject += 1
|
|
245
|
+
self.assertEqual(grant_children_for_subject, 1)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Tests for GroupStrategies (AddMember, RemoveMember, ModifyGroup)."""
|
|
2
|
+
from graphcore.domain.entity.group.Group import Group
|
|
3
|
+
from graphcore.domain.strategy.tests.BaseStrategyTest import BaseStrategyTest
|
|
4
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
5
|
+
from graphcore.errors import StrategyFailed
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class GroupStrategiesTest(BaseStrategyTest):
|
|
9
|
+
async def _create_group(self, name='Engineering'):
|
|
10
|
+
group = self.entity_service.create(Group)
|
|
11
|
+
group.name = name
|
|
12
|
+
group.owner = self.TEST_USER_ENTITY_ID
|
|
13
|
+
with kernel_internal_context():
|
|
14
|
+
group = await self.entity_service.save_entity(group, self.test_principal)
|
|
15
|
+
self.entities_created.append(group.entity_id)
|
|
16
|
+
return group
|
|
17
|
+
|
|
18
|
+
async def test_add_group_member_populates_member_ids(self):
|
|
19
|
+
group = await self._create_group()
|
|
20
|
+
new_member = 'new-member-id'
|
|
21
|
+
|
|
22
|
+
req = self.create_strategy_request(
|
|
23
|
+
'AddMember', group.entity_id, {'principal_entity_id': new_member},
|
|
24
|
+
)
|
|
25
|
+
result = await self.execute_strategy(req)
|
|
26
|
+
self.assert_strategy_success(result)
|
|
27
|
+
|
|
28
|
+
refreshed = await self.entity_service.get_entity(group.entity_id, self.test_principal)
|
|
29
|
+
self.assertIn(new_member, refreshed.member_ids)
|
|
30
|
+
|
|
31
|
+
async def test_remove_group_member_removes_from_roster(self):
|
|
32
|
+
group = await self._create_group()
|
|
33
|
+
member = 'member-to-remove'
|
|
34
|
+
group.add_member(member)
|
|
35
|
+
with kernel_internal_context():
|
|
36
|
+
group = await self.entity_service.save_entity(group, self.test_principal)
|
|
37
|
+
|
|
38
|
+
req = self.create_strategy_request(
|
|
39
|
+
'RemoveMember', group.entity_id, {'principal_entity_id': member},
|
|
40
|
+
)
|
|
41
|
+
result = await self.execute_strategy(req)
|
|
42
|
+
self.assert_strategy_success(result)
|
|
43
|
+
|
|
44
|
+
refreshed = await self.entity_service.get_entity(group.entity_id, self.test_principal)
|
|
45
|
+
self.assertNotIn(member, refreshed.member_ids)
|
|
46
|
+
|
|
47
|
+
async def test_rename_group_updates_name(self):
|
|
48
|
+
group = await self._create_group(name='Old Name')
|
|
49
|
+
|
|
50
|
+
req = self.create_strategy_request(
|
|
51
|
+
'ModifyGroup', group.entity_id, {'name': 'New Name'},
|
|
52
|
+
)
|
|
53
|
+
result = await self.execute_strategy(req)
|
|
54
|
+
self.assert_strategy_success(result)
|
|
55
|
+
|
|
56
|
+
refreshed = await self.entity_service.get_entity(group.entity_id, self.test_principal)
|
|
57
|
+
self.assertEqual(refreshed.name, 'New Name')
|
|
58
|
+
|
|
59
|
+
async def test_add_group_member_rejects_non_group_entity(self):
|
|
60
|
+
from graphcore.domain.entity.Entity import Entity
|
|
61
|
+
|
|
62
|
+
plain = self.entity_service.create(Entity)
|
|
63
|
+
with kernel_internal_context():
|
|
64
|
+
plain = await self.entity_service.save_entity(plain, self.test_principal)
|
|
65
|
+
self.entities_created.append(plain.entity_id)
|
|
66
|
+
|
|
67
|
+
req = self.create_strategy_request(
|
|
68
|
+
'AddMember', plain.entity_id, {'principal_entity_id': 'someone'},
|
|
69
|
+
)
|
|
70
|
+
with self.assertRaises(StrategyFailed):
|
|
71
|
+
await self.execute_strategy(req)
|
|
72
|
+
|
|
73
|
+
async def test_roster_is_not_reachable_through_the_attribute_bag(self):
|
|
74
|
+
"""The two authoring tiers are separate channels. `SetAttributes`
|
|
75
|
+
*is* on Group's allowlist, but `member_ids` and `name` are typed
|
|
76
|
+
columns, not attributes - so a generic attribute write is accepted and
|
|
77
|
+
still cannot touch the roster or the group's identity."""
|
|
78
|
+
group = await self._create_group()
|
|
79
|
+
group.add_member('real-member')
|
|
80
|
+
with kernel_internal_context():
|
|
81
|
+
await self.entity_service.save_entity(group, self.test_principal)
|
|
82
|
+
|
|
83
|
+
req = self.create_strategy_request(
|
|
84
|
+
'SetAttributes', group.entity_id,
|
|
85
|
+
{'attribute_map': {'member_ids': ['smuggled-in'], 'name': 'renamed'}},
|
|
86
|
+
)
|
|
87
|
+
result = await self.execute_strategy(req)
|
|
88
|
+
self.assert_strategy_success(result)
|
|
89
|
+
|
|
90
|
+
stored = await self.entity_service.repository.load(group.entity_id)
|
|
91
|
+
self.assertEqual(stored.member_ids, ['real-member'])
|
|
92
|
+
self.assertEqual(stored.name, 'Engineering')
|
|
93
|
+
|
|
94
|
+
refreshed = await self.entity_service.get_entity(group.entity_id, self.test_principal)
|
|
95
|
+
self.assertEqual(refreshed.member_ids, ['real-member'])
|
|
96
|
+
self.assertEqual(refreshed.to_client()['member_ids'], ['real-member'])
|
|
97
|
+
self.assertEqual(refreshed.to_client()['name'], 'Engineering')
|
|
98
|
+
|
|
99
|
+
self.assertEqual(refreshed.to_client()['attributes'], {'member_ids': ['smuggled-in'], 'name': 'renamed'})
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"""`StrategyRunner` and `ChangeTracker` against an in-memory session: the
|
|
2
|
+
runner's five steps without a kernel."""
|
|
3
|
+
import copy
|
|
4
|
+
|
|
5
|
+
from asgiref.sync import async_to_sync
|
|
6
|
+
from django.test import SimpleTestCase
|
|
7
|
+
|
|
8
|
+
from graphcore.domain.entity.Entity import Entity
|
|
9
|
+
from graphcore.domain.entity.strategy_request.StrategyRequest import StrategyRequest
|
|
10
|
+
from graphcore.domain.strategy.BaseStrategy import Strategy
|
|
11
|
+
from graphcore.domain.strategy.runner import ChangeTracker, StrategyRunner
|
|
12
|
+
from graphcore.domain.strategy.session import StrategySession
|
|
13
|
+
from graphcore.errors import ConcurrentModificationError, InvalidRequest, NotFound, StaleAttempt
|
|
14
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
15
|
+
from graphcore.tests.ids import fixture_id
|
|
16
|
+
from graphcore.types.execution import ExecutionAttempt
|
|
17
|
+
from graphcore.types.identity import Principal
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class FakeSession(StrategySession):
|
|
21
|
+
def __init__(self, tracker, entities, fail_save=None):
|
|
22
|
+
self.principal = Principal(principal_entity_id=fixture_id())
|
|
23
|
+
self.tracker = tracker
|
|
24
|
+
self.entities = entities
|
|
25
|
+
self.fail_save = fail_save
|
|
26
|
+
self.saves = []
|
|
27
|
+
|
|
28
|
+
def create(self, entity_cls, **fields):
|
|
29
|
+
return entity_cls(entity_id=fixture_id())
|
|
30
|
+
|
|
31
|
+
async def get(self, entity_id):
|
|
32
|
+
if entity_id not in self.entities:
|
|
33
|
+
raise NotFound(f"{entity_id} not found")
|
|
34
|
+
entity = copy.deepcopy(self.entities[entity_id])
|
|
35
|
+
self.tracker.record(entity)
|
|
36
|
+
return entity
|
|
37
|
+
|
|
38
|
+
async def get_many(self, entity_ids):
|
|
39
|
+
return {i: await self.get(i) for i in entity_ids if i in self.entities}
|
|
40
|
+
|
|
41
|
+
async def list(self, entity_filter, cursor=None, limit=25):
|
|
42
|
+
raise NotImplementedError
|
|
43
|
+
|
|
44
|
+
async def save(self, entity):
|
|
45
|
+
if self.fail_save:
|
|
46
|
+
raise self.fail_save
|
|
47
|
+
self.saves.append(entity.entity_id)
|
|
48
|
+
entity.version += 1
|
|
49
|
+
self.entities[entity.entity_id] = copy.deepcopy(entity)
|
|
50
|
+
self.tracker.record(entity)
|
|
51
|
+
return entity
|
|
52
|
+
|
|
53
|
+
async def delete_tree(self, root_id):
|
|
54
|
+
raise NotImplementedError
|
|
55
|
+
|
|
56
|
+
async def execute(self, payload, *, wait=True, callback_channel=None):
|
|
57
|
+
raise NotImplementedError
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _strategy(name, apply, read_only=False, schema=None, cleanup=None):
|
|
61
|
+
async def _apply(self):
|
|
62
|
+
return await apply(self)
|
|
63
|
+
|
|
64
|
+
async def _cleanup(self):
|
|
65
|
+
if cleanup:
|
|
66
|
+
await cleanup(self)
|
|
67
|
+
|
|
68
|
+
return type(name, (Strategy,), {
|
|
69
|
+
'apply': _apply, 'cleanup': _cleanup, 'read_only_strategy': read_only,
|
|
70
|
+
'param_schema': schema, 'request_constructor': classmethod(lambda cls, t: None),
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class _Strategies:
|
|
75
|
+
def __init__(self, *classes):
|
|
76
|
+
self.classes = {cls.__name__: cls for cls in classes}
|
|
77
|
+
|
|
78
|
+
def by_name(self, name):
|
|
79
|
+
return self.classes.get(name)
|
|
80
|
+
|
|
81
|
+
def require(self, name):
|
|
82
|
+
if name not in self.classes:
|
|
83
|
+
raise InvalidRequest(f"Strategy {name} is not registered.")
|
|
84
|
+
return self.classes[name]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class _Registry:
|
|
88
|
+
def __init__(self, *classes):
|
|
89
|
+
self.strategies = _Strategies(*classes)
|
|
90
|
+
self.policy = DEFAULT_REGISTRY.policy
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class RunnerTests(SimpleTestCase):
|
|
94
|
+
def run_attempt(self, strategy_cls, param_config=None, target=True, fail_save=None):
|
|
95
|
+
self.target = Entity(entity_id=fixture_id())
|
|
96
|
+
request = StrategyRequest(entity_id=fixture_id())
|
|
97
|
+
request.strategy_name = strategy_cls.__name__
|
|
98
|
+
request.param_config = param_config or {}
|
|
99
|
+
entities = {request.entity_id: request}
|
|
100
|
+
if target:
|
|
101
|
+
entities[self.target.entity_id] = self.target
|
|
102
|
+
tracker = ChangeTracker()
|
|
103
|
+
self.session = FakeSession(tracker, entities, fail_save)
|
|
104
|
+
attempt = ExecutionAttempt(
|
|
105
|
+
request_id=request.entity_id, attempt=1, strategy_name=strategy_cls.__name__,
|
|
106
|
+
target_entity_id=self.target.entity_id, owner_id=self.session.principal.principal_entity_id,
|
|
107
|
+
backend_id='b',
|
|
108
|
+
)
|
|
109
|
+
return async_to_sync(StrategyRunner(_Registry(strategy_cls)).run)(attempt, self.session, tracker)
|
|
110
|
+
|
|
111
|
+
def test_a_changed_target_is_saved_once_and_its_return_value_reported(self):
|
|
112
|
+
async def apply(self):
|
|
113
|
+
self.entity.set_attribute('x', 1)
|
|
114
|
+
self.strategy_request.ret_val['done'] = True
|
|
115
|
+
return self.strategy_request, self.entity
|
|
116
|
+
|
|
117
|
+
result = self.run_attempt(_strategy('Change', apply))
|
|
118
|
+
|
|
119
|
+
self.assertEqual((result.outcome, result.ret_val), ('succeeded', {'done': True}))
|
|
120
|
+
self.assertEqual(self.session.saves, [self.target.entity_id])
|
|
121
|
+
|
|
122
|
+
def test_an_unchanged_or_already_saved_target_is_not_saved_again(self):
|
|
123
|
+
async def unchanged(self):
|
|
124
|
+
return self.strategy_request, self.entity
|
|
125
|
+
|
|
126
|
+
async def saved(self):
|
|
127
|
+
self.entity.set_attribute('x', 1)
|
|
128
|
+
self.entity = await self.save_entity(self.entity)
|
|
129
|
+
return self.strategy_request, self.entity
|
|
130
|
+
|
|
131
|
+
for strategy_cls, saves in ((_strategy('Same', unchanged), 0), (_strategy('Saved', saved), 1)):
|
|
132
|
+
with self.subTest(strategy=strategy_cls.__name__):
|
|
133
|
+
self.assertEqual(self.run_attempt(strategy_cls).outcome, 'succeeded')
|
|
134
|
+
self.assertEqual(len(self.session.saves), saves)
|
|
135
|
+
|
|
136
|
+
def test_a_change_after_a_save_is_saved_by_the_runner(self):
|
|
137
|
+
async def apply(self):
|
|
138
|
+
self.entity.set_attribute('x', 1)
|
|
139
|
+
self.entity = await self.save_entity(self.entity)
|
|
140
|
+
self.entity.set_attribute('x', 2)
|
|
141
|
+
return self.strategy_request, self.entity
|
|
142
|
+
|
|
143
|
+
self.run_attempt(_strategy('Twice', apply))
|
|
144
|
+
|
|
145
|
+
self.assertEqual(self.session.saves, [self.target.entity_id] * 2)
|
|
146
|
+
|
|
147
|
+
def test_an_in_place_mutation_is_detected(self):
|
|
148
|
+
target = Entity(entity_id=fixture_id())
|
|
149
|
+
target.set_attribute('items', [1])
|
|
150
|
+
tracker = ChangeTracker()
|
|
151
|
+
tracker.record(target)
|
|
152
|
+
target.get_attribute('items').append(2)
|
|
153
|
+
self.assertTrue(tracker.is_unsaved(target))
|
|
154
|
+
|
|
155
|
+
def test_a_read_only_strategy_is_never_saved(self):
|
|
156
|
+
async def apply(self):
|
|
157
|
+
self.entity.set_attribute('x', 1)
|
|
158
|
+
return self.strategy_request, self.entity
|
|
159
|
+
|
|
160
|
+
result = self.run_attempt(_strategy('Look', apply, read_only=True))
|
|
161
|
+
|
|
162
|
+
self.assertEqual((result.outcome, self.session.saves), ('succeeded', []))
|
|
163
|
+
|
|
164
|
+
def test_a_conflict_from_apply_or_the_auto_save_is_a_conflict(self):
|
|
165
|
+
async def raises(self):
|
|
166
|
+
raise ConcurrentModificationError('stale')
|
|
167
|
+
|
|
168
|
+
async def changes(self):
|
|
169
|
+
self.entity.set_attribute('x', 1)
|
|
170
|
+
return self.strategy_request, self.entity
|
|
171
|
+
|
|
172
|
+
self.assertEqual(self.run_attempt(_strategy('Raise', raises)).outcome, 'conflict')
|
|
173
|
+
result = self.run_attempt(_strategy('Change', changes), fail_save=ConcurrentModificationError('stale'))
|
|
174
|
+
self.assertEqual(result.outcome, 'conflict')
|
|
175
|
+
|
|
176
|
+
def test_failures_carry_their_message(self):
|
|
177
|
+
async def raises(self):
|
|
178
|
+
raise RuntimeError('boom')
|
|
179
|
+
|
|
180
|
+
async def unused(self):
|
|
181
|
+
return self.strategy_request, self.entity
|
|
182
|
+
|
|
183
|
+
async def changes(self):
|
|
184
|
+
self.entity.set_attribute('x', 1)
|
|
185
|
+
return self.strategy_request, self.entity
|
|
186
|
+
|
|
187
|
+
cases = (
|
|
188
|
+
(_strategy('Boom', raises), {}, True, None, 'boom'),
|
|
189
|
+
(_strategy('Schema', unused, schema={'type': 'object', 'required': ['n']}), {}, True, None,
|
|
190
|
+
'Invalid param_config for Schema'),
|
|
191
|
+
(_strategy('Missing', unused), {}, False, None, 'not found'),
|
|
192
|
+
(_strategy('Fenced', changes), {}, True, StaleAttempt('no longer current'), 'no longer current'),
|
|
193
|
+
)
|
|
194
|
+
for strategy_cls, params, target, fail_save, message in cases:
|
|
195
|
+
with self.subTest(strategy=strategy_cls.__name__):
|
|
196
|
+
result = self.run_attempt(strategy_cls, params, target=target, fail_save=fail_save)
|
|
197
|
+
self.assertEqual(result.outcome, 'failed')
|
|
198
|
+
self.assertIn(message, result.error['error'])
|
|
199
|
+
|
|
200
|
+
def test_an_unregistered_strategy_fails(self):
|
|
201
|
+
result = async_to_sync(StrategyRunner(_Registry()).run)(
|
|
202
|
+
ExecutionAttempt(fixture_id(), 1, 'Nope', None, fixture_id(), 'b'),
|
|
203
|
+
FakeSession(ChangeTracker(), {}), ChangeTracker(),
|
|
204
|
+
)
|
|
205
|
+
self.assertEqual(result.outcome, 'failed')
|
|
206
|
+
|
|
207
|
+
def test_cleanup_runs_and_its_error_is_swallowed(self):
|
|
208
|
+
cleaned = []
|
|
209
|
+
|
|
210
|
+
async def apply(self):
|
|
211
|
+
raise InvalidRequest('bad')
|
|
212
|
+
|
|
213
|
+
async def cleanup(self):
|
|
214
|
+
cleaned.append(True)
|
|
215
|
+
raise RuntimeError('cleanup broke')
|
|
216
|
+
|
|
217
|
+
result = self.run_attempt(_strategy('Clean', apply, cleanup=cleanup))
|
|
218
|
+
|
|
219
|
+
self.assertEqual((result.outcome, result.error, cleaned),
|
|
220
|
+
('failed', {'code': 'invalid_request', 'error': 'bad'}, [True]))
|