Skip to content

Commit b6352f1

Browse files
committed
drafting tests for users repository
1 parent 2c594c6 commit b6352f1

File tree

4 files changed

+327
-14
lines changed

4 files changed

+327
-14
lines changed

services/web/server/src/simcore_service_webserver/users/_users_repository.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ async def search_users_and_get_profile(
319319
connection: AsyncConnection | None = None,
320320
*,
321321
email_like: str,
322+
product_name: ProductName | None = None,
322323
) -> list[Row]:
323324
users_alias = sa.alias(users, name="users_alias")
324325

@@ -352,12 +353,16 @@ async def search_users_and_get_profile(
352353
invited_by,
353354
)
354355

356+
join_condition = users.c.id == users_pre_registration_details.c.user_id
357+
if product_name:
358+
join_condition = join_condition & (
359+
users_pre_registration_details.c.product_name == product_name
360+
)
361+
355362
left_outer_join = (
356363
sa.select(*columns)
357364
.select_from(
358-
users_pre_registration_details.outerjoin(
359-
users, users.c.id == users_pre_registration_details.c.user_id
360-
)
365+
users_pre_registration_details.outerjoin(users, join_condition)
361366
)
362367
.where(users_pre_registration_details.c.pre_email.like(email_like))
363368
)
@@ -366,7 +371,7 @@ async def search_users_and_get_profile(
366371
.select_from(
367372
users.outerjoin(
368373
users_pre_registration_details,
369-
users.c.id == users_pre_registration_details.c.user_id,
374+
join_condition,
370375
)
371376
)
372377
.where(users.c.email.like(email_like))
@@ -412,22 +417,27 @@ async def get_user_products(
412417
return [row async for row in result]
413418

414419

415-
async def create_user_details(
420+
async def create_user_pre_registration(
416421
engine: AsyncEngine,
417422
connection: AsyncConnection | None = None,
418423
*,
419424
email: str,
420425
created_by: UserID,
426+
product_name: ProductName,
421427
**other_values,
422-
) -> None:
428+
) -> int:
423429
async with transaction_context(engine, connection) as conn:
424-
await conn.execute(
425-
sa.insert(users_pre_registration_details).values(
430+
result = await conn.execute(
431+
sa.insert(users_pre_registration_details)
432+
.values(
426433
created_by=created_by,
427434
pre_email=email,
435+
product_name=product_name,
428436
**other_values,
429437
)
438+
.returning(users_pre_registration_details.c.id)
430439
)
440+
return result.scalar_one()
431441

432442

433443
async def get_user_billing_details(

services/web/server/src/simcore_service_webserver/users/_users_rest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
from contextlib import suppress
3+
from itertools import product
34

45
from aiohttp import web
56
from models_library.api_schemas_webserver.users import (
@@ -195,7 +196,10 @@ async def pre_register_user_for_admin(request: web.Request) -> web.Response:
195196
pre_user_profile = await parse_request_body_as(PreRegisteredUserGet, request)
196197

197198
user_profile = await _users_service.pre_register_user(
198-
request.app, profile=pre_user_profile, creator_user_id=req_ctx.user_id
199+
request.app,
200+
profile=pre_user_profile,
201+
creator_user_id=req_ctx.user_id,
202+
product_name=req_ctx.product_name,
199203
)
200204
return envelope_json_response(
201205
user_profile.model_dump(**_RESPONSE_MODEL_MINIMAL_POLICY)

services/web/server/src/simcore_service_webserver/users/_users_service.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141

4242
async def pre_register_user(
4343
app: web.Application,
44+
*,
4445
profile: PreRegisteredUserGet,
4546
creator_user_id: UserID,
47+
product_name: ProductName,
4648
) -> UserForAdminGet:
4749

4850
found = await search_users_as_admin(
49-
app, email_glob=profile.email, include_products=False
51+
app, email_glob=profile.email, product_name=product_name, include_products=False
5052
)
5153
if found:
5254
raise AlreadyPreRegisteredError(num_found=len(found), email=profile.email)
@@ -71,15 +73,16 @@ async def pre_register_user(
7173
if key in details:
7274
details[f"pre_{key}"] = details.pop(key)
7375

74-
await _users_repository.create_user_details(
76+
await _users_repository.create_user_pre_registration(
7577
get_asyncpg_engine(app),
7678
email=profile.email,
7779
created_by=creator_user_id,
80+
product_name=product_name,
7881
**details,
7982
)
8083

8184
found = await search_users_as_admin(
82-
app, email_glob=profile.email, include_products=False
85+
app, email_glob=profile.email, product_name=product_name, include_products=False
8386
)
8487

8588
assert len(found) == 1 # nosec
@@ -130,7 +133,11 @@ async def get_user_id_from_gid(app: web.Application, primary_gid: GroupID) -> Us
130133

131134

132135
async def search_users_as_admin(
133-
app: web.Application, email_glob: str, *, include_products: bool = False
136+
app: web.Application,
137+
*,
138+
email_glob: str,
139+
product_name: ProductName | None = None,
140+
include_products: bool = False,
134141
) -> list[UserForAdminGet]:
135142
"""
136143
WARNING: this information is reserved for admin users. Note that the returned model include UserForAdminGet
@@ -148,7 +155,9 @@ def _glob_to_sql_like(glob_pattern: str) -> str:
148155
return sql_like_pattern.replace("*", "%").replace("?", "_")
149156

150157
rows = await _users_repository.search_users_and_get_profile(
151-
get_asyncpg_engine(app), email_like=_glob_to_sql_like(email_glob)
158+
get_asyncpg_engine(app),
159+
email_like=_glob_to_sql_like(email_glob),
160+
product_name=product_name,
152161
)
153162

154163
async def _list_products_or_none(user_id):

0 commit comments

Comments
 (0)