diff --git a/api/admin.py b/api/admin.py index 0efafa62..06505471 100644 --- a/api/admin.py +++ b/api/admin.py @@ -17,27 +17,19 @@ from .auth import Authentication from .db import Database -from .models import UserGroup from .user_models import User -async def setup_admin_group(db, admin_group): - group_obj = await db.find_one(UserGroup, name=admin_group) - if group_obj is None: - print(f"Creating {admin_group} group...") - group_obj = await db.create(UserGroup(name=admin_group)) - return group_obj - - -async def setup_admin_user(db, username, email, admin_group): +async def setup_admin_user(db, username, email): + """Create an admin user""" user_obj = await db.find_one_by_attributes(User, {'username': username}) if user_obj: print(f"User {username} already exists, aborting.") print(user_obj.json()) return None - password = getpass.getpass(f"Password for user '{args.username}': ") - retyped = getpass.getpass(f"Retype password for user '{args.username}': ") + password = getpass.getpass(f"Password for user '{username}': ") + retyped = getpass.getpass(f"Retype password for user '{username}': ") if password != retyped: print("Sorry, passwords do not match, aborting.") return None @@ -47,7 +39,6 @@ async def setup_admin_user(db, username, email, admin_group): username=username, hashed_password=hashed_password, email=email, - groups=[admin_group], is_superuser=1, is_verified=1, )) @@ -56,8 +47,7 @@ async def setup_admin_user(db, username, email, admin_group): async def main(args): db = Database(args.mongo, args.database) await db.initialize_beanie() - group = await setup_admin_group(db, args.admin_group) - user = await setup_admin_user(db, args.username, args.email, group) + await setup_admin_user(db, args.username, args.email) return True @@ -67,8 +57,6 @@ async def main(args): help="Mongo server connection string") parser.add_argument('--username', default='admin', help="Admin username") - parser.add_argument('--admin-group', default='admin', - help="Admin group name") parser.add_argument('--database', default='kernelci', help="KernelCI database name") parser.add_argument('--email', required=True, diff --git a/api/main.py b/api/main.py index 238419cd..80d15ccc 100644 --- a/api/main.py +++ b/api/main.py @@ -220,7 +220,7 @@ async def update_me(request: Request, user: UserUpdate, """User update route Custom user update router handler will only allow users to update - its own profile. Adding itself to 'admin' group is not allowed. + its own profile. """ if user.username and user.username != current_user.username: existing_user = await db.find_one(User, username=user.username) @@ -232,10 +232,6 @@ async def update_me(request: Request, user: UserUpdate, groups = [] if user.groups: for group_name in user.groups: - if group_name == "admin": - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Unauthorized to add user to 'admin' group") group = await db.find_one(UserGroup, name=group_name) if not group: raise HTTPException( diff --git a/tests/e2e_tests/test_pipeline.py b/tests/e2e_tests/test_pipeline.py index 56176c6f..639f0966 100644 --- a/tests/e2e_tests/test_pipeline.py +++ b/tests/e2e_tests/test_pipeline.py @@ -17,7 +17,7 @@ depends=[ 'e2e_tests/test_subscribe_handler.py::test_subscribe_node_channel'], scope='session') -@pytest.mark.order(5) +@pytest.mark.order(4) @pytest.mark.asyncio async def test_node_pipeline(test_async_client): """ diff --git a/tests/e2e_tests/test_subscribe_handler.py b/tests/e2e_tests/test_subscribe_handler.py index 4413179f..6f0007f8 100644 --- a/tests/e2e_tests/test_subscribe_handler.py +++ b/tests/e2e_tests/test_subscribe_handler.py @@ -12,7 +12,7 @@ @pytest.mark.dependency( depends=['e2e_tests/test_user_creation.py::test_create_regular_user'], scope='session') -@pytest.mark.order(4) +@pytest.mark.order(3) def test_subscribe_node_channel(test_client): """ Test Case : Test KernelCI API '/subscribe' endpoint with 'node' channel @@ -35,7 +35,7 @@ def test_subscribe_node_channel(test_client): @pytest.mark.dependency( depends=['e2e_tests/test_user_creation.py::test_create_regular_user'], scope='session') -@pytest.mark.order(4) +@pytest.mark.order(3) def test_subscribe_test_channel(test_client): """ Test Case : Test KernelCI API '/subscribe' endpoint with 'test_channel' @@ -58,7 +58,7 @@ def test_subscribe_test_channel(test_client): @pytest.mark.dependency( depends=['e2e_tests/test_user_creation.py::test_create_regular_user'], scope='session') -@pytest.mark.order(4) +@pytest.mark.order(3) def test_subscribe_user_group_channel(test_client): """ Test Case : Test KernelCI API '/subscribe' endpoint with 'user_group' diff --git a/tests/e2e_tests/test_user_creation.py b/tests/e2e_tests/test_user_creation.py index 9e281c1c..340342cd 100644 --- a/tests/e2e_tests/test_user_creation.py +++ b/tests/e2e_tests/test_user_creation.py @@ -9,7 +9,6 @@ import pytest from e2e_tests.conftest import db_create -from api.models import UserGroup from api.user_models import User from api.db import Database from api.auth import Authentication @@ -19,7 +18,7 @@ depends=["e2e_tests/test_user_group_handler.py::test_create_user_groups"], scope="session") @pytest.mark.dependency() -@pytest.mark.order(2) +@pytest.mark.order(1) @pytest.mark.asyncio async def test_create_admin_user(test_async_client): """ @@ -38,7 +37,7 @@ async def test_create_admin_user(test_async_client): username=username, hashed_password=hashed_password, email='test-admin@kernelci.org', - groups=[UserGroup(name="admin")], + groups=[], is_superuser=1, is_verified=1 )) @@ -62,7 +61,7 @@ async def test_create_admin_user(test_async_client): @pytest.mark.dependency(depends=["test_create_admin_user"]) -@pytest.mark.order(3) +@pytest.mark.order(2) @pytest.mark.asyncio async def test_create_regular_user(test_async_client): """ diff --git a/tests/e2e_tests/test_user_group_handler.py b/tests/e2e_tests/test_user_group_handler.py index ce4bbe95..fc45b2a2 100644 --- a/tests/e2e_tests/test_user_group_handler.py +++ b/tests/e2e_tests/test_user_group_handler.py @@ -9,52 +9,9 @@ from cloudevents.http import from_json import json -from api.models import UserGroup -from api.db import Database -from e2e_tests.conftest import db_create from .listen_handler import create_listen_task -@pytest.mark.dependency() -@pytest.mark.order(1) -@pytest.mark.asyncio -async def test_create_user_groups(): - """ - Test Case : Create default user groups - """ - default_user_groups = ['admin'] - for group in default_user_groups: - obj = await db_create( - Database.COLLECTIONS[UserGroup], - UserGroup(name=group)) - assert obj is not None - - -@pytest.mark.dependency( - depends=["test_create_user_groups"]) -@pytest.mark.asyncio -async def test_get_user_group(test_async_client): - """ - Test Case : Get user groups - Expected Result : - HTTP Response Code 200 OK - Returns dictionary with UserGroup objects, total number of groups - returned along with limit and offset values - """ - response = await test_async_client.get( - "groups", - ) - assert response.status_code == 200 - assert response.json().keys() == { - 'items', - 'total', - 'limit', - 'offset', - } - assert response.json()['total'] == 1 - assert response.json()['items'][0]['name'] == 'admin' - - @pytest.mark.dependency( depends=[ 'e2e_tests/test_subscribe_handler.py::test_subscribe_user_group_channel'], diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py index 7a97ec2d..ba18682e 100644 --- a/tests/unit_tests/conftest.py +++ b/tests/unit_tests/conftest.py @@ -26,7 +26,6 @@ get_current_user, get_current_superuser, ) -from api.models import UserGroup from api.user_models import User from api.pubsub import PubSub, Subscription @@ -86,7 +85,7 @@ def mock_get_current_admin_user(request: Request): hashed_password='$2b$12$CpJZx5ooxM11bCFXT76/z.o6HWs2sPJy4iP8.' 'xCZGmM8jWXUXJZ4K', email='admin@kernelci.org', - groups=[UserGroup(name='admin')], + groups=[], is_active=True, is_superuser=True, is_verified=True diff --git a/tests/unit_tests/test_user_handler.py b/tests/unit_tests/test_user_handler.py index f7a964da..5cb9f2a1 100644 --- a/tests/unit_tests/test_user_handler.py +++ b/tests/unit_tests/test_user_handler.py @@ -74,7 +74,7 @@ async def test_create_admin_user(test_async_client, mock_db_find_one, id='61bda8f2eb1a63d2b7152419', username='test_admin', email='test-admin@kernelci.org', - groups=[UserGroup(name='admin')], + groups=[], is_active=True, is_verified=False, is_superuser=True