diff --git a/api/main.py b/api/main.py index a2aa15c2..5c9c03f7 100644 --- a/api/main.py +++ b/api/main.py @@ -386,86 +386,6 @@ async def update_password(request: Request, ) -# ----------------------------------------------------------------------------- -# User groups - - -@app.post('/group', response_model=UserGroup, response_model_by_alias=False) -async def post_user_group( - group: UserGroup, - current_user: User = Depends(get_current_superuser)): - """Create new user group""" - metrics.add('http_requests_total', 1) - try: - obj = await db.create(group) - except DuplicateKeyError as error: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail=f"Group '{group.name}' already exists. \ -Use a different group name." - ) from error - await pubsub.publish_cloudevent('user_group', {'op': 'created', - 'id': str(obj.id)}) - return obj - - -@app.get('/groups', response_model=PageModel) -async def get_user_groups(request: Request): - """Get all the user groups if no request parameters have passed. - Get all the matching user groups otherwise.""" - metrics.add('http_requests_total', 1) - query_params = dict(request.query_params) - - # Drop pagination parameters from query as they're already in arguments - for pg_key in ['limit', 'offset']: - query_params.pop(pg_key, None) - - paginated_resp = await db.find_by_attributes(UserGroup, query_params) - paginated_resp.items = serialize_paginated_data( - UserGroup, paginated_resp.items) - return paginated_resp - - -@app.get('/group/{group_id}', response_model=Union[UserGroup, None], - response_model_by_alias=False) -async def get_group(group_id: str): - """Get user group information from the provided group id""" - metrics.add('http_requests_total', 1) - return await db.find_by_id(UserGroup, group_id) - - -@app.delete('/group/{group_id}', - dependencies=[Depends(pagination_ctx(PageModel))], - status_code=status.HTTP_204_NO_CONTENT) -async def delete_group(group_id: str, - current_user: User = Depends(get_current_superuser)): - """Delete user group matching the provided group id""" - metrics.add('http_requests_total', 1) - group_from_id = await db.find_by_id(UserGroup, group_id) - if not group_from_id: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail=f"Group not found with id: {group_id}" - ) - # Remove users from the group before deleting it - users = await db.find_by_attributes( - User, {"groups.name": group_from_id.name}) - for user in users.items: - user['groups'].remove(group_from_id) - await db.update(User(**user)) - - # Remove group from user groups that are permitted to update node - nodes = await db.find_by_attributes( - Node, {"user_groups": group_from_id.name}) - for node in nodes.items: - node['user_groups'].remove(group_from_id.name) - await db.update(Node(**node)) - - await db.delete_by_id(UserGroup, group_id) - - -# ----------------------------------------------------------------------------- -# EventHistory def _get_eventhistory(evdict): """Get EventHistory object from dictionary""" evhist = EventHistory() @@ -1100,7 +1020,7 @@ async def get_metrics(): @app.get('/maintenance/purge-old-nodes') -async def purge_old_nodes(current_user: User = Depends(get_current_superuser)): +async def purge_handler(current_user: User = Depends(get_current_superuser)): """Purge old nodes from the database This is a maintenance operation and should be performed only by superusers. diff --git a/doc/api-details.md b/doc/api-details.md index 808d1021..4bde2171 100644 --- a/doc/api-details.md +++ b/doc/api-details.md @@ -249,54 +249,6 @@ $ curl -X 'DELETE' \ -H 'Authorization: Bearer ' ``` - -## User groups - -User accounts can be added to different user groups. This section -explains different API endpoints for user group management operations. - -### Create a user group (Admin only) - -Admin users can create user groups using `POST /group` endpoint: -``` -$ curl -X 'POST' \ -'http://localhost:8001/group' \ --H 'accept: application/json' \ --H 'Content-Type: application/json' \ --H 'Authorization: Bearer ' \ --d '{"name": "test-user-group"}' -``` - - -### Get all existing user groups - -Get all existing user groups using `GET /groups` endpoint: -``` -$ curl 'http://localhost:8001/groups' -{"items":[{"id":"648ff894bd39930355ed16ad","name":"kernelci"},{"id":"6704f354ffaa8f6fa017faa3","name":"test"}],"total":2,"limit":50,"offset":0} -``` - - -### Get user group matching ID - -To get user group by ID, use `/group` endpoint with group ID as a path parameter: -``` -$ curl 'http://localhost:8001/group/648ff894bd39930355ed16ad' -{"id":"648ff894bd39930355ed16ad","name":"kernelci"} -``` - - -### Delete user group matching ID (Admin only) - -Only admin users can delete existing user groups by providing group ID to -`DELETE /group/` endpoint: -``` -$ curl -X 'DELETE' \ -'http://localhost:8001/group/6704f354ffaa8f6fa017faa3' \ --H 'Authorization: Bearer ' -``` - - ## Nodes `Node` objects form the basis of the API models to represent tests runs, diff --git a/tests/e2e_tests/test_user_group_handler.py b/tests/e2e_tests/test_user_group_handler.py deleted file mode 100644 index ce4ed5b4..00000000 --- a/tests/e2e_tests/test_user_group_handler.py +++ /dev/null @@ -1,61 +0,0 @@ -# SPDX-License-Identifier: LGPL-2.1-or-later -# -# Copyright (C) 2023 Collabora Limited -# Author: Jeny Sadadia - -"""End-to-end test functions for KernelCI API user group handler""" - -import json -import pytest -from cloudevents.http import from_json - -from .listen_handler import create_listen_task - - -@pytest.mark.dependency( - depends=[ - 'tests/e2e_tests/test_subscribe_handler.py::test_subscribe_user_group_channel'], - scope='session') -@pytest.mark.asyncio -async def test_create_and_get_user_group(test_async_client): - """ - Test Case : Create and get a user group by ID - - At first, the test will create asyncio 'Task' instance to receive - pubsub events from 'user_group' channel. - A user group with name 'kernelci' will be created using POST '/group' - request. The 'created' event will be received from the listener task. - The GET '/group/{group_id}' will be sent using group id from event data - to get newly created user group object. - """ - - # Create Task to listen pubsub event on 'user_group' channel - task_listen = create_listen_task(test_async_client, - pytest.user_group_channel_subscription_id) # pylint: disable=no-member - - # Create a user group - response = await test_async_client.post( - "group", - headers={ - "Accept": "application/json", - "Authorization": f"Bearer {pytest.ADMIN_BEARER_TOKEN}" # pylint: disable=no-member - }, - data=json.dumps({"name": "kernelci"}) - ) - assert response.status_code == 200 - assert response.json().keys() == { - 'id', - 'name', - } - - # Get result from the event - await task_listen - event_data = from_json(task_listen.result().json().get('data')).data - assert {'op', 'id'} == event_data.keys() - - # Get user group by ID - response = await test_async_client.get( - f"group/{event_data['id']}" - ) - assert response.status_code == 200 - assert response.json()['name'] == 'kernelci'