11# pylint: disable=redefined-outer-name 
22# pylint: disable=unused-argument 
3+ # pylint: disable=too-many-arguments 
34
45import  datetime 
56from  collections .abc  import  Callable 
2728    FunctionsWriteApiAccessDeniedError ,
2829    FunctionWriteAccessDeniedError ,
2930)
31+ from  models_library .groups  import  EVERYONE_GROUP_ID 
3032from  models_library .products  import  ProductName 
3133from  models_library .rest_ordering  import  OrderBy , OrderDirection 
34+ from  models_library .rest_pagination  import  PageMetaInfoLimitOffset 
3235from  pytest_simcore .helpers .webserver_users  import  UserInfoDict 
3336from  servicelib .rabbitmq .rpc_interfaces .webserver .v1  import  WebServerRpcClient 
3437
@@ -194,7 +197,7 @@ async def test_list_functions(
194197    )
195198
196199    # Assert the list contains the registered function 
197-     assert  len (functions ) >   0 
200+     assert  len (functions ) ==   1 
198201    assert  any (f .uid  ==  registered_function .uid  for  f  in  functions )
199202
200203
@@ -221,7 +224,7 @@ async def test_list_functions_mixed_user(
221224        )
222225        for  _  in  range (2 )
223226    ]
224- 
227+      assert   int ( logged_user [ "primary_gid" ])  !=   1 
225228    # List functions for the other logged user 
226229    other_functions , _  =  await  webserver_rpc_client .functions .list_functions (
227230        pagination_limit = 10 ,
@@ -250,7 +253,7 @@ async def test_list_functions_mixed_user(
250253        product_name = osparc_product_name ,
251254    )
252255    # Assert the list contains only the logged user's function 
253-     assert  len (functions ) ==  2 
256+     assert  len (functions ) ==  len ( registered_functions ) 
254257    assert  all (f .uid  in  [rf .uid  for  rf  in  registered_functions ] for  f  in  functions )
255258
256259    other_functions , _  =  await  webserver_rpc_client .functions .list_functions (
@@ -260,11 +263,49 @@ async def test_list_functions_mixed_user(
260263        product_name = osparc_product_name ,
261264    )
262265    # Assert the list contains only the other user's functions 
263-     assert  len (other_functions ) ==  3 
266+     assert  len (other_functions ) ==  len ( other_registered_function ) 
264267    assert  all (
265268        f .uid  in  [orf .uid  for  orf  in  other_registered_function ] for  f  in  other_functions 
266269    )
267270
271+     # Add other-user permissions to a logged user function 
272+     await  webserver_rpc_client .functions .set_group_permissions (
273+         object_type = "function" ,
274+         permission_group_id = int (other_logged_user ["primary_gid" ]),
275+         object_ids = [registered_functions [0 ].uid ],
276+         user_id = logged_user ["id" ],
277+         product_name = osparc_product_name ,
278+         read = True ,
279+     )
280+ 
281+     other_functions , _  =  await  webserver_rpc_client .functions .list_functions (
282+         pagination_limit = 10 ,
283+         pagination_offset = 0 ,
284+         user_id = other_logged_user ["id" ],
285+         product_name = osparc_product_name ,
286+     )
287+ 
288+     assert  len (other_functions ) ==  len (other_registered_function ) +  1 
289+     assert  any (f .uid  ==  registered_functions [0 ].uid  for  f  in  other_functions )
290+ 
291+     # Add all-user permissions to a logged user function 
292+     await  webserver_rpc_client .functions .set_group_permissions (
293+         object_type = "function" ,
294+         permission_group_id = EVERYONE_GROUP_ID ,
295+         object_ids = [registered_functions [0 ].uid ],
296+         user_id = logged_user ["id" ],
297+         product_name = osparc_product_name ,
298+         read = True ,
299+         write = True ,
300+     )
301+     other_functions , _  =  await  webserver_rpc_client .functions .list_functions (
302+         pagination_limit = 10 ,
303+         pagination_offset = 0 ,
304+         user_id = other_logged_user ["id" ],
305+         product_name = osparc_product_name ,
306+     )
307+     assert  len (other_functions ) ==  len (other_registered_function ) +  1 
308+ 
268309
269310@pytest .mark .parametrize ("user_role" , [UserRole .USER ]) 
270311@pytest .mark .parametrize ( 
@@ -276,38 +317,48 @@ async def test_list_functions_mixed_user(
276317    ], 
277318) 
278319@pytest .mark .parametrize ( 
279-     "test_pagination_limit, test_pagination_offset" , 
320+     "test_pagination_limit, test_pagination_offset, total_number_functions " , 
280321    [ 
281-         (5 , 0 ), 
282-         (2 , 2 ), 
283-         (12 , 4 ), 
322+         (5 , 0 ,  10 ), 
323+         (2 , 2 ,  10 ), 
324+         (12 , 4 ,  10 ), 
284325    ], 
285326) 
286327async  def  test_list_functions_with_pagination_ordering (
287328    client : TestClient ,
288329    add_user_function_api_access_rights : None ,
289330    webserver_rpc_client : WebServerRpcClient ,
290331    create_fake_function_obj : Callable [[FunctionClass ], ProjectFunction ],
291-     clean_functions : None ,
292332    osparc_product_name : ProductName ,
293333    logged_user : UserInfoDict ,
294334    order_by : OrderBy  |  None ,
295335    test_pagination_limit : int ,
296336    test_pagination_offset : int ,
337+     total_number_functions : int ,
338+     clean_functions : None ,
297339):
340+     # Making sure functions are empty before we start 
341+     assert  await  webserver_rpc_client .functions .list_functions (
342+         pagination_limit = 1 ,
343+         pagination_offset = 0 ,
344+         user_id = logged_user ["id" ],
345+         product_name = osparc_product_name ,
346+     ) ==  (
347+         [],
348+         PageMetaInfoLimitOffset (limit = 1 , total = 0 , offset = 0 , count = 0 ),
349+     )
298350    # Register multiple functions 
299-     TOTAL_FUNCTIONS  =  10 
300351    registered_functions  =  [
301352        await  webserver_rpc_client .functions .register_function (
302353            function = create_fake_function_obj (FunctionClass .PROJECT ),
303354            user_id = logged_user ["id" ],
304355            product_name = osparc_product_name ,
305356        )
306-         for  _  in  range (TOTAL_FUNCTIONS )
357+         for  _  in  range (total_number_functions )
307358    ]
308359
309360    # List functions with pagination 
310-     functions , page_info  =  await  webserver_rpc_client .functions .list_functions (
361+     listed_functions , page_info  =  await  webserver_rpc_client .functions .list_functions (
311362        pagination_limit = test_pagination_limit ,
312363        pagination_offset = test_pagination_offset ,
313364        user_id = logged_user ["id" ],
@@ -316,23 +367,26 @@ async def test_list_functions_with_pagination_ordering(
316367    )
317368
318369    # Assert the list contains the correct number of functions 
319-     assert  len (functions ) ==  min (
320-         test_pagination_limit , max (0 , TOTAL_FUNCTIONS  -  test_pagination_offset )
370+     assert  len (listed_functions ) ==  min (
371+         test_pagination_limit , max (0 , total_number_functions  -  test_pagination_offset )
321372    )
322-     assert  all (f .uid  in  [rf .uid  for  rf  in  registered_functions ] for  f  in  functions )
323-     assert  page_info .count  ==  len (functions )
324-     assert  page_info .total  ==  TOTAL_FUNCTIONS 
373+ 
374+     assert  all (
375+         f .uid  in  [rf .uid  for  rf  in  registered_functions ] for  f  in  listed_functions 
376+     )
377+     assert  page_info .count  ==  len (listed_functions )
378+     assert  page_info .total  ==  total_number_functions 
325379
326380    # Verify the functions are sorted correctly based on the order_by parameter 
327381    if  order_by :
328382        field  =  order_by .field 
329383        direction  =  order_by .direction 
330384        sorted_functions  =  sorted (
331-             functions ,
385+             listed_functions ,
332386            key = lambda  f : getattr (f , field ),
333387            reverse = (direction  ==  OrderDirection .DESC ),
334388        )
335-         assert  functions  ==  sorted_functions 
389+         assert  listed_functions  ==  sorted_functions 
336390
337391
338392@pytest .mark .parametrize ( 
0 commit comments