77
88
99import functools
10- from collections .abc import Iterator
10+ from collections .abc import AsyncIterator
1111from copy import deepcopy
1212from http import HTTPStatus
1313from typing import Any
2121from models_library .api_schemas_webserver .users import (
2222 MyProfileRestGet ,
2323)
24+ from models_library .groups import AccessRightsDict
2425from psycopg2 import OperationalError
2526from pytest_simcore .helpers .assert_checks import assert_status
2627from pytest_simcore .helpers .monkeypatch_envs import EnvVarsDict , setenvs_from_dict
27- from pytest_simcore .helpers .postgres_tools import sync_insert_and_get_row_lifespan
2828from pytest_simcore .helpers .webserver_users import UserInfoDict
2929from servicelib .aiohttp import status
3030from servicelib .rest_constants import RESPONSE_MODEL_POLICY
3131from simcore_service_webserver .user_preferences ._service import (
3232 get_frontend_user_preferences_aggregation ,
3333)
3434from sqlalchemy .exc import OperationalError as SQLAlchemyOperationalError
35- from sqlalchemy .ext .asyncio import AsyncConnection
35+ from sqlalchemy .ext .asyncio import AsyncConnection , AsyncEngine
3636
3737
3838@pytest .fixture
@@ -50,12 +50,13 @@ def app_environment(
5050 )
5151
5252
53- @pytest .fixture ( scope = "module" )
54- def support_group (
55- postgres_db : sa . engine . Engine ,
53+ @pytest .fixture
54+ async def support_group (
55+ asyncpg_engine : AsyncEngine ,
5656 product_name : str ,
57- ) -> Iterator [dict [str , Any ]]:
57+ ) -> AsyncIterator [dict [str , Any ]]:
5858 """Creates a standard support group and assigns it to the current product"""
59+ from pytest_simcore .helpers .postgres_tools import insert_and_get_row_lifespan
5960 from simcore_postgres_database .models .groups import groups
6061 from simcore_postgres_database .models .products import products
6162
@@ -66,23 +67,25 @@ def support_group(
6667 "type" : "STANDARD" ,
6768 }
6869
69- with sync_insert_and_get_row_lifespan (
70- postgres_db ,
70+ # pylint: disable=contextmanager-generator-missing-cleanup
71+ async with insert_and_get_row_lifespan (
72+ asyncpg_engine ,
7173 table = groups ,
7274 values = group_values ,
7375 pk_col = groups .c .gid ,
7476 ) as group_row :
7577 group_id = group_row ["gid" ]
7678
7779 # Update product to set support_standard_group_id
78- with postgres_db .begin () as conn :
79- conn .execute (
80+ async with asyncpg_engine .begin () as conn :
81+ await conn .execute (
8082 sa .update (products )
8183 .where (products .c .name == product_name )
8284 .values (support_standard_group_id = group_id )
8385 )
8486
8587 yield group_row
88+ # group will be deleted after test
8689
8790
8891@pytest .mark .parametrize (
@@ -131,7 +134,7 @@ async def test_access_update_profile(
131134
132135
133136@pytest .mark .parametrize ("user_role" , [UserRole .USER ])
134- async def test_get_profile (
137+ async def test_get_profile_user_not_in_support_group (
135138 user_role : UserRole ,
136139 logged_user : UserInfoDict ,
137140 client : TestClient ,
@@ -193,6 +196,81 @@ async def test_get_profile(
193196 )
194197
195198
199+ @pytest .mark .test
200+ @pytest .mark .parametrize ("user_role" , [UserRole .USER ])
201+ async def test_get_profile_user_in_support_group (
202+ user_role : UserRole ,
203+ logged_user : UserInfoDict ,
204+ client : TestClient ,
205+ primary_group : dict [str , Any ],
206+ standard_groups : list [dict [str , Any ]],
207+ all_group : dict [str , str ],
208+ support_group : dict [str , Any ],
209+ ):
210+ assert client .app
211+ from simcore_service_webserver .groups import _groups_repository
212+
213+ # Now add user to support group with read-only access
214+ await _groups_repository .add_new_user_in_group (
215+ client .app ,
216+ group_id = support_group ["gid" ],
217+ new_user_id = logged_user ["id" ],
218+ access_rights = AccessRightsDict (read = True , write = False , delete = False ),
219+ )
220+
221+ url = client .app .router ["get_my_profile" ].url_for ()
222+ assert url .path == "/v0/me"
223+
224+ resp = await client .get (f"{ url } " )
225+ data , error = await assert_status (resp , status .HTTP_200_OK )
226+
227+ assert not error
228+ profile = MyProfileRestGet .model_validate (data )
229+
230+ assert profile .login == logged_user ["email" ]
231+ assert profile .first_name == logged_user .get ("first_name" , None )
232+ assert profile .last_name == logged_user .get ("last_name" , None )
233+ assert profile .role == user_role .name
234+ assert profile .groups
235+ assert profile .expiration_date is None
236+
237+ got_profile_groups = profile .groups .model_dump (** RESPONSE_MODEL_POLICY , mode = "json" )
238+ assert got_profile_groups ["me" ] == primary_group
239+ assert got_profile_groups ["all" ] == all_group
240+ assert got_profile_groups ["product" ] == {
241+ "accessRights" : {"delete" : False , "read" : False , "write" : False },
242+ "description" : "osparc product group" ,
243+ "gid" : 3 ,
244+ "label" : "osparc" ,
245+ "thumbnail" : None ,
246+ }
247+
248+ # support group exists
249+ assert got_profile_groups ["support" ]
250+ support_group_id = got_profile_groups ["support" ]["gid" ]
251+
252+ assert support_group_id == support_group ["gid" ]
253+ assert got_profile_groups ["support" ]["description" ] == support_group ["description" ]
254+ assert "accessRights" not in got_profile_groups ["support" ]
255+
256+ # When user is part of support group, it should appear in standard groups
257+ sorted_by_group_id = functools .partial (sorted , key = lambda d : d ["gid" ])
258+ expected_standard_groups = [
259+ * standard_groups ,
260+ {
261+ "gid" : support_group_id ,
262+ "label" : support_group ["name" ],
263+ "description" : support_group ["description" ],
264+ "thumbnail" : None ,
265+ "accessRights" : {"read" : True , "write" : False , "delete" : False },
266+ },
267+ ]
268+ assert sorted_by_group_id (
269+ got_profile_groups ["organizations" ]
270+ ) == sorted_by_group_id (expected_standard_groups )
271+ assert support_group_id in {g ["gid" ] for g in got_profile_groups ["organizations" ]}
272+
273+
196274@pytest .mark .parametrize ("user_role" , [UserRole .USER ])
197275async def test_update_profile (
198276 user_role : UserRole ,
0 commit comments