refactor(jobs): offload job subscription logic to a separate file

This commit is contained in:
Houkime 2024-05-27 21:13:57 +00:00 committed by Inex Code
parent ccf71078b8
commit 05ffa036b3
2 changed files with 17 additions and 10 deletions

View file

@ -30,8 +30,9 @@ from selfprivacy_api.graphql.queries.storage import Storage
from selfprivacy_api.graphql.queries.system import System from selfprivacy_api.graphql.queries.system import System
from selfprivacy_api.graphql.subscriptions.jobs import ApiJob from selfprivacy_api.graphql.subscriptions.jobs import ApiJob
from selfprivacy_api.jobs import job_notifications from selfprivacy_api.graphql.subscriptions.jobs import (
from selfprivacy_api.graphql.queries.jobs import get_all_jobs job_updates as job_update_generator,
)
from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations
from selfprivacy_api.graphql.queries.users import Users from selfprivacy_api.graphql.queries.users import Users
@ -157,12 +158,10 @@ class Subscription:
self, info: strawberry.types.Info self, info: strawberry.types.Info
) -> AsyncGenerator[List[ApiJob], None]: ) -> AsyncGenerator[List[ApiJob], None]:
reject_if_unauthenticated(info) reject_if_unauthenticated(info)
return job_update_generator()
# Send the complete list of jobs every time anything gets updated
async for notification in job_notifications():
yield get_all_jobs()
@strawberry.subscription @strawberry.subscription
# Used for testing, consider deletion to shrink attack surface
async def count(self, info: strawberry.types.Info) -> AsyncGenerator[int, None]: async def count(self, info: strawberry.types.Info) -> AsyncGenerator[int, None]:
reject_if_unauthenticated(info) reject_if_unauthenticated(info)
for i in range(10): for i in range(10):

View file

@ -162,9 +162,9 @@ def test_websocket_subscription_minimal_unauthorized(unauthenticated_websocket):
async def read_one_job(websocket): async def read_one_job(websocket):
# bug? We only get them starting from the second job update # Bug? We only get them starting from the second job update
# that's why we receive two jobs in the list them # That's why we receive two jobs in the list them
# the first update gets lost somewhere # The first update gets lost somewhere
response = websocket.receive_json() response = websocket.receive_json()
return response return response
@ -215,8 +215,16 @@ def test_websocket_subscription_unauthorized(unauthenticated_websocket):
api_subscribe(websocket, id, JOBS_SUBSCRIPTION) api_subscribe(websocket, id, JOBS_SUBSCRIPTION)
response = websocket.receive_json() 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 == { assert response == {
"id": id, "id": id,
"payload": [{"message": IsAuthenticated.message}], "payload": [{"message": IsAuthenticated.message, "path": ["jobUpdates"]}],
"type": "error", "type": "error",
} }