Skip to content

Commit 64bd7f3

Browse files
committed
tested webserver API
1 parent ec49166 commit 64bd7f3

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

services/web/server/tests/unit/with_dbs/01/storage/conftest.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import logging
77
import random
8-
from collections.abc import Iterator
8+
from collections.abc import Callable, Iterator
99
from pathlib import Path
1010
from threading import Thread
11-
from typing import Annotated
11+
from typing import Annotated, Any
1212

1313
import pytest
1414
import uvicorn
@@ -32,6 +32,7 @@
3232
from models_library.projects_nodes_io import LocationID, StorageFileID
3333
from models_library.users import UserID
3434
from pydantic import AnyUrl, TypeAdapter
35+
from pytest_mock import MockerFixture
3536
from pytest_simcore.helpers.logging_tools import log_context
3637
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
3738
from servicelib.utils import unused_port
@@ -234,8 +235,7 @@ async def complete_upload_file(
234235
file_id: StorageFileID,
235236
body: FileUploadCompletionBody,
236237
request: Request,
237-
):
238-
...
238+
): ...
239239

240240
@router.post(
241241
"/locations/{location_id}/files/{file_id:path}:abort",
@@ -246,8 +246,7 @@ async def abort_upload_file(
246246
location_id: LocationID,
247247
file_id: StorageFileID,
248248
request: Request,
249-
):
250-
...
249+
): ...
251250

252251
app.include_router(router)
253252

@@ -311,3 +310,21 @@ def app_environment(
311310
@pytest.fixture
312311
def location_id(faker: Faker) -> LocationID:
313312
return TypeAdapter(LocationID).validate_python(faker.pyint(min_value=0))
313+
314+
315+
@pytest.fixture
316+
def create_storage_rpc_client_mock(mocker: MockerFixture) -> Callable[[str, Any], None]:
317+
def _(method: str, result_or_exception: Any):
318+
def side_effect(*args, **kwargs):
319+
if isinstance(result_or_exception, Exception):
320+
raise result_or_exception
321+
322+
return result_or_exception
323+
324+
for fct in (
325+
f"simcore_service_webserver.storage._rest.{method}",
326+
f"servicelib.rabbitmq.rpc_interfaces.storage.paths.{method}",
327+
):
328+
mocker.patch(fct, side_effect=side_effect)
329+
330+
return _

services/web/server/tests/unit/with_dbs/01/storage/test_storage.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@
33
# pylint: disable=unused-variable
44
# pylint: disable=too-many-arguments
55

6+
from collections.abc import Callable
67
from typing import Any
78
from urllib.parse import quote
89

910
import pytest
1011
from aiohttp.test_utils import TestClient
1112
from faker import Faker
1213
from fastapi_pagination.cursor import CursorPage
14+
from models_library.api_schemas_rpc_async_jobs.async_jobs import AsyncJobGet, AsyncJobId
1315
from models_library.api_schemas_storage.storage_schemas import (
1416
DatasetMetaDataGet,
1517
FileLocation,
1618
FileMetaDataGet,
1719
FileUploadSchema,
1820
PathMetaDataGet,
1921
)
22+
from models_library.api_schemas_webserver.storage import StorageAsyncJobGet
2023
from models_library.projects_nodes_io import LocationID, StorageFileID
2124
from pydantic import TypeAdapter
2225
from pytest_simcore.helpers.assert_checks import assert_status
2326
from servicelib.aiohttp import status
27+
from servicelib.rabbitmq.rpc_interfaces.async_jobs.async_jobs import (
28+
submit_job,
29+
)
2430
from simcore_postgres_database.models.users import UserRole
2531

2632
API_VERSION = "v0"
@@ -80,6 +86,51 @@ async def test_list_storage_paths(
8086
TypeAdapter(CursorPage[PathMetaDataGet]).validate_python(data)
8187

8288

89+
_faker = Faker()
90+
91+
92+
@pytest.mark.parametrize(
93+
"user_role,expected",
94+
[
95+
(UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED),
96+
(UserRole.GUEST, status.HTTP_202_ACCEPTED),
97+
(UserRole.USER, status.HTTP_202_ACCEPTED),
98+
(UserRole.TESTER, status.HTTP_202_ACCEPTED),
99+
],
100+
)
101+
@pytest.mark.parametrize(
102+
"backend_result_or_exception",
103+
[
104+
AsyncJobGet(job_id=AsyncJobId(f"{_faker.uuid4()}")),
105+
],
106+
ids=lambda x: type(x).__name__,
107+
)
108+
async def test_compute_path_size(
109+
client: TestClient,
110+
logged_user: dict[str, Any],
111+
expected: int,
112+
location_id: LocationID,
113+
faker: Faker,
114+
create_storage_rpc_client_mock: Callable[[str, Any], None],
115+
backend_result_or_exception: Any,
116+
):
117+
create_storage_rpc_client_mock(
118+
submit_job.__name__,
119+
backend_result_or_exception,
120+
)
121+
122+
assert client.app
123+
url = client.app.router["compute_path_size"].url_for(
124+
location_id=f"{location_id}",
125+
path=quote(faker.file_path(absolute=False), safe=""),
126+
)
127+
128+
resp = await client.post(f"{url}", params={"user_id": logged_user["id"]})
129+
data, error = await assert_status(resp, expected)
130+
if not error:
131+
TypeAdapter(StorageAsyncJobGet).validate_python(data)
132+
133+
83134
@pytest.mark.parametrize(
84135
"user_role,expected",
85136
[

services/web/server/tests/unit/with_dbs/01/storage/test_storage_rpc.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# pylint: disable=redefined-outer-name
22
# pylint: disable=unused-argument
3+
from collections.abc import Callable
34
from datetime import datetime
45
from pathlib import Path
5-
from typing import Any, Callable
6+
from typing import Any
67

78
import pytest
89
from aiohttp.test_utils import TestClient
@@ -29,7 +30,6 @@
2930
)
3031
from models_library.generics import Envelope
3132
from models_library.progress_bar import ProgressReport
32-
from pytest_mock import MockerFixture
3333
from pytest_simcore.helpers.webserver_login import UserInfoDict
3434
from servicelib.aiohttp import status
3535
from servicelib.rabbitmq.rpc_interfaces.async_jobs.async_jobs import (
@@ -44,23 +44,6 @@
4444
_faker = Faker()
4545

4646

47-
@pytest.fixture
48-
def create_storage_rpc_client_mock(mocker: MockerFixture) -> Callable[[str, Any], None]:
49-
def _(method: str, result_or_exception: Any):
50-
def side_effect(*args, **kwargs):
51-
if isinstance(result_or_exception, Exception):
52-
raise result_or_exception
53-
54-
return result_or_exception
55-
56-
mocker.patch(
57-
f"simcore_service_webserver.storage._rest.{method}",
58-
side_effect=side_effect,
59-
)
60-
61-
return _
62-
63-
6447
@pytest.mark.parametrize("user_role", [UserRole.USER])
6548
@pytest.mark.parametrize(
6649
"backend_result_or_exception",

0 commit comments

Comments
 (0)