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,733 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from urllib.parse import quote
|
|
3
|
+
from unittest.mock import AsyncMock, patch
|
|
4
|
+
|
|
5
|
+
from django.test import RequestFactory, SimpleTestCase, TestCase, override_settings
|
|
6
|
+
from django.urls import resolve
|
|
7
|
+
|
|
8
|
+
from graphcore.tests.ids import fixture_id
|
|
9
|
+
from graphcore.types.entities import EntityPage, Tombstone, client_view, view_etag
|
|
10
|
+
from graphcore.types.identity import Principal
|
|
11
|
+
from graphcore.types.entity_id import new_entity_id
|
|
12
|
+
from graphcore.transport import views
|
|
13
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
14
|
+
from graphcore.runtime import django_runtime, get_runtime
|
|
15
|
+
from graphcore.errors import Forbidden, NotFound
|
|
16
|
+
from graphcore.execution.tests.support import ExecutionTestCase
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _wire(view: dict) -> dict:
|
|
20
|
+
return {**view, 'etag': view_etag(view)}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class MockRequest:
|
|
24
|
+
"""Simple request object for direct async view testing."""
|
|
25
|
+
|
|
26
|
+
scheme = 'http'
|
|
27
|
+
|
|
28
|
+
def __init__(self, method='POST', body=None, headers=None):
|
|
29
|
+
self.method = method
|
|
30
|
+
self.body = body or b'{}'
|
|
31
|
+
self.headers = headers or {}
|
|
32
|
+
|
|
33
|
+
def get_host(self):
|
|
34
|
+
return 'testserver'
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class FakeEntity:
|
|
38
|
+
def __init__(self, entity_id, writable=True):
|
|
39
|
+
self.entity_id = entity_id
|
|
40
|
+
self._writable = writable
|
|
41
|
+
|
|
42
|
+
def to_client(self):
|
|
43
|
+
return {'entity_id': self.entity_id}
|
|
44
|
+
|
|
45
|
+
def can_write(self, _principal_entity_id, user_group_ids=frozenset()):
|
|
46
|
+
return self._writable
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class TestApiViews(TestCase):
|
|
50
|
+
TEST_USER_ENTITY_ID = 'test-user-entity-id'
|
|
51
|
+
|
|
52
|
+
async def test_real_entities_keep_the_nested_wire_shape_on_every_read_endpoint(self):
|
|
53
|
+
from graphcore.domain.entity.user.User import User
|
|
54
|
+
|
|
55
|
+
entity = User(fixture_id(), username='Typed user', issuer='private', subject='private')
|
|
56
|
+
entity.set_attribute('username', 'Bag user')
|
|
57
|
+
entity.set_attribute('data', {'nested': [1, None]})
|
|
58
|
+
api = get_runtime().graph_api
|
|
59
|
+
page = EntityPage(entities=[entity], next_cursor=None)
|
|
60
|
+
factory = RequestFactory()
|
|
61
|
+
with patch('graphcore.transport.views._principal', new=AsyncMock(return_value=Principal(self.TEST_USER_ENTITY_ID))), \
|
|
62
|
+
patch.object(api, 'get_entity', new=AsyncMock(return_value=entity)), \
|
|
63
|
+
patch.object(api, 'get_entities', new=AsyncMock(return_value={entity.entity_id: entity})), \
|
|
64
|
+
patch.object(api, 'list_entities', new=AsyncMock(return_value=page)), \
|
|
65
|
+
patch.object(api, 'get_referrers', new=AsyncMock(return_value=page)):
|
|
66
|
+
single = await views.api_get_entity(factory.get('/'), entity.entity_id)
|
|
67
|
+
batch = await views.api_get_entities(factory.post('/', {'entity_ids': [entity.entity_id]}, content_type='application/json'))
|
|
68
|
+
listing = await views.api_list_entities(factory.post('/', {'type_ids': ['graphcore.User']}, content_type='application/json'))
|
|
69
|
+
referrers = await views.api_get_referrers(factory.get('/'), entity.entity_id)
|
|
70
|
+
for response in (single, batch, listing, referrers):
|
|
71
|
+
self.assertEqual(response.status_code, 200)
|
|
72
|
+
records = [json.loads(single.content), json.loads(batch.content)['entities'][entity.entity_id],
|
|
73
|
+
json.loads(listing.content)['entities'][0], json.loads(referrers.content)['entities'][0]]
|
|
74
|
+
for record in records:
|
|
75
|
+
self.assertEqual(record, client_view(entity))
|
|
76
|
+
self.assertEqual(record['username'], 'Typed user')
|
|
77
|
+
self.assertEqual(record['attributes']['username'], 'Bag user')
|
|
78
|
+
for key in ('entity_type', 'is_template', 'issuer', 'subject'):
|
|
79
|
+
self.assertNotIn(key, record)
|
|
80
|
+
|
|
81
|
+
async def test_get_entities_requires_auth(self):
|
|
82
|
+
request = MockRequest(
|
|
83
|
+
method='POST',
|
|
84
|
+
body=json.dumps({'entity_ids': ['a']}).encode(),
|
|
85
|
+
headers={}
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
response = await views.api_get_entities(request)
|
|
89
|
+
data = json.loads(response.content)
|
|
90
|
+
|
|
91
|
+
self.assertEqual(response.status_code, 401)
|
|
92
|
+
self.assertEqual(data['error'], 'Authentication required')
|
|
93
|
+
|
|
94
|
+
async def test_get_entities_returns_entities_and_errors(self):
|
|
95
|
+
request = MockRequest(
|
|
96
|
+
method='POST',
|
|
97
|
+
body=json.dumps({'entity_ids': ['ok-1', 'missing-1', 'forbidden-1']}).encode()
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
101
|
+
with patch.object(get_runtime().graph_api, 'entity_service') as entity_service:
|
|
102
|
+
entity_service.get_entities = AsyncMock(return_value={
|
|
103
|
+
'ok-1': FakeEntity('ok-1')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
response = await views.api_get_entities(request)
|
|
107
|
+
|
|
108
|
+
data = json.loads(response.content)
|
|
109
|
+
self.assertEqual(response.status_code, 200)
|
|
110
|
+
self.assertIn('ok-1', data['entities'])
|
|
111
|
+
self.assertEqual(data['entities']['ok-1']['entity_id'], 'ok-1')
|
|
112
|
+
self.assertEqual(len(data['errors']), 2)
|
|
113
|
+
self.assertTrue(all(e['status'] == 404 for e in data['errors']))
|
|
114
|
+
|
|
115
|
+
async def test_get_entities_reports_each_tombstones_cause(self):
|
|
116
|
+
request = MockRequest(
|
|
117
|
+
method='POST',
|
|
118
|
+
body=json.dumps({'entity_ids': ['local-1', 'gc://other/x']}).encode(),
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
122
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
123
|
+
with patch.object(get_runtime().graph_api, 'get_entities', new=AsyncMock(return_value={
|
|
124
|
+
'local-1': FakeEntity('local-1'),
|
|
125
|
+
'gc://other/x': Tombstone('gc://other/x', 'unreachable'),
|
|
126
|
+
})):
|
|
127
|
+
response = await views.api_get_entities(request)
|
|
128
|
+
|
|
129
|
+
data = json.loads(response.content)
|
|
130
|
+
self.assertEqual(list(data['entities']), ['local-1'])
|
|
131
|
+
self.assertEqual(data['errors'], [
|
|
132
|
+
{'entity_id': 'gc://other/x', 'error': 'Entity not found or access denied',
|
|
133
|
+
'cause': 'unreachable', 'status': 502},
|
|
134
|
+
])
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class ExecuteStrategyRoundTripTests(ExecutionTestCase):
|
|
138
|
+
"""The execute endpoint through the real executor, in every environment."""
|
|
139
|
+
|
|
140
|
+
def setUp(self):
|
|
141
|
+
super().setUp()
|
|
142
|
+
patcher = patch('graphcore.transport.views.get_runtime', return_value=self.runtime)
|
|
143
|
+
patcher.start()
|
|
144
|
+
self.addCleanup(patcher.stop)
|
|
145
|
+
|
|
146
|
+
async def _post(self, principal, strategies, await_response):
|
|
147
|
+
request = MockRequest(body=json.dumps({'await_response': await_response, 'strategy': strategies}).encode())
|
|
148
|
+
with patch('graphcore.transport.views._principal', new=AsyncMock(return_value=principal)):
|
|
149
|
+
response = await views.api_execute_strategy_req(request)
|
|
150
|
+
return response.status_code, json.loads(response.content)
|
|
151
|
+
|
|
152
|
+
def _strategy(self, name, **extra):
|
|
153
|
+
return {'strategy_name': name, 'target_entity_id': self.probe.entity_id, 'param_config': {}, **extra}
|
|
154
|
+
|
|
155
|
+
async def test_an_awaited_request_returns_its_terminal_outcome(self):
|
|
156
|
+
await self.seed()
|
|
157
|
+
status, data = await self._post(self.alice, [
|
|
158
|
+
self._strategy('Increment', client_request_id='c-io'),
|
|
159
|
+
self._strategy('CpuIncrement', client_request_id='c-cpu'),
|
|
160
|
+
self._strategy('Fail'),
|
|
161
|
+
], True)
|
|
162
|
+
self.assertEqual(status, 200)
|
|
163
|
+
io, cpu, failed = data['results']
|
|
164
|
+
self.assertEqual((io['status'], io['ret_val'], io['client_request_id']), ('succeeded', {'counter': 1}, 'c-io'))
|
|
165
|
+
self.assertEqual((cpu['status'], cpu['ret_val'], cpu['client_request_id']), ('succeeded', {'counter': 2}, 'c-cpu'))
|
|
166
|
+
self.assertEqual((failed['status'], failed['error']), ('failed', 'boom'))
|
|
167
|
+
self.assertEqual((failed['error_code'], failed['error_details']), ('strategy_failed', {}))
|
|
168
|
+
self.assertTrue(all(r['strategy_request_id'] for r in data['results']))
|
|
169
|
+
self.assertNotIn('error_code', io)
|
|
170
|
+
|
|
171
|
+
async def test_a_refused_run_returns_its_error_code_and_details(self):
|
|
172
|
+
await self.seed()
|
|
173
|
+
status, data = await self._post(self.alice, [self._strategy('Refuse'), self._strategy('CpuRefuse')], True)
|
|
174
|
+
self.assertEqual(status, 200)
|
|
175
|
+
for outcome in data['results']:
|
|
176
|
+
self.assertEqual((outcome['status'], outcome['error'], outcome['error_code']), ('failed', 'stale', 'conflict'))
|
|
177
|
+
self.assertEqual(outcome['error_details'], {'invitation': {'answers': {'dinner': 'accepted'}}})
|
|
178
|
+
self.assertTrue(outcome['strategy_request_id'])
|
|
179
|
+
|
|
180
|
+
async def test_a_fire_and_forget_request_is_acknowledged_then_runs(self):
|
|
181
|
+
await self.seed()
|
|
182
|
+
status, data = await self._post(self.alice, [self._strategy('Increment')], False)
|
|
183
|
+
[outcome] = data['results']
|
|
184
|
+
self.assertEqual(status, 200)
|
|
185
|
+
self.assertFalse(data['await_response'])
|
|
186
|
+
self.assertIn(outcome['status'], ('pending', 'running'))
|
|
187
|
+
await self.settle()
|
|
188
|
+
self.assertEqual((await self.load(outcome['strategy_request_id'])).status, 'succeeded')
|
|
189
|
+
|
|
190
|
+
async def test_a_refused_request_is_a_failed_outcome_and_is_not_saved(self):
|
|
191
|
+
await self.seed()
|
|
192
|
+
status, data = await self._post(self.bob, [self._strategy('Increment'), self._strategy('Nope')], True)
|
|
193
|
+
self.assertEqual(status, 200)
|
|
194
|
+
forbidden, unknown = data['results']
|
|
195
|
+
self.assertEqual(forbidden['status'], 'failed')
|
|
196
|
+
self.assertIn('not allowed', forbidden['error'])
|
|
197
|
+
self.assertIn('not registered', unknown['error'])
|
|
198
|
+
self.assertEqual((forbidden['error_code'], unknown['error_code']), ('forbidden', 'invalid_request'))
|
|
199
|
+
self.assertNotIn('strategy_request_id', forbidden)
|
|
200
|
+
self.assertEqual(await self.requests(), [])
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class TestApiAccessView(TestCase):
|
|
204
|
+
TEST_USER_ENTITY_ID = 'test-user-entity-id'
|
|
205
|
+
|
|
206
|
+
async def test_get_access_requires_auth(self):
|
|
207
|
+
request = MockRequest(method='GET', headers={})
|
|
208
|
+
response = await views.api_get_access(request, 'resource-1')
|
|
209
|
+
self.assertEqual(response.status_code, 401)
|
|
210
|
+
|
|
211
|
+
async def test_get_access_returns_summary_dict(self):
|
|
212
|
+
from graphcore.types.api import AccessSummary
|
|
213
|
+
from graphcore.types.access import GrantInfo
|
|
214
|
+
from graphcore.auth.authz.defaults import ADMIN
|
|
215
|
+
|
|
216
|
+
request = MockRequest(method='GET')
|
|
217
|
+
summary = AccessSummary(
|
|
218
|
+
resource_id='resource-1',
|
|
219
|
+
can_manage=True,
|
|
220
|
+
grants=[GrantInfo(
|
|
221
|
+
subject_id='owner-1', role=ADMIN, provenance='owner', resource_id='resource-1',
|
|
222
|
+
)],
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
with patch(
|
|
226
|
+
'graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
227
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID),
|
|
228
|
+
):
|
|
229
|
+
with patch.object(get_runtime().graph_api, 'get_access', AsyncMock(return_value=summary)):
|
|
230
|
+
response = await views.api_get_access(request, 'resource-1')
|
|
231
|
+
|
|
232
|
+
data = json.loads(response.content)
|
|
233
|
+
self.assertEqual(response.status_code, 200)
|
|
234
|
+
self.assertEqual(data, summary.to_dict())
|
|
235
|
+
|
|
236
|
+
async def test_get_access_forbidden_returns_403(self):
|
|
237
|
+
request = MockRequest(method='GET')
|
|
238
|
+
with patch(
|
|
239
|
+
'graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
240
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID),
|
|
241
|
+
):
|
|
242
|
+
with patch.object(get_runtime().graph_api, 'get_access', AsyncMock(side_effect=Forbidden('no'))):
|
|
243
|
+
response = await views.api_get_access(request, 'resource-1')
|
|
244
|
+
|
|
245
|
+
self.assertEqual(response.status_code, 403)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class TestApiInviteViews(TestCase):
|
|
249
|
+
PRINCIPAL = Principal(principal_entity_id='test-user-entity-id')
|
|
250
|
+
|
|
251
|
+
def _as_principal(self):
|
|
252
|
+
return patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=self.PRINCIPAL)
|
|
253
|
+
|
|
254
|
+
async def test_both_require_auth(self):
|
|
255
|
+
for view in (views.api_create_invite, views.api_redeem_invite):
|
|
256
|
+
with self.subTest(view=view.__name__):
|
|
257
|
+
response = await view(MockRequest(headers={}))
|
|
258
|
+
self.assertEqual(response.status_code, 401)
|
|
259
|
+
|
|
260
|
+
async def test_create_returns_the_token(self):
|
|
261
|
+
body = json.dumps({'resource_id': 'r-1', 'role_name': 'editor', 'max_uses': 2}).encode()
|
|
262
|
+
create = AsyncMock(return_value=('inv-1', 'inv-1.secret'))
|
|
263
|
+
with self._as_principal(), patch.object(get_runtime().graph_api, 'create_invite', create):
|
|
264
|
+
response = await views.api_create_invite(MockRequest(body=body))
|
|
265
|
+
|
|
266
|
+
self.assertEqual(response.status_code, 200)
|
|
267
|
+
self.assertEqual(json.loads(response.content), {'invite_entity_id': 'inv-1', 'token': 'inv-1.secret'})
|
|
268
|
+
create.assert_awaited_once_with(self.PRINCIPAL, 'r-1', 'editor', max_uses=2)
|
|
269
|
+
|
|
270
|
+
async def test_create_without_manage_access_is_403(self):
|
|
271
|
+
body = json.dumps({'resource_id': 'r-1', 'role_name': 'editor'}).encode()
|
|
272
|
+
with self._as_principal(), patch.object(
|
|
273
|
+
get_runtime().graph_api, 'create_invite', AsyncMock(side_effect=Forbidden('no')),
|
|
274
|
+
):
|
|
275
|
+
response = await views.api_create_invite(MockRequest(body=body))
|
|
276
|
+
self.assertEqual(response.status_code, 403)
|
|
277
|
+
|
|
278
|
+
async def test_redeem_returns_the_resource_and_refuses_with_404(self):
|
|
279
|
+
body = json.dumps({'token': 'inv-1.secret'}).encode()
|
|
280
|
+
redeemed = {'resource_id': 'r-1', 'entity_type_id': 'app.Wedding'}
|
|
281
|
+
with self._as_principal(), patch.object(
|
|
282
|
+
get_runtime().graph_api, 'redeem_invite', AsyncMock(return_value=redeemed),
|
|
283
|
+
):
|
|
284
|
+
response = await views.api_redeem_invite(MockRequest(body=body))
|
|
285
|
+
self.assertEqual(json.loads(response.content), redeemed)
|
|
286
|
+
|
|
287
|
+
with self._as_principal(), patch.object(
|
|
288
|
+
get_runtime().graph_api, 'redeem_invite', AsyncMock(side_effect=NotFound('no')),
|
|
289
|
+
):
|
|
290
|
+
response = await views.api_redeem_invite(MockRequest(body=body))
|
|
291
|
+
self.assertEqual(response.status_code, 404)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
class TestApiListEntitiesView(TestCase):
|
|
295
|
+
TEST_USER_ENTITY_ID = 'test-user-entity-id'
|
|
296
|
+
|
|
297
|
+
async def test_list_entities_requires_auth(self):
|
|
298
|
+
request = MockRequest(method='POST', body=json.dumps({'type_ids': ['graphcore.User']}).encode())
|
|
299
|
+
response = await views.api_list_entities(request)
|
|
300
|
+
self.assertEqual(response.status_code, 401)
|
|
301
|
+
|
|
302
|
+
async def test_list_entities_empty_type_ids_returns_400(self):
|
|
303
|
+
request = MockRequest(method='POST', body=json.dumps({'type_ids': []}).encode())
|
|
304
|
+
with patch(
|
|
305
|
+
'graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
306
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID),
|
|
307
|
+
):
|
|
308
|
+
response = await views.api_list_entities(request)
|
|
309
|
+
self.assertEqual(response.status_code, 400)
|
|
310
|
+
|
|
311
|
+
async def test_list_entities_returns_shape(self):
|
|
312
|
+
from graphcore.types.entities import EntityPage, Tombstone
|
|
313
|
+
|
|
314
|
+
request = MockRequest(method='POST', body=json.dumps({'type_ids': ['graphcore.User']}).encode())
|
|
315
|
+
page = EntityPage(entities=[FakeEntity('user-1')], next_cursor='user-1')
|
|
316
|
+
|
|
317
|
+
with patch(
|
|
318
|
+
'graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
319
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID),
|
|
320
|
+
):
|
|
321
|
+
with patch.object(get_runtime().graph_api, 'list_entities', AsyncMock(return_value=page)):
|
|
322
|
+
response = await views.api_list_entities(request)
|
|
323
|
+
|
|
324
|
+
data = json.loads(response.content)
|
|
325
|
+
self.assertEqual(response.status_code, 200)
|
|
326
|
+
self.assertEqual(data, {'entities': [_wire({'entity_id': 'user-1'})], 'next_cursor': 'user-1'})
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
class TestApiListEntitiesWhere(TestCase):
|
|
330
|
+
TEST_USER_ENTITY_ID = 'test-user-entity-id'
|
|
331
|
+
|
|
332
|
+
async def _list(self, body):
|
|
333
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
334
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
335
|
+
return await views.api_list_entities(
|
|
336
|
+
MockRequest(method='POST', body=json.dumps(body).encode()),
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
async def test_where_on_a_typed_column_filters(self):
|
|
340
|
+
from graphcore.domain.entity.user.User import User
|
|
341
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
342
|
+
|
|
343
|
+
service = get_runtime().entity_service
|
|
344
|
+
with kernel_internal_context():
|
|
345
|
+
for username in ('where-alice', 'where-bob'):
|
|
346
|
+
await service.save_entity(service.create(User, username=username), None)
|
|
347
|
+
|
|
348
|
+
response = await self._list({
|
|
349
|
+
'type_ids': ['graphcore.User'],
|
|
350
|
+
'where': {'equal': {'field': 'username', 'value': 'where-alice'}},
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
self.assertEqual(response.status_code, 200)
|
|
354
|
+
self.assertEqual(
|
|
355
|
+
[e['username'] for e in json.loads(response.content)['entities']], ['where-alice'],
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
async def test_where_on_an_unknown_field_returns_400(self):
|
|
359
|
+
response = await self._list({
|
|
360
|
+
'type_ids': ['graphcore.User'],
|
|
361
|
+
'where': {'equal': {'field': 'no_such_field', 'value': 'x'}},
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
self.assertEqual(response.status_code, 400)
|
|
365
|
+
|
|
366
|
+
async def test_a_malformed_where_returns_400(self):
|
|
367
|
+
response = await self._list({
|
|
368
|
+
'type_ids': ['graphcore.User'], 'where': {'bogus': {}},
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
self.assertEqual(response.status_code, 400)
|
|
372
|
+
|
|
373
|
+
async def test_granted_to_me_reaches_the_filter(self):
|
|
374
|
+
from graphcore.types.entities import EntityPage
|
|
375
|
+
|
|
376
|
+
listing = AsyncMock(return_value=EntityPage(entities=[], next_cursor=None))
|
|
377
|
+
with patch.object(get_runtime().graph_api, 'list_entities', listing):
|
|
378
|
+
response = await self._list({'type_ids': ['graphcore.User'], 'granted_to_me': True})
|
|
379
|
+
|
|
380
|
+
self.assertEqual(response.status_code, 200)
|
|
381
|
+
self.assertTrue(listing.call_args.args[1].granted_to_me)
|
|
382
|
+
|
|
383
|
+
async def test_granted_to_me_must_be_a_boolean(self):
|
|
384
|
+
response = await self._list({'type_ids': ['graphcore.User'], 'granted_to_me': 'yes'})
|
|
385
|
+
|
|
386
|
+
self.assertEqual(response.status_code, 400)
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
class TestApiDirectoryViews(TestCase):
|
|
390
|
+
"""Directory endpoints previously had no authentication guard at all -
|
|
391
|
+
anyone reaching the host could enumerate every registered entity type
|
|
392
|
+
and strategy. Step B4 removed the `inspect.getsource` leak from the
|
|
393
|
+
strategy directory response; test_strategy_directory_has_no_source_key
|
|
394
|
+
guards against reintroducing it."""
|
|
395
|
+
|
|
396
|
+
TEST_USER_ENTITY_ID = 'test-user-entity-id'
|
|
397
|
+
|
|
398
|
+
async def test_entity_directory_requires_auth_header(self):
|
|
399
|
+
request = MockRequest(method='GET', headers={})
|
|
400
|
+
response = await views.api_get_entity_directory(request)
|
|
401
|
+
self.assertEqual(response.status_code, 401)
|
|
402
|
+
|
|
403
|
+
async def test_entity_directory_rejects_malformed_token(self):
|
|
404
|
+
request = MockRequest(method='GET', headers={'Authorization': 'Bearer not-a-real-token'})
|
|
405
|
+
response = await views.api_get_entity_directory(request)
|
|
406
|
+
self.assertEqual(response.status_code, 401)
|
|
407
|
+
|
|
408
|
+
async def test_entity_directory_returns_200_with_valid_token(self):
|
|
409
|
+
request = MockRequest(method='GET')
|
|
410
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
411
|
+
response = await views.api_get_entity_directory(request)
|
|
412
|
+
self.assertEqual(response.status_code, 200)
|
|
413
|
+
|
|
414
|
+
async def test_strategy_directory_requires_auth_header(self):
|
|
415
|
+
request = MockRequest(method='GET', headers={})
|
|
416
|
+
response = await views.api_get_strategy_directory(request)
|
|
417
|
+
self.assertEqual(response.status_code, 401)
|
|
418
|
+
|
|
419
|
+
async def test_strategy_directory_rejects_malformed_token(self):
|
|
420
|
+
request = MockRequest(method='GET', headers={'Authorization': 'Bearer not-a-real-token'})
|
|
421
|
+
response = await views.api_get_strategy_directory(request)
|
|
422
|
+
self.assertEqual(response.status_code, 401)
|
|
423
|
+
|
|
424
|
+
async def test_strategy_directory_returns_200_with_valid_token(self):
|
|
425
|
+
request = MockRequest(method='GET')
|
|
426
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
427
|
+
response = await views.api_get_strategy_directory(request)
|
|
428
|
+
self.assertEqual(response.status_code, 200)
|
|
429
|
+
|
|
430
|
+
async def test_strategy_directory_has_no_source_key(self):
|
|
431
|
+
"""No response body from this endpoint should contain a `source` key -
|
|
432
|
+
it used to ship full implementation source via inspect.getsource()."""
|
|
433
|
+
request = MockRequest(method='GET')
|
|
434
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
435
|
+
response = await views.api_get_strategy_directory(request)
|
|
436
|
+
|
|
437
|
+
data = json.loads(response.content)
|
|
438
|
+
for strategy_list in data.values():
|
|
439
|
+
for strategy_info in strategy_list:
|
|
440
|
+
self.assertNotIn('source', strategy_info)
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
class TestApiErrorHygiene(TestCase):
|
|
444
|
+
"""Step A12: unexpected exceptions must never leak their text (raw SQL,
|
|
445
|
+
entity ids, stack frames) to the client - just a generic message and a
|
|
446
|
+
correlation id, with the detail logged server-side."""
|
|
447
|
+
|
|
448
|
+
TEST_USER_ENTITY_ID = 'test-user-entity-id'
|
|
449
|
+
|
|
450
|
+
async def test_get_entities_over_limit_returns_400(self):
|
|
451
|
+
entity_ids = [f'id-{i}' for i in range(get_runtime().graph_api.max_batch_size + 1)]
|
|
452
|
+
request = MockRequest(
|
|
453
|
+
method='POST',
|
|
454
|
+
body=json.dumps({'entity_ids': entity_ids}).encode(),
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
458
|
+
response = await views.api_get_entities(request)
|
|
459
|
+
|
|
460
|
+
self.assertEqual(response.status_code, 400)
|
|
461
|
+
|
|
462
|
+
async def test_execute_strategy_req_over_limit_returns_400(self):
|
|
463
|
+
strategy = [{
|
|
464
|
+
'strategy_name': 'SetAttributes',
|
|
465
|
+
'target_entity_id': f'target-{i}',
|
|
466
|
+
'param_config': {},
|
|
467
|
+
} for i in range(get_runtime().graph_api.max_batch_size + 1)]
|
|
468
|
+
request = MockRequest(
|
|
469
|
+
method='POST',
|
|
470
|
+
body=json.dumps({'strategy': strategy}).encode(),
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
474
|
+
response = await views.api_execute_strategy_req(request)
|
|
475
|
+
|
|
476
|
+
self.assertEqual(response.status_code, 400)
|
|
477
|
+
|
|
478
|
+
async def test_get_entity_unexpected_error_returns_generic_message(self):
|
|
479
|
+
request = MockRequest(method='GET')
|
|
480
|
+
|
|
481
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
482
|
+
with patch.object(get_runtime().graph_api, 'entity_service') as entity_service:
|
|
483
|
+
entity_service.get_entity = AsyncMock(side_effect=RuntimeError('SELECT * FROM secrets failed'))
|
|
484
|
+
|
|
485
|
+
response = await views.api_get_entity(request, 'some-id')
|
|
486
|
+
|
|
487
|
+
data = json.loads(response.content)
|
|
488
|
+
self.assertEqual(response.status_code, 500)
|
|
489
|
+
self.assertNotIn('secrets', json.dumps(data))
|
|
490
|
+
self.assertIn('correlation_id', data)
|
|
491
|
+
|
|
492
|
+
async def test_get_entity_permission_error_hides_detail(self):
|
|
493
|
+
request = MockRequest(method='GET')
|
|
494
|
+
|
|
495
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock, return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
496
|
+
with patch.object(get_runtime().graph_api, 'entity_service') as entity_service:
|
|
497
|
+
entity_service.get_entity = AsyncMock(
|
|
498
|
+
side_effect=Forbidden('User test-user-entity-id does not have read access to entity some-id')
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
response = await views.api_get_entity(request, 'some-id')
|
|
502
|
+
|
|
503
|
+
data = json.loads(response.content)
|
|
504
|
+
self.assertEqual(response.status_code, 403)
|
|
505
|
+
self.assertEqual(data['error'], 'Permission denied')
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
@override_settings(ROOT_URLCONF='graphcore.urls')
|
|
509
|
+
class TestIdentityDocumentView(TestCase):
|
|
510
|
+
"""The one place a deployment states where it is reachable. Serves
|
|
511
|
+
without authentication - a backend identity authenticates nothing."""
|
|
512
|
+
|
|
513
|
+
def get(self):
|
|
514
|
+
return self.client.get('/.well-known/graphcore/')
|
|
515
|
+
|
|
516
|
+
def test_serves_the_stores_identity_without_auth(self):
|
|
517
|
+
response = self.get()
|
|
518
|
+
|
|
519
|
+
self.assertEqual(response.status_code, 200)
|
|
520
|
+
self.assertEqual(
|
|
521
|
+
json.loads(response.content)['backend_id'],
|
|
522
|
+
django_runtime(DEFAULT_REGISTRY).repository.backend_id(),
|
|
523
|
+
)
|
|
524
|
+
|
|
525
|
+
def test_names_the_deployment_entity(self):
|
|
526
|
+
self.assertEqual(
|
|
527
|
+
json.loads(self.get().content)['deployment_entity_id'],
|
|
528
|
+
django_runtime(DEFAULT_REGISTRY).deployment_entity_id,
|
|
529
|
+
)
|
|
530
|
+
|
|
531
|
+
def test_endpoint_is_absolute(self):
|
|
532
|
+
self.assertTrue(json.loads(self.get().content)['endpoint'].endswith('/graphcore/'))
|
|
533
|
+
|
|
534
|
+
def test_handle_is_unset_by_default(self):
|
|
535
|
+
self.assertIsNone(json.loads(self.get().content)['handle'])
|
|
536
|
+
|
|
537
|
+
def test_handle_echoes_the_deployments_setting(self):
|
|
538
|
+
with override_settings(GRAPHCORE_HANDLE='devshell'):
|
|
539
|
+
runtime = django_runtime(DEFAULT_REGISTRY)
|
|
540
|
+
with patch('graphcore.transport.views.get_runtime', return_value=runtime):
|
|
541
|
+
response = self.get()
|
|
542
|
+
|
|
543
|
+
self.assertEqual(json.loads(response.content)['handle'], 'devshell')
|
|
544
|
+
|
|
545
|
+
def test_post_is_refused(self):
|
|
546
|
+
self.assertEqual(self.client.post('/.well-known/graphcore/').status_code, 400)
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
class TestApiGetEntityRemoteTombstones(TestCase):
|
|
550
|
+
"""A target this deployment could not reach is a gateway failure, not a
|
|
551
|
+
missing entity - 502 with the cause the gateway reported."""
|
|
552
|
+
|
|
553
|
+
TEST_USER_ENTITY_ID = 'test-user-entity-id'
|
|
554
|
+
|
|
555
|
+
async def _get(self, cause):
|
|
556
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
557
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
558
|
+
with patch.object(get_runtime().graph_api, 'get_entity',
|
|
559
|
+
new=AsyncMock(return_value=Tombstone('gc://other/x', cause))):
|
|
560
|
+
return await views.api_get_entity(MockRequest(method='GET'), 'gc://other/x')
|
|
561
|
+
|
|
562
|
+
async def test_unreachable_returns_502_with_the_cause(self):
|
|
563
|
+
response = await self._get('unreachable')
|
|
564
|
+
|
|
565
|
+
self.assertEqual(response.status_code, 502)
|
|
566
|
+
self.assertEqual(json.loads(response.content)['cause'], 'unreachable')
|
|
567
|
+
|
|
568
|
+
async def test_undelegated_returns_502_with_the_cause(self):
|
|
569
|
+
response = await self._get('undelegated')
|
|
570
|
+
|
|
571
|
+
self.assertEqual(response.status_code, 502)
|
|
572
|
+
self.assertEqual(json.loads(response.content)['cause'], 'undelegated')
|
|
573
|
+
|
|
574
|
+
async def test_missing_is_still_404(self):
|
|
575
|
+
self.assertEqual((await self._get('missing')).status_code, 404)
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
class TestApiReferrersView(TestCase):
|
|
579
|
+
TEST_USER_ENTITY_ID = 'test-user-entity-id'
|
|
580
|
+
|
|
581
|
+
def _request(self, query=None):
|
|
582
|
+
request = MockRequest(method='GET')
|
|
583
|
+
request.GET = query or {}
|
|
584
|
+
return request
|
|
585
|
+
|
|
586
|
+
async def test_referrers_requires_auth(self):
|
|
587
|
+
response = await views.api_get_referrers(self._request(), 'target-1')
|
|
588
|
+
|
|
589
|
+
self.assertEqual(response.status_code, 401)
|
|
590
|
+
|
|
591
|
+
async def test_referrers_returns_a_page(self):
|
|
592
|
+
page = EntityPage(entities=[FakeEntity('source-1')], next_cursor='source-1')
|
|
593
|
+
|
|
594
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
595
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
596
|
+
with patch.object(get_runtime().graph_api, 'get_referrers',
|
|
597
|
+
new=AsyncMock(return_value=page)) as get_referrers:
|
|
598
|
+
response = await views.api_get_referrers(
|
|
599
|
+
self._request({'cursor': 'c1', 'limit': '5'}), 'target-1',
|
|
600
|
+
)
|
|
601
|
+
|
|
602
|
+
data = json.loads(response.content)
|
|
603
|
+
self.assertEqual(response.status_code, 200)
|
|
604
|
+
self.assertEqual(data['entities'], [_wire({'entity_id': 'source-1'})])
|
|
605
|
+
self.assertEqual(data['next_cursor'], 'source-1')
|
|
606
|
+
self.assertEqual(get_referrers.await_args.kwargs, {'where': None, 'cursor': 'c1', 'limit': 5})
|
|
607
|
+
|
|
608
|
+
async def test_referrers_decodes_where_from_the_query_string(self):
|
|
609
|
+
from graphcore.types.query import Equal
|
|
610
|
+
|
|
611
|
+
page = EntityPage(entities=[], next_cursor=None)
|
|
612
|
+
where = {'equal': {'field': 'entity_type_id', 'value': 'graphcore.View'}}
|
|
613
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
614
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
615
|
+
with patch.object(get_runtime().graph_api, 'get_referrers',
|
|
616
|
+
new=AsyncMock(return_value=page)) as get_referrers:
|
|
617
|
+
response = await views.api_get_referrers(
|
|
618
|
+
self._request({'where': json.dumps(where)}), 'target-1',
|
|
619
|
+
)
|
|
620
|
+
|
|
621
|
+
self.assertEqual(response.status_code, 200)
|
|
622
|
+
self.assertEqual(
|
|
623
|
+
get_referrers.await_args.kwargs['where'], Equal('entity_type_id', 'graphcore.View'),
|
|
624
|
+
)
|
|
625
|
+
|
|
626
|
+
async def test_referrers_rejects_a_where_that_is_not_json(self):
|
|
627
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
628
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
629
|
+
response = await views.api_get_referrers(self._request({'where': '{nope'}), 'target-1')
|
|
630
|
+
|
|
631
|
+
self.assertEqual(response.status_code, 400)
|
|
632
|
+
|
|
633
|
+
async def test_referrers_rejects_a_non_numeric_limit(self):
|
|
634
|
+
with patch('graphcore.transport.views._principal', new_callable=AsyncMock,
|
|
635
|
+
return_value=Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)):
|
|
636
|
+
response = await views.api_get_referrers(self._request({'limit': 'lots'}), 'target-1')
|
|
637
|
+
|
|
638
|
+
self.assertEqual(response.status_code, 400)
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
@override_settings(ROOT_URLCONF='graphcore.urls')
|
|
642
|
+
class EntityIdRoutingTests(SimpleTestCase):
|
|
643
|
+
"""An entity identifier carries `/`, so the id-bearing routes take a
|
|
644
|
+
`path` converter and accept the id raw or percent-encoded."""
|
|
645
|
+
|
|
646
|
+
def setUp(self):
|
|
647
|
+
self.entity_id = fixture_id()
|
|
648
|
+
|
|
649
|
+
def assert_routes(self, url, view_name, kwarg, expected):
|
|
650
|
+
match = resolve(url)
|
|
651
|
+
self.assertEqual(match.url_name, view_name)
|
|
652
|
+
self.assertEqual(match.kwargs[kwarg], expected)
|
|
653
|
+
|
|
654
|
+
def test_get_entity_route_carries_the_whole_identifier(self):
|
|
655
|
+
self.assert_routes(
|
|
656
|
+
f'/api/get_entity/{self.entity_id}/', 'api_get_entity', 'entity_id', self.entity_id,
|
|
657
|
+
)
|
|
658
|
+
|
|
659
|
+
def test_get_entity_route_accepts_a_percent_encoded_identifier(self):
|
|
660
|
+
# Clients encode the id; the server hands Django a decoded path, so
|
|
661
|
+
# both spellings reach the same route with the same id.
|
|
662
|
+
request = RequestFactory().get(f'/api/get_entity/{quote(self.entity_id, safe="")}/')
|
|
663
|
+
|
|
664
|
+
self.assert_routes(request.path_info, 'api_get_entity', 'entity_id', self.entity_id)
|
|
665
|
+
|
|
666
|
+
def test_referrers_route_carries_the_whole_identifier(self):
|
|
667
|
+
self.assert_routes(
|
|
668
|
+
f'/api/referrers/{self.entity_id}/', 'api_get_referrers', 'entity_id', self.entity_id,
|
|
669
|
+
)
|
|
670
|
+
|
|
671
|
+
def test_access_route_carries_the_whole_identifier(self):
|
|
672
|
+
self.assert_routes(
|
|
673
|
+
f'/api/access/{self.entity_id}/', 'api_get_access', 'resource_id', self.entity_id,
|
|
674
|
+
)
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
class TestAuthChallengeAndMe(TestCase):
|
|
678
|
+
def setUp(self):
|
|
679
|
+
from graphcore.auth.authn.testing import TestIssuer
|
|
680
|
+
|
|
681
|
+
self.issuer = TestIssuer()
|
|
682
|
+
self.enterContext(self.issuer.trusted())
|
|
683
|
+
|
|
684
|
+
async def test_401_points_at_protected_resource_metadata(self):
|
|
685
|
+
response = await views.api_get_entity_directory(RequestFactory().get('/'))
|
|
686
|
+
self.assertEqual(response.status_code, 401)
|
|
687
|
+
self.assertEqual(
|
|
688
|
+
response['WWW-Authenticate'],
|
|
689
|
+
'Bearer resource_metadata="http://testserver/.well-known/oauth-protected-resource"',
|
|
690
|
+
)
|
|
691
|
+
|
|
692
|
+
@override_settings(GRAPHCORE_PUBLIC_URL='https://app.example/')
|
|
693
|
+
async def test_401_uses_public_url_when_set(self):
|
|
694
|
+
response = await views.api_get_entity_directory(RequestFactory().get('/'))
|
|
695
|
+
self.assertIn('resource_metadata="https://app.example/.well-known/', response['WWW-Authenticate'])
|
|
696
|
+
|
|
697
|
+
async def test_me_returns_the_callers_ids(self):
|
|
698
|
+
token = self.issuer.token('me-sub', preferred_username='me')
|
|
699
|
+
response = await views.api_auth_me(
|
|
700
|
+
RequestFactory().get('/', HTTP_AUTHORIZATION=f'Bearer {token}'),
|
|
701
|
+
)
|
|
702
|
+
self.assertEqual(response.status_code, 200)
|
|
703
|
+
body = json.loads(response.content)
|
|
704
|
+
from graphcore.auth.authn.users import find_user_by_identity
|
|
705
|
+
user = await find_user_by_identity(get_runtime().entity_service, self.issuer.issuer, 'me-sub')
|
|
706
|
+
self.assertEqual(body['principal_entity_id'], user.entity_id)
|
|
707
|
+
self.assertEqual(body['deployment_entity_id'], get_runtime().deployment_entity_id)
|
|
708
|
+
self.assertEqual(body['username'], 'me')
|
|
709
|
+
self.assertEqual(body['issuer'], self.issuer.issuer)
|
|
710
|
+
|
|
711
|
+
async def test_me_names_a_service_account_by_its_client(self):
|
|
712
|
+
from graphcore.auth.authn import service_accounts
|
|
713
|
+
from graphcore.auth.authn.service_accounts import ServiceClient
|
|
714
|
+
|
|
715
|
+
gateway = ServiceClient('wedding', 'gateway', 'wedding-gateway')
|
|
716
|
+
token = self.issuer.token('sa-sub', azp='wedding-gateway')
|
|
717
|
+
with patch.object(service_accounts, 'declared_service_clients', return_value={'wedding-gateway': gateway}):
|
|
718
|
+
response = await views.api_auth_me(
|
|
719
|
+
RequestFactory().get('/', HTTP_AUTHORIZATION=f'Bearer {token}'),
|
|
720
|
+
)
|
|
721
|
+
|
|
722
|
+
body = json.loads(response.content)
|
|
723
|
+
account = await service_accounts.find_service_account(get_runtime().entity_service, 'wedding-gateway')
|
|
724
|
+
self.assertEqual(body['principal_entity_id'], account.entity_id)
|
|
725
|
+
self.assertEqual(body['username'], 'wedding-gateway')
|
|
726
|
+
|
|
727
|
+
async def test_me_requires_a_token(self):
|
|
728
|
+
response = await views.api_auth_me(RequestFactory().get('/'))
|
|
729
|
+
self.assertEqual(response.status_code, 401)
|
|
730
|
+
|
|
731
|
+
@override_settings(ROOT_URLCONF='graphcore.urls')
|
|
732
|
+
def test_me_is_routed(self):
|
|
733
|
+
self.assertEqual(resolve('/api/auth/me/').url_name, 'api_auth_me')
|