|
| 1 | +import asyncio |
| 2 | + |
1 | 3 | from fastapi import status |
2 | 4 |
|
3 | | -from tests.api import client |
| 5 | +from app.db.models import Group |
| 6 | +from tests.api import TestSession, client |
| 7 | + |
| 8 | + |
| 9 | +async def _create_group_record(name: str) -> int: |
| 10 | + async with TestSession() as session: |
| 11 | + group = Group(name=name, inbounds=[], is_disabled=False) |
| 12 | + session.add(group) |
| 13 | + await session.commit() |
| 14 | + await session.refresh(group) |
| 15 | + return group.id |
| 16 | + |
| 17 | + |
| 18 | +async def _delete_group_record(group_id: int): |
| 19 | + async with TestSession() as session: |
| 20 | + group = await session.get(Group, group_id) |
| 21 | + if group: |
| 22 | + await session.delete(group) |
| 23 | + await session.commit() |
4 | 24 |
|
5 | 25 |
|
6 | 26 | def test_admin_login(): |
@@ -101,6 +121,72 @@ def test_disable_admin(): |
101 | 121 | assert response.json()["detail"] == "your account has been disabled" |
102 | 122 |
|
103 | 123 |
|
| 124 | +def test_admin_delete_all_users_endpoint(access_token): |
| 125 | + """Test deleting all users belonging to an admin.""" |
| 126 | + |
| 127 | + admin_username = "testadminbulkdelete" |
| 128 | + admin_password = "TestAdminBulkdelete#11" |
| 129 | + |
| 130 | + response = client.post( |
| 131 | + url="/api/admin", |
| 132 | + json={"username": admin_username, "password": admin_password, "is_sudo": False}, |
| 133 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 134 | + ) |
| 135 | + assert response.status_code == status.HTTP_201_CREATED |
| 136 | + |
| 137 | + group_id = asyncio.run(_create_group_record(f"{admin_username}_group")) |
| 138 | + |
| 139 | + created_users = [] |
| 140 | + for idx in range(2): |
| 141 | + user_name = f"{admin_username}_user_{idx}" |
| 142 | + user_response = client.post( |
| 143 | + "/api/user", |
| 144 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 145 | + json={ |
| 146 | + "username": user_name, |
| 147 | + "proxy_settings": {}, |
| 148 | + "group_ids": [group_id], |
| 149 | + "data_limit": 1024, |
| 150 | + "data_limit_reset_strategy": "no_reset", |
| 151 | + "status": "active", |
| 152 | + }, |
| 153 | + ) |
| 154 | + assert user_response.status_code == status.HTTP_201_CREATED |
| 155 | + created_users.append(user_name) |
| 156 | + |
| 157 | + ownership_response = client.put( |
| 158 | + f"/api/user/{user_name}/set_owner", |
| 159 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 160 | + params={"admin_username": admin_username}, |
| 161 | + ) |
| 162 | + assert ownership_response.status_code == status.HTTP_200_OK |
| 163 | + assert ownership_response.json()["admin"]["username"] == admin_username |
| 164 | + |
| 165 | + response = client.delete( |
| 166 | + url=f"/api/admin/{admin_username}/users", |
| 167 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 168 | + ) |
| 169 | + assert response.status_code == status.HTTP_200_OK |
| 170 | + assert response.json()["deleted"] == len(created_users) |
| 171 | + |
| 172 | + for username in created_users: |
| 173 | + user_check = client.get( |
| 174 | + "/api/users", |
| 175 | + params={"username": username}, |
| 176 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 177 | + ) |
| 178 | + assert user_check.status_code == status.HTTP_200_OK |
| 179 | + assert user_check.json()["users"] == [] |
| 180 | + |
| 181 | + cleanup = client.delete( |
| 182 | + url=f"/api/admin/{admin_username}", |
| 183 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 184 | + ) |
| 185 | + assert cleanup.status_code == status.HTTP_204_NO_CONTENT |
| 186 | + |
| 187 | + asyncio.run(_delete_group_record(group_id)) |
| 188 | + |
| 189 | + |
104 | 190 | def test_admin_delete(access_token): |
105 | 191 | """Test that the admin delete route is accessible.""" |
106 | 192 |
|
|
0 commit comments