33# pylint: disable=unused-variable
44
55import asyncio
6+ import contextlib
67import logging
78import re
8- from collections .abc import AsyncIterable , Awaitable , Callable
9+ from collections .abc import AsyncIterable , AsyncIterator , Awaitable , Callable
910from copy import deepcopy
1011from enum import Enum
1112from pathlib import Path
8283WAIT_FOR_COMPLETE_GC_CYCLE = GARBAGE_COLLECTOR_INTERVAL + SERVICE_DELETION_DELAY + 2
8384
8485
86+ @pytest .fixture
87+ async def exit_stack () -> AsyncIterator [contextlib .AsyncExitStack ]:
88+ """Provides an AsyncExitStack that gets cleaned up after each test"""
89+ async with contextlib .AsyncExitStack () as stack :
90+ yield stack
91+
92+
8593@pytest .fixture (autouse = True )
8694def _drop_and_recreate_postgres (database_from_template_before_each_function ):
8795 return
@@ -203,14 +211,20 @@ async def _fake_background_task(app: web.Application):
203211 )
204212
205213
206- async def login_user (client : TestClient ):
214+ async def login_user (client : TestClient , * , exit_stack : contextlib . AsyncExitStack ):
207215 """returns a logged in regular user"""
208- return await log_client_in (client = client , user_data = {"role" : UserRole .USER .name })
216+ return await log_client_in (
217+ client = client , user_data = {"role" : UserRole .USER .name }, exit_stack = exit_stack
218+ )
209219
210220
211- async def login_guest_user (client : TestClient ):
221+ async def login_guest_user (
222+ client : TestClient , * , exit_stack : contextlib .AsyncExitStack
223+ ):
212224 """returns a logged in Guest user"""
213- return await log_client_in (client = client , user_data = {"role" : UserRole .GUEST .name })
225+ return await log_client_in (
226+ client = client , user_data = {"role" : UserRole .GUEST .name }, exit_stack = exit_stack
227+ )
214228
215229
216230async def new_project (
@@ -482,10 +496,11 @@ async def test_t1_while_guest_is_connected_no_resources_are_removed(
482496 aiopg_engine : aiopg .sa .engine .Engine ,
483497 tests_data_dir : Path ,
484498 osparc_product_name : str ,
499+ exit_stack : contextlib .AsyncExitStack ,
485500):
486501 """while a GUEST user is connected GC will not remove none of its projects nor the user itself"""
487502 assert client .app
488- logged_guest_user = await login_guest_user (client )
503+ logged_guest_user = await login_guest_user (client , exit_stack = exit_stack )
489504 empty_guest_user_project = await new_project (
490505 client , logged_guest_user , osparc_product_name , tests_data_dir
491506 )
@@ -508,10 +523,11 @@ async def test_t2_cleanup_resources_after_browser_is_closed(
508523 aiopg_engine : aiopg .sa .engine .Engine ,
509524 tests_data_dir : Path ,
510525 osparc_product_name : str ,
526+ exit_stack : contextlib .AsyncExitStack ,
511527):
512528 """After a GUEST users with one opened project closes browser tab regularly (GC cleans everything)"""
513529 assert client .app
514- logged_guest_user = await login_guest_user (client )
530+ logged_guest_user = await login_guest_user (client , exit_stack = exit_stack )
515531 empty_guest_user_project = await new_project (
516532 client , logged_guest_user , osparc_product_name , tests_data_dir
517533 )
@@ -557,11 +573,12 @@ async def test_t3_gc_will_not_intervene_for_regular_users_and_their_resources(
557573 fake_project : dict ,
558574 tests_data_dir : Path ,
559575 osparc_product_name : str ,
576+ exit_stack : contextlib .AsyncExitStack ,
560577):
561578 """after a USER disconnects the GC will remove none of its projects or templates nor the user itself"""
562579 number_of_projects = 5
563580 number_of_templates = 5
564- logged_user = await login_user (client )
581+ logged_user = await login_user (client , exit_stack = exit_stack )
565582 user_projects = [
566583 await new_project (client , logged_user , osparc_product_name , tests_data_dir )
567584 for _ in range (number_of_projects )
@@ -604,16 +621,17 @@ async def test_t4_project_shared_with_group_transferred_to_user_in_group_on_owne
604621 aiopg_engine : aiopg .sa .engine .Engine ,
605622 tests_data_dir : Path ,
606623 osparc_product_name : str ,
624+ exit_stack : contextlib .AsyncExitStack ,
607625):
608626 """
609627 USER "u1" creates a GROUP "g1" and invites USERS "u2" and "u3";
610628 USER "u1" creates a project and shares it with "g1";
611629 USER "u1" is manually marked as "GUEST";
612630 EXPECTED: one of the users in the "g1" will become the new owner of the project and "u1" will be deleted
613631 """
614- u1 = await login_user (client )
615- u2 = await login_user (client )
616- u3 = await login_user (client )
632+ u1 = await login_user (client , exit_stack = exit_stack )
633+ u2 = await login_user (client , exit_stack = exit_stack )
634+ u3 = await login_user (client , exit_stack = exit_stack )
617635
618636 # creating g1 and inviting u2 and u3
619637 g1 = await get_group (client , u1 )
@@ -648,15 +666,16 @@ async def test_t5_project_shared_with_other_users_transferred_to_one_of_them(
648666 aiopg_engine : aiopg .sa .engine .Engine ,
649667 tests_data_dir : Path ,
650668 osparc_product_name : str ,
669+ exit_stack : contextlib .AsyncExitStack ,
651670):
652671 """
653672 USER "u1" creates a project and shares it with "u2" and "u3";
654673 USER "u1" is manually marked as "GUEST";
655674 EXPECTED: one of "u2" or "u3" will become the new owner of the project and "u1" will be deleted
656675 """
657- u1 = await login_user (client )
658- u2 = await login_user (client )
659- u3 = await login_user (client )
676+ u1 = await login_user (client , exit_stack = exit_stack )
677+ u2 = await login_user (client , exit_stack = exit_stack )
678+ u3 = await login_user (client , exit_stack = exit_stack )
660679
661680 q_u2 = await fetch_user_from_db (aiopg_engine , u2 )
662681 assert q_u2
@@ -694,6 +713,7 @@ async def test_t6_project_shared_with_group_transferred_to_last_user_in_group_on
694713 aiopg_engine : aiopg .sa .engine .Engine ,
695714 tests_data_dir : Path ,
696715 osparc_product_name : str ,
716+ exit_stack : contextlib .AsyncExitStack ,
697717):
698718 """
699719 USER "u1" creates a GROUP "g1" and invites USERS "u2" and "u3";
@@ -703,9 +723,9 @@ async def test_t6_project_shared_with_group_transferred_to_last_user_in_group_on
703723 the new owner either "u2" or "u3" will be manually marked as "GUEST";
704724 EXPECTED: the GUEST user will be deleted and the project will pass to the last member of "g1"
705725 """
706- u1 = await login_user (client )
707- u2 = await login_user (client )
708- u3 = await login_user (client )
726+ u1 = await login_user (client , exit_stack = exit_stack )
727+ u2 = await login_user (client , exit_stack = exit_stack )
728+ u3 = await login_user (client , exit_stack = exit_stack )
709729
710730 # creating g1 and inviting u2 and u3
711731 g1 = await get_group (client , u1 )
@@ -765,6 +785,7 @@ async def test_t7_project_shared_with_group_transferred_from_one_member_to_the_l
765785 aiopg_engine : aiopg .sa .engine .Engine ,
766786 tests_data_dir : Path ,
767787 osparc_product_name : str ,
788+ exit_stack : contextlib .AsyncExitStack ,
768789):
769790 """
770791 USER "u1" creates a GROUP "g1" and invites USERS "u2" and "u3";
@@ -777,9 +798,9 @@ async def test_t7_project_shared_with_group_transferred_from_one_member_to_the_l
777798 EXPECTED: the last user will be removed and the project will be removed
778799 """
779800 assert client .app
780- u1 = await login_user (client )
781- u2 = await login_user (client )
782- u3 = await login_user (client )
801+ u1 = await login_user (client , exit_stack = exit_stack )
802+ u2 = await login_user (client , exit_stack = exit_stack )
803+ u3 = await login_user (client , exit_stack = exit_stack )
783804
784805 # creating g1 and inviting u2 and u3
785806 g1 = await get_group (client , u1 )
@@ -853,6 +874,7 @@ async def test_t8_project_shared_with_other_users_transferred_to_one_of_them_unt
853874 aiopg_engine : aiopg .sa .engine .Engine ,
854875 tests_data_dir : Path ,
855876 osparc_product_name : str ,
877+ exit_stack : contextlib .AsyncExitStack ,
856878):
857879 """
858880 USER "u1" creates a project and shares it with "u2" and "u3";
@@ -861,9 +883,9 @@ async def test_t8_project_shared_with_other_users_transferred_to_one_of_them_unt
861883 same as T5 => afterwards afterwards the new owner either "u2" or "u3" will be manually marked as "GUEST";
862884 EXPECTED: the GUEST user will be deleted and the project will pass to the last member of "g1"
863885 """
864- u1 = await login_user (client )
865- u2 = await login_user (client )
866- u3 = await login_user (client )
886+ u1 = await login_user (client , exit_stack = exit_stack )
887+ u2 = await login_user (client , exit_stack = exit_stack )
888+ u3 = await login_user (client , exit_stack = exit_stack )
867889
868890 q_u2 = await fetch_user_from_db (aiopg_engine , u2 )
869891 assert q_u2
@@ -927,6 +949,7 @@ async def test_t9_project_shared_with_other_users_transferred_between_them_and_t
927949 aiopg_engine : aiopg .sa .engine .Engine ,
928950 tests_data_dir : Path ,
929951 osparc_product_name : str ,
952+ exit_stack : contextlib .AsyncExitStack ,
930953):
931954 """
932955 USER "u1" creates a project and shares it with "u2" and "u3";
@@ -937,9 +960,9 @@ async def test_t9_project_shared_with_other_users_transferred_between_them_and_t
937960 same as T8 => afterwards the last user will be marked as "GUEST";
938961 EXPECTED: the last user will be removed and the project will be removed
939962 """
940- u1 = await login_user (client )
941- u2 = await login_user (client )
942- u3 = await login_user (client )
963+ u1 = await login_user (client , exit_stack = exit_stack )
964+ u2 = await login_user (client , exit_stack = exit_stack )
965+ u3 = await login_user (client , exit_stack = exit_stack )
943966
944967 q_u2 = await fetch_user_from_db (aiopg_engine , u2 )
945968 assert q_u2
@@ -1014,6 +1037,7 @@ async def test_t10_owner_and_all_shared_users_marked_as_guests(
10141037 aiopg_engine : aiopg .sa .engine .Engine ,
10151038 tests_data_dir : Path ,
10161039 osparc_product_name : str ,
1040+ exit_stack : contextlib .AsyncExitStack ,
10171041):
10181042 """
10191043 USER "u1" creates a project and shares it with "u2" and "u3";
@@ -1026,9 +1050,9 @@ async def test_t10_owner_and_all_shared_users_marked_as_guests(
10261050 )
10271051 assert not gc_task .done ()
10281052
1029- u1 = await login_user (client )
1030- u2 = await login_user (client )
1031- u3 = await login_user (client )
1053+ u1 = await login_user (client , exit_stack = exit_stack )
1054+ u2 = await login_user (client , exit_stack = exit_stack )
1055+ u3 = await login_user (client , exit_stack = exit_stack )
10321056
10331057 q_u2 = await fetch_user_from_db (aiopg_engine , u2 )
10341058 q_u3 = await fetch_user_from_db (aiopg_engine , u3 )
@@ -1067,16 +1091,17 @@ async def test_t11_owner_and_all_users_in_group_marked_as_guests(
10671091 aiopg_engine : aiopg .sa .engine .Engine ,
10681092 tests_data_dir : Path ,
10691093 osparc_product_name : str ,
1094+ exit_stack : contextlib .AsyncExitStack ,
10701095):
10711096 """
10721097 USER "u1" creates a group and invites "u2" and "u3";
10731098 USER "u1" creates a project and shares it with the group
10741099 USER "u1", "u2" and "u3" are manually marked as "GUEST"
10751100 EXPECTED: the project and all the users are removed
10761101 """
1077- u1 = await login_user (client )
1078- u2 = await login_user (client )
1079- u3 = await login_user (client )
1102+ u1 = await login_user (client , exit_stack = exit_stack )
1103+ u2 = await login_user (client , exit_stack = exit_stack )
1104+ u3 = await login_user (client , exit_stack = exit_stack )
10801105
10811106 # creating g1 and inviting u2 and u3
10821107 g1 = await get_group (client , u1 )
0 commit comments