diff --git a/selfprivacy_api/graphql/schema.py b/selfprivacy_api/graphql/schema.py index e4e7264..078ee3d 100644 --- a/selfprivacy_api/graphql/schema.py +++ b/selfprivacy_api/graphql/schema.py @@ -28,6 +28,8 @@ from selfprivacy_api.graphql.queries.services import Services from selfprivacy_api.graphql.queries.storage import Storage from selfprivacy_api.graphql.queries.system import System +from selfprivacy_api.graphql.subscriptions.jobs import JobSubscriptions + from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations from selfprivacy_api.graphql.queries.users import Users from selfprivacy_api.jobs.test import test_job @@ -129,16 +131,19 @@ class Mutation( code=200, ) - pass - @strawberry.type class Subscription: """Root schema for subscriptions""" - @strawberry.subscription(permission_classes=[IsAuthenticated]) - async def count(self, target: int = 100) -> AsyncGenerator[int, None]: - for i in range(target): + @strawberry.field(permission_classes=[IsAuthenticated]) + def jobs(self) -> JobSubscriptions: + """Jobs subscriptions""" + return JobSubscriptions() + + @strawberry.subscription + async def count(self) -> AsyncGenerator[int, None]: + for i in range(10): yield i await asyncio.sleep(0.5) diff --git a/tests/test_graphql/test_websocket.py b/tests/test_graphql/test_websocket.py index d534269..58681e0 100644 --- a/tests/test_graphql/test_websocket.py +++ b/tests/test_graphql/test_websocket.py @@ -2,22 +2,22 @@ from tests.common import generate_jobs_subscription from selfprivacy_api.graphql.queries.jobs import Job as _Job from selfprivacy_api.jobs import Jobs -# JOBS_SUBSCRIPTION = """ -# jobUpdates { -# uid -# typeId -# name -# description -# status -# statusText -# progress -# createdAt -# updatedAt -# finishedAt -# error -# result -# } -# """ +JOBS_SUBSCRIPTION = """ +jobUpdates { + uid + typeId + name + description + status + statusText + progress + createdAt + updatedAt + finishedAt + error + result +} +""" def test_websocket_connection_bare(authorized_client): @@ -50,12 +50,62 @@ def test_websocket_graphql_ping(authorized_client): assert pong == {"type": "pong"} +def init_graphql(websocket): + websocket.send_json({"type": "connection_init", "payload": {}}) + ack = websocket.receive_json() + assert ack == {"type": "connection_ack"} + + +def test_websocket_subscription_minimal(authorized_client): + client = authorized_client + with client.websocket_connect( + "/graphql", subprotocols=["graphql-transport-ws"] + ) as websocket: + init_graphql(websocket) + websocket.send_json( + { + "id": "3aaa2445", + "type": "subscribe", + "payload": { + "query": "subscription TestSubscription {count}", + }, + } + ) + response = websocket.receive_json() + assert response == { + "id": "3aaa2445", + "payload": {"data": {"count": 0}}, + "type": "next", + } + response = websocket.receive_json() + assert response == { + "id": "3aaa2445", + "payload": {"data": {"count": 1}}, + "type": "next", + } + response = websocket.receive_json() + assert response == { + "id": "3aaa2445", + "payload": {"data": {"count": 2}}, + "type": "next", + } + + # def test_websocket_subscription(authorized_client): # client = authorized_client # with client.websocket_connect( -# "/graphql", subprotocols=["graphql-transport-ws", "graphql-ws"] +# "/graphql", subprotocols=["graphql-transport-ws"] # ) as websocket: -# websocket.send(generate_jobs_subscription([JOBS_SUBSCRIPTION])) -# Jobs.add("bogus","bogus.bogus", "yyyaaaaayy") -# joblist = websocket.receive_json() -# raise NotImplementedError(joblist) +# init_graphql(websocket) +# websocket.send_json( +# { +# "id": "3aaa2445", +# "type": "subscribe", +# "payload": { +# "query": generate_jobs_subscription([JOBS_SUBSCRIPTION]), +# }, +# } +# ) +# Jobs.add("bogus", "bogus.bogus", "yyyaaaaayy") +# response = websocket.receive_json() +# raise NotImplementedError(response)