|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=too-many-arguments |
| 3 | +# pylint: disable=unused-argument |
| 4 | +# pylint: disable=unused-variable |
| 5 | + |
| 6 | +from collections.abc import AsyncGenerator, Callable, Coroutine |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import pytest |
| 10 | +from aiohttp.test_utils import TestClient |
| 11 | +from models_library.groups import GroupMember, StandardGroupCreate |
| 12 | +from pytest_simcore.helpers.webserver_users import UserInfoDict |
| 13 | +from simcore_postgres_database.models.users import UserRole |
| 14 | +from simcore_service_webserver.groups import _groups_repository |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +async def create_test_group( |
| 19 | + client: TestClient, logged_user: UserInfoDict |
| 20 | +) -> AsyncGenerator[Callable[..., Coroutine[Any, Any, Any]], None]: |
| 21 | + """Fixture that creates a standard group and ensures cleanup.""" |
| 22 | + created_groups = [] |
| 23 | + |
| 24 | + async def _create_group( |
| 25 | + name: str = "Test Group", |
| 26 | + description: str = "A test group", |
| 27 | + thumbnail: str | None = None, |
| 28 | + ): |
| 29 | + group, _ = await _groups_repository.create_standard_group( |
| 30 | + app=client.app, |
| 31 | + user_id=logged_user["id"], |
| 32 | + create=StandardGroupCreate( |
| 33 | + name=name, |
| 34 | + description=description, |
| 35 | + thumbnail=thumbnail, |
| 36 | + ), |
| 37 | + ) |
| 38 | + created_groups.append(group) |
| 39 | + return group |
| 40 | + |
| 41 | + yield _create_group |
| 42 | + |
| 43 | + # Cleanup all created groups |
| 44 | + for group in created_groups: |
| 45 | + await _groups_repository.delete_standard_group( |
| 46 | + app=client.app, user_id=logged_user["id"], group_id=group.gid |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("user_role", [UserRole.USER]) |
| 51 | +async def test_list_users_in_group_owner_only( |
| 52 | + client: TestClient, |
| 53 | + user_role: UserRole, |
| 54 | + logged_user: UserInfoDict, |
| 55 | + create_test_group: Callable[..., Coroutine[Any, Any, Any]], |
| 56 | +): |
| 57 | + """Test list_users_in_group returns only the owner for a new group.""" |
| 58 | + assert client.app |
| 59 | + |
| 60 | + # Create a standard group |
| 61 | + group = await create_test_group( |
| 62 | + name="Test Owner Only Group", |
| 63 | + description="A test group to check owner-only user list", |
| 64 | + ) |
| 65 | + |
| 66 | + # List users in the group - should only contain the owner |
| 67 | + users_in_group = await _groups_repository.list_users_in_group( |
| 68 | + app=client.app, group_id=group.gid |
| 69 | + ) |
| 70 | + |
| 71 | + # Should contain exactly one user (the owner) |
| 72 | + assert len(users_in_group) == 1 |
| 73 | + assert isinstance(users_in_group[0], GroupMember) |
| 74 | + assert users_in_group[0].id == logged_user["id"] |
0 commit comments