Skip to content

Commit 0ce1efc

Browse files
unit tests
1 parent c3ef94d commit 0ce1efc

File tree

3 files changed

+133
-7
lines changed

3 files changed

+133
-7
lines changed

packages/models-library/src/models_library/projects.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from datetime import datetime
6-
from enum import Enum, auto
6+
from enum import Enum
77
from typing import Annotated, Any, Final, TypeAlias
88
from uuid import UUID
99

@@ -54,9 +54,9 @@ class ProjectType(str, Enum):
5454

5555

5656
class ProjectTemplateType(StrAutoEnum):
57-
TEMPLATE = auto()
58-
TUTORIAL = auto()
59-
HYPERTOOL = auto()
57+
TEMPLATE = "TEMPLATE"
58+
TUTORIAL = "TUTORIAL"
59+
HYPERTOOL = "HYPERTOOL"
6060

6161

6262
class BaseProjectModel(BaseModel):

services/web/server/src/simcore_service_webserver/projects/_controller/_rest_exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ..exceptions import (
2828
ClustersKeeperNotAvailableError,
2929
DefaultPricingUnitNotFoundError,
30+
InsufficientRoleForProjectTemplateTypeUpdateError,
3031
NodeNotFoundError,
3132
ParentNodeNotFoundError,
3233
ProjectDeleteError,
@@ -40,6 +41,7 @@
4041
ProjectOwnerNotFoundInTheProjectAccessRightsError,
4142
ProjectStartsTooManyDynamicNodesError,
4243
ProjectTooManyProjectOpenedError,
44+
ProjectTypeAndTemplateIncompatibilityError,
4345
ProjectWalletPendingTransactionError,
4446
WrongTagIdsInQueryError,
4547
)
@@ -88,6 +90,10 @@
8890
status.HTTP_403_FORBIDDEN,
8991
"Do not have sufficient access rights on project {project_uuid} for this action",
9092
),
93+
InsufficientRoleForProjectTemplateTypeUpdateError: HttpErrorInfo(
94+
status.HTTP_403_FORBIDDEN,
95+
"Do not have sufficient access rights on updating project template type",
96+
),
9197
ProjectInvalidUsageError: HttpErrorInfo(
9298
status.HTTP_422_UNPROCESSABLE_ENTITY,
9399
"Invalid usage for project",
@@ -124,6 +130,10 @@
124130
status.HTTP_400_BAD_REQUEST,
125131
"Wrong tag IDs in query",
126132
),
133+
ProjectTypeAndTemplateIncompatibilityError: HttpErrorInfo(
134+
status.HTTP_400_BAD_REQUEST,
135+
"Wrong project type and template type combination: {reason}",
136+
),
127137
}
128138

129139

services/web/server/tests/unit/with_dbs/02/test_projects_crud_handlers__list_with_query_params.py

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
import sqlalchemy as sa
1717
from aiohttp.test_utils import TestClient
1818
from models_library.folders import FolderID
19-
from models_library.projects import ProjectID
19+
from models_library.projects import ProjectID, ProjectTemplateType
2020
from models_library.users import UserID
2121
from pydantic import BaseModel, PositiveInt
2222
from pytest_mock import MockerFixture
23+
from pytest_simcore.helpers.assert_checks import assert_status
2324
from pytest_simcore.helpers.webserver_login import UserInfoDict
2425
from pytest_simcore.helpers.webserver_parametrizations import (
2526
ExpectedResponse,
2627
standard_role_response,
2728
)
2829
from pytest_simcore.helpers.webserver_projects import create_project
30+
from servicelib.aiohttp import status
2931
from simcore_postgres_database.models.folders_v2 import folders_v2
3032
from simcore_postgres_database.models.projects_to_folders import projects_to_folders
3133
from simcore_service_webserver._meta import api_version_prefix
@@ -39,6 +41,18 @@ def standard_user_role() -> tuple[str, tuple[UserRole, ExpectedResponse]]:
3941
return (all_roles[0], [pytest.param(*all_roles[1][2], id="standard user role")])
4042

4143

44+
def standard_and_tester_user_roles() -> tuple[str, tuple[UserRole, ExpectedResponse]]:
45+
all_roles = standard_role_response()
46+
47+
return (
48+
all_roles[0],
49+
[
50+
pytest.param(*all_roles[1][2], id="standard user role"),
51+
pytest.param(*all_roles[1][3], id="tester_user_role"),
52+
],
53+
)
54+
55+
4256
@pytest.fixture
4357
def mock_catalog_api_get_services_for_user_in_product(mocker: MockerFixture):
4458
mocker.patch(
@@ -509,7 +523,7 @@ async def test_list_projects_for_specific_folder_id(
509523
)
510524

511525

512-
@pytest.mark.parametrize(*standard_user_role())
526+
@pytest.mark.parametrize(*standard_and_tester_user_roles())
513527
async def test_list_and_patch_projects_with_template_type(
514528
client: TestClient,
515529
logged_user: UserDict,
@@ -527,16 +541,18 @@ async def test_list_and_patch_projects_with_template_type(
527541
"TEMPLATE",
528542
"TEMPLATE",
529543
]
544+
generated_projects = []
530545
for _type in projects_type:
531546
project_data = deepcopy(fake_project)
532-
await _new_project(
547+
prj = await _new_project(
533548
client,
534549
logged_user["id"],
535550
osparc_product_name,
536551
tests_data_dir,
537552
project_data,
538553
as_template=_type == "TEMPLATE",
539554
)
555+
generated_projects.append(prj)
540556

541557
base_url = client.app.router["list_projects"].url_for()
542558
# Now we will test listing with type=user
@@ -589,3 +605,103 @@ async def test_list_and_patch_projects_with_template_type(
589605
resp = await client.get(f"{url}")
590606
data = await resp.json()
591607
assert resp.status == 422
608+
609+
# Now we will test listing with type=template and template_type=TEMPLATE
610+
query_parameters = {"type": "template", "template_type": "TEMPLATE"}
611+
url = base_url.with_query(**query_parameters)
612+
613+
resp = await client.get(f"{url}")
614+
data = await resp.json()
615+
616+
assert resp.status == 200
617+
_assert_response_data(
618+
data,
619+
2,
620+
0,
621+
2,
622+
"/v0/projects?type=template&template_type=TEMPLATE&offset=0&limit=20",
623+
2,
624+
)
625+
626+
# Lets now PATCH the template project (Currently user is not tester so it should fail)
627+
patch_url = client.app.router["patch_project"].url_for(
628+
project_id=generated_projects[-1]["uuid"] # <-- Patching template project
629+
)
630+
resp = await client.patch(
631+
f"{patch_url}",
632+
data=json.dumps(
633+
{
634+
"templateType": ProjectTemplateType.HYPERTOOL.value,
635+
}
636+
),
637+
)
638+
if UserRole(logged_user["role"]) >= UserRole.TESTER:
639+
await assert_status(resp, status.HTTP_204_NO_CONTENT)
640+
else:
641+
await assert_status(resp, status.HTTP_403_FORBIDDEN)
642+
643+
if UserRole(logged_user["role"]) >= UserRole.TESTER:
644+
# Now we will test listing with type=user and template_type=null
645+
query_parameters = {"type": "user", "template_type": "null"}
646+
url = base_url.with_query(**query_parameters)
647+
648+
resp = await client.get(f"{url}")
649+
data = await resp.json()
650+
651+
assert resp.status == 200
652+
_assert_response_data(
653+
data,
654+
3,
655+
0,
656+
3,
657+
"/v0/projects?type=user&template_type=null&offset=0&limit=20",
658+
3,
659+
)
660+
661+
# Now we will test listing with type=user and template_type=HYPERTOOL
662+
query_parameters = {"type": "template", "template_type": "HYPERTOOL"}
663+
url = base_url.with_query(**query_parameters)
664+
665+
resp = await client.get(f"{url}")
666+
data = await resp.json()
667+
668+
assert resp.status == 200
669+
_assert_response_data(
670+
data,
671+
1,
672+
0,
673+
1,
674+
"/v0/projects?type=template&template_type=HYPERTOOL&offset=0&limit=20",
675+
1,
676+
)
677+
678+
# Now we will test listing with type=template and template_type=TEMPLATE
679+
query_parameters = {"type": "template", "template_type": "TEMPLATE"}
680+
url = base_url.with_query(**query_parameters)
681+
682+
resp = await client.get(f"{url}")
683+
data = await resp.json()
684+
685+
assert resp.status == 200
686+
_assert_response_data(
687+
data,
688+
1,
689+
0,
690+
1,
691+
"/v0/projects?type=template&template_type=TEMPLATE&offset=0&limit=20",
692+
1,
693+
)
694+
695+
# Lets now PATCH the standard project
696+
patch_url = client.app.router["patch_project"].url_for(
697+
project_id=generated_projects[0]["uuid"] # <-- Patching standard project
698+
)
699+
resp = await client.patch(
700+
f"{patch_url}",
701+
data=json.dumps(
702+
{
703+
"templateType": ProjectTemplateType.HYPERTOOL.value,
704+
}
705+
),
706+
)
707+
await assert_status(resp, status.HTTP_400_BAD_REQUEST)

0 commit comments

Comments
 (0)