From dd67345d748cb5cf313d4fa9bc41069bcba441a7 Mon Sep 17 00:00:00 2001 From: bkis Date: Wed, 18 Dec 2024 11:20:20 +0100 Subject: [PATCH] Fix tests --- Tekst-API/openapi.json | 12 ++-- Tekst-API/tekst/routers/platform.py | 6 +- Tekst-API/tekst/tasks.py | 8 ++- Tekst-API/tests/test_api_resources.py | 9 ++- Tekst-API/tests/test_api_search.py | 4 ++ Tekst-API/tests/test_api_tasks.py | 84 ++++++++++++++++++++++----- Tekst-Web/src/api/schema.d.ts | 8 +-- 7 files changed, 98 insertions(+), 33 deletions(-) diff --git a/Tekst-API/openapi.json b/Tekst-API/openapi.json index 56a5fdb7..c2eac0c9 100644 --- a/Tekst-API/openapi.json +++ b/Tekst-API/openapi.json @@ -3010,8 +3010,8 @@ "tags": [ "platform" ], - "summary": "Get user tasks status", - "operationId": "getUserTasksStatus", + "summary": "Get user tasks", + "operationId": "getUserTasks", "security": [ { "APIKeyCookie": [] @@ -3029,16 +3029,16 @@ "anyOf": [ { "type": "string", - "maxLength": 1024 + "maxLength": 2048 }, { "type": "null" } ], - "description": "Pickup keys for accessing the tasks in case they are requested by a non-authenticated user", + "description": "Comma-separated pickup keys for accessing the tasks in case they are requested by a non-authenticated user", "title": "Pickup-Keys" }, - "description": "Pickup keys for accessing the tasks in case they are requested by a non-authenticated user" + "description": "Comma-separated pickup keys for accessing the tasks in case they are requested by a non-authenticated user" } ], "responses": { @@ -3051,7 +3051,7 @@ "items": { "$ref": "#/components/schemas/TaskRead" }, - "title": "Response Get User Tasks Status Platform Tasks User Get" + "title": "Response Get User Tasks Platform Tasks User Get" } } } diff --git a/Tekst-API/tekst/routers/platform.py b/Tekst-API/tekst/routers/platform.py index 12fa5f28..e82ea93c 100644 --- a/Tekst-API/tekst/routers/platform.py +++ b/Tekst-API/tekst/routers/platform.py @@ -272,16 +272,16 @@ async def get_all_tasks_status( ] ), ) -async def get_user_tasks_status( +async def get_user_tasks( user: OptionalUserDep, pickup_keys: Annotated[ str | None, Header( description=( - "Pickup keys for accessing the tasks in case they " + "Comma-separated pickup keys for accessing the tasks in case they " "are requested by a non-authenticated user" ), - max_length=1024, + max_length=2048, ), ] = None, ) -> list[tasks.TaskDocument]: diff --git a/Tekst-API/tekst/tasks.py b/Tekst-API/tekst/tasks.py index 384480b7..32971b05 100644 --- a/Tekst-API/tekst/tasks.py +++ b/Tekst-API/tekst/tasks.py @@ -213,6 +213,7 @@ async def get_tasks( ) -> list[TaskDocument]: # run tasks cleanup first await cleanup_tasks() + # select tasks: get user-specific tasks if initiated by a regular user, # get all tasks if initiated by a superuser or None (system-internal) if not user: @@ -224,14 +225,15 @@ async def get_tasks( else: # regular user or superuser that wants only user-specific tasks query = Eq(TaskDocument.user_id, user.id) + tasks = await TaskDocument.find(query).to_list() # delete retrieved user tasks that are done/failed - # (excluding successful exports, because these are still - # needed for locating generated files) + # (excluding artifact-producing tasks, because these are still + # needed for locating the generated artifacts/files) if user and not get_all: for task in tasks: if ( - task.status in ["done", "failed"] + task.status in ("done", "failed") and task.user_id is not None and (not task.task_type.artifact or task.status == "failed") ): diff --git a/Tekst-API/tests/test_api_resources.py b/Tekst-API/tests/test_api_resources.py index 59894d40..87e0f3a5 100644 --- a/Tekst-API/tests/test_api_resources.py +++ b/Tekst-API/tests/test_api_resources.py @@ -1024,6 +1024,7 @@ async def test_export_content( logout, wait_for_task_success, wrong_id, + config, ): await insert_sample_data() await login() @@ -1068,7 +1069,7 @@ async def test_export_content( for fmt in formats: for target in targets: - # export + # create export resp = await test_client.get( f"/resources/{target['res_id']}/export", params={ @@ -1080,6 +1081,12 @@ async def test_export_content( assert status_assertion(202, resp) assert "id" in resp.json() assert await wait_for_task_success(resp.json()["id"]) + # download generated artifact + resp = await test_client.get( + "/platform/tasks/download", + params={"pickupKey": resp.json()["pickupKey"]}, + ) + assert status_assertion(200, resp) # log out for the next tests await logout() diff --git a/Tekst-API/tests/test_api_search.py b/Tekst-API/tests/test_api_search.py index 21b0fdc9..63b98e73 100644 --- a/Tekst-API/tests/test_api_search.py +++ b/Tekst-API/tests/test_api_search.py @@ -28,10 +28,14 @@ async def test_admin_create_search_index( ): await insert_sample_data() await login(is_superuser=True) + + # create task to create index resp = await test_client.get("/search/index/create") assert status_assertion(202, resp) assert "id" in resp.json() assert await wait_for_task_success(resp.json()["id"]) + + # get index info resp = await test_client.get("/search/index/info") assert status_assertion(200, resp) assert isinstance(resp.json(), list) diff --git a/Tekst-API/tests/test_api_tasks.py b/Tekst-API/tests/test_api_tasks.py index 1f30f69e..b68e556d 100644 --- a/Tekst-API/tests/test_api_tasks.py +++ b/Tekst-API/tests/test_api_tasks.py @@ -1,3 +1,5 @@ +import asyncio + from uuid import uuid4 import pytest @@ -10,12 +12,66 @@ async def test_get_non_user_tasks( config, test_client: AsyncClient, status_assertion, + wait_for_task_success, + insert_sample_data, ): + await insert_sample_data() + + # get (non-)user tasks (none, no pickup keys) resp = await test_client.get("/platform/tasks/user") assert status_assertion(200, resp) assert isinstance(resp.json(), list) assert len(resp.json()) == 0 + # request resource export + resp = await test_client.get( + "/resources/66471b68ba9e65342c8e495b/export", + params={ + "format": "csv", + "from": "654b825533ee5737b297f8e5", + "to": "654b825533ee5737b297f8f2", + }, + ) + assert status_assertion(202, resp) + assert "id" in resp.json() + task_id = resp.json()["id"] + pickup_key = resp.json()["pickupKey"] + + # wait for task to finish (to make sure it's "done" before requesting tasks again) + await wait_for_task_success(task_id) + + # get (non-)user tasks + # (one now, requesting it does not delete the task as it produced an artifact) + resp = await test_client.get( + "/platform/tasks/user", + headers={"pickup-keys": pickup_key}, + ) + assert status_assertion(200, resp) + assert isinstance(resp.json(), list) + assert len(resp.json()) == 1 + assert resp.json()[0]["id"] == task_id + assert resp.json()[0]["status"] == "done" + + # download generated artifact + resp = await test_client.get( + "/platform/tasks/download", + params={"pickupKey": pickup_key}, + ) + assert status_assertion(200, resp) + + # wait a bit because task deletion after download is async + await asyncio.sleep(2) + + # get (non-)user tasks + # (no tasks anymore, as task got deleted when artifact was downloaded) + resp = await test_client.get( + "/platform/tasks/user", + headers={"pickup-keys": pickup_key}, + ) + assert status_assertion(200, resp) + assert isinstance(resp.json(), list) + assert len(resp.json()) == 0 + @pytest.mark.anyio async def test_get_user_tasks( @@ -24,8 +80,10 @@ async def test_get_user_tasks( status_assertion, login, wait_for_task_success, + insert_sample_data, ): - await login() + await insert_sample_data() + await login(is_superuser=True) # get user tasks (none yet) resp = await test_client.get("/platform/tasks/user") @@ -33,29 +91,23 @@ async def test_get_user_tasks( assert isinstance(resp.json(), list) assert len(resp.json()) == 0 - # request resource export - resp = await test_client.get( - "/resources/66471b68ba9e65342c8e495b/export", - params={ - "format": "csv", - "from": "654b825533ee5737b297f8e5", - "to": "654b825533ee5737b297f8f2", - }, - ) + # create task to create index (to have a task to work with) + resp = await test_client.get("/search/index/create") assert status_assertion(202, resp) assert "id" in resp.json() - task_id = resp.json()["id"] - - # wait for task to finish (to make sure it's "done" before requesting tasks again) - wait_for_task_success(task_id) + assert await wait_for_task_success(resp.json()["id"]) - # get user tasks (one now, requesting it deletes the "done" task) + # get user tasks + # (one now should be deleted on requesting tasks as it's + # not a task that produced an artifact) resp = await test_client.get("/platform/tasks/user") assert status_assertion(200, resp) assert isinstance(resp.json(), list) assert len(resp.json()) == 1 + assert resp.json()[0]["status"] == "done" - # get user tasks (no tasks anymore) + # get user tasks + # (no tasks anymore, as task got deleted) resp = await test_client.get("/platform/tasks/user") assert status_assertion(200, resp) assert isinstance(resp.json(), list) diff --git a/Tekst-Web/src/api/schema.d.ts b/Tekst-Web/src/api/schema.d.ts index 10223dd2..1c5f41cf 100644 --- a/Tekst-Web/src/api/schema.d.ts +++ b/Tekst-Web/src/api/schema.d.ts @@ -559,8 +559,8 @@ export interface paths { path?: never; cookie?: never; }; - /** Get user tasks status */ - get: operations['getUserTasksStatus']; + /** Get user tasks */ + get: operations['getUserTasks']; put?: never; post?: never; delete?: never; @@ -7584,11 +7584,11 @@ export interface operations { }; }; }; - getUserTasksStatus: { + getUserTasks: { parameters: { query?: never; header?: { - /** @description Pickup keys for accessing the tasks in case they are requested by a non-authenticated user */ + /** @description Comma-separated pickup keys for accessing the tasks in case they are requested by a non-authenticated user */ 'pickup-keys'?: string | null; }; path?: never;