Skip to content

Commit 895c830

Browse files
committed
adds tests
1 parent 6ee6462 commit 895c830

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

services/web/server/src/simcore_service_webserver/studies_dispatcher/_repository.py

Whitespace-only changes.

services/web/server/src/simcore_service_webserver/studies_dispatcher/_service.py

Whitespace-only changes.

services/web/server/tests/unit/with_dbs/03/test_users_rest_profile.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,182 @@ async def test_get_profile_with_failing_db_connection(
467467
data, error = await assert_status(resp, expected)
468468
assert not data
469469
assert error["message"] == "Authentication service is temporary unavailable"
470+
471+
472+
@pytest.fixture
473+
async def user_pre_registration(
474+
client: TestClient,
475+
logged_user: UserInfoDict,
476+
) -> AsyncIterator[int]:
477+
"""Creates pre-registration data for the logged user and yields the pre-registration ID.
478+
Automatically cleans up after the test."""
479+
from simcore_postgres_database.models.users_details import (
480+
users_pre_registration_details,
481+
)
482+
from simcore_service_webserver.db.plugin import get_asyncpg_engine
483+
from simcore_service_webserver.users._accounts_repository import (
484+
create_user_pre_registration,
485+
)
486+
487+
asyncpg_engine = get_asyncpg_engine(client.app)
488+
489+
# Create pre-registration data for the logged user
490+
pre_registration_details = {
491+
"pre_first_name": "Pre-Registered",
492+
"pre_last_name": "User",
493+
"institution": "Test University",
494+
"address": "123 Test Street",
495+
"city": "Test City",
496+
"state": "Test State",
497+
"postal_code": "12345",
498+
"country": "US",
499+
}
500+
501+
pre_registration_id = await create_user_pre_registration(
502+
asyncpg_engine,
503+
email=logged_user["email"],
504+
created_by=None, # Self-registration
505+
product_name="osparc",
506+
link_to_existing_user=True,
507+
**pre_registration_details,
508+
)
509+
510+
try:
511+
yield pre_registration_id
512+
finally:
513+
# Clean up pre-registration data
514+
async with asyncpg_engine.connect() as conn:
515+
await conn.execute(
516+
sa.delete(users_pre_registration_details).where(
517+
users_pre_registration_details.c.id == pre_registration_id
518+
)
519+
)
520+
await conn.commit()
521+
522+
523+
@pytest.mark.parametrize("user_role", [UserRole.USER])
524+
async def test_get_profile_user_with_pre_registration(
525+
user_role: UserRole,
526+
logged_user: UserInfoDict,
527+
client: TestClient,
528+
primary_group: dict[str, Any],
529+
standard_groups: list[dict[str, Any]],
530+
all_group: dict[str, str],
531+
product: Product,
532+
user_pre_registration: int,
533+
):
534+
"""Test getting profile of a user that has pre-registration data"""
535+
assert client.app
536+
537+
url = client.app.router["get_my_profile"].url_for()
538+
assert url.path == "/v0/me"
539+
540+
resp = await client.get(f"{url}")
541+
data, error = await assert_status(resp, status.HTTP_200_OK)
542+
543+
assert not error
544+
profile = MyProfileRestGet.model_validate(data)
545+
546+
assert profile.login == logged_user["email"]
547+
assert profile.first_name == logged_user.get("first_name", None)
548+
assert profile.last_name == logged_user.get("last_name", None)
549+
assert profile.role == user_role.name
550+
assert profile.groups
551+
assert profile.expiration_date is None
552+
553+
# Verify profile groups structure is intact
554+
got_profile_groups = profile.groups.model_dump(**RESPONSE_MODEL_POLICY, mode="json")
555+
assert got_profile_groups["me"] == primary_group
556+
assert got_profile_groups["all"] == all_group
557+
558+
# Verify contact information from pre-registration is populated
559+
assert profile.contact is not None
560+
assert profile.contact.institution == "Test University"
561+
assert profile.contact.address == "123 Test Street"
562+
assert profile.contact.city == "Test City"
563+
assert profile.contact.state == "Test State"
564+
assert profile.contact.postal_code == "12345"
565+
assert profile.contact.country == "US"
566+
567+
# Verify preferences are still working
568+
assert profile.preferences == await get_frontend_user_preferences_aggregation(
569+
client.app, user_id=logged_user["id"], product_name="osparc"
570+
)
571+
572+
573+
@pytest.mark.parametrize("user_role", [UserRole.USER])
574+
async def test_get_profile_user_without_pre_registration(
575+
user_role: UserRole,
576+
logged_user: UserInfoDict,
577+
client: TestClient,
578+
primary_group: dict[str, Any],
579+
standard_groups: list[dict[str, Any]],
580+
all_group: dict[str, str],
581+
product: Product,
582+
):
583+
"""Test getting profile of a user that does not have pre-registration data"""
584+
assert client.app
585+
586+
from simcore_service_webserver.db.plugin import get_asyncpg_engine
587+
from simcore_service_webserver.users._accounts_repository import (
588+
search_merged_pre_and_registered_users,
589+
)
590+
591+
asyncpg_engine = get_asyncpg_engine(client.app)
592+
593+
# Verify user has no pre-registration data
594+
pre_reg_users = await search_merged_pre_and_registered_users(
595+
asyncpg_engine,
596+
email_like=logged_user["email"],
597+
product_name="osparc",
598+
)
599+
600+
# Filter for exact email match and pre-registration records
601+
user_pre_regs = [
602+
row
603+
for row in pre_reg_users
604+
if row.pre_email == logged_user["email"] and row.id is not None
605+
]
606+
assert len(user_pre_regs) == 0, "User should not have pre-registration data"
607+
608+
url = client.app.router["get_my_profile"].url_for()
609+
assert url.path == "/v0/me"
610+
611+
resp = await client.get(f"{url}")
612+
data, error = await assert_status(resp, status.HTTP_200_OK)
613+
614+
assert not error
615+
profile = MyProfileRestGet.model_validate(data)
616+
617+
assert profile.login == logged_user["email"]
618+
assert profile.first_name == logged_user.get("first_name", None)
619+
assert profile.last_name == logged_user.get("last_name", None)
620+
assert profile.role == user_role.name
621+
assert profile.groups
622+
assert profile.expiration_date is None
623+
624+
# Verify profile groups structure
625+
got_profile_groups = profile.groups.model_dump(**RESPONSE_MODEL_POLICY, mode="json")
626+
assert got_profile_groups["me"] == primary_group
627+
assert got_profile_groups["all"] == all_group
628+
assert got_profile_groups["product"] == {
629+
"accessRights": {"delete": False, "read": False, "write": False},
630+
"description": "osparc product group",
631+
"gid": product.group_id,
632+
"label": "osparc",
633+
"thumbnail": None,
634+
}
635+
636+
# Verify no contact information since no pre-registration exists
637+
assert profile.contact is None
638+
639+
# Verify standard groups
640+
sorted_by_group_id = functools.partial(sorted, key=lambda d: d["gid"])
641+
assert sorted_by_group_id(
642+
got_profile_groups["organizations"]
643+
) == sorted_by_group_id(standard_groups)
644+
645+
# Verify preferences are working
646+
assert profile.preferences == await get_frontend_user_preferences_aggregation(
647+
client.app, user_id=logged_user["id"], product_name="osparc"
648+
)

0 commit comments

Comments
 (0)