33# pylint: disable=unused-variable
44
55import asyncio
6+ import contextlib
67import logging
78import re
89from collections .abc import AsyncIterable , Awaitable , Callable
@@ -203,14 +204,20 @@ async def _fake_background_task(app: web.Application):
203204 )
204205
205206
206- async def login_user (client : TestClient ):
207+ async def login_user (client : TestClient , * , exit_stack : contextlib . AsyncExitStack ):
207208 """returns a logged in regular user"""
208- return await log_client_in (client = client , user_data = {"role" : UserRole .USER .name })
209+ return await log_client_in (
210+ client = client , user_data = {"role" : UserRole .USER .name }, exit_stack = exit_stack
211+ )
209212
210213
211- async def login_guest_user (client : TestClient ):
214+ async def login_guest_user (
215+ client : TestClient , * , exit_stack : contextlib .AsyncExitStack
216+ ):
212217 """returns a logged in Guest user"""
213- return await log_client_in (client = client , user_data = {"role" : UserRole .GUEST .name })
218+ return await log_client_in (
219+ client = client , user_data = {"role" : UserRole .GUEST .name }, exit_stack = exit_stack
220+ )
214221
215222
216223async def new_project (
@@ -482,10 +489,11 @@ async def test_t1_while_guest_is_connected_no_resources_are_removed(
482489 aiopg_engine : aiopg .sa .engine .Engine ,
483490 tests_data_dir : Path ,
484491 osparc_product_name : str ,
492+ exit_stack : contextlib .AsyncExitStack ,
485493):
486494 """while a GUEST user is connected GC will not remove none of its projects nor the user itself"""
487495 assert client .app
488- logged_guest_user = await login_guest_user (client )
496+ logged_guest_user = await login_guest_user (client , exit_stack = exit_stack )
489497 empty_guest_user_project = await new_project (
490498 client , logged_guest_user , osparc_product_name , tests_data_dir
491499 )
@@ -508,10 +516,11 @@ async def test_t2_cleanup_resources_after_browser_is_closed(
508516 aiopg_engine : aiopg .sa .engine .Engine ,
509517 tests_data_dir : Path ,
510518 osparc_product_name : str ,
519+ exit_stack : contextlib .AsyncExitStack ,
511520):
512521 """After a GUEST users with one opened project closes browser tab regularly (GC cleans everything)"""
513522 assert client .app
514- logged_guest_user = await login_guest_user (client )
523+ logged_guest_user = await login_guest_user (client , exit_stack = exit_stack )
515524 empty_guest_user_project = await new_project (
516525 client , logged_guest_user , osparc_product_name , tests_data_dir
517526 )
@@ -557,11 +566,12 @@ async def test_t3_gc_will_not_intervene_for_regular_users_and_their_resources(
557566 fake_project : dict ,
558567 tests_data_dir : Path ,
559568 osparc_product_name : str ,
569+ exit_stack : contextlib .AsyncExitStack ,
560570):
561571 """after a USER disconnects the GC will remove none of its projects or templates nor the user itself"""
562572 number_of_projects = 5
563573 number_of_templates = 5
564- logged_user = await login_user (client )
574+ logged_user = await login_user (client , exit_stack = exit_stack )
565575 user_projects = [
566576 await new_project (client , logged_user , osparc_product_name , tests_data_dir )
567577 for _ in range (number_of_projects )
@@ -604,16 +614,17 @@ async def test_t4_project_shared_with_group_transferred_to_user_in_group_on_owne
604614 aiopg_engine : aiopg .sa .engine .Engine ,
605615 tests_data_dir : Path ,
606616 osparc_product_name : str ,
617+ exit_stack : contextlib .AsyncExitStack ,
607618):
608619 """
609620 USER "u1" creates a GROUP "g1" and invites USERS "u2" and "u3";
610621 USER "u1" creates a project and shares it with "g1";
611622 USER "u1" is manually marked as "GUEST";
612623 EXPECTED: one of the users in the "g1" will become the new owner of the project and "u1" will be deleted
613624 """
614- u1 = await login_user (client )
615- u2 = await login_user (client )
616- u3 = await login_user (client )
625+ u1 = await login_user (client , exit_stack = exit_stack )
626+ u2 = await login_user (client , exit_stack = exit_stack )
627+ u3 = await login_user (client , exit_stack = exit_stack )
617628
618629 # creating g1 and inviting u2 and u3
619630 g1 = await get_group (client , u1 )
@@ -648,15 +659,16 @@ async def test_t5_project_shared_with_other_users_transferred_to_one_of_them(
648659 aiopg_engine : aiopg .sa .engine .Engine ,
649660 tests_data_dir : Path ,
650661 osparc_product_name : str ,
662+ exit_stack : contextlib .AsyncExitStack ,
651663):
652664 """
653665 USER "u1" creates a project and shares it with "u2" and "u3";
654666 USER "u1" is manually marked as "GUEST";
655667 EXPECTED: one of "u2" or "u3" will become the new owner of the project and "u1" will be deleted
656668 """
657- u1 = await login_user (client )
658- u2 = await login_user (client )
659- u3 = await login_user (client )
669+ u1 = await login_user (client , exit_stack = exit_stack )
670+ u2 = await login_user (client , exit_stack = exit_stack )
671+ u3 = await login_user (client , exit_stack = exit_stack )
660672
661673 q_u2 = await fetch_user_from_db (aiopg_engine , u2 )
662674 assert q_u2
@@ -694,6 +706,7 @@ async def test_t6_project_shared_with_group_transferred_to_last_user_in_group_on
694706 aiopg_engine : aiopg .sa .engine .Engine ,
695707 tests_data_dir : Path ,
696708 osparc_product_name : str ,
709+ exit_stack : contextlib .AsyncExitStack ,
697710):
698711 """
699712 USER "u1" creates a GROUP "g1" and invites USERS "u2" and "u3";
@@ -703,9 +716,9 @@ async def test_t6_project_shared_with_group_transferred_to_last_user_in_group_on
703716 the new owner either "u2" or "u3" will be manually marked as "GUEST";
704717 EXPECTED: the GUEST user will be deleted and the project will pass to the last member of "g1"
705718 """
706- u1 = await login_user (client )
707- u2 = await login_user (client )
708- u3 = await login_user (client )
719+ u1 = await login_user (client , exit_stack = exit_stack )
720+ u2 = await login_user (client , exit_stack = exit_stack )
721+ u3 = await login_user (client , exit_stack = exit_stack )
709722
710723 # creating g1 and inviting u2 and u3
711724 g1 = await get_group (client , u1 )
@@ -765,6 +778,7 @@ async def test_t7_project_shared_with_group_transferred_from_one_member_to_the_l
765778 aiopg_engine : aiopg .sa .engine .Engine ,
766779 tests_data_dir : Path ,
767780 osparc_product_name : str ,
781+ exit_stack : contextlib .AsyncExitStack ,
768782):
769783 """
770784 USER "u1" creates a GROUP "g1" and invites USERS "u2" and "u3";
@@ -777,9 +791,9 @@ async def test_t7_project_shared_with_group_transferred_from_one_member_to_the_l
777791 EXPECTED: the last user will be removed and the project will be removed
778792 """
779793 assert client .app
780- u1 = await login_user (client )
781- u2 = await login_user (client )
782- u3 = await login_user (client )
794+ u1 = await login_user (client , exit_stack = exit_stack )
795+ u2 = await login_user (client , exit_stack = exit_stack )
796+ u3 = await login_user (client , exit_stack = exit_stack )
783797
784798 # creating g1 and inviting u2 and u3
785799 g1 = await get_group (client , u1 )
@@ -853,6 +867,7 @@ async def test_t8_project_shared_with_other_users_transferred_to_one_of_them_unt
853867 aiopg_engine : aiopg .sa .engine .Engine ,
854868 tests_data_dir : Path ,
855869 osparc_product_name : str ,
870+ exit_stack : contextlib .AsyncExitStack ,
856871):
857872 """
858873 USER "u1" creates a project and shares it with "u2" and "u3";
@@ -861,9 +876,9 @@ async def test_t8_project_shared_with_other_users_transferred_to_one_of_them_unt
861876 same as T5 => afterwards afterwards the new owner either "u2" or "u3" will be manually marked as "GUEST";
862877 EXPECTED: the GUEST user will be deleted and the project will pass to the last member of "g1"
863878 """
864- u1 = await login_user (client )
865- u2 = await login_user (client )
866- u3 = await login_user (client )
879+ u1 = await login_user (client , exit_stack = exit_stack )
880+ u2 = await login_user (client , exit_stack = exit_stack )
881+ u3 = await login_user (client , exit_stack = exit_stack )
867882
868883 q_u2 = await fetch_user_from_db (aiopg_engine , u2 )
869884 assert q_u2
@@ -927,6 +942,7 @@ async def test_t9_project_shared_with_other_users_transferred_between_them_and_t
927942 aiopg_engine : aiopg .sa .engine .Engine ,
928943 tests_data_dir : Path ,
929944 osparc_product_name : str ,
945+ exit_stack : contextlib .AsyncExitStack ,
930946):
931947 """
932948 USER "u1" creates a project and shares it with "u2" and "u3";
@@ -937,9 +953,9 @@ async def test_t9_project_shared_with_other_users_transferred_between_them_and_t
937953 same as T8 => afterwards the last user will be marked as "GUEST";
938954 EXPECTED: the last user will be removed and the project will be removed
939955 """
940- u1 = await login_user (client )
941- u2 = await login_user (client )
942- u3 = await login_user (client )
956+ u1 = await login_user (client , exit_stack = exit_stack )
957+ u2 = await login_user (client , exit_stack = exit_stack )
958+ u3 = await login_user (client , exit_stack = exit_stack )
943959
944960 q_u2 = await fetch_user_from_db (aiopg_engine , u2 )
945961 assert q_u2
@@ -1014,6 +1030,7 @@ async def test_t10_owner_and_all_shared_users_marked_as_guests(
10141030 aiopg_engine : aiopg .sa .engine .Engine ,
10151031 tests_data_dir : Path ,
10161032 osparc_product_name : str ,
1033+ exit_stack : contextlib .AsyncExitStack ,
10171034):
10181035 """
10191036 USER "u1" creates a project and shares it with "u2" and "u3";
@@ -1026,9 +1043,9 @@ async def test_t10_owner_and_all_shared_users_marked_as_guests(
10261043 )
10271044 assert not gc_task .done ()
10281045
1029- u1 = await login_user (client )
1030- u2 = await login_user (client )
1031- u3 = await login_user (client )
1046+ u1 = await login_user (client , exit_stack = exit_stack )
1047+ u2 = await login_user (client , exit_stack = exit_stack )
1048+ u3 = await login_user (client , exit_stack = exit_stack )
10321049
10331050 q_u2 = await fetch_user_from_db (aiopg_engine , u2 )
10341051 q_u3 = await fetch_user_from_db (aiopg_engine , u3 )
@@ -1067,16 +1084,17 @@ async def test_t11_owner_and_all_users_in_group_marked_as_guests(
10671084 aiopg_engine : aiopg .sa .engine .Engine ,
10681085 tests_data_dir : Path ,
10691086 osparc_product_name : str ,
1087+ exit_stack : contextlib .AsyncExitStack ,
10701088):
10711089 """
10721090 USER "u1" creates a group and invites "u2" and "u3";
10731091 USER "u1" creates a project and shares it with the group
10741092 USER "u1", "u2" and "u3" are manually marked as "GUEST"
10751093 EXPECTED: the project and all the users are removed
10761094 """
1077- u1 = await login_user (client )
1078- u2 = await login_user (client )
1079- u3 = await login_user (client )
1095+ u1 = await login_user (client , exit_stack = exit_stack )
1096+ u2 = await login_user (client , exit_stack = exit_stack )
1097+ u3 = await login_user (client , exit_stack = exit_stack )
10801098
10811099 # creating g1 and inviting u2 and u3
10821100 g1 = await get_group (client , u1 )
0 commit comments