From 05ffa036b3d916720ed3276603f5db4bd890309e Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 27 May 2024 21:13:57 +0000 Subject: [PATCH] refactor(jobs): offload job subscription logic to a separate file --- selfprivacy_api/graphql/schema.py | 11 +++++------ tests/test_graphql/test_websocket.py | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/selfprivacy_api/graphql/schema.py b/selfprivacy_api/graphql/schema.py index 3280396..05e6bf9 100644 --- a/selfprivacy_api/graphql/schema.py +++ b/selfprivacy_api/graphql/schema.py @@ -30,8 +30,9 @@ from selfprivacy_api.graphql.queries.storage import Storage from selfprivacy_api.graphql.queries.system import System from selfprivacy_api.graphql.subscriptions.jobs import ApiJob -from selfprivacy_api.jobs import job_notifications -from selfprivacy_api.graphql.queries.jobs import get_all_jobs +from selfprivacy_api.graphql.subscriptions.jobs import ( + job_updates as job_update_generator, +) from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations from selfprivacy_api.graphql.queries.users import Users @@ -157,12 +158,10 @@ class Subscription: self, info: strawberry.types.Info ) -> AsyncGenerator[List[ApiJob], None]: reject_if_unauthenticated(info) - - # Send the complete list of jobs every time anything gets updated - async for notification in job_notifications(): - yield get_all_jobs() + return job_update_generator() @strawberry.subscription + # Used for testing, consider deletion to shrink attack surface async def count(self, info: strawberry.types.Info) -> AsyncGenerator[int, None]: reject_if_unauthenticated(info) for i in range(10): diff --git a/tests/test_graphql/test_websocket.py b/tests/test_graphql/test_websocket.py index 49cc944..d538ca1 100644 --- a/tests/test_graphql/test_websocket.py +++ b/tests/test_graphql/test_websocket.py @@ -162,9 +162,9 @@ def test_websocket_subscription_minimal_unauthorized(unauthenticated_websocket): async def read_one_job(websocket): - # bug? We only get them starting from the second job update - # that's why we receive two jobs in the list them - # the first update gets lost somewhere + # Bug? We only get them starting from the second job update + # That's why we receive two jobs in the list them + # The first update gets lost somewhere response = websocket.receive_json() return response @@ -215,8 +215,16 @@ def test_websocket_subscription_unauthorized(unauthenticated_websocket): api_subscribe(websocket, id, JOBS_SUBSCRIPTION) response = websocket.receive_json() + # I do not really know why strawberry gives more info on this + # One versus the counter + payload = response["payload"][0] + assert isinstance(payload, dict) + assert "locations" in payload.keys() + # It looks like this 'locations': [{'column': 32, 'line': 1}] + # We cannot test locations feasibly + del payload["locations"] assert response == { "id": id, - "payload": [{"message": IsAuthenticated.message}], + "payload": [{"message": IsAuthenticated.message, "path": ["jobUpdates"]}], "type": "error", }