1616import sqlalchemy as sa
1717from aiohttp .test_utils import TestClient
1818from models_library .folders import FolderID
19- from models_library .projects import ProjectID
19+ from models_library .projects import ProjectID , ProjectTemplateType
2020from models_library .users import UserID
2121from pydantic import BaseModel , PositiveInt
2222from pytest_mock import MockerFixture
23+ from pytest_simcore .helpers .assert_checks import assert_status
2324from pytest_simcore .helpers .webserver_login import UserInfoDict
2425from pytest_simcore .helpers .webserver_parametrizations import (
2526 ExpectedResponse ,
2627 standard_role_response ,
2728)
2829from pytest_simcore .helpers .webserver_projects import create_project
30+ from servicelib .aiohttp import status
2931from simcore_postgres_database .models .folders_v2 import folders_v2
3032from simcore_postgres_database .models .projects_to_folders import projects_to_folders
3133from 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
4357def 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 ())
513527async 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