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.
Files changed (343) hide show
  1. graphcore/__init__.py +5 -0
  2. graphcore/agent_frontend/__init__.py +8 -0
  3. graphcore/agent_frontend/api.py +69 -0
  4. graphcore/agent_frontend/base/__init__.py +11 -0
  5. graphcore/agent_frontend/base/views.py +167 -0
  6. graphcore/agent_frontend/client.py +31 -0
  7. graphcore/agent_frontend/context.py +157 -0
  8. graphcore/agent_frontend/errors.py +12 -0
  9. graphcore/agent_frontend/loading.py +54 -0
  10. graphcore/agent_frontend/registry.py +42 -0
  11. graphcore/agent_frontend/render.py +207 -0
  12. graphcore/agent_frontend/server.py +103 -0
  13. graphcore/agent_frontend/signatures.py +35 -0
  14. graphcore/agent_frontend/testing.py +45 -0
  15. graphcore/agent_frontend/tests/__init__.py +0 -0
  16. graphcore/agent_frontend/tests/fakes.py +114 -0
  17. graphcore/agent_frontend/tests/test_frontend.py +447 -0
  18. graphcore/agent_frontend/tests/test_loading.py +83 -0
  19. graphcore/agent_frontend/tests/test_server.py +109 -0
  20. graphcore/agent_frontend/tools.py +137 -0
  21. graphcore/api/__init__.py +4 -0
  22. graphcore/api/graph_api.py +378 -0
  23. graphcore/api/predicate_codec.py +81 -0
  24. graphcore/api/tests/__init__.py +0 -0
  25. graphcore/api/tests/test_changed_entities.py +109 -0
  26. graphcore/api/tests/test_graph_api.py +290 -0
  27. graphcore/api/tests/test_graph_api_access.py +94 -0
  28. graphcore/api/tests/test_graph_api_grants.py +90 -0
  29. graphcore/api/tests/test_predicate_codec.py +83 -0
  30. graphcore/api/tests/test_subtree_query.py +79 -0
  31. graphcore/apps.py +71 -0
  32. graphcore/auth/__init__.py +1 -0
  33. graphcore/auth/authn/README.md +178 -0
  34. graphcore/auth/authn/__init__.py +3 -0
  35. graphcore/auth/authn/authenticator.py +120 -0
  36. graphcore/auth/authn/onboarding.py +105 -0
  37. graphcore/auth/authn/principals.py +20 -0
  38. graphcore/auth/authn/resource_metadata.py +43 -0
  39. graphcore/auth/authn/service_accounts.py +96 -0
  40. graphcore/auth/authn/testing.py +96 -0
  41. graphcore/auth/authn/tests/__init__.py +0 -0
  42. graphcore/auth/authn/tests/test_authenticator.py +145 -0
  43. graphcore/auth/authn/tests/test_onboarding.py +145 -0
  44. graphcore/auth/authn/tests/test_service_accounts.py +180 -0
  45. graphcore/auth/authn/users.py +59 -0
  46. graphcore/auth/authz/__init__.py +0 -0
  47. graphcore/auth/authz/access_port.py +120 -0
  48. graphcore/auth/authz/adapter/__init__.py +0 -0
  49. graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
  50. graphcore/auth/authz/adapter/tests/__init__.py +0 -0
  51. graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
  52. graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
  53. graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
  54. graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
  55. graphcore/auth/authz/defaults.py +26 -0
  56. graphcore/auth/authz/kernel_policy.py +191 -0
  57. graphcore/auth/authz/kernel_writes.py +31 -0
  58. graphcore/auth/authz/lookups.py +25 -0
  59. graphcore/auth/authz/policy.py +403 -0
  60. graphcore/auth/authz/tests/__init__.py +0 -0
  61. graphcore/auth/authz/tests/test_can_contain.py +150 -0
  62. graphcore/auth/authz/tests/test_contained_by.py +167 -0
  63. graphcore/auth/authz/tests/test_creation.py +391 -0
  64. graphcore/auth/authz/tests/test_delete_permission.py +82 -0
  65. graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
  66. graphcore/auth/authz/tests/test_integration.py +637 -0
  67. graphcore/auth/authz/tests/test_invites.py +184 -0
  68. graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
  69. graphcore/auth/authz/tests/test_lockout.py +132 -0
  70. graphcore/auth/authz/tests/test_policy.py +329 -0
  71. graphcore/auth/authz/tests/test_primitives.py +73 -0
  72. graphcore/auth/authz/tests/test_principal_creation.py +62 -0
  73. graphcore/bootstrap/__init__.py +0 -0
  74. graphcore/bootstrap/deployment.py +67 -0
  75. graphcore/bootstrap/signals.py +16 -0
  76. graphcore/bootstrap/tests/__init__.py +0 -0
  77. graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
  78. graphcore/broadcast/__init__.py +4 -0
  79. graphcore/broadcast/broadcaster.py +201 -0
  80. graphcore/broadcast/channels.py +56 -0
  81. graphcore/broadcast/tests/__init__.py +0 -0
  82. graphcore/broadcast/tests/test_broadcaster.py +444 -0
  83. graphcore/celery.py +14 -0
  84. graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
  85. graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
  86. graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
  87. graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
  88. graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
  89. graphcore/docs/RUNTIME.md +90 -0
  90. graphcore/docs/SCHEDULING_TDD.md +244 -0
  91. graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
  92. graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
  93. graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
  94. graphcore/docs/__init__.py +0 -0
  95. graphcore/docs/doc_topics.py +97 -0
  96. graphcore/docs/docs_discovery.py +64 -0
  97. graphcore/docs/public_api.py +56 -0
  98. graphcore/domain/__init__.py +0 -0
  99. graphcore/domain/entity/Entity.py +204 -0
  100. graphcore/domain/entity/__init__.py +0 -0
  101. graphcore/domain/entity/deployment/Deployment.py +25 -0
  102. graphcore/domain/entity/deployment/__init__.py +0 -0
  103. graphcore/domain/entity/grant/Grant.py +31 -0
  104. graphcore/domain/entity/grant/__init__.py +0 -0
  105. graphcore/domain/entity/group/Group.py +48 -0
  106. graphcore/domain/entity/group/README.md +88 -0
  107. graphcore/domain/entity/group/__init__.py +0 -0
  108. graphcore/domain/entity/invite/Invite.py +31 -0
  109. graphcore/domain/entity/invite/__init__.py +0 -0
  110. graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
  111. graphcore/domain/entity/request_template/__init__.py +0 -0
  112. graphcore/domain/entity/request_template/tests/__init__.py +0 -0
  113. graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
  114. graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
  115. graphcore/domain/entity/service_account/__init__.py +0 -0
  116. graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
  117. graphcore/domain/entity/strategy_request/__init__.py +0 -0
  118. graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
  119. graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
  120. graphcore/domain/entity/tests/__init__.py +0 -0
  121. graphcore/domain/entity/tests/test_entity.py +227 -0
  122. graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
  123. graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
  124. graphcore/domain/entity/user/User.py +39 -0
  125. graphcore/domain/entity/user/__init__.py +0 -0
  126. graphcore/domain/entity/user/tests/__init__.py +0 -0
  127. graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
  128. graphcore/domain/entity/view/View.py +37 -0
  129. graphcore/domain/entity/view/__init__.py +0 -0
  130. graphcore/domain/entity/view/position.py +78 -0
  131. graphcore/domain/entity/view/tests/__init__.py +0 -0
  132. graphcore/domain/entity/view/tests/test_position.py +71 -0
  133. graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
  134. graphcore/domain/strategy/BaseStrategy.py +759 -0
  135. graphcore/domain/strategy/DeploymentStrategies.py +55 -0
  136. graphcore/domain/strategy/GrantStrategies.py +189 -0
  137. graphcore/domain/strategy/GroupStrategies.py +193 -0
  138. graphcore/domain/strategy/InviteStrategies.py +84 -0
  139. graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
  140. graphcore/domain/strategy/ViewStrategies.py +219 -0
  141. graphcore/domain/strategy/__init__.py +0 -0
  142. graphcore/domain/strategy/runner.py +84 -0
  143. graphcore/domain/strategy/session.py +56 -0
  144. graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
  145. graphcore/domain/strategy/tests/__init__.py +0 -0
  146. graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
  147. graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
  148. graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
  149. graphcore/domain/strategy/tests/test_runner.py +220 -0
  150. graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
  151. graphcore/domain/strategy/tests/test_utils.py +95 -0
  152. graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
  153. graphcore/errors.py +145 -0
  154. graphcore/execution/README.md +97 -0
  155. graphcore/execution/StrategyExecutorService.py +333 -0
  156. graphcore/execution/__init__.py +0 -0
  157. graphcore/execution/dispatcher.py +19 -0
  158. graphcore/execution/environments/__init__.py +0 -0
  159. graphcore/execution/environments/celery.py +59 -0
  160. graphcore/execution/environments/in_process.py +55 -0
  161. graphcore/execution/local_session.py +117 -0
  162. graphcore/execution/redis_run_events.py +74 -0
  163. graphcore/execution/requests.py +22 -0
  164. graphcore/execution/run_events.py +85 -0
  165. graphcore/execution/strategy_context.py +54 -0
  166. graphcore/execution/tasks.py +34 -0
  167. graphcore/execution/tests/__init__.py +5 -0
  168. graphcore/execution/tests/contract.py +171 -0
  169. graphcore/execution/tests/support.py +310 -0
  170. graphcore/execution/tests/test_dispatchers.py +76 -0
  171. graphcore/execution/tests/test_executor.py +368 -0
  172. graphcore/execution/tests/test_local_session.py +95 -0
  173. graphcore/execution/tests/test_reconcile_runs.py +134 -0
  174. graphcore/execution/tests/test_run_events.py +106 -0
  175. graphcore/execution/tests/test_strategy_context.py +62 -0
  176. graphcore/gateway/__init__.py +1 -0
  177. graphcore/gateway/remote.py +35 -0
  178. graphcore/gateway/tests/__init__.py +0 -0
  179. graphcore/gateway/tests/test_remote.py +30 -0
  180. graphcore/graph/EntityService.py +710 -0
  181. graphcore/graph/__init__.py +0 -0
  182. graphcore/graph/entity_cache.py +152 -0
  183. graphcore/graph/tests/__init__.py +4 -0
  184. graphcore/graph/tests/test_EntityService.py +908 -0
  185. graphcore/graph/tests/test_EntityService_list.py +170 -0
  186. graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
  187. graphcore/graph/tests/test_EntityService_referrers.py +236 -0
  188. graphcore/graph/tests/test_EntityService_routing.py +119 -0
  189. graphcore/graph/tests/test_dependent_on.py +141 -0
  190. graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
  191. graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
  192. graphcore/graph/tests/test_entity_cache.py +159 -0
  193. graphcore/graph/tests/test_null_entity_cache.py +12 -0
  194. graphcore/management/__init__.py +0 -0
  195. graphcore/management/commands/__init__.py +0 -0
  196. graphcore/management/commands/dev_reset.py +18 -0
  197. graphcore/management/commands/graphcore_createadmin.py +65 -0
  198. graphcore/management/commands/graphcore_policy.py +59 -0
  199. graphcore/management/commands/graphcore_status.py +98 -0
  200. graphcore/management/tests/__init__.py +0 -0
  201. graphcore/management/tests/test_createadmin.py +78 -0
  202. graphcore/management/tests/test_policy_command.py +46 -0
  203. graphcore/management/tests/test_status_command.py +85 -0
  204. graphcore/migrations/0001_initial.py +186 -0
  205. graphcore/migrations/0002_invite.py +26 -0
  206. graphcore/migrations/__init__.py +0 -0
  207. graphcore/models.py +11 -0
  208. graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
  209. graphcore/persistence/__init__.py +6 -0
  210. graphcore/persistence/django/__init__.py +0 -0
  211. graphcore/persistence/django/backends.py +43 -0
  212. graphcore/persistence/django/bindings.py +28 -0
  213. graphcore/persistence/django/cache.py +38 -0
  214. graphcore/persistence/django/compiler.py +214 -0
  215. graphcore/persistence/django/identity.py +18 -0
  216. graphcore/persistence/django/models.py +252 -0
  217. graphcore/persistence/django/repository.py +525 -0
  218. graphcore/persistence/django/tests/__init__.py +0 -0
  219. graphcore/persistence/django/tests/test_backends.py +68 -0
  220. graphcore/persistence/django/tests/test_compiler.py +269 -0
  221. graphcore/persistence/django/tests/test_repository.py +472 -0
  222. graphcore/persistence/django/tests/test_shared_column.py +120 -0
  223. graphcore/persistence/django/tests/test_threads.py +50 -0
  224. graphcore/persistence/django/tests/test_update_shape.py +175 -0
  225. graphcore/persistence/django/threads.py +38 -0
  226. graphcore/persistence/repository.py +172 -0
  227. graphcore/persistence/tests/__init__.py +0 -0
  228. graphcore/persistence/tests/contract.py +688 -0
  229. graphcore/persistence/tests/evaluate.py +51 -0
  230. graphcore/persistence/tests/fixtures.py +32 -0
  231. graphcore/persistence/tests/memory.py +263 -0
  232. graphcore/persistence/tests/test_memory_repository.py +10 -0
  233. graphcore/persistence/tests/test_query_types.py +127 -0
  234. graphcore/public/__init__.py +9 -0
  235. graphcore/public/api.py +9 -0
  236. graphcore/public/apps.py +10 -0
  237. graphcore/public/auth/__init__.py +1 -0
  238. graphcore/public/auth/grants.py +7 -0
  239. graphcore/public/auth/onboarding.py +8 -0
  240. graphcore/public/auth/perms.py +7 -0
  241. graphcore/public/auth/policy.py +10 -0
  242. graphcore/public/auth/roles.py +8 -0
  243. graphcore/public/auth/subjects.py +7 -0
  244. graphcore/public/entities/__init__.py +32 -0
  245. graphcore/public/errors.py +19 -0
  246. graphcore/public/persistence/__init__.py +44 -0
  247. graphcore/public/persistence/django.py +24 -0
  248. graphcore/public/scheduling.py +23 -0
  249. graphcore/public/strategies/__init__.py +85 -0
  250. graphcore/public/test_settings.py +16 -0
  251. graphcore/public/testing.py +47 -0
  252. graphcore/registry/EntityDirectory.py +246 -0
  253. graphcore/registry/README.md +147 -0
  254. graphcore/registry/StrategyDirectory.py +308 -0
  255. graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
  256. graphcore/registry/__init__.py +0 -0
  257. graphcore/registry/app_discovery.py +108 -0
  258. graphcore/registry/decorators.py +77 -0
  259. graphcore/registry/discovery.py +111 -0
  260. graphcore/registry/registry.py +79 -0
  261. graphcore/registry/tests/__init__.py +0 -0
  262. graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
  263. graphcore/registry/tests/test_app_config.py +36 -0
  264. graphcore/registry/tests/test_discovery.py +147 -0
  265. graphcore/registry/tests/test_entity_directory_storage.py +73 -0
  266. graphcore/registry/tests/test_import_package.py +87 -0
  267. graphcore/registry/tests/test_principal_flag.py +67 -0
  268. graphcore/registry/tests/test_strategy_directory.py +29 -0
  269. graphcore/routes/__init__.py +116 -0
  270. graphcore/routes/tests/__init__.py +0 -0
  271. graphcore/routes/tests/test_routes.py +90 -0
  272. graphcore/runtime.py +243 -0
  273. graphcore/scheduling/__init__.py +0 -0
  274. graphcore/scheduling/beat_scheduler.py +164 -0
  275. graphcore/scheduling/beat_tasks.py +63 -0
  276. graphcore/scheduling/scheduler.py +37 -0
  277. graphcore/scheduling/tests/__init__.py +0 -0
  278. graphcore/scheduling/tests/contract.py +136 -0
  279. graphcore/scheduling/tests/memory.py +42 -0
  280. graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
  281. graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
  282. graphcore/scheduling/tests/test_ticks.py +143 -0
  283. graphcore/tasks.py +9 -0
  284. graphcore/test_settings.py +78 -0
  285. graphcore/testing/__init__.py +0 -0
  286. graphcore/testing/case.py +112 -0
  287. graphcore/testing/runner.py +32 -0
  288. graphcore/testing/settings.py +26 -0
  289. graphcore/testing/tests/__init__.py +0 -0
  290. graphcore/testing/tests/agent_view_fixture.py +19 -0
  291. graphcore/testing/tests/test_case_and_runner.py +219 -0
  292. graphcore/testing/tests/test_settings_for_apps.py +50 -0
  293. graphcore/tests/__init__.py +0 -0
  294. graphcore/tests/ids.py +14 -0
  295. graphcore/tests/public_api_manifest.py +102 -0
  296. graphcore/tests/runs.py +7 -0
  297. graphcore/tests/test_app_discovery.py +90 -0
  298. graphcore/tests/test_composition_boundaries.py +231 -0
  299. graphcore/tests/test_docs_discovery.py +108 -0
  300. graphcore/tests/test_no_print.py +70 -0
  301. graphcore/tests/test_packaging.py +64 -0
  302. graphcore/tests/test_public_api_boundaries.py +229 -0
  303. graphcore/tests/test_public_api_contract.py +62 -0
  304. graphcore/tests/test_public_api_docs.py +28 -0
  305. graphcore/tests/test_runtime.py +42 -0
  306. graphcore/tests/test_shared_type_boundaries.py +128 -0
  307. graphcore/tests/test_shared_types.py +144 -0
  308. graphcore/tests/test_test_settings.py +45 -0
  309. graphcore/transport/__init__.py +0 -0
  310. graphcore/transport/agent_asgi.py +68 -0
  311. graphcore/transport/agent_client.py +85 -0
  312. graphcore/transport/channel_layer.py +68 -0
  313. graphcore/transport/consumers.py +507 -0
  314. graphcore/transport/routing.py +6 -0
  315. graphcore/transport/tests/__init__.py +0 -0
  316. graphcore/transport/tests/test_agent_client.py +31 -0
  317. graphcore/transport/tests/test_api_views.py +733 -0
  318. graphcore/transport/tests/test_channel_layer.py +96 -0
  319. graphcore/transport/tests/test_consumers.py +963 -0
  320. graphcore/transport/tests/test_resource_metadata.py +67 -0
  321. graphcore/transport/urls.py +29 -0
  322. graphcore/transport/views.py +358 -0
  323. graphcore/types/__init__.py +4 -0
  324. graphcore/types/access.py +114 -0
  325. graphcore/types/api.py +76 -0
  326. graphcore/types/entities.py +100 -0
  327. graphcore/types/entity_id.py +85 -0
  328. graphcore/types/execution.py +41 -0
  329. graphcore/types/identity.py +20 -0
  330. graphcore/types/onboarding.py +20 -0
  331. graphcore/types/query.py +116 -0
  332. graphcore/types/schedules.py +91 -0
  333. graphcore/types/strategy.py +73 -0
  334. graphcore/types/tests/__init__.py +0 -0
  335. graphcore/types/tests/test_entity_id.py +50 -0
  336. graphcore/urls.py +5 -0
  337. graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
  338. graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
  339. graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
  340. graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
  341. graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
  342. graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
  343. graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,963 @@
1
+ """
2
+ Tests for WebSocket consumers with callback pattern.
3
+
4
+ Tests cover:
5
+ - Connection and disconnection
6
+ - Command handling
7
+ - Strategy execution (fire-and-forget and callback patterns)
8
+ - Callback handlers
9
+ - Error handling
10
+ """
11
+ import asyncio
12
+ import json
13
+ import time
14
+ from unittest.mock import AsyncMock, MagicMock, patch, call
15
+ from django.test import TransactionTestCase
16
+ from channels.testing import WebsocketCommunicator
17
+ from channels.layers import get_channel_layer
18
+ from asgiref.sync import async_to_sync
19
+ import jwt
20
+
21
+ import graphcore.transport.consumers as consumers
22
+ from graphcore.transport.consumers import GlobalEntityConsumer
23
+ from graphcore.auth.authn.testing import TestIssuer
24
+ from graphcore.types.identity import Principal
25
+ from graphcore.domain.entity.Entity import Entity
26
+ from graphcore.types.entities import client_view
27
+ from graphcore.domain.entity.strategy_request.StrategyRequest import StrategyRequest
28
+ from graphcore.types.strategy import StrategyRequestPayload
29
+ from graphcore.registry.registry import DEFAULT_REGISTRY
30
+ from graphcore.runtime import django_runtime
31
+ from graphcore.execution.tests.support import ExecutionTestCase
32
+
33
+
34
+ class _Clock:
35
+ """The consumer's wall clock, `offset` seconds ahead of the real one."""
36
+
37
+ def __init__(self, offset: float = 0.0):
38
+ self.offset = offset
39
+
40
+ def time(self) -> float:
41
+ return time.time() + self.offset
42
+
43
+
44
+ def _expires_at(token: str) -> int:
45
+ return jwt.decode(token, options={'verify_signature': False})['exp']
46
+
47
+
48
+ class GlobalEntityConsumerTestCase(TransactionTestCase):
49
+ """Test GlobalEntityConsumer WebSocket functionality"""
50
+
51
+ def setUp(self):
52
+ """Set up test fixtures"""
53
+ self.channel_layer = get_channel_layer()
54
+ self.runtime = django_runtime(DEFAULT_REGISTRY)
55
+ self._get_runtime_patch = patch.object(consumers, 'get_runtime', lambda: self.runtime)
56
+ self._get_runtime_patch.start()
57
+ self.addCleanup(self._get_runtime_patch.stop)
58
+ self.issuer = TestIssuer()
59
+ self.enterContext(self.issuer.trusted())
60
+
61
+ def get_test_token(self):
62
+ return self.issuer.token('test-user')
63
+
64
+ async def test_connect_and_disconnect(self):
65
+ """Test WebSocket connection and disconnection"""
66
+ test_token = self.issuer.token('test-user')
67
+
68
+ communicator = WebsocketCommunicator(
69
+ GlobalEntityConsumer.as_asgi(),
70
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
71
+ )
72
+
73
+ # Test connection
74
+ connected, subprotocol = await communicator.connect()
75
+ self.assertTrue(connected)
76
+
77
+ # Should receive connected message
78
+ response = await communicator.receive_json_from()
79
+ self.assertEqual(response['type'], 'connected')
80
+ self.assertEqual(response['message'], 'Connected to user-scoped entity updates')
81
+ self.assertIsNotNone(response.get('principal_entity_id'))
82
+ self.assertEqual(response['max_batch_size'], self.runtime.graph_api.max_batch_size)
83
+
84
+ # Test disconnection
85
+ await communicator.disconnect()
86
+ async def test_ping_command(self):
87
+ """Test ping command returns pong"""
88
+ test_token = self.issuer.token('test-user')
89
+
90
+ communicator = WebsocketCommunicator(
91
+ GlobalEntityConsumer.as_asgi(),
92
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
93
+ )
94
+ await communicator.connect()
95
+ await communicator.receive_json_from() # Skip connected message
96
+
97
+ # Send ping
98
+ await communicator.send_json_to({
99
+ 'command': 'ping'
100
+ })
101
+
102
+ # Should receive pong
103
+ response = await communicator.receive_json_from()
104
+ self.assertEqual(response['type'], 'pong')
105
+ self.assertEqual(response['message'], 'Pong')
106
+
107
+ await communicator.disconnect()
108
+ async def test_unknown_command(self):
109
+ """Test unknown command returns error"""
110
+ test_token = self.issuer.token('test-user')
111
+
112
+ communicator = WebsocketCommunicator(
113
+ GlobalEntityConsumer.as_asgi(),
114
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
115
+ )
116
+ await communicator.connect()
117
+ await communicator.receive_json_from() # Skip connected message
118
+
119
+ # Send unknown command
120
+ await communicator.send_json_to({
121
+ 'command': 'invalid_command'
122
+ })
123
+
124
+ # Should receive error
125
+ response = await communicator.receive_json_from()
126
+ self.assertEqual(response['type'], 'error')
127
+ self.assertIn('Unknown command', response['message'])
128
+
129
+ await communicator.disconnect()
130
+ async def test_subscribe_entities(self):
131
+ """Test subscribing to entities"""
132
+ test_token = self.issuer.token('test-user')
133
+
134
+ communicator = WebsocketCommunicator(
135
+ GlobalEntityConsumer.as_asgi(),
136
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
137
+ )
138
+ await communicator.connect()
139
+ await communicator.receive_json_from() # Skip connected message
140
+
141
+ # Mock entity service
142
+ mock_entity = self.runtime.entity_service.create(Entity)
143
+ mock_entity.set_attribute('name', 'Test Entity')
144
+
145
+ with patch.object(self.runtime.graph_api, 'get_entities', new_callable=AsyncMock) as mock_get_entities:
146
+ mock_get_entities.return_value = {mock_entity.entity_id: mock_entity}
147
+
148
+ # Send subscribe command
149
+ await communicator.send_json_to({
150
+ 'command': 'subscribe_entities',
151
+ 'entity_ids': [mock_entity.entity_id]
152
+ })
153
+
154
+ # Should receive entity_update
155
+ response = await communicator.receive_json_from()
156
+ self.assertEqual(response['type'], 'entity_update')
157
+ self.assertIn(mock_entity.entity_id, response['entities'])
158
+
159
+ # Should receive subscriptions_complete
160
+ response = await communicator.receive_json_from()
161
+ self.assertEqual(response['type'], 'subscriptions_complete')
162
+
163
+ await communicator.disconnect()
164
+ async def test_subscribe_entities_over_limit_rejected(self):
165
+ """Step A12: an unbounded entity_ids batch must be rejected before it
166
+ reaches the service layer."""
167
+ test_token = self.issuer.token('test-user')
168
+
169
+ communicator = WebsocketCommunicator(
170
+ GlobalEntityConsumer.as_asgi(),
171
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
172
+ )
173
+ await communicator.connect()
174
+ await communicator.receive_json_from() # Skip connected message
175
+
176
+ entity_ids = [f'id-{i}' for i in range(self.runtime.graph_api.max_batch_size + 1)]
177
+ await communicator.send_json_to({
178
+ 'command': 'subscribe_entities',
179
+ 'entity_ids': entity_ids
180
+ })
181
+
182
+ response = await communicator.receive_json_from()
183
+ self.assertEqual(response['type'], 'error')
184
+ self.assertIn('exceeds the maximum', response['message'])
185
+
186
+ await communicator.disconnect()
187
+
188
+ async def test_get_entity(self):
189
+ """Test getting a single entity"""
190
+ test_token = self.issuer.token('test-user')
191
+
192
+ communicator = WebsocketCommunicator(
193
+ GlobalEntityConsumer.as_asgi(),
194
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
195
+ )
196
+ await communicator.connect()
197
+ await communicator.receive_json_from() # Skip connected message
198
+
199
+ # Mock entity service
200
+ mock_entity = self.runtime.entity_service.create(Entity)
201
+ mock_entity.set_attribute('name', 'Test Entity')
202
+
203
+ with patch.object(self.runtime.graph_api, 'get_entity', new_callable=AsyncMock) as mock_get:
204
+ mock_get.return_value = mock_entity
205
+
206
+ # Send get_entity command
207
+ await communicator.send_json_to({
208
+ 'command': 'get_entity',
209
+ 'entity_id': mock_entity.entity_id
210
+ })
211
+
212
+ # Should receive entity_update
213
+ response = await communicator.receive_json_from()
214
+ self.assertEqual(response['type'], 'entity_update')
215
+ self.assertEqual(response['entities'][mock_entity.entity_id], client_view(mock_entity))
216
+ self.assertEqual(response['entities'][mock_entity.entity_id]['attributes'], {'name': 'Test Entity'})
217
+ self.assertNotIn('name', response['entities'][mock_entity.entity_id])
218
+
219
+ await communicator.disconnect()
220
+ async def test_get_entity_not_found(self):
221
+ """Test getting a non-existent entity"""
222
+ test_token = self.issuer.token('test-user')
223
+
224
+ communicator = WebsocketCommunicator(
225
+ GlobalEntityConsumer.as_asgi(),
226
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
227
+ )
228
+ await communicator.connect()
229
+ await communicator.receive_json_from() # Skip connected message
230
+
231
+ test_entity_id = 'non-existent-id'
232
+
233
+ with patch.object(self.runtime.graph_api, 'get_entity', new_callable=AsyncMock) as mock_get:
234
+ from graphcore.types.entities import Tombstone
235
+ mock_get.return_value = Tombstone(entity_id=test_entity_id, cause='missing')
236
+
237
+ # Send get_entity command
238
+ await communicator.send_json_to({
239
+ 'command': 'get_entity',
240
+ 'entity_id': test_entity_id
241
+ })
242
+
243
+ # Should receive entity_not_found
244
+ response = await communicator.receive_json_from()
245
+ self.assertEqual(response['type'], 'entity_not_found')
246
+ self.assertEqual(response['entity_id'], test_entity_id)
247
+
248
+ await communicator.disconnect()
249
+ async def _connected(self) -> WebsocketCommunicator:
250
+ communicator = WebsocketCommunicator(
251
+ GlobalEntityConsumer.as_asgi(),
252
+ "/ws/entities/", subprotocols=["graphcore.bearer", self.issuer.token('test-user')]
253
+ )
254
+ await communicator.connect()
255
+ await communicator.receive_json_from() # connected
256
+ return communicator
257
+
258
+ async def test_resync_sends_what_changed_then_done(self):
259
+ communicator = await self._connected()
260
+ entity = self.runtime.entity_service.create(Entity)
261
+
262
+ with patch.object(self.runtime.graph_api, 'changed_entities', new_callable=AsyncMock) as changed:
263
+ changed.return_value = {entity.entity_id: entity}
264
+ await communicator.send_json_to({
265
+ 'command': 'resync', 'resync_id': 7, 'entities': {entity.entity_id: 'stale'},
266
+ })
267
+
268
+ update = await communicator.receive_json_from()
269
+ done = await communicator.receive_json_from()
270
+
271
+ self.assertEqual(changed.call_args.args[1], {entity.entity_id: 'stale'})
272
+ self.assertEqual(update, {'type': 'entity_update', 'entities': {entity.entity_id: client_view(entity)}})
273
+ self.assertEqual(done, {'type': 'resync_done', 'resync_id': 7, 'error': None})
274
+ await communicator.disconnect()
275
+
276
+ async def test_a_malformed_resync_still_ends_with_done(self):
277
+ communicator = await self._connected()
278
+
279
+ await communicator.send_json_to({'command': 'resync', 'resync_id': 1, 'entities': ['gc://a/1']})
280
+
281
+ done = await communicator.receive_json_from()
282
+ self.assertEqual((done['type'], done['resync_id']), ('resync_done', 1))
283
+ self.assertIn('entities must map', done['error'])
284
+ await communicator.disconnect()
285
+
286
+ async def test_resync_requested_tells_the_client_to_resync(self):
287
+ consumer = GlobalEntityConsumer()
288
+ consumer.send = AsyncMock()
289
+
290
+ await consumer.resync_requested({'type': 'resync.requested'})
291
+
292
+ self.assertEqual(json.loads(consumer.send.call_args[0][0]), {'type': 'resync'})
293
+
294
+ async def test_strategy_complete_is_forwarded_as_a_strategy_result(self):
295
+ consumer = GlobalEntityConsumer()
296
+ consumer.send = AsyncMock()
297
+ for event, extra in (
298
+ ({'status': 'succeeded', 'success': True, 'ret_val': {'value': 42}}, {'ret_val': {'value': 42}}),
299
+ ({'status': 'failed', 'success': False, 'error': 'boom'}, {'error': 'boom', 'error_details': {}}),
300
+ ({'status': 'failed', 'success': False, 'error': 'stale', 'code': 'conflict', 'details': {'a': 1}},
301
+ {'error': 'stale', 'error_code': 'conflict', 'error_details': {'a': 1}}),
302
+ ):
303
+ await consumer.strategy_complete({
304
+ 'type': 'strategy_complete', 'request_id': 'r-1', 'target_entity_id': 't-1',
305
+ 'client_request_id': 'c-1', **event,
306
+ })
307
+ sent = json.loads(consumer.send.call_args[0][0])
308
+ self.assertEqual(sent['type'], 'strategy_result')
309
+ self.assertEqual((sent['request_id'], sent['target_entity_id'], sent['client_request_id']),
310
+ ('r-1', 't-1', 'c-1'))
311
+ self.assertEqual((sent['status'], sent['success']), (event['status'], event['success']))
312
+ for key, value in extra.items():
313
+ self.assertEqual(sent[key], value)
314
+
315
+ async def test_entity_update_handler(self):
316
+ """Test entity_update handler (called by channel layer)"""
317
+ # Test the entity_update handler directly with a mock
318
+ test_entities = {
319
+ 'entity-1': {'name': 'Entity 1'},
320
+ 'entity-2': {'name': 'Entity 2'}
321
+ }
322
+
323
+ # Create a consumer instance and mock its send method
324
+ consumer = GlobalEntityConsumer()
325
+ consumer.send = AsyncMock()
326
+
327
+ await consumer.entity_update({
328
+ 'entities': test_entities
329
+ })
330
+
331
+ # Verify send was called with correct data
332
+ consumer.send.assert_called_once()
333
+ call_args = consumer.send.call_args[1]['text_data']
334
+ sent_data = json.loads(call_args)
335
+ self.assertEqual(sent_data['type'], 'entity_update')
336
+ self.assertEqual(sent_data['entities'], test_entities)
337
+
338
+ async def test_execute_strategy_error_handling(self):
339
+ """Test error handling in strategy execution.
340
+
341
+ An unexpected exception must not leak its text to the client (Step
342
+ A12) - the client gets a generic message plus a correlation id, and
343
+ the real detail goes to the log."""
344
+ test_token = self.issuer.token('test-user')
345
+
346
+ communicator = WebsocketCommunicator(
347
+ GlobalEntityConsumer.as_asgi(),
348
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
349
+ )
350
+ await communicator.connect()
351
+ await communicator.receive_json_from() # Skip connected message
352
+
353
+ mock_entity = self.runtime.entity_service.create(Entity)
354
+
355
+ with patch.object(self.runtime.graph_api, 'get_entity', new_callable=AsyncMock) as mock_get:
356
+ mock_get.return_value = mock_entity
357
+
358
+ with patch.object(self.runtime.executor, 'submit', new_callable=AsyncMock) as mock_exec:
359
+ mock_exec.side_effect = Exception('Test error')
360
+
361
+ # Send execute_strategy command with await_response=True
362
+ await communicator.send_json_to({
363
+ 'command': 'execute_strategy',
364
+ 'await_response': True,
365
+ 'strategy': [{
366
+ 'strategy_name': 'TestStrategy',
367
+ 'param_config': {},
368
+ 'target_entity_id': mock_entity.entity_id,
369
+ }]
370
+ })
371
+
372
+ # Should receive strategy_result with error
373
+ response = await communicator.receive_json_from()
374
+ self.assertEqual(response['type'], 'strategy_result')
375
+ self.assertFalse(response['success'])
376
+ self.assertNotIn('Test error', response['error'])
377
+ self.assertIn('Internal server error', response['error'])
378
+
379
+ await communicator.disconnect()
380
+
381
+ async def test_execute_strategy_forwards_forbidden_message(self):
382
+ from graphcore.errors import Forbidden
383
+
384
+ test_token = self.issuer.token('test-user')
385
+ communicator = WebsocketCommunicator(
386
+ GlobalEntityConsumer.as_asgi(),
387
+ "/ws/entities/", subprotocols=["graphcore.bearer", test_token]
388
+ )
389
+ await communicator.connect()
390
+ await communicator.receive_json_from()
391
+
392
+ denied = (
393
+ 'Strategy SetAttributes is not allowed on entity '
394
+ 'target-id (type Publication)'
395
+ )
396
+ with patch.object(
397
+ self.runtime.executor, 'submit',
398
+ new_callable=AsyncMock,
399
+ side_effect=Forbidden(denied),
400
+ ):
401
+ await communicator.send_json_to({
402
+ 'command': 'execute_strategy',
403
+ 'await_response': True,
404
+ 'strategy': [{
405
+ 'strategy_name': 'SetAttributes',
406
+ 'target_entity_id': 'target-id',
407
+ }]
408
+ })
409
+ response = await communicator.receive_json_from()
410
+
411
+ self.assertEqual(response['type'], 'strategy_result')
412
+ self.assertFalse(response['success'])
413
+ self.assertEqual(response['error'], denied)
414
+ await communicator.disconnect()
415
+
416
+
417
+ class ExecuteRoundTripTests(ExecutionTestCase):
418
+ """`execute_strategy` over a real connection, through the real executor
419
+ and channel layer, in every environment."""
420
+
421
+ def setUp(self):
422
+ super().setUp()
423
+ patcher = patch.object(consumers, 'get_runtime', lambda: self.runtime)
424
+ patcher.start()
425
+ self.addCleanup(patcher.stop)
426
+ self.issuer = TestIssuer()
427
+ self.enterContext(self.issuer.trusted())
428
+ self._subjects = {}
429
+
430
+ async def user(self, username):
431
+ from graphcore.domain.entity.user.User import User
432
+ from graphcore.execution.strategy_context import kernel_internal_context
433
+
434
+ user = self.service.create(User, username=username)
435
+ self.issuer.bind(user)
436
+ with kernel_internal_context():
437
+ user = await self.service.save_entity(user, Principal(principal_entity_id=user.entity_id))
438
+ self._subjects[user.entity_id] = user.subject
439
+ return Principal(principal_entity_id=user.entity_id)
440
+
441
+ async def connect(self, principal):
442
+ token = self.issuer.token(self._subjects[principal.principal_entity_id])
443
+ communicator = WebsocketCommunicator(
444
+ GlobalEntityConsumer.as_asgi(), "/ws/entities/", subprotocols=["graphcore.bearer", token],
445
+ )
446
+ connected, _ = await communicator.connect()
447
+ assert connected
448
+ await communicator.receive_json_from()
449
+ return communicator
450
+
451
+ async def until(self, communicator, message_type, max_messages=30):
452
+ for _ in range(max_messages):
453
+ message = await communicator.receive_json_from(timeout=5)
454
+ if message['type'] == message_type:
455
+ return message
456
+ raise AssertionError(f"no {message_type} within {max_messages} messages")
457
+
458
+ async def execute(self, communicator, await_response, *strategies):
459
+ await communicator.send_json_to({
460
+ 'command': 'execute_strategy', 'await_response': await_response,
461
+ 'strategy': [{'target_entity_id': self.probe.entity_id, 'param_config': {}, **s} for s in strategies],
462
+ })
463
+
464
+ @async_to_sync
465
+ async def test_an_awaited_run_is_queued_then_resolved_in_every_environment(self):
466
+ await self.seed()
467
+ socket = await self.connect(self.alice)
468
+ for counter, name in enumerate(('Increment', 'CpuIncrement'), start=1):
469
+ await self.execute(socket, True, {'strategy_name': name, 'client_request_id': f'c-{name}'})
470
+ queued = await self.until(socket, 'strategy_queued')
471
+ self.assertEqual((queued['client_request_id'], queued['target_entity_id']),
472
+ (f'c-{name}', self.probe.entity_id))
473
+ result = await self.until(socket, 'strategy_result')
474
+ self.assertEqual(result['request_id'], queued['request_id'])
475
+ self.assertEqual(result['client_request_id'], f'c-{name}')
476
+ self.assertEqual((result['status'], result['success'], result['ret_val']),
477
+ ('succeeded', True, {'counter': counter}))
478
+ await socket.disconnect()
479
+
480
+ @async_to_sync
481
+ async def test_a_failed_run_resolves_with_its_error(self):
482
+ await self.seed()
483
+ socket = await self.connect(self.alice)
484
+ for name in ('Fail', 'CpuFail'):
485
+ await self.execute(socket, True, {'strategy_name': name, 'client_request_id': 'c-fail'})
486
+ result = await self.until(socket, 'strategy_result')
487
+ self.assertEqual((result['status'], result['success'], result['error'], result['client_request_id']),
488
+ ('failed', False, 'boom', 'c-fail'))
489
+ self.assertEqual(result['error_code'], 'strategy_failed')
490
+ await socket.disconnect()
491
+
492
+ @async_to_sync
493
+ async def test_a_refused_run_resolves_with_its_error_code_and_details(self):
494
+ await self.seed()
495
+ socket = await self.connect(self.alice)
496
+ for name in ('Refuse', 'CpuRefuse'):
497
+ await self.execute(socket, True, {'strategy_name': name, 'client_request_id': 'c-refuse'})
498
+ result = await self.until(socket, 'strategy_result')
499
+ self.assertEqual((result['status'], result['error'], result['error_code']), ('failed', 'stale', 'conflict'))
500
+ self.assertEqual(result['error_details'], {'invitation': {'answers': {'dinner': 'accepted'}}})
501
+ await socket.disconnect()
502
+
503
+ @async_to_sync
504
+ async def test_a_refused_run_resolves_at_once_and_is_not_saved(self):
505
+ await self.seed()
506
+ socket = await self.connect(self.bob)
507
+ await self.execute(socket, True, {'strategy_name': 'Increment', 'client_request_id': 'c-no'})
508
+ result = await self.until(socket, 'strategy_result')
509
+ self.assertEqual((result['request_id'], result['success'], result['client_request_id']), (None, False, 'c-no'))
510
+ self.assertIn('not allowed', result['error'])
511
+ self.assertEqual((result['error_code'], result['error_details']), ('forbidden', {}))
512
+ self.assertEqual(await self.requests(), [])
513
+ await socket.disconnect()
514
+
515
+ @async_to_sync
516
+ async def test_a_fire_and_forget_run_is_acknowledged_and_runs(self):
517
+ await self.seed()
518
+ socket = await self.connect(self.alice)
519
+ # CPU first: its eager run finishes inside dispatch, so neither run writes while the other does,
520
+ # which in-memory SQLite refuses instead of waiting.
521
+ await self.execute(socket, False, {'strategy_name': 'CpuIncrement'}, {'strategy_name': 'Increment'})
522
+ acks = [await self.until(socket, 'strategy_executed') for _ in range(2)]
523
+ await self.settle()
524
+ for ack in acks:
525
+ self.assertEqual((await self.load(ack['request_id'])).status, 'succeeded')
526
+ self.assertEqual(await self.counter(), 2)
527
+ await socket.disconnect()
528
+
529
+
530
+
531
+ class ConsumerAuthTestCase(TransactionTestCase):
532
+ """Test WebSocket authentication and authorization.
533
+
534
+ `TransactionTestCase`, like every other connecting case here: the
535
+ consumer reads the database from its own event loop, which a
536
+ `TestCase`'s wrapping atomic block leaves unusable on PostgreSQL."""
537
+
538
+ def setUp(self):
539
+ self.issuer = TestIssuer()
540
+ self.enterContext(self.issuer.trusted())
541
+
542
+ @async_to_sync
543
+ async def test_connection_rejected_without_token(self):
544
+ """WebSocket accepts handshake, sends auth_error, then closes without token."""
545
+ communicator = WebsocketCommunicator(
546
+ GlobalEntityConsumer.as_asgi(),
547
+ "/ws/entities/"
548
+ )
549
+ connected, _ = await communicator.connect(timeout=1)
550
+ self.assertTrue(connected)
551
+ response = await communicator.receive_json_from()
552
+ self.assertEqual(response['type'], 'auth_error')
553
+ self.assertEqual(response['code'], 'NO_TOKEN')
554
+
555
+ @async_to_sync
556
+ async def test_connection_rejected_with_invalid_token(self):
557
+ """WebSocket accepts handshake, sends auth_error, then closes with invalid token."""
558
+ communicator = WebsocketCommunicator(
559
+ GlobalEntityConsumer.as_asgi(),
560
+ "/ws/entities/", subprotocols=["graphcore.bearer", 'invalid-token']
561
+ )
562
+ connected, _ = await communicator.connect(timeout=1)
563
+ self.assertTrue(connected)
564
+ response = await communicator.receive_json_from()
565
+ self.assertEqual(response['type'], 'auth_error')
566
+ self.assertEqual(response['code'], 'INVALID_TOKEN')
567
+
568
+ @async_to_sync
569
+ async def test_token_in_query_string_is_ignored(self):
570
+ communicator = WebsocketCommunicator(
571
+ GlobalEntityConsumer.as_asgi(),
572
+ f"/ws/entities/?token={self.issuer.token('valid-user')}"
573
+ )
574
+ connected, _ = await communicator.connect(timeout=1)
575
+ self.assertTrue(connected)
576
+ response = await communicator.receive_json_from()
577
+ self.assertEqual(response['code'], 'NO_TOKEN')
578
+
579
+ @async_to_sync
580
+ async def test_handshake_selects_the_bearer_subprotocol(self):
581
+ communicator = WebsocketCommunicator(
582
+ GlobalEntityConsumer.as_asgi(), "/ws/entities/",
583
+ subprotocols=["graphcore.bearer", self.issuer.token('valid-user')],
584
+ )
585
+ connected, subprotocol = await communicator.connect()
586
+ self.assertTrue(connected)
587
+ self.assertEqual(subprotocol, 'graphcore.bearer')
588
+ await communicator.disconnect()
589
+
590
+ @async_to_sync
591
+ async def test_connection_accepted_with_valid_token(self):
592
+ """WebSocket accepts connection with valid token."""
593
+ from graphcore.auth.authn.users import find_user_by_identity
594
+ from graphcore.runtime import get_runtime
595
+
596
+ token = self.issuer.token('valid-user')
597
+ communicator = WebsocketCommunicator(
598
+ GlobalEntityConsumer.as_asgi(),
599
+ "/ws/entities/", subprotocols=["graphcore.bearer", token]
600
+ )
601
+ connected, _ = await communicator.connect()
602
+ self.assertTrue(connected)
603
+
604
+ # Should receive connected message with principal_entity_id. Whether
605
+ # group_subscriptions_degraded is set here depends on whether the
606
+ # test DB backend supports the JSONB group-membership lookup (see
607
+ # test_connect_surfaces_degraded_group_subscriptions_on_lookup_failure
608
+ # for a deterministic check of that field).
609
+ response = await communicator.receive_json_from()
610
+ self.assertEqual(response['type'], 'connected')
611
+ user = await find_user_by_identity(get_runtime().entity_service, self.issuer.issuer, 'valid-user')
612
+ self.assertEqual(response['principal_entity_id'], user.entity_id)
613
+ self.assertIn('group_subscriptions_degraded', response)
614
+
615
+ await communicator.disconnect()
616
+
617
+ async def _connect(self, token):
618
+ communicator = WebsocketCommunicator(
619
+ GlobalEntityConsumer.as_asgi(), "/ws/entities/", subprotocols=["graphcore.bearer", token],
620
+ )
621
+ connected, _ = await communicator.connect()
622
+ self.assertTrue(connected)
623
+ self.assertEqual((await communicator.receive_json_from())['type'], 'connected')
624
+ return communicator
625
+
626
+ @async_to_sync
627
+ async def test_reauth_swaps_the_token_and_keeps_the_connection(self):
628
+ clock = _Clock()
629
+ with patch.object(consumers, 'time', clock):
630
+ communicator = await self._connect(self.issuer.token('valid-user', expires_in=60))
631
+ await communicator.send_json_to({'command': 'reauth', 'token': self.issuer.token('valid-user')})
632
+ self.assertEqual((await communicator.receive_json_from())['type'], 'reauthenticated')
633
+ clock.offset = 120
634
+ await communicator.send_json_to({'command': 'ping'})
635
+ self.assertEqual((await communicator.receive_json_from())['type'], 'pong')
636
+ await communicator.disconnect()
637
+
638
+ @async_to_sync
639
+ async def test_reauth_as_another_identity_closes_the_connection(self):
640
+ communicator = await self._connect(self.issuer.token('valid-user'))
641
+ await communicator.send_json_to({'command': 'reauth', 'token': self.issuer.token('someone-else')})
642
+ response = await communicator.receive_json_from()
643
+ self.assertEqual((response['type'], response['code']), ('auth_error', 'INVALID_TOKEN'))
644
+ self.assertEqual((await communicator.receive_output())['code'], 4002)
645
+
646
+ @async_to_sync
647
+ async def test_connection_closes_when_its_token_expires(self):
648
+ token = self.issuer.token('valid-user', expires_in=60)
649
+ clock = _Clock(offset=_expires_at(token) - time.time() - 0.2)
650
+ with patch.object(consumers, 'time', clock):
651
+ communicator = WebsocketCommunicator(
652
+ GlobalEntityConsumer.as_asgi(), "/ws/entities/", subprotocols=["graphcore.bearer", token],
653
+ )
654
+ await communicator.connect()
655
+ types = []
656
+ while 'auth_error' not in types:
657
+ response = await communicator.receive_json_from(timeout=3)
658
+ types.append(response['type'])
659
+ self.assertEqual(response['code'], 'TOKEN_EXPIRED')
660
+ self.assertEqual((await communicator.receive_output())['code'], 4002)
661
+
662
+ @async_to_sync
663
+ async def test_commands_on_an_expired_token_are_refused_when_the_timer_is_late(self):
664
+ """The expiry timer sleeps on the loop's monotonic clock, which stops while
665
+ the host sleeps; the wall-clock check on each command still refuses."""
666
+ with patch.object(GlobalEntityConsumer, '_arm_expiry', lambda self, principal: None):
667
+ communicator = await self._connect(self.issuer.token('valid-user', expires_in=60))
668
+ with patch.object(consumers, 'time', MagicMock(time=lambda: 10 ** 10)), \
669
+ patch.object(consumers, 'get_runtime') as mock_runtime:
670
+ await communicator.send_json_to({'command': 'execute_strategy', 'strategy': [{}]})
671
+ response = await communicator.receive_json_from()
672
+ self.assertEqual((response['type'], response['code']), ('auth_error', 'TOKEN_EXPIRED'))
673
+ self.assertEqual((await communicator.receive_output())['code'], 4002)
674
+ mock_runtime.return_value.graph_api.execute_strategies.assert_not_called()
675
+
676
+ @async_to_sync
677
+ async def test_reauth_is_accepted_after_expiry(self):
678
+ clock = _Clock()
679
+ with patch.object(GlobalEntityConsumer, '_arm_expiry', lambda self, principal: None), \
680
+ patch.object(consumers, 'time', clock):
681
+ communicator = await self._connect(self.issuer.token('valid-user', expires_in=60))
682
+ clock.offset = 120
683
+ await communicator.send_json_to({'command': 'reauth', 'token': self.issuer.token('valid-user')})
684
+ self.assertEqual((await communicator.receive_json_from())['type'], 'reauthenticated')
685
+ await communicator.send_json_to({'command': 'ping'})
686
+ self.assertEqual((await communicator.receive_json_from())['type'], 'pong')
687
+ await communicator.disconnect()
688
+
689
+ @async_to_sync
690
+ async def test_connect_surfaces_degraded_group_subscriptions_on_lookup_failure(self):
691
+ """Step A12: a failing group lookup during connect must be logged and
692
+ surfaced to the client, not silently swallowed into an empty list."""
693
+ token = self.issuer.token('valid-user')
694
+ communicator = WebsocketCommunicator(
695
+ GlobalEntityConsumer.as_asgi(),
696
+ "/ws/entities/", subprotocols=["graphcore.bearer", token]
697
+ )
698
+
699
+ from graphcore.runtime import get_runtime
700
+
701
+ with patch.object(
702
+ get_runtime().access,
703
+ 'granted_resources',
704
+ AsyncMock(side_effect=RuntimeError('db unavailable')),
705
+ ):
706
+ with patch('graphcore.transport.consumers.log_and_correlate') as mock_log:
707
+ connected, _ = await communicator.connect()
708
+ self.assertTrue(connected)
709
+
710
+ response = await communicator.receive_json_from()
711
+ self.assertEqual(response['type'], 'connected')
712
+ self.assertTrue(response['group_subscriptions_degraded'])
713
+ mock_log.assert_called_once()
714
+
715
+ await communicator.disconnect()
716
+
717
+
718
+ class _ScopeChannelHub(Entity):
719
+ """A shareable scope type, standing in for the TDD's DocumentHub."""
720
+
721
+ @classmethod
722
+ def get_entity_type_id(cls) -> str:
723
+ return "graphcore._ScopeChannelHub"
724
+
725
+
726
+ class _ScopeChannelDocument(Entity):
727
+ """A plain contained type with no grants of its own - standing in for
728
+ the TDD's Document, which inherits read reach from its parent scope."""
729
+
730
+ @classmethod
731
+ def get_entity_type_id(cls) -> str:
732
+ return "graphcore._ScopeChannelDocument"
733
+
734
+
735
+ TEST_REGISTRY = DEFAULT_REGISTRY.fork_open()
736
+ TEST_REGISTRY.entities.register_entity(_ScopeChannelHub, app_label='graphcore')
737
+ TEST_REGISTRY.policy.define(_ScopeChannelHub, shareable=True)
738
+ TEST_REGISTRY.entities.register_entity(_ScopeChannelDocument, app_label='graphcore')
739
+
740
+
741
+ class ScopeChannelTestCase(TransactionTestCase):
742
+ """Step 10: consumer migration to scope channels - the TDD's worked
743
+ fan-out/grant/revoke/group examples, over a real WebSocket connection."""
744
+
745
+ def setUp(self):
746
+ self.runtime = django_runtime(TEST_REGISTRY)
747
+ self._get_runtime_patch = patch.object(consumers, 'get_runtime', lambda: self.runtime)
748
+ self._get_runtime_patch.start()
749
+ self.addCleanup(self._get_runtime_patch.stop)
750
+ self.issuer = TestIssuer()
751
+ self.enterContext(self.issuer.trusted())
752
+
753
+ self.api = self.runtime.graph_api
754
+ self._subjects = {}
755
+ self.OWNER = async_to_sync(self._create_user_entity)('owner')
756
+ self.owner_principal = Principal(principal_entity_id=self.OWNER)
757
+ self.hub = async_to_sync(self._create_hub)()
758
+
759
+ async def _create_hub(self):
760
+ from graphcore.execution.strategy_context import kernel_internal_context
761
+
762
+ hub = self.api.entity_service.create(_ScopeChannelHub)
763
+ hub.owner = self.OWNER
764
+ with kernel_internal_context():
765
+ return await self.api.entity_service.save_entity(hub, self.owner_principal)
766
+
767
+ async def _save_child_document(self):
768
+ from graphcore.execution.strategy_context import kernel_internal_context
769
+
770
+ document = self.api.entity_service.create(_ScopeChannelDocument)
771
+ document.set_parent(self.hub.entity_id)
772
+ document.set_attribute('name', 'Nested document')
773
+ with kernel_internal_context():
774
+ return await self.api.entity_service.save_entity(document, self.owner_principal)
775
+
776
+ async def _create_user_entity(self, username: str) -> str:
777
+ """Grant strategies validate the subject is a real persisted entity -
778
+ create a minimal one rather than granting to a bare id string."""
779
+ from graphcore.domain.entity.user.User import User
780
+ from graphcore.execution.strategy_context import kernel_internal_context
781
+
782
+ user = self.runtime.entity_service.create(User)
783
+ user.username = username
784
+ self.issuer.bind(user)
785
+ with kernel_internal_context():
786
+ user = await self.api.entity_service.save_entity(user, Principal(principal_entity_id=user.entity_id))
787
+ self._subjects[user.entity_id] = user.subject
788
+ return user.entity_id
789
+
790
+ async def _receive_until(self, communicator: WebsocketCommunicator, matches, max_messages: int = 5):
791
+ """Receive up to `max_messages`, skipping incidental resource-lifecycle
792
+ updates (e.g. the hub's own children_ids projection refresh when a grant is
793
+ created/deleted as its child), until one satisfies `matches`."""
794
+ for _ in range(max_messages):
795
+ message = await communicator.receive_json_from()
796
+ if matches(message):
797
+ return message
798
+ raise AssertionError(f"no matching message received within {max_messages} messages")
799
+
800
+ async def _connect(self, principal_entity_id: str) -> WebsocketCommunicator:
801
+ token = self.issuer.token(self._subjects[principal_entity_id])
802
+ communicator = WebsocketCommunicator(GlobalEntityConsumer.as_asgi(), "/ws/entities/", subprotocols=["graphcore.bearer", token])
803
+ connected, _ = await communicator.connect()
804
+ assert connected
805
+ await communicator.receive_json_from() # connected message
806
+ return communicator
807
+
808
+ @async_to_sync
809
+ async def test_editor_receives_child_saves_via_scope_channel(self):
810
+ """Bob holds Editor on the hub, connects, and a child document (with
811
+ no grants of its own) is saved - Bob receives it via scope_{hub}."""
812
+ bob = await self._create_user_entity('bob')
813
+ stranger = await self._create_user_entity('stranger')
814
+ await self.api.grant(self.owner_principal, self.hub.entity_id, bob, 'editor')
815
+
816
+ bob_socket = await self._connect(bob)
817
+ stranger_socket = await self._connect(stranger)
818
+
819
+ document = await self._save_child_document()
820
+
821
+ response = await bob_socket.receive_json_from()
822
+ self.assertEqual(response['type'], 'entity_update')
823
+ self.assertIn(document.entity_id, response['entities'])
824
+ self.assertEqual(response['entities'][document.entity_id]['attributes'], {'name': 'Nested document'})
825
+ self.assertNotIn('name', response['entities'][document.entity_id])
826
+
827
+ await stranger_socket.receive_nothing()
828
+
829
+ await bob_socket.disconnect()
830
+ await stranger_socket.disconnect()
831
+
832
+ @async_to_sync
833
+ async def test_owner_receives_new_child_without_scope_grant(self):
834
+ """A direct owner receives a new child even when its parent is not
835
+ shareable and therefore contributes no pre-existing scope channel."""
836
+ from graphcore.execution.strategy_context import kernel_internal_context
837
+
838
+ folder = self.api.entity_service.create(_ScopeChannelDocument)
839
+ with kernel_internal_context():
840
+ folder = await self.api.entity_service.save_entity(folder, self.owner_principal)
841
+ stranger = await self._create_user_entity('owner-stranger')
842
+ owner_socket = await self._connect(self.OWNER)
843
+ stranger_socket = await self._connect(stranger)
844
+
845
+ document = self.api.entity_service.create(_ScopeChannelDocument)
846
+ document.parent_id = folder.entity_id
847
+ with kernel_internal_context():
848
+ document = await self.api.entity_service.save_entity(document, self.owner_principal)
849
+
850
+ response = await owner_socket.receive_json_from()
851
+ self.assertEqual(response['type'], 'entity_update')
852
+ self.assertIn(document.entity_id, response['entities'])
853
+ await stranger_socket.receive_nothing()
854
+
855
+ await owner_socket.disconnect()
856
+ await stranger_socket.disconnect()
857
+
858
+ @async_to_sync
859
+ async def test_scope_owner_receives_collaborator_owned_child(self):
860
+ """A shareable ancestor's owner receives a child owned by its
861
+ creator without needing an explicit grant on the ancestor."""
862
+ from graphcore.execution.strategy_context import kernel_internal_context
863
+
864
+ collaborator = await self._create_user_entity('owner-collaborator')
865
+ stranger = await self._create_user_entity('owner-scope-stranger')
866
+ owner_socket = await self._connect(self.OWNER)
867
+ stranger_socket = await self._connect(stranger)
868
+
869
+ document = self.api.entity_service.create(_ScopeChannelDocument)
870
+ document.parent_id = self.hub.entity_id
871
+ with kernel_internal_context():
872
+ document = await self.api.entity_service.save_entity(document, Principal(principal_entity_id=collaborator))
873
+
874
+ response = await owner_socket.receive_json_from()
875
+ self.assertEqual(response['type'], 'entity_update')
876
+ self.assertIn(document.entity_id, response['entities'])
877
+ self.assertEqual(document.owner, collaborator)
878
+ await stranger_socket.receive_nothing()
879
+
880
+ await owner_socket.disconnect()
881
+ await stranger_socket.disconnect()
882
+
883
+ @async_to_sync
884
+ async def test_grant_while_connected_joins_and_delivers_subsequent_saves(self):
885
+ """Alice connects with no grant, gets a fresh grant, and immediately
886
+ sees both the join notification and subsequent child saves."""
887
+ alice = await self._create_user_entity('alice')
888
+ alice_socket = await self._connect(alice)
889
+
890
+ await self.api.grant(self.owner_principal, self.hub.entity_id, alice, 'viewer')
891
+
892
+ granted = await self._receive_until(alice_socket, lambda m: m['type'] == 'access_granted')
893
+ self.assertEqual(granted['resource_id'], self.hub.entity_id)
894
+
895
+ document = await self._save_child_document()
896
+ response = await self._receive_until(
897
+ alice_socket, lambda m: m['type'] == 'entity_update' and document.entity_id in m['entities']
898
+ )
899
+ self.assertIn(document.entity_id, response['entities'])
900
+
901
+ await alice_socket.disconnect()
902
+
903
+ @async_to_sync
904
+ async def test_revoke_while_connected_leaves_and_stops_delivery(self):
905
+ """Revoking Alice's grant notifies her and she stops receiving saves
906
+ on that scope thereafter."""
907
+ alice = await self._create_user_entity('alice2')
908
+ await self.api.grant(self.owner_principal, self.hub.entity_id, alice, 'viewer')
909
+ alice_socket = await self._connect(alice)
910
+
911
+ await self.api.revoke(self.owner_principal, self.hub.entity_id, alice)
912
+
913
+ revoked = await self._receive_until(alice_socket, lambda m: m['type'] == 'access_revoked')
914
+ self.assertEqual(revoked['resource_id'], self.hub.entity_id)
915
+
916
+ await self._save_child_document()
917
+ await alice_socket.receive_nothing()
918
+
919
+ await alice_socket.disconnect()
920
+
921
+ @async_to_sync
922
+ async def test_group_member_receives_saves_and_stops_after_removal(self):
923
+ """A grant to a group reaches a connected member; removing them from
924
+ the roster stops delivery (cache-bust, no explicit leave message)."""
925
+ from graphcore.domain.entity.group.Group import Group
926
+ from graphcore.execution.strategy_context import kernel_internal_context
927
+
928
+ member = await self._create_user_entity('group-member')
929
+ group = self.runtime.entity_service.create(Group)
930
+ group.name = 'Engineering'
931
+ group.owner = self.OWNER
932
+ group.add_member(member)
933
+ with kernel_internal_context():
934
+ group = await self.api.entity_service.save_entity(group, self.owner_principal)
935
+
936
+ await self.api.grant(self.owner_principal, self.hub.entity_id, group.entity_id, 'viewer')
937
+
938
+ # The reverse member_ids lookup uses a Postgres-only JSONB operator
939
+ # (`?`), unavailable on the SQLite test backend - warm the adapter's
940
+ # cache directly, matching test_default_adapter.py's GroupGrantTests.
941
+ adapter = self.runtime.access
942
+ adapter.cache.set(
943
+ adapter._subject_groups_cache_key(member),
944
+ [group.entity_id],
945
+ timeout=300,
946
+ )
947
+
948
+ member_socket = await self._connect(member)
949
+
950
+ document = await self._save_child_document()
951
+ response = await self._receive_until(
952
+ member_socket, lambda m: m['type'] == 'entity_update' and document.entity_id in m['entities']
953
+ )
954
+ self.assertIn(document.entity_id, response['entities'])
955
+
956
+ group.remove_member(member)
957
+ with kernel_internal_context():
958
+ await self.api.entity_service.save_entity(group, self.owner_principal)
959
+
960
+ await self._save_child_document()
961
+ await member_socket.receive_nothing()
962
+
963
+ await member_socket.disconnect()