Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bkis committed Dec 18, 2024
1 parent d972932 commit dd67345
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 33 deletions.
12 changes: 6 additions & 6 deletions Tekst-API/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3010,8 +3010,8 @@
"tags": [
"platform"
],
"summary": "Get user tasks status",
"operationId": "getUserTasksStatus",
"summary": "Get user tasks",
"operationId": "getUserTasks",
"security": [
{
"APIKeyCookie": []
Expand All @@ -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": {
Expand All @@ -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"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Tekst-API/tekst/routers/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
8 changes: 5 additions & 3 deletions Tekst-API/tekst/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
):
Expand Down
9 changes: 8 additions & 1 deletion Tekst-API/tests/test_api_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ async def test_export_content(
logout,
wait_for_task_success,
wrong_id,
config,
):
await insert_sample_data()
await login()
Expand Down Expand Up @@ -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={
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions Tekst-API/tests/test_api_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
84 changes: 68 additions & 16 deletions Tekst-API/tests/test_api_tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from uuid import uuid4

import pytest
Expand All @@ -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(
Expand All @@ -24,38 +80,34 @@ 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")
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",
},
)
# 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)
Expand Down
8 changes: 4 additions & 4 deletions Tekst-Web/src/api/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit dd67345

Please sign in to comment.