feat: 添加系统配置、审计中心、通知中心、文件管理模块

This commit is contained in:
张翔
2026-03-11 12:11:59 +08:00
commit 52c66444a5
264 changed files with 10109 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
"""测试模块"""
+83
View File
@@ -0,0 +1,83 @@
"""
认证测试用例
"""
import pytest
from api.auth_api import AuthAPI
from config.settings import settings
@pytest.mark.auth
@pytest.mark.smoke
class TestAuth:
"""认证测试类"""
@pytest.mark.asyncio
async def test_login_success(self, http_client):
"""测试成功登录"""
auth_api = AuthAPI(http_client)
response = await auth_api.login(settings.TEST_USERNAME, settings.TEST_PASSWORD)
assert response.status_code == 200
data = response.json()
assert "accessToken" in data
assert "refreshToken" in data
assert isinstance(data["accessToken"], str)
assert isinstance(data["refreshToken"], str)
@pytest.mark.asyncio
async def test_login_invalid_credentials(self, http_client):
"""测试无效凭证登录"""
auth_api = AuthAPI(http_client)
response = await auth_api.login("invalid_user", "invalid_password")
assert response.status_code == 401
@pytest.mark.asyncio
async def test_login_missing_fields(self, http_client):
"""测试缺少必填字段"""
auth_api = AuthAPI(http_client)
response = await http_client.post("/api/auth/login", json={
"username": "test"
})
assert response.status_code == 400
@pytest.mark.asyncio
async def test_refresh_token_success(self, http_client, auth_token):
"""测试刷新token成功"""
auth_api = AuthAPI(http_client)
login_response = await auth_api.login(settings.TEST_USERNAME, settings.TEST_PASSWORD)
refresh_token = login_response.json().get("refreshToken")
response = await auth_api.refresh_token(refresh_token)
assert response.status_code == 200
data = response.json()
assert "accessToken" in data
assert "refreshToken" in data
@pytest.mark.asyncio
async def test_refresh_token_invalid(self, http_client):
"""测试无效刷新token"""
auth_api = AuthAPI(http_client)
response = await auth_api.refresh_token("invalid_refresh_token")
assert response.status_code == 401
@pytest.mark.asyncio
async def test_logout_success(self, http_client, auth_token):
"""测试登出成功"""
auth_api = AuthAPI(http_client)
response = await auth_api.logout(auth_token)
assert response.status_code == 200
@pytest.mark.asyncio
async def test_logout_without_token(self, http_client):
"""测试无token登出"""
auth_api = AuthAPI(http_client)
response = await http_client.post("/api/auth/logout")
assert response.status_code == 400
+149
View File
@@ -0,0 +1,149 @@
"""
字典管理测试用例
"""
import pytest
from api.dictionary_api import DictionaryAPI
@pytest.mark.dictionary
@pytest.mark.regression
class TestDictionary:
"""字典管理测试类"""
@pytest.mark.asyncio
async def test_create_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试创建字典成功"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.create_dictionary(test_dictionary_data)
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["type"] == test_dictionary_data["type"]
assert data["code"] == test_dictionary_data["code"]
assert data["name"] == test_dictionary_data["name"]
cleanup_dictionary.append(data["id"])
@pytest.mark.asyncio
async def test_create_dictionary_duplicate_type_code(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试创建重复类型和编码"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.create_dictionary(test_dictionary_data)
assert response.status_code in [400, 409]
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_dictionary_by_id_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试根据ID获取字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.get_dictionary_by_id(dict_id)
assert response.status_code == 200
data = response.json()
assert data["id"] == dict_id
assert data["type"] == test_dictionary_data["type"]
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_dictionary_by_id_not_found(self, authenticated_client):
"""测试获取不存在的字典"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.get_dictionary_by_id(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_dictionaries_by_type_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试根据类型获取字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.get_dictionaries_by_type(test_dictionary_data["type"])
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert any(d["id"] == dict_id for d in data)
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_all_dictionaries_success(self, authenticated_client):
"""测试获取所有字典成功"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.get_all_dictionaries()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试更新字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
update_data = {"name": "Updated name"}
response = await dict_api.update_dictionary(dict_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["name"] == "Updated name"
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_delete_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试删除字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.delete_dictionary(dict_id)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_check_type_and_code_exists_true(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试检查类型和编码存在-返回true"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.check_type_and_code_exists(
test_dictionary_data["type"],
test_dictionary_data["code"]
)
assert response.status_code == 200
assert response.json() is True
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_check_type_and_code_exists_false(self, authenticated_client):
"""测试检查类型和编码存在-返回false"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.check_type_and_code_exists("NONEXISTENT_TYPE", "NONEXISTENT_CODE")
assert response.status_code == 200
assert response.json() is False
+127
View File
@@ -0,0 +1,127 @@
"""
OAuth2客户端管理测试用例
"""
import pytest
from httpx import AsyncClient
@pytest.mark.oauth2
@pytest.mark.regression
class TestOAuth2:
"""OAuth2客户端管理测试类"""
@pytest.fixture
def test_oauth2_client_data(self):
"""测试OAuth2客户端数据"""
import time
timestamp = int(time.time() * 1000)
return {
"clientId": f"test-client-{timestamp}",
"clientSecret": "secret123",
"clientName": "Test Client",
"webServerRedirectUri": "http://localhost:8080/callback",
"scope": "read,write",
"authorizedGrantTypes": "authorization_code,refresh_token",
"accessTokenValiditySeconds": 7200,
"refreshTokenValiditySeconds": 2592000,
"autoApprove": False,
"enabled": True
}
@pytest.fixture
async def cleanup_oauth2_client(self, authenticated_client: AsyncClient):
"""清理测试OAuth2客户端"""
client_ids = []
yield client_ids
for client_id in client_ids:
try:
await authenticated_client.delete(f"/api/oauth2/clients/{client_id}")
except Exception:
pass
@pytest.mark.asyncio
async def test_create_oauth2_client_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试创建OAuth2客户端成功"""
response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["clientId"] == test_oauth2_client_data["clientId"]
assert data["clientName"] == test_oauth2_client_data["clientName"]
assert "clientSecret" not in data or data["clientSecret"] != test_oauth2_client_data["clientSecret"]
cleanup_oauth2_client.append(data["id"])
@pytest.mark.asyncio
async def test_get_oauth2_client_by_id_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试根据ID获取OAuth2客户端成功"""
create_response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
client_id = create_response.json()["id"]
response = await authenticated_client.get(f"/api/oauth2/clients/{client_id}")
assert response.status_code == 200
data = response.json()
assert data["id"] == client_id
assert data["clientId"] == test_oauth2_client_data["clientId"]
cleanup_oauth2_client.append(client_id)
@pytest.mark.asyncio
async def test_get_oauth2_client_by_id_not_found(self, authenticated_client):
"""测试获取不存在的OAuth2客户端"""
response = await authenticated_client.get("/api/oauth2/clients/999999")
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_oauth2_client_by_client_id_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试根据clientId获取OAuth2客户端成功"""
create_response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
client_id = create_response.json()["id"]
response = await authenticated_client.get(f"/api/oauth2/clients/client-id/{test_oauth2_client_data['clientId']}")
assert response.status_code == 200
data = response.json()
assert data["clientId"] == test_oauth2_client_data["clientId"]
cleanup_oauth2_client.append(client_id)
@pytest.mark.asyncio
async def test_get_all_oauth2_clients_success(self, authenticated_client):
"""测试获取所有OAuth2客户端成功"""
response = await authenticated_client.get("/api/oauth2/clients")
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_oauth2_client_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试更新OAuth2客户端成功"""
create_response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
client_id = create_response.json()["id"]
update_data = {"clientName": "Updated Client Name"}
response = await authenticated_client.put(f"/api/oauth2/clients/{client_id}", json=update_data)
assert response.status_code == 200
data = response.json()
assert data["clientName"] == "Updated Client Name"
cleanup_oauth2_client.append(client_id)
@pytest.mark.asyncio
async def test_delete_oauth2_client_success(self, authenticated_client, test_oauth2_client_data, cleanup_oauth2_client):
"""测试删除OAuth2客户端成功"""
create_response = await authenticated_client.post("/api/oauth2/clients", json=test_oauth2_client_data)
client_id = create_response.json()["id"]
response = await authenticated_client.delete(f"/api/oauth2/clients/{client_id}")
assert response.status_code == 204
+185
View File
@@ -0,0 +1,185 @@
"""
角色管理测试用例
"""
import pytest
from api.role_api import RoleAPI
@pytest.mark.role
@pytest.mark.regression
class TestRole:
"""角色管理测试类"""
@pytest.mark.asyncio
async def test_create_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试创建角色成功"""
role_api = RoleAPI(authenticated_client)
response = await role_api.create_role(test_role_data)
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["name"] == test_role_data["name"]
assert data["description"] == test_role_data["description"]
assert data["permissions"] == test_role_data["permissions"]
cleanup_role.append(data["id"])
@pytest.mark.asyncio
async def test_create_role_duplicate_name(self, authenticated_client, test_role_data, cleanup_role):
"""测试创建重复角色名"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.create_role(test_role_data)
assert response.status_code in [400, 409]
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_get_role_by_id_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试根据ID获取角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.get_role_by_id(role_id)
assert response.status_code == 200
data = response.json()
assert data["id"] == role_id
assert data["name"] == test_role_data["name"]
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_get_role_by_id_not_found(self, authenticated_client):
"""测试获取不存在的角色"""
role_api = RoleAPI(authenticated_client)
response = await role_api.get_role_by_id(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_role_by_name_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试根据名称获取角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.get_role_by_name(test_role_data["name"])
assert response.status_code == 200
data = response.json()
assert data["name"] == test_role_data["name"]
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_get_all_roles_success(self, authenticated_client):
"""测试获取所有角色成功"""
role_api = RoleAPI(authenticated_client)
response = await role_api.get_all_roles()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试更新角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
update_data = {"description": "Updated description"}
response = await role_api.update_role(role_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["description"] == "Updated description"
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_delete_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试删除角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.delete_role(role_id)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_logical_delete_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试逻辑删除角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.logical_delete_role(role_id)
assert response.status_code == 200
get_response = await role_api.get_role_by_id(role_id)
assert get_response.status_code == 404
get_deleted_response = await role_api.get_all_roles(include_deleted=True)
deleted_roles = get_deleted_response.json()
assert any(r["id"] == role_id for r in deleted_roles)
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_restore_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试恢复角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
await role_api.logical_delete_role(role_id)
response = await role_api.restore_role(role_id)
assert response.status_code == 200
get_response = await role_api.get_role_by_id(role_id)
assert get_response.status_code == 200
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_check_name_exists_true(self, authenticated_client, test_role_data, cleanup_role):
"""测试检查角色名存在-返回true"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.check_name_exists(test_role_data["name"])
assert response.status_code == 200
assert response.json() is True
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_check_name_exists_false(self, authenticated_client):
"""测试检查角色名存在-返回false"""
role_api = RoleAPI(authenticated_client)
response = await role_api.check_name_exists("NONEXISTENT_ROLE")
assert response.status_code == 200
assert response.json() is False
+193
View File
@@ -0,0 +1,193 @@
"""
用户管理测试用例
"""
import pytest
from api.user_api import UserAPI
from config.settings import settings
@pytest.mark.user
@pytest.mark.regression
class TestUser:
"""用户管理测试类"""
@pytest.mark.asyncio
async def test_create_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试创建用户成功"""
user_api = UserAPI(authenticated_client)
response = await user_api.create_user(test_user_data)
print(f"Response status: {response.status_code}")
print(f"Response text: {response.text}")
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["username"] == test_user_data["username"]
assert data["email"] == test_user_data["email"]
assert "password" not in data or data["password"] != test_user_data["password"]
cleanup_user.append(data["id"])
@pytest.mark.asyncio
async def test_create_user_duplicate_username(self, authenticated_client, test_user_data, cleanup_user):
"""测试创建重复用户名"""
user_api = UserAPI(authenticated_client)
await user_api.create_user(test_user_data)
response = await user_api.create_user(test_user_data)
assert response.status_code in [400, 409]
@pytest.mark.asyncio
async def test_get_user_by_id_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试根据ID获取用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.get_user_by_id(user_id)
assert response.status_code == 200
data = response.json()
assert data["id"] == user_id
assert data["username"] == test_user_data["username"]
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_get_user_by_id_not_found(self, authenticated_client):
"""测试获取不存在的用户"""
user_api = UserAPI(authenticated_client)
response = await user_api.get_user_by_id(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_all_users_success(self, authenticated_client):
"""测试获取所有用户成功"""
user_api = UserAPI(authenticated_client)
response = await user_api.get_all_users()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试更新用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
update_data = {"email": "updated@example.com"}
response = await user_api.update_user(user_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["email"] == "updated@example.com"
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_delete_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试删除用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.delete_user(user_id)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_logical_delete_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试逻辑删除用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.logical_delete_user(user_id)
assert response.status_code == 200
get_response = await user_api.get_user_by_id(user_id)
assert get_response.status_code == 404
get_deleted_response = await user_api.get_all_users(include_deleted=True)
deleted_users = get_deleted_response.json()
assert any(u["id"] == user_id for u in deleted_users)
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_restore_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试恢复用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
await user_api.logical_delete_user(user_id)
response = await user_api.restore_user(user_id)
assert response.status_code == 200
get_response = await user_api.get_user_by_id(user_id)
assert get_response.status_code == 200
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_check_username_exists_true(self, authenticated_client, test_user_data, cleanup_user):
"""测试检查用户名存在-返回true"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.check_username_exists(test_user_data["username"])
assert response.status_code == 200
assert response.json() is True
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_check_username_exists_false(self, authenticated_client):
"""测试检查用户名存在-返回false"""
user_api = UserAPI(authenticated_client)
response = await user_api.check_username_exists("nonexistent_user")
assert response.status_code == 200
assert response.json() is False
@pytest.mark.asyncio
async def test_check_email_exists_true(self, authenticated_client, test_user_data, cleanup_user):
"""测试检查邮箱存在-返回true"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.check_email_exists(test_user_data["email"])
assert response.status_code == 200
assert response.json() is True
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_check_email_exists_false(self, authenticated_client):
"""测试检查邮箱存在-返回false"""
user_api = UserAPI(authenticated_client)
response = await user_api.check_email_exists("nonexistent@example.com")
assert response.status_code == 200
assert response.json() is False