From 8aa9d1aae635aedf3b2ef19c2022ef8689d5bbfb Mon Sep 17 00:00:00 2001 From: grossmj Date: Sun, 3 Sep 2023 17:58:51 +0700 Subject: [PATCH] Upgrade dependencies and fix user group tests --- gns3server/api/routes/controller/groups.py | 5 ++++ requirements.txt | 4 +-- tests/api/routes/controller/test_groups.py | 30 +++++++++++++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/gns3server/api/routes/controller/groups.py b/gns3server/api/routes/controller/groups.py index d4652353..c74f2b8c 100644 --- a/gns3server/api/routes/controller/groups.py +++ b/gns3server/api/routes/controller/groups.py @@ -196,6 +196,11 @@ async def add_member_to_group( if not user: raise ControllerNotFoundError(f"User '{user_id}' not found") + user_groups = await users_repo.get_user_memberships(user_id) + for group in user_groups: + if group.user_group_id == user_group_id: + raise ControllerBadRequestError(f"Username '{user.username}' is already member of group '{group.name}'") + user_group = await users_repo.add_member_to_user_group(user_group_id, user) if not user_group: raise ControllerNotFoundError(f"User group '{user_group_id}' not found") diff --git a/requirements.txt b/requirements.txt index cd5242e6..1c5b44b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ uvicorn==0.22.0 # v0.22.0 is the last to support Python 3.7 -fastapi==0.103.0 +fastapi==0.103.1 python-multipart==0.0.6 websockets==11.0.3 aiohttp>=3.8.5,<3.9 @@ -10,7 +10,7 @@ sentry-sdk==1.30.0,<1.31 psutil==5.9.5 distro>=1.8.0 py-cpuinfo==9.0.0 -sqlalchemy==2.0.18 +sqlalchemy==2.0.20 aiosqlite==0.19.0 alembic==1.12.0 passlib[bcrypt]==1.7.4 diff --git a/tests/api/routes/controller/test_groups.py b/tests/api/routes/controller/test_groups.py index 5dee0809..642f283e 100644 --- a/tests/api/routes/controller/test_groups.py +++ b/tests/api/routes/controller/test_groups.py @@ -22,7 +22,7 @@ from httpx import AsyncClient from sqlalchemy.ext.asyncio import AsyncSession from gns3server.db.repositories.users import UsersRepository -from gns3server.schemas.controller.users import User +from gns3server.schemas.controller.users import User, UserGroupCreate pytestmark = pytest.mark.asyncio @@ -106,7 +106,7 @@ class TestGroupRoutes: class TestGroupMembersRoutes: - async def test_add_member_to_group( + async def test_add_to_group_already_member( self, app: FastAPI, client: AsyncClient, @@ -123,6 +123,28 @@ class TestGroupMembersRoutes: user_id=str(test_user.user_id) ) ) + assert response.status_code == status.HTTP_400_BAD_REQUEST + + async def test_add_member_to_group( + self, + app: FastAPI, + client: AsyncClient, + test_user: User, + db_session: AsyncSession + ) -> None: + + user_repo = UsersRepository(db_session) + new_user_group = UserGroupCreate( + name="test_group", + ) + group_in_db = await user_repo.create_user_group(new_user_group) + response = await client.put( + app.url_path_for( + "add_member_to_group", + user_group_id=group_in_db.user_group_id, + user_id=str(test_user.user_id) + ) + ) assert response.status_code == status.HTTP_204_NO_CONTENT members = await user_repo.get_user_group_members(group_in_db.user_group_id) assert len(members) == 1 @@ -136,7 +158,7 @@ class TestGroupMembersRoutes: ) -> None: user_repo = UsersRepository(db_session) - group_in_db = await user_repo.get_user_group_by_name("Users") + group_in_db = await user_repo.get_user_group_by_name("test_group") response = await client.get( app.url_path_for( "get_user_group_members", @@ -154,7 +176,7 @@ class TestGroupMembersRoutes: ) -> None: user_repo = UsersRepository(db_session) - group_in_db = await user_repo.get_user_group_by_name("Users") + group_in_db = await user_repo.get_user_group_by_name("test_group") response = await client.delete( app.url_path_for(