Skip to content

Commit 1e3bb5e

Browse files
committed
drafts tests
1 parent 1af02db commit 1e3bb5e

File tree

4 files changed

+201
-50
lines changed

4 files changed

+201
-50
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# pylint: disable=protected-access
2+
# pylint: disable=redefined-outer-name
3+
# pylint: disable=too-many-arguments
4+
# pylint: disable=too-many-statements
5+
# pylint: disable=unused-argument
6+
# pylint: disable=unused-variable
7+
8+
9+
from collections.abc import AsyncIterable, Callable
10+
from pathlib import Path
11+
12+
import pytest
13+
from aiohttp.test_utils import TestClient
14+
from aioresponses import aioresponses
15+
from models_library.products import ProductName
16+
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
17+
from pytest_simcore.helpers.typing_env import EnvVarsDict
18+
from pytest_simcore.helpers.webserver_login import NewUser, UserInfoDict
19+
from pytest_simcore.helpers.webserver_parametrizations import MockedStorageSubsystem
20+
from pytest_simcore.helpers.webserver_projects import NewProject
21+
from simcore_service_webserver.projects.models import ProjectDict
22+
23+
24+
@pytest.fixture
25+
def app_environment(
26+
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
27+
) -> EnvVarsDict:
28+
return app_environment | setenvs_from_dict(
29+
monkeypatch, {"WEBSERVER_DEV_FEATURES_ENABLED": "1"}
30+
)
31+
32+
33+
@pytest.fixture
34+
async def other_user(
35+
client: TestClient, logged_user: UserInfoDict
36+
) -> AsyncIterable[UserInfoDict]:
37+
# new user different from logged_user
38+
async with NewUser(
39+
{
40+
"name": f"other_user_than_{logged_user['name']}",
41+
"role": "USER",
42+
},
43+
client.app,
44+
) as user:
45+
yield user
46+
47+
48+
@pytest.fixture
49+
async def other_user_project(
50+
client: TestClient,
51+
fake_project: ProjectDict,
52+
other_user: UserInfoDict,
53+
tests_data_dir: Path,
54+
osparc_product_name: ProductName,
55+
) -> AsyncIterable[ProjectDict]:
56+
async with NewProject(
57+
fake_project,
58+
client.app,
59+
user_id=other_user["id"],
60+
product_name=osparc_product_name,
61+
tests_data_dir=tests_data_dir,
62+
) as project:
63+
yield project
64+
65+
66+
@pytest.fixture
67+
def mocked_catalog(
68+
user_project: ProjectDict,
69+
catalog_subsystem_mock: Callable[[list[ProjectDict]], None],
70+
):
71+
catalog_subsystem_mock([user_project])
72+
73+
74+
@pytest.fixture
75+
def mocked_director_v2(director_v2_service_mock: aioresponses):
76+
...
77+
78+
79+
@pytest.fixture
80+
def mocked_storage(storage_subsystem_mock: MockedStorageSubsystem):
81+
...

services/web/server/tests/unit/with_dbs/03/test_trash.py renamed to services/web/server/tests/unit/with_dbs/03/trash/test_trash.py

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,20 @@
77

88

99
import asyncio
10-
from collections.abc import AsyncIterable, Callable
10+
from collections.abc import AsyncIterable
1111
from unittest.mock import MagicMock
1212
from uuid import UUID
1313

1414
import arrow
1515
import pytest
1616
from aiohttp.test_utils import TestClient
17-
from aioresponses import aioresponses
1817
from models_library.api_schemas_webserver.folders_v2 import FolderGet
1918
from models_library.api_schemas_webserver.projects import ProjectGet, ProjectListItem
2019
from models_library.api_schemas_webserver.workspaces import WorkspaceGet
2120
from models_library.rest_pagination import Page
2221
from pytest_mock import MockerFixture
2322
from pytest_simcore.helpers.assert_checks import assert_status
24-
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
25-
from pytest_simcore.helpers.typing_env import EnvVarsDict
26-
from pytest_simcore.helpers.webserver_login import NewUser, UserInfoDict
27-
from pytest_simcore.helpers.webserver_parametrizations import MockedStorageSubsystem
23+
from pytest_simcore.helpers.webserver_login import UserInfoDict
2824
from servicelib.aiohttp import status
2925
from simcore_service_webserver.db.models import UserRole
3026
from simcore_service_webserver.projects._groups_api import ProjectGroupGet
@@ -33,38 +29,11 @@
3329
from yarl import URL
3430

3531

36-
@pytest.fixture
37-
def app_environment(
38-
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
39-
) -> EnvVarsDict:
40-
return app_environment | setenvs_from_dict(
41-
monkeypatch, {"WEBSERVER_DEV_FEATURES_ENABLED": "1"}
42-
)
43-
44-
4532
@pytest.fixture
4633
def user_role() -> UserRole:
4734
return UserRole.USER
4835

4936

50-
@pytest.fixture
51-
def mocked_catalog(
52-
user_project: ProjectDict,
53-
catalog_subsystem_mock: Callable[[list[ProjectDict]], None],
54-
):
55-
catalog_subsystem_mock([user_project])
56-
57-
58-
@pytest.fixture
59-
def mocked_director_v2(director_v2_service_mock: aioresponses):
60-
...
61-
62-
63-
@pytest.fixture
64-
def mocked_storage(storage_subsystem_mock: MockedStorageSubsystem):
65-
...
66-
67-
6837
@pytest.mark.acceptance_test(
6938
"For https://github.com/ITISFoundation/osparc-simcore/pull/6579"
7039
)
@@ -190,21 +159,6 @@ async def test_trash_projects( # noqa: PLR0915
190159
mock_remove_dynamic_services.assert_awaited()
191160

192161

193-
@pytest.fixture
194-
async def other_user(
195-
client: TestClient, logged_user: UserInfoDict
196-
) -> AsyncIterable[UserInfoDict]:
197-
# new user different from logged_user
198-
async with NewUser(
199-
{
200-
"name": f"other_user_than_{logged_user['name']}",
201-
"role": "USER",
202-
},
203-
client.app,
204-
) as user:
205-
yield user
206-
207-
208162
async def test_trash_projects_shared_among_users(
209163
client: TestClient,
210164
logged_user: UserInfoDict,
@@ -432,7 +386,7 @@ async def test_trash_folder_with_content(
432386
await assert_status(resp, status.HTTP_200_OK)
433387
page = Page[FolderGet].model_validate(await resp.json())
434388
assert page.meta.total == 1
435-
assert page.data[0].folder_id == folder.folder_id
389+
assert page.data[0] == folder
436390

437391
resp = await client.get(
438392
"/v0/folders",
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# pylint: disable=protected-access
2+
# pylint: disable=redefined-outer-name
3+
# pylint: disable=too-many-arguments
4+
# pylint: disable=too-many-statements
5+
# pylint: disable=unused-argument
6+
# pylint: disable=unused-variable
7+
8+
9+
import contextlib
10+
from collections.abc import AsyncIterator
11+
12+
import pytest
13+
from aiohttp.test_utils import TestClient
14+
from models_library.api_schemas_webserver.projects import ProjectGet
15+
from pytest_simcore.helpers.assert_checks import assert_status
16+
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
17+
from pytest_simcore.helpers.typing_env import EnvVarsDict
18+
from pytest_simcore.helpers.webserver_login import UserInfoDict
19+
from servicelib.aiohttp import status
20+
from simcore_service_webserver.db.models import UserRole
21+
from simcore_service_webserver.projects import _trash_service
22+
from simcore_service_webserver.projects.models import ProjectDict
23+
from simcore_service_webserver.trash._service import delete_expired_trash
24+
25+
26+
@pytest.fixture
27+
def app_environment(
28+
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
29+
) -> EnvVarsDict:
30+
return app_environment | setenvs_from_dict(
31+
monkeypatch, {"TRASH_RETENTION_DAYS": "0"}
32+
)
33+
34+
35+
@pytest.fixture
36+
def user_role() -> UserRole:
37+
return UserRole.USER
38+
39+
40+
@contextlib.asynccontextmanager
41+
async def _client_session_with_user(
42+
client: TestClient, user: UserInfoDict
43+
) -> AsyncIterator[TestClient]:
44+
assert client.app
45+
46+
url = client.app.router["logout"].url_for()
47+
resp = await client.post(f"{url}")
48+
await assert_status(resp, status.HTTP_200_OK)
49+
50+
url = client.app.router["login"].url_for()
51+
resp = await client.post(
52+
f"{url}",
53+
json={
54+
"email": user["email"],
55+
"password": user["raw_password"],
56+
},
57+
)
58+
await assert_status(resp, status.HTTP_200_OK)
59+
60+
yield client
61+
62+
url = client.app.router["logout"].url_for()
63+
resp = await client.post(f"{url}")
64+
await assert_status(resp, status.HTTP_200_OK)
65+
66+
67+
async def test_trash_service__delete_expired_trash(
68+
client: TestClient,
69+
logged_user: UserInfoDict,
70+
user_project: ProjectDict,
71+
other_user: UserInfoDict,
72+
other_user_project: ProjectDict,
73+
mocked_catalog: None,
74+
mocked_director_v2: None,
75+
):
76+
assert client.app
77+
assert logged_user["id"] != other_user["id"]
78+
79+
# TRASH projects
80+
# logged_user trashes his project
81+
user_project_id = user_project["uuid"]
82+
await _trash_service.trash_project(
83+
client.app,
84+
product_name="osparc",
85+
user_id=logged_user["id"],
86+
project_id=user_project_id,
87+
force_stop_first=True,
88+
explicit=True,
89+
)
90+
91+
# other_user trashes his project
92+
other_user_project_id = other_user_project["uuid"]
93+
await _trash_service.trash_project(
94+
client.app,
95+
product_name="osparc",
96+
user_id=other_user["id"],
97+
project_id=other_user_project_id,
98+
force_stop_first=True,
99+
explicit=True,
100+
)
101+
102+
resp = await client.get(f"/v0/projects/{user_project_id}")
103+
data, _ = await assert_status(resp, status.HTTP_200_OK)
104+
assert ProjectGet.model_validate(data).trashed_by == logged_user["primary_gid"]
105+
106+
# UNDER TEST: Run delete_expired_trash
107+
await delete_expired_trash(client.app)
108+
109+
# ASSERT: logged_user tries to get the project and expects 404
110+
resp = await client.get(f"/v0/projects/{user_project_id}")
111+
await assert_status(resp, status.HTTP_404_NOT_FOUND)
112+
113+
# ASSERT: other_user tries to get the project and expects 404
114+
async with _client_session_with_user(client, other_user):
115+
resp = await client.get(f"/v0/projects/{other_user_project_id}")
116+
await assert_status(resp, status.HTTP_404_NOT_FOUND)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ async def user_project(
631631
fake_project: ProjectDict,
632632
logged_user: UserInfoDict,
633633
tests_data_dir: Path,
634-
osparc_product_name: str,
634+
osparc_product_name: ProductName,
635635
) -> AsyncIterator[ProjectDict]:
636636
async with NewProject(
637637
fake_project,

0 commit comments

Comments
 (0)