test: P2测试覆盖 — user/pool/schema_drift端点 43个新测试 (895→1017全通过)
新增测试文件: - test_user_endpoint.py (18 tests): 用户CRUD/列表过滤/权限控制/自删保护 - test_pool_endpoint.py (12 tests): 连接池状态/重置/概览/mock外部依赖 - test_schema_drift_endpoint.py (13 tests): Schema快照捕获/漂移检测/历史查询 覆盖率提升: - user端点: 0→18 tests (CRUD+权限+重复用户名+自删保护) - pool端点: 0→12 tests (stats/reset/overview+角色访问控制) - schema_drift端点: 0→13 tests (capture/check/history+dict序列化兼容) 修复: run.sh启动脚本同步
This commit is contained in:
1 parent
b39c426317
commit
305bbb153c
8 files changed
+1874
-7
No files matched your search
@@ -9,8 +9,11 @@
|
||||
- 集成测试: query/ticket 端点权限检查
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import pytest
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import HTTPException
|
||||
from unittest.mock import patch, AsyncMock, MagicMock
|
||||
|
||||
from app.models.instance import Instance
|
||||
from app.models.instance_group import InstanceGroup, user_groups
|
||||
@@ -243,7 +246,7 @@ class TestPermissionEndpointIntegration:
|
||||
async def test_query_with_permission(
|
||||
self, client, db, developer_user, group_with_permissions
|
||||
):
|
||||
"""有 query:select 权限 → 查询不被 403"""
|
||||
"""有 query:select 权限 → 查询不被 403(mock DB连接避免挂起)"""
|
||||
group, instance = group_with_permissions
|
||||
await db.execute(
|
||||
user_groups.insert().values(
|
||||
@@ -254,12 +257,38 @@ class TestPermissionEndpointIntegration:
|
||||
token = create_access_token(
|
||||
data={"sub": str(developer_user.id), "role": developer_user.role}
|
||||
)
|
||||
resp = await client.post(
|
||||
"/api/v1/query",
|
||||
json={"instance_id": instance.id, "database": "test", "sql": "SELECT 1"},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
# 不应是 403(可能 400/500 因为连不上真DB,但权限通过)
|
||||
|
||||
# Mock aiomysql 防止真实连接挂起(见 skill #20, #37)
|
||||
mock_cursor = AsyncMock()
|
||||
mock_cursor.description = [("1",)]
|
||||
mock_cursor.fetchall.return_value = [{"1": 1}]
|
||||
mock_cursor.close = AsyncMock()
|
||||
|
||||
@asynccontextmanager
|
||||
async def mock_cursor_ctx(*a, **kw):
|
||||
yield mock_cursor
|
||||
|
||||
mock_conn = AsyncMock()
|
||||
mock_conn.cursor = mock_cursor_ctx
|
||||
mock_conn.commit = AsyncMock()
|
||||
mock_conn.rollback = AsyncMock()
|
||||
mock_conn.close = AsyncMock()
|
||||
|
||||
mock_aiomysql = MagicMock()
|
||||
mock_aiomysql.connect = AsyncMock(return_value=mock_conn)
|
||||
mock_aiomysql.DictCursor = MagicMock()
|
||||
|
||||
with patch.dict(sys.modules, {"aiomysql": mock_aiomysql}):
|
||||
resp = await client.post(
|
||||
"/api/v1/query",
|
||||
json={
|
||||
"instance_id": instance.id,
|
||||
"database": "test",
|
||||
"sql": "SELECT 1",
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
# 不应是 403(权限通过,mock 返回成功)
|
||||
assert resp.status_code != 403
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in new issue
Block a user