11import contextlib
22import re
33from collections .abc import AsyncIterator
4- from datetime import datetime
5- from typing import Any , TypedDict
4+ from typing import Any
65
7- from aiohttp import web
86from aiohttp .test_utils import TestClient
9- from models_library .users import UserID
107from servicelib .aiohttp import status
11- from simcore_postgres_database .models .users import users as users_table
12- from simcore_service_webserver .db .models import UserRole , UserStatus
13- from simcore_service_webserver .db .plugin import get_asyncpg_engine
14- from simcore_service_webserver .groups import api as groups_service
158from simcore_service_webserver .login ._constants import MSG_LOGGED_IN
169from simcore_service_webserver .login ._invitations_service import create_invitation_token
17- from simcore_service_webserver .products .products_service import list_products
10+ from simcore_service_webserver .login ._login_repository_legacy import (
11+ get_plugin_storage ,
12+ )
1813from simcore_service_webserver .security import security_service
19- from sqlalchemy .ext .asyncio import AsyncEngine
2014from yarl import URL
2115
2216from .assert_checks import assert_status
23- from .faker_factories import DEFAULT_FAKER , DEFAULT_TEST_PASSWORD , random_user
24- from .postgres_tools import insert_and_get_row_lifespan
25-
26-
27- # WARNING: DO NOT use UserDict is already in https://docs.python.org/3/library/collections.html#collections.UserDictclass UserRowDict(TypedDict):
28- # NOTE: this is modified dict version of packages/postgres-database/src/simcore_postgres_database/models/users.py for testing purposes
29- class _UserInfoDictRequired (TypedDict , total = True ):
30- id : int
31- name : str
32- email : str
33- primary_gid : str
34- raw_password : str
35- status : UserStatus
36- role : UserRole
37-
38-
39- class UserInfoDict (_UserInfoDictRequired , total = False ):
40- created_at : datetime
41- password_hash : str
42- first_name : str
43- last_name : str
44- phone : str
45-
17+ from .faker_factories import DEFAULT_FAKER
18+ from .webserver_users import NewUser , UserInfoDict , _create_account_in_db
4619
4720TEST_MARKS = re .compile (r"TEST (\w+):(.*)" )
4821
@@ -65,89 +38,6 @@ def parse_link(text):
6538 return URL (link ).path
6639
6740
68- async def _create_user_in_db (
69- sqlalchemy_async_engine : AsyncEngine ,
70- exit_stack : contextlib .AsyncExitStack ,
71- data : dict | None = None ,
72- ) -> UserInfoDict :
73-
74- # create fake
75- data = data or {}
76- data .setdefault ("status" , UserStatus .ACTIVE .name )
77- data .setdefault ("role" , UserRole .USER .name )
78-
79- raw_password = DEFAULT_TEST_PASSWORD
80-
81- # inject in db
82- user = await exit_stack .enter_async_context (
83- insert_and_get_row_lifespan ( # pylint:disable=contextmanager-generator-missing-cleanup
84- sqlalchemy_async_engine ,
85- table = users_table ,
86- values = random_user (password = raw_password , ** data ),
87- pk_col = users_table .c .id ,
88- )
89- )
90- assert "first_name" in user
91- assert "last_name" in user
92-
93- return UserInfoDict (
94- # required
95- # - in db
96- id = user ["id" ],
97- name = user ["name" ],
98- email = user ["email" ],
99- primary_gid = user ["primary_gid" ],
100- status = (
101- UserStatus (user ["status" ])
102- if not isinstance (user ["status" ], UserStatus )
103- else user ["status" ]
104- ),
105- role = (
106- UserRole (user ["role" ])
107- if not isinstance (user ["role" ], UserRole )
108- else user ["role" ]
109- ),
110- # optional
111- # - in db
112- created_at = (
113- user ["created_at" ]
114- if isinstance (user ["created_at" ], datetime )
115- else datetime .fromisoformat (user ["created_at" ])
116- ),
117- password_hash = user ["password_hash" ],
118- first_name = user ["first_name" ],
119- last_name = user ["last_name" ],
120- phone = user ["phone" ],
121- # extras
122- raw_password = raw_password ,
123- )
124-
125-
126- async def _register_user_in_default_product (app : web .Application , user_id : UserID ):
127- products = list_products (app )
128- assert products
129- product_name = products [0 ].name
130-
131- return await groups_service .auto_add_user_to_product_group (
132- app , user_id , product_name = product_name
133- )
134-
135-
136- async def _create_account_in_db (
137- app : web .Application ,
138- exit_stack : contextlib .AsyncExitStack ,
139- user_data : dict [str , Any ] | None = None ,
140- ) -> UserInfoDict :
141- # users, groups in db
142- user = await _create_user_in_db (
143- get_asyncpg_engine (app ), exit_stack = exit_stack , data = user_data
144- )
145-
146- # user has default product
147- await _register_user_in_default_product (app , user_id = user ["id" ])
148- return user
149-
150-
15141async def log_client_in (
15242 client : TestClient ,
15343 user_data : dict [str , Any ] | None = None ,
@@ -178,30 +68,6 @@ async def log_client_in(
17868 return user
17969
18070
181- class NewUser :
182- def __init__ (
183- self ,
184- user_data : dict [str , Any ] | None = None ,
185- app : web .Application | None = None ,
186- ):
187- self .user_data = user_data
188- self .user = None
189-
190- assert app
191- self .app = app
192-
193- self .exit_stack = contextlib .AsyncExitStack ()
194-
195- async def __aenter__ (self ) -> UserInfoDict :
196- self .user = await _create_account_in_db (
197- self .app , self .exit_stack , self .user_data
198- )
199- return self .user
200-
201- async def __aexit__ (self , * args ):
202- await self .exit_stack .aclose ()
203-
204-
20571class LoggedUser (NewUser ):
20672 def __init__ (self , client : TestClient , user_data = None , * , check_if_succeeds = True ):
20773 super ().__init__ (user_data , client .app )
@@ -266,13 +132,12 @@ def __init__(
266132 self .confirmation = None
267133 self .trial_days = trial_days
268134 self .extra_credits_in_usd = extra_credits_in_usd
135+ self .db = get_plugin_storage (self .app )
269136
270137 async def __aenter__ (self ) -> "NewInvitation" :
271138 # creates host user
272139 assert self .client .app
273- self .user = await _create_user_in_db (
274- self .client .app , self .exit_stack , self .user_data
275- )
140+ self .user = await super ().__aenter__ ()
276141
277142 self .confirmation = await create_invitation_token (
278143 self .db ,
0 commit comments