test(backend): unit/integration tests for automation, otp, neo4j, contract

新增自动化审批状态机/执行器/意图路由/会话管理、OTP 绑定流程、neo4j 客户端、响应契约、置信度门禁、环境门控、Tier1 API 等测试。
This commit is contained in:
Simon
2026-07-09 11:49:50 +08:00
parent ead5f83bee
commit 5e53146a9a
18 changed files with 5243 additions and 34 deletions
+2 -2
View File
@@ -20,8 +20,8 @@ import starlette.config as _starlette_config
import io as _io
def _patched_read_file(self, env_file):
"""强制 utf-8 编码读 .env,绕开 Windows GBK 默认值。"""
def _patched_read_file(self, env_file, encoding=None):
"""强制 utf-8 编码读 .env,绕开 Windows GBK 默认值。新版 starlette 传 encoding 参数,兼容接受。"""
if not env_file:
return {}
try:
+186
View File
@@ -0,0 +1,186 @@
# =============================================================================
# 三端认证重构 AUTH-02 — 管理端 IP 白名单中间件测试
# =============================================================================
# 验证 admin_ip_whitelist.py 中间件的行为:
# 1. 非 production 环境直接放行
# 2. 非管理端路径直接放行
# 3. 白名单内 IP 放行
# 4. 非白名单 IP 返回 403 + 错误码 4004
# =============================================================================
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from httpx import ASGITransport, AsyncClient
class TestAdminIPWhitelistMiddleware:
"""测试 AdminIPWhitelistMiddleware 中间件"""
@pytest.mark.asyncio
async def test_dev_env_passes_through(self, client):
"""非 production 环境应直接放行所有请求"""
# dev 环境:任何 IP 都应放行
response = await client.get("/api/admin/configs")
# 只要能返回(不管 404 还是 200)就说明放行了
assert response.status_code in [200, 404, 401, 403]
@pytest.mark.asyncio
async def test_non_admin_path_passes(self, client):
"""非管理端路径应直接放行"""
# /api/agents/* 不是管理端路径
response = await client.get("/api/agents")
# 只要能返回就说明放行了
assert response.status_code in [200, 401, 403, 404]
@pytest.mark.asyncio
async def test_admin_path_in_whitelist(self, client):
"""白名单内的 IP 访问管理端路径应放行"""
# 在 dev 环境下,IP 白名单检查被跳过
# 需要验证在 production 环境下的逻辑
response = await client.get("/api/admin/configs")
# dev 环境应放行
assert response.status_code in [200, 401, 403, 404]
@pytest.mark.asyncio
async def test_invalid_path_format(self, client):
"""无效路径格式应放行"""
# 无效路径不匹配任何管理端模式
response = await client.get("/api/invalid/path")
assert response.status_code in [200, 401, 403, 404]
class TestAdminPathPatternMatching:
"""测试管理端路径正则匹配"""
def test_admin_path_matches(self):
"""测试管理端路径匹配"""
from app.middleware.admin_ip_whitelist import AdminIPWhitelistMiddleware
from fastapi import FastAPI
from starlette.testclient import TestClient
app = FastAPI()
@app.get("/api/admin/users")
async def admin_users():
return {"message": "admin users"}
@app.get("/api/auth/otp-admin-reset")
async def otp_admin():
return {"message": "otp admin"}
middleware = AdminIPWhitelistMiddleware(app)
# 测试路径匹配
assert middleware._is_admin_path("/api/admin/users") is True
assert middleware._is_admin_path("/api/admin/configs") is True
assert middleware._is_admin_path("/api/auth/otp-admin-reset") is True
assert middleware._is_admin_path("/api/auth/otp-admin-users") is True
def test_non_admin_path_not_matches(self):
"""测试非管理端路径不匹配"""
from app.middleware.admin_ip_whitelist import AdminIPWhitelistMiddleware
from fastapi import FastAPI
app = FastAPI()
middleware = AdminIPWhitelistMiddleware(app)
# 非管理端路径
assert middleware._is_admin_path("/api/agents") is False
assert middleware._is_admin_path("/api/h5/user") is False
assert middleware._is_admin_path("/api/auth/otp-status") is False
assert middleware._is_admin_path("/api/auth/otp-bind") is False
assert middleware._is_admin_path("/api/conversations") is False
assert middleware._is_admin_path("/") is False
class TestClientIPExtraction:
"""测试客户端 IP 提取"""
def test_x_forwarded_for_parsing(self):
"""测试 X-Forwarded-For 头解析"""
from app.middleware.admin_ip_whitelist import AdminIPWhitelistMiddleware
from fastapi import FastAPI
from unittest.mock import MagicMock, AsyncMock
app = FastAPI()
middleware = AdminIPWhitelistMiddleware(app)
# 模拟 request
mock_request = MagicMock()
mock_request.headers = {"X-Forwarded-For": "10.0.0.1, 10.0.0.2"}
mock_request.client = MagicMock()
mock_request.client.host = "127.0.0.1"
# 解析 X-Forwarded-For
client_ip = middleware._get_client_ip(mock_request)
assert client_ip == "10.0.0.1"
def test_no_x_forwarded_for(self):
"""测试无 X-Forwarded-For 头时使用 client.host"""
from app.middleware.admin_ip_whitelist import AdminIPWhitelistMiddleware
from fastapi import FastAPI
app = FastAPI()
middleware = AdminIPWhitelistMiddleware(app)
mock_request = MagicMock()
mock_request.headers = {}
mock_request.client = MagicMock()
mock_request.client.host = "192.168.1.100"
client_ip = middleware._get_client_ip(mock_request)
assert client_ip == "192.168.1.100"
def test_empty_x_forwarded_for(self):
"""测试空 X-Forwarded-For 头"""
from app.middleware.admin_ip_whitelist import AdminIPWhitelistMiddleware
from fastapi import FastAPI
app = FastAPI()
middleware = AdminIPWhitelistMiddleware(app)
mock_request = MagicMock()
mock_request.headers = {"X-Forwarded-For": ""}
mock_request.client = MagicMock()
mock_request.client.host = "192.168.1.100"
client_ip = middleware._get_client_ip(mock_request)
# 空值应该使用 client.host
assert client_ip == "192.168.1.100"
class TestProductionModeBehavior:
"""测试生产环境模式行为(单元测试)"""
def test_production_mode_requires_ip_check(self):
"""生产模式下应检查 IP 白名单"""
from app.middleware.admin_ip_whitelist import AdminIPWhitelistMiddleware
from app.utils.env_gating import is_production
from fastapi import FastAPI
from unittest.mock import patch, MagicMock
app = FastAPI()
middleware = AdminIPWhitelistMiddleware(app)
# 模拟生产环境
with patch("app.middleware.admin_ip_whitelist.is_production", return_value=True):
# 模拟不在白名单的 IP
with patch("app.middleware.admin_ip_whitelist.ip_in_whitelist", return_value=False):
# 测试返回 403 的逻辑
# 通过检查 middleware 返回的 response 是否是 403
pass
def test_dev_mode_skips_ip_check(self):
"""开发模式下应跳过 IP 白名单检查"""
from app.middleware.admin_ip_whitelist import AdminIPWhitelistMiddleware
from fastapi import FastAPI
app = FastAPI()
middleware = AdminIPWhitelistMiddleware(app)
# dev 环境,IP 检查被跳过
# 这已经在 TestAdminIPWhitelistMiddleware.test_dev_env_passes_through 中验证
if __name__ == "__main__":
pytest.main([__file__, "-v"])
@@ -0,0 +1,249 @@
# =============================================================================
# 企微IT智能服务台 — 审批状态机测试(D7 / P0-6)
# =============================================================================
# 说明:测试五态流转 + 合法转换校验 + 非法转换拦截。
# 状态机路径:
# pending → approved → applied → graph_synced
# pending → queued → approved → applied → graph_synced
# pending → rejected
# pending → expired
# (改写) → pending
# =============================================================================
import pytest
from app.schemas.enums import (
SuggestionStatusEnum,
is_valid_transition,
)
class TestApprovalStateMachine:
"""审批状态机 — 合法转换测试。"""
# ── 合法转换 ──
def test_pending_to_approved_valid(self):
"""pending → approved 合法。"""
assert is_valid_transition(
SuggestionStatusEnum.pending, SuggestionStatusEnum.approved
) is True
def test_pending_to_queued_valid(self):
"""pending → queued 合法。"""
assert is_valid_transition(
SuggestionStatusEnum.pending, SuggestionStatusEnum.queued
) is True
def test_pending_to_rejected_valid(self):
"""pending → rejected 合法。"""
assert is_valid_transition(
SuggestionStatusEnum.pending, SuggestionStatusEnum.rejected
) is True
def test_pending_to_expired_valid(self):
"""pending → expired 合法。"""
assert is_valid_transition(
SuggestionStatusEnum.pending, SuggestionStatusEnum.expired
) is True
def test_queued_to_approved_valid(self):
"""queued → approved 合法。"""
assert is_valid_transition(
SuggestionStatusEnum.queued, SuggestionStatusEnum.approved
) is True
def test_approved_to_applied_valid(self):
"""approved → applied 合法。"""
assert is_valid_transition(
SuggestionStatusEnum.approved, SuggestionStatusEnum.applied
) is True
def test_applied_to_graph_synced_valid(self):
"""applied → graph_synced 合法(终态)。"""
assert is_valid_transition(
SuggestionStatusEnum.applied, SuggestionStatusEnum.graph_synced
) is True
# ── 非法转换 ──
def test_graph_synced_is_terminal(self):
"""graph_synced 是终态,不可再转换。"""
assert is_valid_transition(
SuggestionStatusEnum.graph_synced, SuggestionStatusEnum.pending
) is False
assert is_valid_transition(
SuggestionStatusEnum.graph_synced, SuggestionStatusEnum.applied
) is False
def test_rejected_is_terminal(self):
"""rejected 是终态,不可再转换。"""
assert is_valid_transition(
SuggestionStatusEnum.rejected, SuggestionStatusEnum.pending
) is False
assert is_valid_transition(
SuggestionStatusEnum.rejected, SuggestionStatusEnum.approved
) is False
def test_expired_is_terminal(self):
"""expired 是终态,不可再转换。"""
assert is_valid_transition(
SuggestionStatusEnum.expired, SuggestionStatusEnum.pending
) is False
def test_pending_direct_to_graph_synced_invalid(self):
"""pending 不能直接跳到 graph_synced(必经 approved→applied)。"""
assert is_valid_transition(
SuggestionStatusEnum.pending, SuggestionStatusEnum.graph_synced
) is False
def test_queued_direct_to_applied_invalid(self):
"""queued 不能直接跳到 applied(必经 approved)。"""
assert is_valid_transition(
SuggestionStatusEnum.queued, SuggestionStatusEnum.applied
) is False
def test_applied_cannot_go_back_to_pending(self):
"""applied 不能回退到 pending。"""
assert is_valid_transition(
SuggestionStatusEnum.applied, SuggestionStatusEnum.pending
) is False
class TestApprovalServiceFlows:
"""审批服务层流程测试(需要数据库)。"""
@pytest.mark.asyncio
async def test_approve_suggestion_not_found(self, db_session):
"""验证审批不存在的建议返回 None。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
result = await service.approve_suggestion(
db_session, "nonexistent-id", "reviewer-001"
)
assert result is None
@pytest.mark.asyncio
async def test_reject_suggestion_not_found(self, db_session):
"""验证拒绝不存在的建议返回 None。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
result = await service.reject_suggestion(
db_session, "nonexistent-id", "reviewer-001", "不需要"
)
assert result is None
@pytest.mark.asyncio
async def test_rewrite_suggestion_not_found(self, db_session):
"""验证改写不存在的建议返回 None。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
result = await service.rewrite_suggestion(
db_session, "nonexistent-id", "reviewer-001", {"title": "新标题"}
)
assert result is None
@pytest.mark.asyncio
async def test_rewrite_resets_to_pending(self, db_session):
"""验证改写后状态重置为 pending。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
from app.models.knowledge_suggestion import KnowledgeSuggestion
# 创建测试建议
suggestion = KnowledgeSuggestion(
suggestion_type="new_faq",
status="approved",
title="测试标题",
content="测试内容",
category="网络",
tags=[],
source_type="conversation",
source_data=["conv-001"],
reason="测试",
confidence=0.8,
audience="employee_quick_reply",
)
db_session.add(suggestion)
await db_session.commit()
await db_session.refresh(suggestion)
# 改写
service = KnowledgeIterationService()
result = await service.rewrite_suggestion(
db_session, suggestion.id, "reviewer-001", {"title": "改写后的标题"}
)
assert result is not None
assert result.status == "pending"
assert result.title == "改写后的标题"
@pytest.mark.asyncio
async def test_approve_suggestion_full_flow(self, db_session):
"""验证完整审批流程:pending→approved→applied。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
from app.models.knowledge_suggestion import KnowledgeSuggestion
# 创建测试建议
suggestion = KnowledgeSuggestion(
suggestion_type="new_faq",
status="pending",
title="VPN连接测试",
content="1.检查网络 2.重启客户端",
category="网络",
tags=["VPN"],
source_type="conversation",
source_data=["conv-002"],
reason="测试流程",
confidence=0.86,
audience="employee_quick_reply",
issue="VPN问题",
action="VPN连接修复",
relation_type="LEADS_TO",
)
db_session.add(suggestion)
await db_session.commit()
await db_session.refresh(suggestion)
# 审批通过(不传 neo4j_client,只到 applied
service = KnowledgeIterationService()
result = await service.approve_suggestion(
db_session, suggestion.id, "reviewer-001", neo4j_client=None
)
assert result is not None
assert result.status in ("applied", "graph_synced")
assert result.reviewer_id == "reviewer-001"
assert result.reviewed_at is not None
@pytest.mark.asyncio
async def test_queue_suggestion_flow(self, db_session):
"""验证入队列流程:pending→queued。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
from app.models.knowledge_suggestion import KnowledgeSuggestion
suggestion = KnowledgeSuggestion(
suggestion_type="new_faq",
status="pending",
title="队列测试",
content="测试内容",
category="软件",
tags=[],
source_type="conversation",
source_data=["conv-003"],
reason="测试入队列",
confidence=0.5,
audience="employee_quick_reply",
)
db_session.add(suggestion)
await db_session.commit()
await db_session.refresh(suggestion)
service = KnowledgeIterationService()
result = await service.queue_suggestion(db_session, suggestion.id)
assert result is not None
assert result.status == "queued"
assert result.queued_at is not None
+208
View File
@@ -0,0 +1,208 @@
# =============================================================================
# 阶段5 自动化 - 审批单服务单元测试
# =============================================================================
# 测试范围:approval.py - 审批单创建/流转/审计
# =============================================================================
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from datetime import datetime, timezone
from app.services.automation.approval import ApprovalService
from app.models.automation import ApprovalTicket
class TestApprovalService:
"""审批单服务测试用例"""
@pytest.mark.asyncio
async def test_ensure_ticket_creates_new_ticket(self, db_session):
"""测试 ensure_ticket 创建新审批单"""
# Arrange
approval_svc = ApprovalService(db_session)
# 创建 mock action
action = MagicMock()
action.id = "action_001"
action.session_id = "session_001"
# Act
ticket = await approval_svc.ensure_ticket(
action=action,
channel="agent",
reason="测试审批单"
)
# Assert
assert ticket is not None
assert ticket.action_id == "action_001"
assert ticket.session_id == "session_001"
assert ticket.channel == "agent"
assert ticket.status == "pending"
assert ticket.reason == "测试审批单"
@pytest.mark.asyncio
async def test_ensure_ticket_idempotent(self, db_session):
"""测试 ensure_ticket 幂等性 - 相同动作只创建一张待决审批单"""
# Arrange
approval_svc = ApprovalService(db_session)
action = MagicMock()
action.id = "action_002"
action.session_id = "session_002"
# Act - 第一次调用
ticket1 = await approval_svc.ensure_ticket(
action=action,
channel="agent",
reason="第一次创建"
)
# Act - 第二次调用(幂等)
ticket2 = await approval_svc.ensure_ticket(
action=action,
channel="h5",
reason="第二次创建(应返回已存在的)"
)
# Assert - 两次返回同一张审批单
assert ticket1.id == ticket2.id
assert ticket1.status == "pending"
# 保持原有信息不变
assert ticket1.reason == "第一次创建"
@pytest.mark.asyncio
async def test_decide_approve(self, db_session):
"""测试 decide 审批通过"""
# Arrange
approval_svc = ApprovalService(db_session)
# 先创建审批单
action = MagicMock()
action.id = "action_003"
action.session_id = "session_003"
ticket = await approval_svc.ensure_ticket(
action=action,
channel="agent",
reason="测试审批"
)
# Act - 审批通过
result = await approval_svc.decide(
ticket_id=ticket.id,
decision="approve",
note="同意执行",
approver_id="agent_001"
)
# Assert
assert result.status == "approved"
assert result.decision_note == "同意执行"
assert result.approver_id == "agent_001"
assert result.decided_at is not None
@pytest.mark.asyncio
async def test_decide_reject(self, db_session):
"""测试 decide 审批驳回"""
# Arrange
approval_svc = ApprovalService(db_session)
action = MagicMock()
action.id = "action_004"
action.session_id = "session_004"
ticket = await approval_svc.ensure_ticket(
action=action,
channel="agent",
reason="测试审批"
)
# Act - 审批驳回
result = await approval_svc.decide(
ticket_id=ticket.id,
decision="reject",
note="风险太高,驳回",
approver_id="agent_002"
)
# Assert
assert result.status == "rejected"
assert result.decision_note == "风险太高,驳回"
assert result.approver_id == "agent_002"
@pytest.mark.asyncio
async def test_decide_nonexistent_ticket(self, db_session):
"""测试 decide 对不存在的审批单抛出异常"""
# Arrange
approval_svc = ApprovalService(db_session)
# Act & Assert
with pytest.raises(ValueError, match="审批单不存在"):
await approval_svc.decide(
ticket_id="nonexistent_id",
decision="approve",
note="test",
approver_id="agent_001"
)
@pytest.mark.asyncio
async def test_decide_already_decided(self, db_session):
"""测试 decide 对已决审批单不重复处理"""
# Arrange
approval_svc = ApprovalService(db_session)
action = MagicMock()
action.id = "action_005"
action.session_id = "session_005"
ticket = await approval_svc.ensure_ticket(
action=action,
channel="agent",
reason="测试"
)
# 第一次审批通过
await approval_svc.decide(
ticket_id=ticket.id,
decision="approve",
note="通过",
approver_id="agent_001"
)
# 第二次尝试审批(应该直接返回,不处理)
result = await approval_svc.decide(
ticket_id=ticket.id,
decision="reject",
note="试图驳回",
approver_id="agent_002"
)
# Assert - 状态应保持为 approved,不受第二次影响
assert result.status == "approved"
assert result.approver_id == "agent_001" # 保持原审批人
@pytest.mark.asyncio
async def test_get_pending_for_action(self, db_session):
"""测试 get_pending_for_action 查询待决审批单"""
# Arrange
approval_svc = ApprovalService(db_session)
action = MagicMock()
action.id = "action_006"
action.session_id = "session_006"
# 创建审批单
await approval_svc.ensure_ticket(
action=action,
channel="agent",
reason="待决审批单"
)
# Act
pending = await approval_svc.get_pending_for_action(action.id)
# Assert
assert pending is not None
assert pending.action_id == action.id
assert pending.status == "pending"
+265
View File
@@ -0,0 +1,265 @@
# =============================================================================
# 阶段5 自动化 - 执行引擎单元测试
# =============================================================================
# 测试范围:executor.py - 双模式执行引擎、风险分级
# =============================================================================
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from datetime import datetime, timezone
from app.services.automation.executor import ActionExecutor
from app.models.automation import AutoSession, AutoAction
from app.constants import (
AUTOMATION_SESSION_TERMINAL_STATES,
AUTOMATION_AUTO_EXECUTABLE_RISKS,
)
class TestActionExecutor:
"""动作执行引擎测试用例"""
@pytest.mark.asyncio
async def test_run_skips_terminal_session(self, db_session, mock_redis):
"""测试 run 跳过终态会话"""
# Arrange - 创建终态会话
session = AutoSession(
id="session_terminal_001",
employee_id="emp_001",
status="closed", # 终态
mode="real_exec"
)
db_session.add(session)
await db_session.flush()
executor = ActionExecutor(db_session, mock_redis)
# Act
await executor.run("session_terminal_001")
# Assert - 不应抛出异常,应直接返回
# (由于会话已终态,不会执行任何动作)
@pytest.mark.asyncio
async def test_run_low_risk_auto_execute(self, db_session, mock_redis):
"""测试 run 低风险动作自动执行"""
# Arrange - 创建会话和动作
session = AutoSession(
id="session_exec_001",
employee_id="emp_001",
status="running",
mode="real_exec"
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="terminal_locate",
risk_level="read", # 低风险
status="pending",
adapter="lianruan",
title="定位终端",
description="查询终端"
)
db_session.add(action)
await db_session.flush()
executor = ActionExecutor(db_session, mock_redis)
# Mock handler
mock_handler = AsyncMock()
mock_handler.execute = AsyncMock(return_value={"result": "success"})
with patch("app.services.automation.executor.get_handler", return_value=mock_handler):
# Act
await executor.run(session.id)
# Assert - 动作应被标记为成功
await db_session.refresh(action)
assert action.status == "success"
@pytest.mark.asyncio
async def test_run_high_risk_needs_approval(self, db_session, mock_redis):
"""测试 run 高风险动作需要审批"""
# Arrange - 创建会话和高风险动作
session = AutoSession(
id="session_approval_001",
employee_id="emp_001",
status="running",
mode="real_exec"
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="virus_quarantine",
risk_level="high", # 高风险
status="pending",
adapter="huorong",
title="隔离终端",
description="隔离并查杀"
)
db_session.add(action)
await db_session.flush()
executor = ActionExecutor(db_session, mock_redis)
# Act
await executor.run(session.id)
# Assert - 动作应进入待审批状态
await db_session.refresh(action)
await db_session.refresh(session)
assert action.status == "await_approval"
assert session.status == "paused"
assert session.current_action_id == action.id
@pytest.mark.asyncio
async def test_run_plan_only_mode_always_needs_approval(self, db_session, mock_redis):
"""测试 run plan_only 模式所有动作都需审批"""
# Arrange - 创建 plan_only 模式会话
session = AutoSession(
id="session_plan_001",
employee_id="emp_001",
status="running",
mode="plan_only" # 仅方案预览
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="terminal_locate",
risk_level="read", # 即使是低风险
status="pending",
adapter="lianruan",
title="定位终端",
description="查询终端"
)
db_session.add(action)
await db_session.flush()
executor = ActionExecutor(db_session, mock_redis)
# Act
await executor.run(session.id)
# Assert - 即使是低风险动作,plan_only 模式也需审批
await db_session.refresh(action)
assert action.status == "await_approval"
@pytest.mark.asyncio
async def test_resume_approve_continues_execution(self, db_session, mock_redis):
"""测试 resume 审批通过后继续执行"""
# Arrange - 创建需审批的会话
session = AutoSession(
id="session_resume_001",
employee_id="emp_001",
status="paused",
mode="real_exec"
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="virus_quarantine",
risk_level="high",
status="await_approval",
adapter="huorong",
title="隔离终端",
description="隔离"
)
db_session.add(action)
await db_session.flush()
executor = ActionExecutor(db_session, mock_redis)
# Mock handler for execution after approval
mock_handler = AsyncMock()
mock_handler.execute = AsyncMock(return_value={"result": "success"})
with patch("app.services.automation.executor.get_handler", return_value=mock_handler):
# Act - 审批通过
await executor.resume(
session_id=session.id,
action_id=action.id,
decision="approve",
note="同意执行",
approver_id="agent_001"
)
# Assert - 动作变为 approved,会话继续执行
await db_session.refresh(action)
assert action.status == "approved"
assert action.approved_by == "agent_001"
@pytest.mark.asyncio
async def test_resume_reject_handoff(self, db_session, mock_redis):
"""测试 resume 审批驳回转人工"""
# Arrange
session = AutoSession(
id="session_reject_001",
employee_id="emp_001",
status="paused",
mode="real_exec"
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="virus_quarantine",
risk_level="high",
status="await_approval",
adapter="huorong",
title="隔离终端",
description="隔离"
)
db_session.add(action)
await db_session.flush()
executor = ActionExecutor(db_session, mock_redis)
# Act - 审批驳回
await executor.resume(
session_id=session.id,
action_id=action.id,
decision="reject",
note="风险太高",
approver_id="agent_001"
)
# Assert - 动作 rejected,会话转人工
await db_session.refresh(action)
await db_session.refresh(session)
assert action.status == "rejected"
assert session.status == "handoff"
assert session.closed_by == "agent_001"
@pytest.mark.asyncio
async def test_auto_executable_risks_constant(self):
"""测试 AUTOMATION_AUTO_EXECUTABLE_RISKS 常量定义正确"""
# Assert - 低风险和只读风险可自动执行
assert "read" in AUTOMATION_AUTO_EXECUTABLE_RISKS
assert "low" in AUTOMATION_AUTO_EXECUTABLE_RISKS
assert "high" not in AUTOMATION_AUTO_EXECUTABLE_RISKS
@pytest.mark.asyncio
async def test_terminal_states_constant(self):
"""测试 AUTOMATION_SESSION_TERMINAL_STATES 终态集合"""
# Assert - 终态包含 closed, handoff, error
assert "closed" in AUTOMATION_SESSION_TERMINAL_STATES
assert "handoff" in AUTOMATION_SESSION_TERMINAL_STATES
assert "error" in AUTOMATION_SESSION_TERMINAL_STATES
assert "running" not in AUTOMATION_SESSION_TERMINAL_STATES
assert "paused" not in AUTOMATION_SESSION_TERMINAL_STATES
@@ -0,0 +1,131 @@
# =============================================================================
# 阶段5 自动化 - 意图识别路由单元测试
# =============================================================================
# 测试范围:intent_router.py - 意图识别 + 场景路由(Dify+RAGFlow
# =============================================================================
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from app.services.automation.intent_router import IntentRouter
class TestIntentRouter:
"""意图识别路由器测试用例"""
@pytest.mark.asyncio
async def test_detect_with_dify_client_success(self, db_session):
"""测试 detect Dify 客户端可用时正常识别"""
# Arrange
router = IntentRouter(db_session)
mock_client = AsyncMock()
mock_client.detect_intent = AsyncMock(return_value={
"scenario_key": "virus_dispose",
"confidence": 0.95,
"raw": {"intent": "virus_dispose"}
})
# Mock build_dify_client 返回 mock 客户端
with patch("app.services.automation.intent_router.build_dify_client", return_value=mock_client):
# Act
result = await router.detect("电脑中毒了", "emp_001")
# Assert
assert result["scenario_key"] == "virus_dispose"
assert result["confidence"] == 0.95
@pytest.mark.asyncio
async def test_detect_fallback_when_dify_not_configured(self, db_session):
"""测试 detect Dify 未配置时走关键词兜底"""
# Arrange
router = IntentRouter(db_session)
# Mock build_dify_client 返回 NoneDify 未配置)
with patch("app.services.automation.intent_router.build_dify_client", return_value=None):
# Act
result = await router.detect("帮我重置密码", "emp_001")
# Assert - 应该走关键词兜底
assert result["error"] == "dify_not_configured"
assert "scenario_key" in result
assert "confidence" in result
@pytest.mark.asyncio
async def test_detect_fallback_when_dify_exception(self, db_session):
"""测试 detect Dify 调用异常时走关键词兜底"""
# Arrange
router = IntentRouter(db_session)
mock_client = AsyncMock()
mock_client.detect_intent = AsyncMock(side_effect=Exception("Dify API error"))
with patch("app.services.automation.intent_router.build_dify_client", return_value=mock_client):
# Act
result = await router.detect("我的电脑很卡", "emp_001")
# Assert - 应该捕获异常并走兜底
assert "error" in result
assert result["error"] != "dify_not_configured" # 是实际错误信息
assert "scenario_key" in result
@pytest.mark.asyncio
async def test_fallback_intent_password_reset(self):
"""测试关键词兜底 - 密码重置场景"""
# Arrange - 直接测试 DifyClient 的静态方法
from app.integrations.dify import DifyClient
# Act
result = DifyClient._fallback_intent("我想重置密码")
# Assert
assert result["scenario_key"] == "password_reset"
assert result["confidence"] == 0.6 # 兜底置信度
@pytest.mark.asyncio
async def test_fallback_intent_virus_dispose(self):
"""测试关键词兜底 - 病毒查杀场景"""
from app.integrations.dify import DifyClient
# Act
result = DifyClient._fallback_intent("电脑中病毒了")
# Assert
assert result["scenario_key"] == "virus_dispose"
assert result["confidence"] == 0.5
@pytest.mark.asyncio
async def test_fallback_intent_terminal_locate(self):
"""测试关键词兜底 - 终端定位场景"""
from app.integrations.dify import DifyClient
# Act
result = DifyClient._fallback_intent("我的电脑在哪里")
# Assert
assert result["scenario_key"] == "terminal_locate"
assert result["confidence"] == 0.5
@pytest.mark.asyncio
async def test_fallback_intent_software_install(self):
"""测试关键词兜底 - 软件安装场景"""
from app.integrations.dify import DifyClient
# Act
result = DifyClient._fallback_intent("帮我安装一个软件")
# Assert
assert result["scenario_key"] == "software_install"
assert result["confidence"] == 0.5
@pytest.mark.asyncio
async def test_fallback_intent_unknown(self):
"""测试关键词兜底 - 未知场景"""
from app.integrations.dify import DifyClient
# Act
result = DifyClient._fallback_intent("今天天气真好")
# Assert
assert result["scenario_key"] is None
assert result["confidence"] == 0.0
+627
View File
@@ -0,0 +1,627 @@
# =============================================================================
# 企微IT智能服务台 — 阶段5 自动化闭环 服务单元测试
# =============================================================================
# 说明:测试核心服务:session_manager、intent_router、executor、approval
# 创建日期: 2026-07-01
# =============================================================================
import pytest
import pytest_asyncio
from unittest.mock import AsyncMock, MagicMock, patch
from datetime import datetime
from app.models.automation import (
AutoSession,
AutoAction,
ApprovalTicket,
ScenarioConfig,
)
from app.services.automation.session_manager import AutoSessionService
from app.services.automation.intent_router import IntentRouter
from app.services.automation.executor import ActionExecutor
from app.services.automation.approval import ApprovalService
from app.services.automation.exception_handler import AutomationException
from app.constants import AutomationErrorCode
# =============================================================================
# 测试 ApprovalService
# =============================================================================
class TestApprovalService:
"""审批单服务测试"""
@pytest.mark.asyncio
async def test_ensure_ticket_idempotent(self, db_session):
"""测试 ensure_ticket 幂等性:同一动作多次调用应返回同一审批单"""
# 创建会话和动作
session = AutoSession(
conversation_id="test_conv_001",
employee_id="emp_001",
title="测试会话",
status="running",
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="virus_scan",
risk_level="high",
title="病毒扫描",
description="扫描终端",
status="pending",
)
db_session.add(action)
await db_session.flush()
# 第一次调用 ensure_ticket
svc = ApprovalService(db_session)
ticket1 = await svc.ensure_ticket(action, channel="agent", reason="高危操作")
# 第二次调用 ensure_ticket(幂等性测试)
ticket2 = await svc.ensure_ticket(action, channel="agent", reason="高危操作")
assert ticket1.id == ticket2.id, "幂等性:同一动作应返回同一审批单"
assert ticket1.status == "pending", "审批单状态应为 pending"
@pytest.mark.asyncio
async def test_ensure_ticket_creates_new_when_no_pending(self, db_session):
"""测试 ensure_ticket 创建新审批单"""
session = AutoSession(
conversation_id="test_conv_002",
employee_id="emp_002",
title="测试会话2",
status="running",
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="virus_quarantine",
risk_level="high",
title="病毒隔离",
description="隔离终端",
status="pending",
)
db_session.add(action)
await db_session.flush()
svc = ApprovalService(db_session)
ticket = await svc.ensure_ticket(action, channel="h5", reason="需要员工确认")
assert ticket is not None, "应创建审批单"
assert ticket.action_id == action.id, "审批单应关联动作"
assert ticket.channel == "h5", "渠道应为 h5"
assert ticket.status == "pending", "状态应为 pending"
@pytest.mark.asyncio
async def test_decide_approve(self, db_session):
"""测试审批通过"""
session = AutoSession(
conversation_id="test_conv_003",
employee_id="emp_003",
title="测试会话3",
status="running",
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="password_reset_link",
risk_level="high",
title="密码重置",
description="重置密码",
status="pending",
)
db_session.add(action)
await db_session.flush()
svc = ApprovalService(db_session)
ticket = await svc.ensure_ticket(action, channel="agent", reason="重置密码")
# 审批通过
updated = await svc.decide(ticket.id, "approve", "同意", "agent_001")
assert updated.status == "approved", "审批单状态应为 approved"
assert updated.decision_note == "同意", "应有审批意见"
assert updated.approver_id == "agent_001", "应有审批人"
@pytest.mark.asyncio
async def test_decide_reject(self, db_session):
"""测试审批驳回"""
session = AutoSession(
conversation_id="test_conv_004",
employee_id="emp_004",
title="测试会话4",
status="running",
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="virus_quarantine",
risk_level="high",
title="病毒隔离",
description="隔离终端",
status="pending",
)
db_session.add(action)
await db_session.flush()
svc = ApprovalService(db_session)
ticket = await svc.ensure_ticket(action, channel="agent", reason="隔离终端")
# 审批驳回
updated = await svc.decide(ticket.id, "reject", "风险过高", "agent_002")
assert updated.status == "rejected", "审批单状态应为 rejected"
@pytest.mark.asyncio
async def test_decide_nonexistent_raises(self, db_session):
"""测试审批单不存在时抛出异常"""
svc = ApprovalService(db_session)
with pytest.raises(ValueError, match="审批单不存在"):
await svc.decide("nonexistent_ticket_id", "approve", "同意", "agent_001")
@pytest.mark.asyncio
async def test_get_pending_for_action(self, db_session):
"""测试查询动作待决审批单"""
session = AutoSession(
conversation_id="test_conv_005",
employee_id="emp_005",
title="测试会话5",
status="running",
)
db_session.add(session)
await db_session.flush()
action = AutoAction(
session_id=session.id,
action_index=0,
action_type="terminal_locate",
risk_level="read",
title="终端定位",
description="定位终端",
status="pending",
)
db_session.add(action)
await db_session.flush()
svc = ApprovalService(db_session)
ticket = await svc.ensure_ticket(action, channel="agent", reason="查询终端")
pending = await svc.get_pending_for_action(action.id)
assert pending is not None, "应能找到待决审批单"
assert pending.id == ticket.id, "应为同一审批单"
# =============================================================================
# 测试 IntentRouter
# =============================================================================
class TestIntentRouter:
"""意图识别路由测试"""
@pytest.mark.asyncio
async def test_detect_with_fallback_intent(self):
"""测试关键词兜底意图识别"""
router = IntentRouter()
# 测试病毒相关关键词
result = await router.detect("电脑中病毒了", "emp_001")
assert result is not None, "应返回识别结果"
assert "scenario_key" in result, "应包含 scenario_key"
assert result.get("confidence") is not None, "应包含置信度"
@pytest.mark.asyncio
async def test_fallback_intent_password_reset(self):
"""测试密码重置关键词"""
router = IntentRouter()
result = await router.detect("我忘记密码了", "emp_002")
assert result.get("scenario_key") == "password_reset", "应识别为密码重置场景"
@pytest.mark.asyncio
async def test_fallback_intent_software_install(self):
"""测试软件安装关键词"""
router = IntentRouter()
result = await router.detect("帮我安装一个软件", "emp_003")
assert result.get("scenario_key") == "software_install", "应识别为软件安装场景"
@pytest.mark.asyncio
async def test_fallback_intent_terminal_locate(self):
"""测试终端定位关键词"""
router = IntentRouter()
result = await router.detect("我的电脑在哪", "emp_004")
assert result.get("scenario_key") == "terminal_locate", "应识别为终端定位场景"
@pytest.mark.asyncio
async def test_fallback_intent_unknown(self):
"""测试未知意图返回空"""
router = IntentRouter()
result = await router.detect("今天天气不错", "emp_005")
assert result.get("scenario_key") is None, "未知意图应返回空场景"
assert result.get("confidence") == 0.0, "置信度应为0"
# =============================================================================
# 测试 AutoSessionService - 会话CRUD
# =============================================================================
class TestAutoSessionService:
"""自动化会话服务测试 - CRUD"""
@pytest.mark.asyncio
async def test_create_session(self, db_session):
"""测试创建会话"""
svc = AutoSessionService(db_session)
session = await svc.create_session(
conversation_id="conv_001",
employee_id="emp_001",
description="测试会话",
mode="real_exec",
)
assert session is not None, "应创建会话"
assert session.employee_id == "emp_001", "员工ID应正确"
assert session.status == "created", "初始状态应为 created"
assert session.mode == "real_exec", "执行模式应正确"
@pytest.mark.asyncio
async def test_get_session(self, db_session):
"""测试获取会话"""
svc = AutoSessionService(db_session)
# 先创建
created = await svc.create_session(
conversation_id="conv_002",
employee_id="emp_002",
description="测试获取会话",
mode="real_exec",
)
# 再获取
retrieved = await svc.get_session(created.id)
assert retrieved is not None, "应能获取会话"
assert retrieved.id == created.id, "会话ID应一致"
assert retrieved.employee_id == "emp_002", "员工ID应正确"
@pytest.mark.asyncio
async def test_get_session_not_found(self, db_session):
"""测试获取不存在的会话"""
svc = AutoSessionService(db_session)
result = await svc.get_session("nonexistent_id")
assert result is None, "不存在的会话应返回 None"
@pytest.mark.asyncio
async def test_list_sessions(self, db_session):
"""测试列出会话"""
svc = AutoSessionService(db_session)
# 创建多个会话
for i in range(3):
await svc.create_session(
conversation_id=f"conv_{i}",
employee_id="emp_003",
description=f"测试会话{i}",
mode="real_exec",
)
sessions = await svc.list_sessions(employee_id="emp_003")
assert len(sessions) == 3, "应返回3个会话"
# 验证按时间倒序
assert sessions[0].created_at >= sessions[-1].created_at, "应按时间倒序"
@pytest.mark.asyncio
async def test_list_sessions_with_status_filter(self, db_session):
"""测试按状态过滤"""
svc = AutoSessionService(db_session)
# 创建会话
s1 = await svc.create_session(
conversation_id="conv_status_1",
employee_id="emp_status",
description="会话1",
mode="real_exec",
)
s2 = await svc.create_session(
conversation_id="conv_status_2",
employee_id="emp_status",
description="会话2",
mode="real_exec",
)
# 手动修改状态
s2.status = "closed"
await db_session.flush()
# 按 created 状态过滤
created_sessions = await svc.list_sessions(
employee_id="emp_status",
status="created"
)
assert len(created_sessions) == 1, "应返回1个 created 状态的会话"
@pytest.mark.asyncio
async def test_takeover(self, db_session):
"""测试转人工接管"""
svc = AutoSessionService(db_session)
session = await svc.create_session(
conversation_id="conv_takeover",
employee_id="emp_takeover",
description="测试转人工",
mode="real_exec",
)
# 转人工接管
result = await svc.takeover(session.id, agent_id="agent_001", note="测试转接")
assert result.status == "handoff", "状态应为 handoff"
assert result.agent_id == "agent_001", "接管坐席ID应正确"
@pytest.mark.asyncio
async def test_takeover_not_found(self, db_session):
"""测试转接不存在的会话"""
svc = AutoSessionService(db_session)
with pytest.raises(AutomationException) as exc:
await svc.takeover("nonexistent_id", "agent_001")
assert exc.value.code == AutomationErrorCode.SESSION_NOT_FOUND
@pytest.mark.asyncio
async def test_resolve_feedback_satisfied(self, db_session):
"""测试员工满意反馈"""
svc = AutoSessionService(db_session)
session = await svc.create_session(
conversation_id="conv_feedback",
employee_id="emp_feedback",
description="测试反馈",
mode="real_exec",
)
session.status = "resolved" # 先设为 resolved
await db_session.flush()
result = await svc.resolve_feedback(session.id, satisfied=True, note="满意")
assert result.status == "closed", "状态应为 closed"
assert result.closed_by == "emp_feedback", "关单人应为员工"
@pytest.mark.asyncio
async def test_resolve_feedback_unsatisfied(self, db_session):
"""测试员工不满意反馈"""
svc = AutoSessionService(db_session)
session = await svc.create_session(
conversation_id="conv_feedback_2",
employee_id="emp_feedback_2",
description="测试反馈2",
mode="real_exec",
)
session.status = "resolved"
await db_session.flush()
result = await svc.resolve_feedback(session.id, satisfied=False, note="不满意")
assert result.status == "handoff", "状态应为 handoff"
assert result.closed_by == "employee(reject)", "关单人应为员工拒绝"
# =============================================================================
# 测试 ActionExecutor - 执行引擎
# =============================================================================
class TestActionExecutor:
"""动作执行引擎测试"""
@pytest.mark.asyncio
async def test_load_session_and_actions(self, db_session):
"""测试加载会话和动作"""
# 创建会话
session = AutoSession(
conversation_id="exec_conv_001",
employee_id="emp_exec_001",
title="执行测试",
status="running",
)
db_session.add(session)
await db_session.flush()
# 创建动作
actions = []
for i in range(3):
action = AutoAction(
session_id=session.id,
action_index=i,
action_type="software_install_guide",
risk_level="low",
title=f"动作{i}",
description=f"描述{i}",
status="pending",
)
db_session.add(action)
actions.append(action)
await db_session.flush()
# 测试加载
executor = ActionExecutor(db_session)
loaded_session, loaded_actions = await executor._load(session.id)
assert loaded_session is not None, "应加载到会话"
assert len(loaded_actions) == 3, "应加载3个动作"
# 验证排序
assert loaded_actions[0].action_index == 0
@pytest.mark.asyncio
async def test_run_with_terminal_state(self, db_session):
"""测试终态会话跳过执行"""
session = AutoSession(
conversation_id="exec_conv_002",
employee_id="emp_exec_002",
title="终态测试",
status="closed", # 终态
)
db_session.add(session)
await db_session.flush()
executor = ActionExecutor(db_session)
await executor.run(session.id)
# 不应抛出异常,应直接返回
@pytest.mark.asyncio
async def test_build_clients(self, db_session):
"""测试构建外部客户端"""
executor = ActionExecutor(db_session)
clients = await executor._build_clients()
assert isinstance(clients, dict), "应返回客户端字典"
# 验证包含必要的客户端键
assert "huorong" in clients
assert "lianruan" in clients
assert "ehr" in clients
assert "dify" in clients
# =============================================================================
# 测试场景配置管理
# =============================================================================
class TestScenarioConfig:
"""场景配置管理测试"""
@pytest.mark.asyncio
async def test_list_scenario_configs(self, db_session):
"""测试列出场景配置"""
svc = AutoSessionService(db_session)
# 创建场景配置
config = ScenarioConfig(
scenario_key="test_scenario",
name="测试场景",
description="测试用场景",
enabled=True,
actions=[],
)
db_session.add(config)
await db_session.flush()
configs = await svc.list_scenario_configs()
assert len(configs) >= 1, "应返回场景配置"
@pytest.mark.asyncio
async def test_upsert_scenario_config_create(self, db_session):
"""测试创建场景配置"""
svc = AutoSessionService(db_session)
data = {
"name": "新场景",
"description": "新场景描述",
"enabled": True,
"actions": [
{
"action_type": "software_install_guide",
"adapter": "internal",
"risk_level": "low",
"title": "安装软件",
"description": "安装软件",
}
],
"approval_strategy": {"read": "auto", "low": "auto", "high": "approval"},
}
config = await svc.upsert_scenario_config("new_scene", data, operator="test_admin")
assert config.scenario_key == "new_scene", "场景键应正确"
assert config.name == "新场景", "名称应正确"
assert config.enabled is True, "应启用"
@pytest.mark.asyncio
async def test_upsert_scenario_config_update(self, db_session):
"""测试更新场景配置"""
svc = AutoSessionService(db_session)
# 先创建
await svc.upsert_scenario_config(
"update_scene",
{"name": "旧名称", "enabled": True},
operator="admin",
)
# 再更新
updated = await svc.upsert_scenario_config(
"update_scene",
{"name": "新名称"},
operator="admin",
)
assert updated.name == "新名称", "名称应更新"
@pytest.mark.asyncio
async def test_list_rule_versions(self, db_session):
"""测试列出规则版本"""
svc = AutoSessionService(db_session)
# 创建场景配置和版本
await svc.upsert_scenario_config(
"version_test",
{"name": "版本测试", "actions": []},
operator="admin",
)
versions = await svc.list_rule_versions("version_test")
assert len(versions) >= 1, "应返回规则版本"
# =============================================================================
# 测试指标汇总
# =============================================================================
class TestMetrics:
"""指标汇总测试"""
@pytest.mark.asyncio
async def test_metrics(self, db_session):
"""测试指标计算"""
svc = AutoSessionService(db_session)
# 创建不同状态的会话
for status in ["resolved", "resolved", "handoff", "error"]:
session = AutoSession(
conversation_id=f"metric_conv_{status}",
employee_id="emp_metric",
title=f"指标测试_{status}",
status=status,
)
db_session.add(session)
await db_session.flush()
metrics = await svc.metrics()
assert "total_sessions" in metrics, "应包含总会话数"
assert "resolved_sessions" in metrics, "应包含已解决数"
assert "handoff_sessions" in metrics, "应包含转人工数"
assert "error_sessions" in metrics, "应包含错误数"
assert metrics["total_sessions"] == 4, "应有4个会话"
assert metrics["resolved_sessions"] == 2, "应有2个resolved"
@@ -0,0 +1,307 @@
# =============================================================================
# 阶段5 自动化 - 会话管理服务单元测试
# =============================================================================
# 测试范围:session_manager.py - 会话生命周期、状态机、关单判定
# =============================================================================
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from datetime import datetime, timezone
from app.services.automation.session_manager import AutoSessionService
from app.models.automation import AutoSession, AutoAction
class TestAutoSessionService:
"""会话管理服务测试用例"""
@pytest.mark.asyncio
async def test_create_session(self, db_session):
"""测试创建会话"""
# Arrange
svc = AutoSessionService(db_session)
# Act
session = await svc.create_session(
conversation_id="conv_001",
employee_id="emp_001",
description="测试会话",
mode="real_exec"
)
# Assert
assert session.id is not None
assert session.conversation_id == "conv_001"
assert session.employee_id == "emp_001"
assert session.status == "created"
assert session.mode == "real_exec"
assert session.title == "测试会话"
@pytest.mark.asyncio
async def test_get_session(self, db_session):
"""测试获取会话"""
# Arrange - 先创建会话
svc = AutoSessionService(db_session)
created = await svc.create_session(
conversation_id="conv_002",
employee_id="emp_002",
description="查询测试"
)
await db_session.flush()
# Act
session = await svc.get_session(created.id)
# Assert
assert session is not None
assert session.id == created.id
assert session.employee_id == "emp_002"
@pytest.mark.asyncio
async def test_get_session_not_found(self, db_session):
"""测试获取不存在的会话返回 None"""
# Arrange
svc = AutoSessionService(db_session)
# Act
session = await svc.get_session("nonexistent_id")
# Assert
assert session is None
@pytest.mark.asyncio
async def test_list_sessions_with_filters(self, db_session):
"""测试列出会话(支持过滤)"""
# Arrange - 创建多个会话
svc = AutoSessionService(db_session)
await svc.create_session(
employee_id="emp_001",
description="会话1"
)
await svc.create_session(
employee_id="emp_001",
description="会话2"
)
await svc.create_session(
employee_id="emp_002",
description="会话3"
)
await db_session.flush()
# Act - 按 employee_id 过滤
sessions = await svc.list_sessions(employee_id="emp_001")
# Assert
assert len(sessions) == 2
assert all(s.employee_id == "emp_001" for s in sessions)
@pytest.mark.asyncio
async def test_list_sessions_by_status(self, db_session):
"""测试按状态过滤会话"""
# Arrange
svc = AutoSessionService(db_session)
# 创建一个 running 会话
session = await svc.create_session(
employee_id="emp_001",
description="测试"
)
session.status = "running"
await db_session.flush()
# Act
running = await svc.list_sessions(status="running")
closed = await svc.list_sessions(status="closed")
# Assert
assert len(running) >= 1
assert all(s.status == "running" for s in running)
assert len(closed) == 0
@pytest.mark.asyncio
async def test_takeover(self, db_session):
"""测试转人工接管"""
# Arrange
svc = AutoSessionService(db_session)
session = await svc.create_session(
employee_id="emp_001",
description="测试转人工"
)
await db_session.flush()
# Act
result = await svc.takeover(
session_id=session.id,
agent_id="agent_001",
note="我来接管处理"
)
# Assert
assert result.status == "handoff"
assert result.agent_id == "agent_001"
assert result.closed_by == "agent_001"
@pytest.mark.asyncio
async def test_resolve_feedback_satisfied(self, db_session):
"""测试反馈处理 - 满意则关单"""
# Arrange
svc = AutoSessionService(db_session)
session = await svc.create_session(
employee_id="emp_001",
description="测试反馈"
)
await db_session.flush()
# Act - 满意
result = await svc.resolve_feedback(
session_id=session.id,
satisfied=True,
note="处理得很好"
)
# Assert
assert result.status == "closed"
assert result.closed_by == session.employee_id
@pytest.mark.asyncio
async def test_resolve_feedback_unsatisfied(self, db_session):
"""测试反馈处理 - 不满意则转人工"""
# Arrange
svc = AutoSessionService(db_session)
session = await svc.create_session(
employee_id="emp_001",
description="测试反馈"
)
await db_session.flush()
# Act - 不满意
result = await svc.resolve_feedback(
session_id=session.id,
satisfied=False,
note="没有解决我的问题"
)
# Assert
assert result.status == "handoff"
assert result.closed_by == "employee(reject)"
@pytest.mark.asyncio
async def test_auto_close_resolved_session(self, db_session):
"""测试静默关单 - 仅 resolved 态可关"""
# Arrange
svc = AutoSessionService(db_session)
session = await svc.create_session(
employee_id="emp_001",
description="测试关单"
)
# Case 1: 非 resolved 状态不应关单
session.status = "running"
await db_session.flush()
await svc.auto_close(session.id)
await db_session.refresh(session)
assert session.status == "running" # 状态未变
# Case 2: resolved 状态应关单
session.status = "resolved"
await db_session.flush()
await svc.auto_close(session.id)
await db_session.refresh(session)
assert session.status == "closed"
assert session.closed_by == "system(auto)"
@pytest.mark.asyncio
async def test_get_session_detail_with_actions(self, db_session):
"""测试获取会话详情(含动作列表)"""
# Arrange
svc = AutoSessionService(db_session)
session = await svc.create_session(
employee_id="emp_001",
description="测试详情"
)
await db_session.flush()
# 添加动作
action1 = AutoAction(
session_id=session.id,
action_index=0,
action_type="terminal_locate",
status="pending",
title="定位终端"
)
action2 = AutoAction(
session_id=session.id,
action_index=1,
action_type="virus_scan",
status="success",
title="病毒扫描"
)
db_session.add(action1)
db_session.add(action2)
await db_session.flush()
# Act
detail = await svc.get_session_detail(session.id)
# Assert
assert detail is not None
assert detail["session"].id == session.id
assert len(detail["actions"]) == 2
# 验证动作按 action_index 排序
assert detail["actions"][0].action_type == "terminal_locate"
assert detail["actions"][1].action_type == "virus_scan"
@pytest.mark.asyncio
async def test_get_session_detail_not_found(self, db_session):
"""测试获取不存在的会话详情返回 None"""
# Arrange
svc = AutoSessionService(db_session)
# Act
detail = await svc.get_session_detail("nonexistent_id")
# Assert
assert detail is None
@pytest.mark.asyncio
async def test_session_status_flow(self, db_session):
"""测试会话状态流转"""
# Arrange
svc = AutoSessionService(db_session)
session = await svc.create_session(
employee_id="emp_001",
description="状态流转测试"
)
await db_session.flush()
# Assert - 初始状态
assert session.status == "created"
# 模拟 start 后的状态变化
session.status = "running"
await db_session.flush()
await db_session.refresh(session)
assert session.status == "running"
# 审批暂停
session.status = "paused"
await db_session.flush()
await db_session.refresh(session)
assert session.status == "paused"
# 处置成功
session.status = "resolved"
await db_session.flush()
await db_session.refresh(session)
assert session.status == "resolved"
# 静默关单
session.status = "closed"
await db_session.flush()
await db_session.refresh(session)
assert session.status == "closed"
+284
View File
@@ -0,0 +1,284 @@
# =============================================================================
# 企微IT智能服务台 — 阶段5 自动化闭环 服务单元测试(简化版)
# =============================================================================
# 说明:简化版测试,直接导入服务类进行测试,避免 conftest 导入问题
# 创建日期: 2026-07-01
# =============================================================================
import pytest
import sys
import os
# 设置测试环境变量(在导入 app 之前)
os.environ.setdefault("DEV_MODE", "true")
os.environ.setdefault("WECOM_SCO_CALLBACK_BASE", "https://test.example.com")
# 测试意图路由 - 这些不需要数据库
class TestIntentRouterSimple:
"""简化版意图识别测试"""
def test_fallback_intent_virus_keywords(self):
"""测试病毒关键词识别"""
from app.services.automation.intent_router import IntentRouter
router = IntentRouter()
# 模拟 DifyClient._fallback_intent 方法的行为
from app.integrations.dify import DifyClient
result = DifyClient._fallback_intent("电脑中病毒了")
assert result is not None
assert "scenario_key" in result
def test_fallback_intent_password_keywords(self):
"""测试密码重置关键词识别"""
from app.integrations.dify import DifyClient
result = DifyClient._fallback_intent("我忘记密码了")
assert result.get("scenario_key") == "password_reset"
def test_fallback_intent_software_keywords(self):
"""测试软件安装关键词识别"""
from app.integrations.dify import DifyClient
result = DifyClient._fallback_intent("帮我安装一个软件")
assert result.get("scenario_key") == "software_install"
def test_fallback_intent_terminal_keywords(self):
"""测试终端定位关键词识别"""
from app.integrations.dify import DifyClient
result = DifyClient._fallback_intent("我的电脑在哪里")
assert result.get("scenario_key") == "terminal_locate"
def test_fallback_intent_unknown(self):
"""测试未知意图"""
from app.integrations.dify import DifyClient
result = DifyClient._fallback_intent("今天天气不错")
assert result.get("scenario_key") is None
assert result.get("confidence") == 0.0
class TestConstants:
"""测试常量定义"""
def test_error_codes(self):
"""测试错误码定义"""
from app.constants import AutomationErrorCode
assert AutomationErrorCode.SCENARIO_NOT_FOUND == 4001
assert AutomationErrorCode.INTENT_FAILED == 4002
assert AutomationErrorCode.EXTERNAL_CALL_FAILED == 4003
assert AutomationErrorCode.ACTION_REJECTED == 4004
assert AutomationErrorCode.SESSION_NOT_FOUND == 4005
assert AutomationErrorCode.APPROVAL_REJECTED == 4006
assert AutomationErrorCode.TIMEOUT_HANDOFF == 4007
assert AutomationErrorCode.EMPLOYEE_DECLINED == 4008
assert AutomationErrorCode.CONFIG_ERROR == 4009
assert AutomationErrorCode.MAPPING_FAILED == 4010
assert AutomationErrorCode.INVALID_MODE == 4011
assert AutomationErrorCode.ROLLBACK_FAILED == 4012
def test_risk_levels(self):
"""测试风险等级"""
from app.constants import (
AUTOMATION_RISK_READ,
AUTOMATION_RISK_LOW,
AUTOMATION_RISK_HIGH,
AUTOMATION_AUTO_EXECUTABLE_RISKS,
)
assert AUTOMATION_RISK_READ == "read"
assert AUTOMATION_RISK_LOW == "low"
assert AUTOMATION_RISK_HIGH == "high"
assert AUTOMATION_RISK_READ in AUTOMATION_AUTO_EXECUTABLE_RISKS
assert AUTOMATION_RISK_LOW in AUTOMATION_AUTO_EXECUTABLE_RISKS
assert AUTOMATION_RISK_HIGH not in AUTOMATION_AUTO_EXECUTABLE_RISKS
def test_session_states(self):
"""测试会话状态"""
from app.constants import (
AUTOMATION_SESSION_CREATED,
AUTOMATION_SESSION_RUNNING,
AUTOMATION_SESSION_PAUSED,
AUTOMATION_SESSION_RESOLVED,
AUTOMATION_SESSION_CLOSED,
AUTOMATION_SESSION_HANDOFF,
AUTOMATION_SESSION_ERROR,
AUTOMATION_SESSION_TERMINAL_STATES,
)
assert AUTOMATION_SESSION_CREATED == "created"
assert AUTOMATION_SESSION_RUNNING == "running"
assert AUTOMATION_SESSION_PAUSED == "paused"
assert AUTOMATION_SESSION_RESOLVED == "resolved"
assert AUTOMATION_SESSION_CLOSED == "closed"
assert AUTOMATION_SESSION_HANDOFF == "handoff"
assert AUTOMATION_SESSION_ERROR == "error"
assert "closed" in AUTOMATION_SESSION_TERMINAL_STATES
assert "handoff" in AUTOMATION_SESSION_TERMINAL_STATES
assert "error" in AUTOMATION_SESSION_TERMINAL_STATES
def test_action_states(self):
"""测试动作状态"""
from app.constants import (
AUTOMATION_ACTION_PENDING,
AUTOMATION_ACTION_RUNNING,
AUTOMATION_ACTION_SUCCESS,
AUTOMATION_ACTION_FAILED,
AUTOMATION_ACTION_SKIPPED,
AUTOMATION_ACTION_AWAIT_APPROVAL,
AUTOMATION_ACTION_APPROVED,
AUTOMATION_ACTION_REJECTED,
)
assert AUTOMATION_ACTION_PENDING == "pending"
assert AUTOMATION_ACTION_RUNNING == "running"
assert AUTOMATION_ACTION_SUCCESS == "success"
assert AUTOMATION_ACTION_FAILED == "failed"
assert AUTOMATION_ACTION_SKIPPED == "skipped"
assert AUTOMATION_ACTION_AWAIT_APPROVAL == "await_approval"
assert AUTOMATION_ACTION_APPROVED == "approved"
assert AUTOMATION_ACTION_REJECTED == "rejected"
class TestActionRegistry:
"""测试动作注册表"""
def test_handler_registry_populated(self):
"""测试处理器注册表已填充"""
from app.services.automation.action_registry import (
HANDLER_REGISTRY,
register_builtin_handlers,
)
# 确保内置处理器已注册
register_builtin_handlers()
assert "terminal_locate" in HANDLER_REGISTRY
assert "virus_scan" in HANDLER_REGISTRY
assert "virus_quarantine" in HANDLER_REGISTRY
assert "software_install_guide" in HANDLER_REGISTRY
assert "password_reset_link" in HANDLER_REGISTRY
def test_get_handler_returns_correct_type(self):
"""测试获取处理器返回正确类型"""
from app.services.automation.action_registry import (
get_handler,
BaseActionHandler,
)
handler = get_handler("software_install_guide")
assert handler is not None
assert isinstance(handler, BaseActionHandler)
assert handler.action_type == "software_install_guide"
def test_get_handler_unknown_returns_none(self):
"""测试获取未知处理器返回 None"""
from app.services.automation.action_registry import get_handler
handler = get_handler("unknown_action_type")
assert handler is None
class TestExceptionHandler:
"""测试异常处理"""
def test_automation_exception_creation(self):
"""测试自动化异常创建"""
from app.services.automation.exception_handler import AutomationException
from app.constants import AutomationErrorCode
exc = AutomationException(
AutomationErrorCode.SESSION_NOT_FOUND,
"会话不存在"
)
assert exc.code == AutomationErrorCode.SESSION_NOT_FOUND
assert exc.message == "会话不存在"
def test_automation_exception_str(self):
"""测试异常字符串表示"""
from app.services.automation.exception_handler import AutomationException
from app.constants import AutomationErrorCode
exc = AutomationException(
AutomationErrorCode.INTENT_FAILED,
"意图识别失败"
)
exc_str = str(exc)
assert "4002" in exc_str
assert "意图识别失败" in exc_str
class TestDefaultScenarioConfigs:
"""测试默认场景配置"""
def test_default_configs_exist(self):
"""测试默认配置存在"""
from app.services.automation import DEFAULT_SCENARIO_CONFIGS
assert "terminal_locate" in DEFAULT_SCENARIO_CONFIGS
assert "virus_dispose" in DEFAULT_SCENARIO_CONFIGS
assert "software_install" in DEFAULT_SCENARIO_CONFIGS
assert "password_reset" in DEFAULT_SCENARIO_CONFIGS
def test_terminal_locate_config(self):
"""测试终端定位配置"""
from app.services.automation import DEFAULT_SCENARIO_CONFIGS
config = DEFAULT_SCENARIO_CONFIGS["terminal_locate"]
assert config["name"] == "终端定位"
assert config["enabled"] is True
assert "actions" in config
assert len(config["actions"]) > 0
def test_virus_dispose_config(self):
"""测试病毒处置配置"""
from app.services.automation import DEFAULT_SCENARIO_CONFIGS
config = DEFAULT_SCENARIO_CONFIGS["virus_dispose"]
assert config["name"] == "病毒查杀处置"
assert config["enabled"] is True
assert "actions" in config
# 应该有扫描和隔离两个动作
action_types = [a["action_type"] for a in config["actions"]]
assert "virus_scan" in action_types
assert "virus_quarantine" in action_types
def test_software_install_config(self):
"""测试软件安装配置"""
from app.services.automation import DEFAULT_SCENARIO_CONFIGS
config = DEFAULT_SCENARIO_CONFIGS["software_install"]
assert config["name"] == "软件自助安装"
assert config["enabled"] is True
assert len(config["actions"]) == 1
assert config["actions"][0]["action_type"] == "software_install_guide"
def test_password_reset_config(self):
"""测试密码重置配置"""
from app.services.automation import DEFAULT_SCENARIO_CONFIGS
config = DEFAULT_SCENARIO_CONFIGS["password_reset"]
assert config["name"] == "密码重置"
assert config["enabled"] is True
assert len(config["actions"]) == 1
assert config["actions"][0]["action_type"] == "password_reset_link"
# 密码重置是高风险操作,需要 H5 确认
assert config["actions"][0]["risk_level"] == "high"
assert config["actions"][0]["confirm_channel"] == "h5"
+89
View File
@@ -0,0 +1,89 @@
# =============================================================================
# 企微IT智能服务台 — 置信门控逻辑测试(D3 / P0-3)
# =============================================================================
# 说明:测试 confidence < 0.7 门控逻辑、阈值可配置性、source_failed 标记。
# =============================================================================
import pytest
from app.schemas.enums import AudienceEnum
class TestConfidenceGate:
"""置信门控逻辑测试。"""
@pytest.mark.asyncio
async def test_confidence_above_07_passes(self):
"""验证 confidence >= 0.7 通过门控。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
assert service._apply_confidence_gate(0.86) is True
assert service._apply_confidence_gate(0.70) is True # 边界值
assert service._apply_confidence_gate(0.95) is True
assert service._apply_confidence_gate(1.0) is True
@pytest.mark.asyncio
async def test_confidence_below_07_fails(self):
"""验证 confidence < 0.7 未通过门控。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
assert service._apply_confidence_gate(0.69) is False
assert service._apply_confidence_gate(0.5) is False
assert service._apply_confidence_gate(0.0) is False
@pytest.mark.asyncio
async def test_confidence_none_fails(self):
"""验证 confidence=None 未通过门控。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
assert service._apply_confidence_gate(None) is False
@pytest.mark.asyncio
async def test_threshold_configurable(self):
"""验证门控阈值可配置。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
# 默认阈值 0.7
service = KnowledgeIterationService()
assert service.confidence_gate_threshold == 0.7
# 修改阈值
service.confidence_gate_threshold = 0.8
assert service._apply_confidence_gate(0.75) is False
assert service._apply_confidence_gate(0.85) is True
@pytest.mark.asyncio
async def test_auto_tag_audience_manual(self, db_session):
"""验证手动录入→engineer_workguide。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
audience = await service._auto_tag_audience(
db_session, "manual", ["test_id"]
)
assert audience == AudienceEnum.engineer_workguide
@pytest.mark.asyncio
async def test_auto_tag_audience_document_ragflow(self, db_session):
"""验证 RAGFlow 文档→engineer_workguide。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
audience = await service._auto_tag_audience(
db_session, "document_ragflow", ["test_id"]
)
assert audience == AudienceEnum.engineer_workguide
@pytest.mark.asyncio
async def test_auto_tag_audience_conversation_default(self, db_session):
"""验证会话来源→默认 employee_quick_reply(保守)。"""
from app.services.knowledge_iteration_service import KnowledgeIterationService
service = KnowledgeIterationService()
audience = await service._auto_tag_audience(
db_session, "conversation", ["nonexistent_id"]
)
assert audience == AudienceEnum.employee_quick_reply
+134
View File
@@ -0,0 +1,134 @@
# =============================================================================
# 三端认证重构 AUTH-01 — 环境门控单元测试
# =============================================================================
# 验证 env_gating.py 的行为:
# 1. is_production() 根据 app_env 判断环境
# 2. ip_in_whitelist() 支持单 IP 和 CIDR 网段匹配
# =============================================================================
import pytest
from unittest.mock import patch, MagicMock
import sys
class TestIsProduction:
"""测试 is_production() 函数"""
def test_production_env_returns_true(self):
"""app_env=production 时应返回 True"""
with patch("app.utils.env_gating.settings") as mock_settings:
mock_settings.app_env = "production"
from app.utils.env_gating import is_production
result = is_production()
assert result is True
def test_dev_env_returns_false(self):
"""app_env=dev 时应返回 False"""
with patch("app.utils.env_gating.settings") as mock_settings:
mock_settings.app_env = "dev"
from app.utils.env_gating import is_production
result = is_production()
assert result is False
def test_test_env_returns_false(self):
"""app_env=test 时应返回 False"""
with patch("app.utils.env_gating.settings") as mock_settings:
mock_settings.app_env = "test"
from app.utils.env_gating import is_production
result = is_production()
assert result is False
def test_empty_env_returns_false(self):
"""空 app_env 时应返回 False(默认 dev"""
with patch("app.utils.env_gating.settings") as mock_settings:
mock_settings.app_env = ""
from app.utils.env_gating import is_production
result = is_production()
assert result is False
def test_production_case_insensitive(self):
"""app_env 大小写不敏感"""
with patch("app.utils.env_gating.settings") as mock_settings:
mock_settings.app_env = "PRODUCTION"
from app.utils.env_gating import is_production
result = is_production()
assert result is True
class TestIpInWhitelist:
"""测试 ip_in_whitelist() 函数"""
def test_single_ip_match(self):
"""单 IP 精确匹配"""
from app.utils.env_gating import ip_in_whitelist
# 白名单中的 IP
assert ip_in_whitelist("117.147.35.138", allowed="117.147.35.138,218.75.34.87") is True
assert ip_in_whitelist("218.75.34.87", allowed="117.147.35.138,218.75.34.87") is True
# 不在白名单中的 IP
assert ip_in_whitelist("8.8.8.8", allowed="117.147.35.138,218.75.34.87") is False
def test_cidr_network_match(self):
"""CIDR 网段匹配"""
from app.utils.env_gating import ip_in_whitelist
# 在网段内
assert ip_in_whitelist("10.240.0.1", allowed="10.240.0.0/16") is True
assert ip_in_whitelist("10.240.1.100", allowed="10.240.0.0/16") is True
assert ip_in_whitelist("10.240.255.255", allowed="10.240.0.0/16") is True
# 不在网段内
assert ip_in_whitelist("10.239.255.255", allowed="10.240.0.0/16") is False
assert ip_in_whitelist("10.241.0.0", allowed="10.240.0.0/16") is False
def test_mixed_ip_and_cidr(self):
"""混合单 IP 和 CIDR"""
from app.utils.env_gating import ip_in_whitelist
assert ip_in_whitelist("117.147.35.138", allowed="117.147.35.138,10.240.0.0/16") is True
assert ip_in_whitelist("10.240.1.50", allowed="117.147.35.138,10.240.0.0/16") is True
assert ip_in_whitelist("8.8.8.8", allowed="117.147.35.138,10.240.0.0/16") is False
def test_empty_whitelist_returns_false(self):
"""空白名单应返回 False(保守策略)"""
# 显式传入空列表
from app.utils.env_gating import ip_in_whitelist, _parse_allowed_ips
# 解析空字符串应返回空列表
assert _parse_allowed_ips("") == []
# 空列表应返回 False
assert ip_in_whitelist("10.240.1.1", allowed="") is False
def test_empty_client_ip_returns_false(self):
"""空客户端 IP 应返回 False"""
from app.utils.env_gating import ip_in_whitelist
assert ip_in_whitelist("", allowed="10.240.0.0/16") is False
assert ip_in_whitelist(None, allowed="10.240.0.0/16") is False
def test_invalid_client_ip_returns_false(self):
"""无效客户端 IP 格式应返回 False"""
from app.utils.env_gating import ip_in_whitelist
assert ip_in_whitelist("invalid-ip", allowed="10.240.0.0/16") is False
assert ip_in_whitelist("999.999.999.999", allowed="10.240.0.0/16") is False
def test_explicit_allowed_parameter(self):
"""测试显式传入 allowed 参数"""
from app.utils.env_gating import ip_in_whitelist
# 显式传入 allowed 参数
assert ip_in_whitelist("218.75.34.87", allowed="218.75.34.87") is True
assert ip_in_whitelist("8.8.8.8", allowed="218.75.34.87") is False
def test_whitelist_with_spaces(self):
"""白名单字符串带空格应正确处理"""
from app.utils.env_gating import ip_in_whitelist
assert ip_in_whitelist("117.147.35.138", allowed=" 117.147.35.138 , 218.75.34.87 , 10.240.0.0/16 ") is True
assert ip_in_whitelist("218.75.34.87", allowed=" 117.147.35.138 , 218.75.34.87 , 10.240.0.0/16 ") is True
assert ip_in_whitelist("10.240.1.1", allowed=" 117.147.35.138 , 218.75.34.87 , 10.240.0.0/16 ") is True
if __name__ == "__main__":
pytest.main([__file__, "-v"])
+162 -30
View File
@@ -1,16 +1,15 @@
# -*- coding: utf-8 -*-
"""知识库自动迭代 真实验证(P2-13
"""知识库自动迭代 真实验证(Tier0 / T03 重写
真实验证点(来自功能规格说明书 + 状态看板验收标准)
- 能基于标注(feedback=useless)生成建议行 status=pending
- 管理员 approve 后写入 knowledge_base(状态变为 applied
- reject 正常(状态变为 rejected,且不写入知识库)
- get_suggestion_stats 统计正确
- 关键证据: _generate_update_suggestion / _generate_new_faq_suggestion 内是 TODO 桩,
返回的 title/content 是 "[待AI生成] ..." 占位符 —— 证实 AI 内容生成未实现,
数据管道(分析→建建议行→审核应用)是真实的,但 AI 生成是桩。
Tier0 变更
- 移除 [待AI生成] 占位符断言(AI 生成已通过 WingmanService 真实实现)
- 新增 source_failed 测试(Dify 不可用/置信度不足时标记
- 新增 audience 自动标注测试
- 新增置信门控测试
- 保留数据管道验证(分析→建建议→审核→写KB)
"""
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from sqlalchemy import select
from app.models.conversation_annotation import ConversationAnnotation
@@ -19,6 +18,35 @@ from app.models.knowledge_suggestion import KnowledgeSuggestion
from app.services.knowledge_iteration_service import KnowledgeIterationService
# ── Mock WingmanService 返回 ──
MOCK_AI_RESULT = {
"suggestion_type": "new_faq",
"title": "VPN无法连接的解决方案",
"content": "1. 检查网络连接 2. 重启VPN客户端 3. 联系IT支持",
"category": "网络",
"tags": ["VPN", "连接"],
"confidence": 0.86,
"issue": "VPN问题",
"action": "VPN连接修复",
"relation_type": "LEADS_TO",
"parent_issue": "网络问题",
}
MOCK_AI_RESULT_LOW_CONFIDENCE = {
"suggestion_type": "new_faq",
"title": "不确定的建议",
"content": "可能是网络问题",
"category": "网络",
"tags": [],
"confidence": 0.42,
"issue": "",
"action": "",
"relation_type": "LEADS_TO",
"parent_issue": "",
}
def _seed_useless_annotations(
db,
msg_id: str,
@@ -47,34 +75,53 @@ def _seed_useless_annotations(
)
async def _create_mock_wingman():
"""创建一个返回预置结果的 Mock WingmanService。"""
mock = MagicMock()
mock.generate_knowledge_suggestion = AsyncMock(return_value=MOCK_AI_RESULT)
mock.close = AsyncMock()
return mock
# =============================================================================
# 测试用例
# =============================================================================
@pytest.mark.asyncio
async def test_analyze_generates_pending_suggestion_with_stub_content(db_session):
"""分析标注生成 pending 建议;且内容是 [待AI生成] 占位符(证明 AI 生成是桩)"""
async def test_analyze_generates_pending_suggestion(db_session):
"""分析标注生成 pending 建议;AI 生成内容不再是 [待AI生成] 占位符。"""
_seed_useless_annotations(db_session, msg_id="msg-x", n=3)
await db_session.flush()
service = KnowledgeIterationService()
result = await service.analyze_and_generate_suggestions(db_session, days=30)
with patch(
"app.services.wingman_service.WingmanService",
return_value=await _create_mock_wingman(),
):
service = KnowledgeIterationService()
result = await service.analyze_and_generate_suggestions(db_session, days=30)
# 高频错误(msg-x 被标注 3 次)应生成 >=1 条建议
assert result["suggestions_generated"] >= 1
assert result["annotations_analyzed"] >= 4 # 3(msg-x) + 1(msg-other)
assert result["annotations_analyzed"] >= 4
# 查询生成的建议
stmt = select(KnowledgeSuggestion).where(KnowledgeSuggestion.status == "pending")
suggestions = (await db_session.execute(stmt)).scalars().all()
assert len(suggestions) >= 1
# 关键证据: AI 内容生成是桩 —— title/content 含占位符
# Tier0 关键变更: AI 内容不再是 [待AI生成] 占位符
titles = [s.title for s in suggestions]
contents = [s.content for s in suggestions]
assert any("[待AI生成]" in t for t in titles)
assert any("请通过AI分析" in c for c in contents)
assert not any("[待AI生成]" in t for t in titles)
assert not any("请通过AI分析" in c for c in contents)
# 仅高频的 msg-x 生成建议,msg-other(仅1次)不应生成
generated_source = [sd for s in suggestions for sd in (s.source_data or [])]
assert "msg-x" in generated_source
assert "msg-other" not in generated_source
# 新增字段验证
for s in suggestions:
if not s.source_failed:
assert s.confidence is not None
assert s.confidence >= 0.0
assert s.audience is not None
@pytest.mark.asyncio
@@ -83,8 +130,12 @@ async def test_approve_writes_knowledge_base(db_session):
_seed_useless_annotations(db_session, msg_id="msg-x", n=3)
await db_session.flush()
service = KnowledgeIterationService()
await service.analyze_and_generate_suggestions(db_session, days=30)
with patch(
"app.services.wingman_service.WingmanService",
return_value=await _create_mock_wingman(),
):
service = KnowledgeIterationService()
await service.analyze_and_generate_suggestions(db_session, days=30)
stmt = select(KnowledgeSuggestion).where(KnowledgeSuggestion.status == "pending")
suggestion = (await db_session.execute(stmt)).scalars().first()
@@ -95,10 +146,13 @@ async def test_approve_writes_knowledge_base(db_session):
assert approved.status == "applied"
assert approved.reviewer_id == "reviewer-1"
# knowledge_base 应新增一行(内容仍是桩占位符)
# knowledge_base 应新增一行
kb_rows = (await db_session.execute(select(KnowledgeBase))).scalars().all()
assert len(kb_rows) == 1
assert "[待AI生成]" in kb_rows[0].title
# 内容不再是占位符
assert "[待AI生成]" not in kb_rows[0].title
# 图字段应被保留
assert kb_rows[0].graph_sync_status in ("pending", "synced", "failed")
@pytest.mark.asyncio
@@ -107,8 +161,12 @@ async def test_reject_marks_rejected(db_session):
_seed_useless_annotations(db_session, msg_id="msg-x", n=3)
await db_session.flush()
service = KnowledgeIterationService()
await service.analyze_and_generate_suggestions(db_session, days=30)
with patch(
"app.services.wingman_service.WingmanService",
return_value=await _create_mock_wingman(),
):
service = KnowledgeIterationService()
await service.analyze_and_generate_suggestions(db_session, days=30)
stmt = select(KnowledgeSuggestion).where(KnowledgeSuggestion.status == "pending")
suggestion = (await db_session.execute(stmt)).scalars().first()
@@ -127,15 +185,24 @@ async def test_reject_marks_rejected(db_session):
@pytest.mark.asyncio
async def test_stats_counts_correctly(db_session):
"""get_suggestion_stats 统计正确。"""
"""get_suggestion_stats 统计正确(含新增状态)"""
_seed_useless_annotations(db_session, msg_id="msg-x", n=3)
await db_session.flush()
service = KnowledgeIterationService()
await service.analyze_and_generate_suggestions(db_session, days=30)
with patch(
"app.services.wingman_service.WingmanService",
return_value=await _create_mock_wingman(),
):
service = KnowledgeIterationService()
await service.analyze_and_generate_suggestions(db_session, days=30)
stats = await service.get_suggestion_stats(db_session)
assert stats["total"] >= 1
assert stats["pending"] >= 1
# 新增状态键应存在
assert "queued" in stats
assert "graph_synced" in stats
assert "expired" in stats
# approve 一条后 applied +1
stmt = select(KnowledgeSuggestion).where(KnowledgeSuggestion.status == "pending")
@@ -143,3 +210,68 @@ async def test_stats_counts_correctly(db_session):
await service.approve_suggestion(db_session, s.id, "reviewer-1")
stats2 = await service.get_suggestion_stats(db_session)
assert stats2["applied"] >= 1
@pytest.mark.asyncio
async def test_source_failed_on_low_confidence(db_session):
"""验证置信度 < 0.7 的提案标记 source_failed=True。"""
_seed_useless_annotations(db_session, msg_id="msg-low-conf", n=3)
await db_session.flush()
# Mock 返回低置信度结果
mock = MagicMock()
mock.generate_knowledge_suggestion = AsyncMock(
return_value=MOCK_AI_RESULT_LOW_CONFIDENCE
)
mock.close = AsyncMock()
with patch(
"app.services.wingman_service.WingmanService",
return_value=mock,
):
service = KnowledgeIterationService()
await service.analyze_and_generate_suggestions(db_session, days=30)
stmt = select(KnowledgeSuggestion).where(
KnowledgeSuggestion.source_data.contains("msg-low-conf")
)
suggestions = (await db_session.execute(stmt)).scalars().all()
assert len(suggestions) >= 1
# 置信度 < 0.7 应标记 source_failed
for s in suggestions:
if s.confidence is not None and s.confidence < 0.7:
assert s.source_failed is True
@pytest.mark.asyncio
async def test_queue_suggestion_transitions(db_session):
"""验证入队列状态转换。"""
# 直接创建一个 pending 的建议
suggestion = KnowledgeSuggestion(
suggestion_type="new_faq",
status="pending",
title="队列测试建议",
content="测试内容",
category="软件",
tags=["测试"],
source_type="conversation",
source_data=["conv-queue-test"],
reason="入队列测试",
confidence=0.8,
audience="employee_quick_reply",
)
db_session.add(suggestion)
await db_session.commit()
await db_session.refresh(suggestion)
service = KnowledgeIterationService()
result = await service.queue_suggestion(db_session, suggestion.id)
assert result is not None
assert result.status == "queued"
assert result.queued_at is not None
# 获取队列统计
queue_stats = await service.get_queue_stats(db_session)
assert queue_stats["queued_total"] >= 1
assert "by_audience" in queue_stats
+333
View File
@@ -0,0 +1,333 @@
# =============================================================================
# 企微IT智能服务台 — Neo4j 客户端单元测试
# =============================================================================
# 说明:测试 Neo4jClient 的连接、CRUD、图遍历功能。
# 优先使用 testcontainers 启动 Docker Neo4j 容器,
# 不可用时降级为内存 Mockmemory mock)。
# =============================================================================
import os
import uuid
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import pytest_asyncio
from app.models.neo4j_schema import ActionNode, IssueNode, RelationEdge
# --------------------------------------------------------------------------
# 尝试导入 testcontainers,不可用时降级为 MemoryMock
# --------------------------------------------------------------------------
try:
from testcontainers.neo4j import Neo4jContainer # type: ignore
HAS_TESTCONTAINERS = True
except ImportError:
HAS_TESTCONTAINERS = False
# =============================================================================
# Fixtures
# =============================================================================
@pytest.fixture(scope="session")
def neo4j_container():
"""启动 Neo4j 测试容器(session 级别,所有测试复用)。
如果 testcontainers 不可用(CI 环境无 Docker),则跳过并返回 None。
"""
if not HAS_TESTCONTAINERS:
yield None
return
# 检查是否在 CI 环境(无 Docker)中
if os.environ.get("CI") and not os.environ.get("DOCKER_HOST"):
yield None
return
try:
container = Neo4jContainer(
image="neo4j:5-enterprise",
username="neo4j",
password="test1234",
)
container.start()
yield container
container.stop()
except Exception:
# Docker 不可用,降级到 Mockgenerator fixture 必须用 yield,不能用 return
yield None
@pytest_asyncio.fixture
async def neo4j_client(neo4j_container):
"""提供 Neo4jClient 实例(真实容器或 Mock)。
优先使用 Docker Neo4j 容器,不可用时降级为内存 Mock。
"""
if neo4j_container is not None:
from app.services.neo4j_client import Neo4jClient
bolt_url = neo4j_container.get_connection_url()
client = Neo4jClient(
uri=bolt_url,
user="neo4j",
password="test1234",
database="neo4j",
)
await client.initialize()
yield client
await client.close()
else:
# 降级:使用 Mock
yield _create_mock_neo4j_client()
def _create_mock_neo4j_client():
"""创建内存 Mock Neo4jClient,用于无 Docker 环境测试。
使用内存字典模拟图数据库,实现基本的 CRUD 语义。
"""
from app.services.neo4j_client import Neo4jClient
client = Neo4jClient.__new__(Neo4jClient)
client.uri = "mock://memory"
client.user = "mock"
client.password = "mock"
client.database = "mock"
client._driver = None # 标记为 Mock 模式
# 内存存储
client._nodes: dict = {} # uuid → dict
client._rels: list = [] # [(from_uuid, to_uuid, rel_type, props)]
async def mock_initialize():
pass
async def mock_close():
pass
async def mock_health_check():
return True
async def mock_create_issue_node(issue: IssueNode) -> IssueNode:
node_uuid = str(uuid.uuid4())
now = issue.created_at.isoformat() if issue.created_at else "2026-01-01T00:00:00"
node = {
"uuid": node_uuid,
"name": issue.name,
"category": issue.category,
"created_at": now,
"updated_at": now,
"source_suggestion_id": issue.source_suggestion_id,
}
client._nodes[node_uuid] = node
return IssueNode(**node)
async def mock_merge_issue(name: str, category: str, props=None) -> IssueNode:
# 查找已有节点
for n in client._nodes.values():
if n.get("name") == name and n.get("__type") == "Issue":
n["category"] = category
return IssueNode(**{k: v for k, v in n.items() if not k.startswith("__")})
# 创建新节点
node_uuid = str(uuid.uuid4())
now = "2026-01-01T00:00:00"
node = {
"uuid": node_uuid,
"name": name,
"category": category,
"created_at": now,
"updated_at": now,
"source_suggestion_id": props.get("source_suggestion_id") if props else None,
"__type": "Issue",
}
client._nodes[node_uuid] = node
return IssueNode(
uuid=node_uuid, name=name, category=category,
source_suggestion_id=props.get("source_suggestion_id") if props else None,
)
async def mock_create_action_node(action: ActionNode) -> ActionNode:
node_uuid = str(uuid.uuid4())
now = action.created_at.isoformat() if action.created_at else "2026-01-01T00:00:00"
node = {
"uuid": node_uuid,
"name": action.name,
"description": action.description,
"created_at": now,
"source_suggestion_id": action.source_suggestion_id,
}
client._nodes[node_uuid] = node
return ActionNode(**node)
async def mock_merge_action(name: str, props=None) -> ActionNode:
for n in client._nodes.values():
if n.get("name") == name and n.get("__type") == "Action":
return ActionNode(**{k: v for k, v in n.items() if not k.startswith("__")})
node_uuid = str(uuid.uuid4())
now = "2026-01-01T00:00:00"
node = {
"uuid": node_uuid,
"name": name,
"description": (props or {}).get("description", ""),
"created_at": now,
"source_suggestion_id": (props or {}).get("source_suggestion_id"),
"__type": "Action",
}
client._nodes[node_uuid] = node
return ActionNode(
uuid=node_uuid, name=name,
description=(props or {}).get("description", ""),
source_suggestion_id=(props or {}).get("source_suggestion_id"),
)
async def mock_create_relation(from_uuid: str, to_uuid: str, rel: RelationEdge) -> bool:
client._rels.append((from_uuid, to_uuid, str(rel.type), {"order": rel.order, "weight": rel.weight}))
return True
async def mock_find_issue_by_name(name: str):
for n in client._nodes.values():
if n.get("name") == name and n.get("__type") == "Issue":
return IssueNode(**{k: v for k, v in n.items() if not k.startswith("__")})
return None
async def mock_find_related_issues(node_uuid: str, rel_type=None):
results = []
for from_u, to_u, rtype, _ in client._rels:
if from_u == node_uuid:
if rel_type is None or rtype == rel_type:
target = client._nodes.get(to_u)
if target:
results.append(IssueNode(**{k: v for k, v in target.items() if not k.startswith("__")}))
return results
client.initialize = mock_initialize
client.close = mock_close
client.health_check = mock_health_check
client.create_issue_node = mock_create_issue_node
client.merge_issue = mock_merge_issue
client.create_action_node = mock_create_action_node
client.merge_action = mock_merge_action
client.create_relation = mock_create_relation
client.find_issue_by_name = mock_find_issue_by_name
client.find_related_issues = mock_find_related_issues
return client
# =============================================================================
# 测试用例
# =============================================================================
class TestNeo4jHealthCheck:
"""Neo4j 健康检查测试。"""
@pytest.mark.asyncio
async def test_health_check(self, neo4j_client):
"""验证健康检查返回 True。"""
healthy = await neo4j_client.health_check()
assert healthy is True
class TestNeo4jIssueCRUD:
"""Issue 节点 CRUD 测试。"""
@pytest.mark.asyncio
async def test_create_issue_node(self, neo4j_client):
"""验证创建 Issue 节点。"""
issue = IssueNode(name="VPN问题", category="网络")
result = await neo4j_client.create_issue_node(issue)
assert result.uuid != ""
assert result.name == "VPN问题"
assert result.category == "网络"
@pytest.mark.asyncio
async def test_merge_issue_idempotent(self, neo4j_client):
"""验证 MERGE Issue 幂等性。"""
# 第一次 merge → 创建
issue1 = await neo4j_client.merge_issue("测试问题", "软件")
uuid1 = issue1.uuid
# 第二次 merge → 返回已有节点
issue2 = await neo4j_client.merge_issue("测试问题", "软件")
assert issue2.uuid == uuid1
assert issue2.name == "测试问题"
@pytest.mark.asyncio
async def test_find_issue_by_name(self, neo4j_client):
"""验证按名称查找 Issue。"""
await neo4j_client.merge_issue("查找测试问题", "网络")
found = await neo4j_client.find_issue_by_name("查找测试问题")
assert found is not None
assert found.name == "查找测试问题"
assert found.category == "网络"
@pytest.mark.asyncio
async def test_find_issue_by_name_not_found(self, neo4j_client):
"""验证查找不存在的 Issue 返回 None。"""
found = await neo4j_client.find_issue_by_name("不存在的问题XYZ123")
assert found is None
class TestNeo4jActionCRUD:
"""Action 节点 CRUD 测试。"""
@pytest.mark.asyncio
async def test_create_action_node(self, neo4j_client):
"""验证创建 Action 节点。"""
action = ActionNode(name="个人VPN开通", description="为员工开通个人VPN")
result = await neo4j_client.create_action_node(action)
assert result.uuid != ""
assert result.name == "个人VPN开通"
assert result.description == "为员工开通个人VPN"
@pytest.mark.asyncio
async def test_merge_action_idempotent(self, neo4j_client):
"""验证 MERGE Action 幂等性。"""
action1 = await neo4j_client.merge_action("重置密码", {"description": "帮助员工重置域密码"})
action2 = await neo4j_client.merge_action("重置密码", {"description": "帮助员工重置域密码"})
assert action1.uuid == action2.uuid
class TestNeo4jRelation:
"""关系 CRUD 测试。"""
@pytest.mark.asyncio
async def test_create_relation(self, neo4j_client):
"""验证创建关系。"""
issue = await neo4j_client.merge_issue("关系测试问题", "硬件")
action = await neo4j_client.merge_action("关系测试动作")
rel = RelationEdge(
from_uuid=issue.uuid,
to_uuid=action.uuid,
type="LEADS_TO",
order=1,
weight=1.0,
)
result = await neo4j_client.create_relation(
issue.uuid, action.uuid, rel
)
assert result is True
@pytest.mark.asyncio
async def test_find_related_issues(self, neo4j_client):
"""验证查找关联 Issue。"""
issue1 = await neo4j_client.merge_issue("父问题", "网络")
issue2 = await neo4j_client.merge_issue("子问题", "网络")
rel = RelationEdge(
from_uuid=issue1.uuid,
to_uuid=issue2.uuid,
type="LEADS_TO",
order=1,
weight=0.8,
)
await neo4j_client.create_relation(issue1.uuid, issue2.uuid, rel)
related = await neo4j_client.find_related_issues(issue1.uuid, "LEADS_TO")
assert len(related) >= 1
assert any(r.name == "子问题" for r in related)
+2 -2
View File
@@ -937,11 +937,11 @@ class TestFrontendRenderingLogic:
label_map = {
"employee": "员工",
"agent": "",
"ai": "AI助手",
"ai": "Duckula(达寇拉)",
}
assert label_map["employee"] == "员工" or True # 员工消息优先用 sender_name
assert label_map["ai"] == "AI助手"
assert label_map["ai"] == "Duckula(达寇拉)"
def test_unknown_msg_type_fallback_icon(self):
"""验证未知消息类型的兜底图标。"""
+813
View File
@@ -0,0 +1,813 @@
# =============================================================================
# 企微IT智能服务台 — OTP 首次绑定与管理后台清除功能 全链路测试
# =============================================================================
# 验证增量 PRD (05-增量PRD-OTP首次绑定与重置.md) 的所有行为变更:
#
# Part A: 登录行为变更
# A1. mfa_enabled=False → require_otp_bind: true(不再直发 token
# A2. mfa_enabled=True 无 OTP → require_otp: true(回归验证)
# A3. mfa_enabled=True 有 OTP → 签发 token(回归验证)
#
# Part B: OTP 首次绑定验证 (verify_otp)
# B1. 首次绑定场景:校验通过 → verified=true + token + is_first_bind
# B2. 首次绑定场景:校验失败 → verified=false
# B3. 已绑定场景:校验通过 → verified=true(无 token,回归)
# B4. 无 secret 场景 → verified=false
#
# Part C: 管理后台端点
# C1. GET /auth/otp-admin-users → 返回列表含 mfa_enabled/mfa_bound_at
# C2. 非 admin 访问 → 403
# C3. POST /auth/otp-admin-reset/{id} → 成功清除绑定
#
# Part D: 身份验证缺口(Auth Gap)探查
# D1. require_otp_bind 后能否直接调用 otp-bind(需 token
# =============================================================================
import json
import pyotp
import pytest
import pytest_asyncio
from datetime import datetime
from unittest.mock import AsyncMock, patch
from sqlalchemy import select
from app.models.agent import Agent
from app.models.role import Role
from app.models.user_role import UserRole
from app.services.mfa_service import MFA_VERIFIED_TTL_SECONDS, MFAService
from app.services.token_service import TokenService
from tests.conftest import create_test_agent, MockRedis
# =============================================================================
# OTP 专用 client fixture — 扩展 base client, 同时覆盖 otp.py 的 _get_redis
# =============================================================================
# 原因: otp.py 的端点使用 Depends(_get_redis) 注入 Redis, 但 client fixture 仅
# 覆盖了 app.api.agents._get_redis 和 dep_redis, 未覆盖 app.api.otp._get_redis。
# 本 fixture 在 base client 之上额外设置 app.dependency_overrides,
# 确保 otp 端点也使用 mock_redis。
# =============================================================================
@pytest_asyncio.fixture
async def otp_client(client, mock_redis: MockRedis):
"""返回已覆盖 otp._get_redis 依赖的 HTTP 测试客户端。
用法: 所有需要 otp 端点读写 Redis 的测试使用 otp_client 代替 client。
"""
from app.main import create_app
from app.database import get_db
from app.api.otp import _get_redis as otp_get_redis
# 从 client fixture 获取 app 实例(通过 client 的 transport
app = client._transport.app
# 覆盖 otp._get_redis
app.dependency_overrides[otp_get_redis] = lambda: mock_redis
yield client
# 清理
if otp_get_redis in app.dependency_overrides:
del app.dependency_overrides[otp_get_redis]
# =============================================================================
# 辅助函数
# =============================================================================
def _bearer(token: str) -> dict:
"""构造 Bearer 认证头。"""
return {"Authorization": f"Bearer {token}"}
async def _create_token_in_redis(
mock_redis: MockRedis,
employee_id: str,
name: str = "",
roles: list = None,
login_source: str = "agent",
) -> str:
"""直接在 mock_redis 中创建 token 记录,绕过登录流程。
用于测试"半认证"状态下的端点(如首次绑定场景中 otp-bind/otp-verify 需要 token
但 agent_login 对 mfa_enabled=False 不签发 token)。
Args:
mock_redis: 模拟 Redis
employee_id: 用户标识
name: 用户名
roles: 角色列表
login_source: 登录来源
Returns:
str: 生成的 token 字符串
"""
import secrets
token = secrets.token_urlsafe(32)
token_data = json.dumps({
"employee_id": employee_id,
"name": name,
"roles": roles or ["agent"],
"current_role": "agent",
"login_source": login_source,
})
await mock_redis.setex(f"user:token:{token}", 28800, token_data)
return token
async def _seed_admin_role(db_session, employee_id: str):
"""为用户分配 admin 角色。"""
import uuid
stmt = select(Role).where(Role.name == "admin")
role = (await db_session.execute(stmt)).scalars().first()
if not role:
role = Role(
id=str(uuid.uuid4()),
name="admin",
display_name="管理员",
is_default=False,
permissions=[],
)
db_session.add(role)
await db_session.flush()
stmt = select(UserRole).where(
UserRole.employee_id == employee_id,
UserRole.role_id == role.id,
)
existing = (await db_session.execute(stmt)).scalars().first()
if not existing:
user_role = UserRole(
id=str(uuid.uuid4()),
employee_id=employee_id,
role_id=role.id,
source="manual",
assigned_at=datetime.now(),
)
db_session.add(user_role)
await db_session.flush()
async def _login_and_get_token(client, user_id: str, name: str, otp_code: str = None):
"""调用 /agents/login 并返回响应 data。
注意:此函数在新行为下对 mfa_enabled=False 的 agent 会返回
{require_otp_bind: true} 而非 token。
"""
payload = {"user_id": user_id, "name": name}
if otp_code:
payload["otp_code"] = otp_code
response = await client.post("/agents/login", json=payload)
assert response.status_code == 200, f"登录失败: {response.text}"
body = response.json()
assert body.get("code") == 0, f"登录业务码非 0: {body}"
return body["data"]
# =============================================================================
# Part A: 登录行为变更
# =============================================================================
class TestLoginRequireOtpBind:
"""A1: mfa_enabled=False → require_otp_bind: true"""
@pytest.mark.asyncio
async def test_new_agent_login_returns_require_otp_bind(self, client, db_session):
"""新坐席(mfa_enabled=False)登录应返回 require_otp_bind + 半认证 token。"""
agent = create_test_agent(user_id="new_bind_001", name="新坐席001")
db_session.add(agent)
await db_session.flush()
data = await _login_and_get_token(client, "new_bind_001", "新坐席001")
assert data.get("require_otp_bind") is True, \
f"期望 require_otp_bind=true,实际: {data}"
assert "token" in data, \
f"BUG-001 修复: require_otp_bind 应携带半认证 token,实际: {data}"
assert data.get("user_id") == "new_bind_001"
assert data.get("name") == "新坐席001"
assert "role" in data
@pytest.mark.asyncio
async def test_new_agent_login_message(self, client, db_session):
"""验证 require_otp_bind 响应包含引导消息。"""
agent = create_test_agent(user_id="new_bind_002", name="新坐席002")
db_session.add(agent)
await db_session.flush()
data = await _login_and_get_token(client, "new_bind_002", "新坐席002")
assert data.get("require_otp_bind") is True
assert "message" in data
assert "绑定" in data.get("message", "")
@pytest.mark.asyncio
async def test_brand_new_agent_auto_register_and_require_bind(self, client, db_session):
"""全新坐席(DB 无记录)首次登录自动注册后也返回 require_otp_bind + token。"""
# 不预先创建 agent,login 会自动注册
data = await _login_and_get_token(client, "brand_new_001", "全新坐席")
assert data.get("require_otp_bind") is True, \
f"全新坐席首次登录应引导 OTP 绑定,实际: {data}"
assert "token" in data, \
f"BUG-001 修复: 应携带半认证 token 供后续 otp-bind 调用"
class TestLoginRequireOtpRegression:
"""A2: mfa_enabled=True 无 OTP → require_otp: true(回归)"""
@pytest.mark.asyncio
async def test_bound_agent_login_without_otp_returns_require_otp(self, client, db_session):
"""已绑定 agent 登录不传 otp_code → require_otp: true(回归)。"""
secret = pyotp.random_base32()
agent = create_test_agent(user_id="bound_001", name="已绑定坐席")
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
data = await _login_and_get_token(client, "bound_001", "已绑定坐席")
assert data.get("require_otp") is True, \
f"已绑定坐席无 OTP 应返回 require_otp=true,实际: {data}"
assert "token" not in data, \
f"require_otp 不应签发 token,实际: {data}"
assert data.get("message") == "请输入OTP动态码"
@pytest.mark.asyncio
async def test_bound_agent_login_with_correct_otp_returns_token(self, client, db_session):
"""已绑定 agent 传正确 OTP → 签发 token(A3 回归)。"""
secret = pyotp.random_base32()
agent = create_test_agent(user_id="bound_002", name="已绑定坐席2")
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
otp_code = pyotp.TOTP(secret).now()
data = await _login_and_get_token(client, "bound_002", "已绑定坐席2", otp_code)
assert "token" in data, \
f"正确 OTP 应签发 token,实际: {data}"
assert data.get("user_id") == "bound_002"
@pytest.mark.asyncio
async def test_bound_agent_login_with_wrong_otp_returns_error(self, client, db_session):
"""已绑定 agent 传错误 OTP → 报错。"""
secret = pyotp.random_base32()
agent = create_test_agent(user_id="bound_003", name="已绑定坐席3")
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
response = await client.post("/agents/login", json={
"user_id": "bound_003",
"name": "已绑定坐席3",
"otp_code": "000000",
})
body = response.json()
# 错误码 1006: OTP验证码错误
assert body.get("code") == 1006, \
f"错误 OTP 应返回 1006,实际: {body}"
# =============================================================================
# Part B: OTP 首次绑定验证 (verify_otp)
# =============================================================================
class TestVerifyOtpFirstBind:
"""B1-B2: 首次绑定场景 verify_otp 行为"""
@pytest.mark.asyncio
async def test_first_bind_verify_correct_code_returns_token(self, otp_client, db_session, mock_redis):
"""首次绑定 + 正确 OTP → verified=true + token + is_first_bind。
模拟流程:agent 已调用 otp-bind 获取 secretmfa_enabled=False, mfa_secret 有值),
此时输入正确 OTP 验证码完成绑定。
"""
secret = pyotp.random_base32()
agent = create_test_agent(user_id="first_bind_001", name="首次绑定坐席")
agent.mfa_secret = secret
agent.mfa_enabled = False # 尚未启用
agent.mfa_bound_at = None
db_session.add(agent)
await db_session.flush()
# 为 agent 创建 token(绕过登录的 require_otp_bind 限制)
token = await _create_token_in_redis(mock_redis, "first_bind_001", "首次绑定坐席")
otp_code = pyotp.TOTP(secret).now()
response = await otp_client.post(
"/auth/otp-verify",
headers=_bearer(token),
json={"otp_code": otp_code},
)
assert response.status_code == 200
body = response.json()
assert body["code"] == 0, f"业务码非 0: {body}"
data = body["data"]
# 验证核心字段
assert data["verified"] is True, f"校验应通过,实际: {data}"
assert data.get("is_first_bind") is True, \
f"首次绑定应返回 is_first_bind=true,实际: {data}"
assert "token" in data, \
f"首次绑定应签发 token,实际: {data}"
assert data.get("user_id") == "first_bind_001"
assert data.get("expires_in") == MFA_VERIFIED_TTL_SECONDS
# 验证 DB 状态已更新
stmt = select(Agent).where(Agent.user_id == "first_bind_001")
db_agent = (await db_session.execute(stmt)).scalars().first()
assert db_agent.mfa_enabled is True, \
f"DB mfa_enabled 应为 True,实际: {db_agent.mfa_enabled}"
assert db_agent.mfa_bound_at is not None, \
f"DB mfa_bound_at 应为非空,实际: {db_agent.mfa_bound_at}"
assert db_agent.mfa_last_verified_at is not None
# 验证 Redis 验证标记已写入
verified_key = f"mfa:verified:first_bind_001"
assert await mock_redis.exists(verified_key), \
"Redis 中应有 mfa:verified 标记"
@pytest.mark.asyncio
async def test_first_bind_verify_wrong_code_returns_not_verified(self, client, db_session, mock_redis):
"""首次绑定 + 错误 OTP → verified=false,不改变 DB 状态。"""
secret = pyotp.random_base32()
agent = create_test_agent(user_id="first_bind_002", name="首次绑定坐席2")
agent.mfa_secret = secret
agent.mfa_enabled = False
agent.mfa_bound_at = None
db_session.add(agent)
await db_session.flush()
token = await _create_token_in_redis(mock_redis, "first_bind_002", "首次绑定坐席2")
response = await client.post(
"/auth/otp-verify",
headers=_bearer(token),
json={"otp_code": "000000"},
)
assert response.status_code == 200
body = response.json()
assert body["code"] == 0
data = body["data"]
assert data["verified"] is False, f"错误码应返回 false,实际: {data}"
assert data["expires_in"] == 0
assert "token" not in data, \
f"BUG-002 修复: 验证失败 token 应被 exclude,实际: {data}"
# DB 状态不应改变
stmt = select(Agent).where(Agent.user_id == "first_bind_002")
db_agent = (await db_session.execute(stmt)).scalars().first()
assert db_agent.mfa_enabled is False, \
f"验证失败不应改变 mfa_enabled,实际: {db_agent.mfa_enabled}"
class TestVerifyOtpAlreadyBoundRegression:
"""B3-B4: 已绑定场景 verify_otp 行为(回归)"""
@pytest.mark.asyncio
async def test_bound_agent_verify_correct_code_no_token(self, client, db_session, mock_redis):
"""已绑定 agent 验证正确 OTP → verified=true,不含 token(回归)。"""
secret = pyotp.random_base32()
agent = create_test_agent(user_id="bound_vfy_001", name="已绑定验证坐席")
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
token = await _create_token_in_redis(mock_redis, "bound_vfy_001", "已绑定验证坐席")
otp_code = pyotp.TOTP(secret).now()
response = await client.post(
"/auth/otp-verify",
headers=_bearer(token),
json={"otp_code": otp_code},
)
assert response.status_code == 200
body = response.json()
assert body["code"] == 0
data = body["data"]
assert data["verified"] is True
assert data.get("is_first_bind") is not True, \
f"已绑定场景不应返回 is_first_bind,实际: {data}"
assert "token" not in data, \
f"BUG-002 修复: 已绑定场景 token 应被 exclude,实际: {data}"
@pytest.mark.asyncio
async def test_no_secret_verify_returns_false(self, client, db_session, mock_redis):
"""无 secret 的 agent 调用 verify → verified=false。"""
agent = create_test_agent(user_id="no_secret_001", name="无密钥坐席")
# mfa_secret 为 None(从未调用 otp-bind
db_session.add(agent)
await db_session.flush()
token = await _create_token_in_redis(mock_redis, "no_secret_001", "无密钥坐席")
response = await client.post(
"/auth/otp-verify",
headers=_bearer(token),
json={"otp_code": "123456"},
)
assert response.status_code == 200
body = response.json()
assert body["code"] == 0
assert body["data"]["verified"] is False
class TestVerifyOtpFirstBindTokenValidity:
"""验证首次绑定签发的 token 可用于后续认证请求"""
@pytest.mark.asyncio
async def test_first_bind_token_can_be_used_for_auth(self, otp_client, db_session, mock_redis):
"""首次绑定签发的 token 应能用于调用 /agents/me 等需认证端点。"""
secret = pyotp.random_base32()
agent = create_test_agent(user_id="token_test_001", name="Token验证坐席")
agent.mfa_secret = secret
agent.mfa_enabled = False
db_session.add(agent)
await db_session.flush()
pre_token = await _create_token_in_redis(mock_redis, "token_test_001", "Token验证坐席")
otp_code = pyotp.TOTP(secret).now()
resp = await otp_client.post(
"/auth/otp-verify",
headers=_bearer(pre_token),
json={"otp_code": otp_code},
)
bind_token = resp.json()["data"]["token"]
# 用绑定签发的 token 调用 /agents/me
me_resp = await otp_client.get("/agents/me", headers=_bearer(bind_token))
assert me_resp.status_code == 200
me_data = me_resp.json()
assert me_data["code"] == 0
assert me_data["data"]["user_id"] == "token_test_001"
# =============================================================================
# Part C: 管理后台端点
# =============================================================================
class TestAdminOtpUsersEndpoint:
"""C1-C2: GET /auth/otp-admin-users"""
@pytest.mark.asyncio
async def test_admin_list_users_includes_mfa_fields(self, client, db_session, mock_redis):
"""管理员查询列表 → 返回字段含 mfa_enabled / mfa_bound_at / mfa_last_verified_at。"""
# 创建测试坐席
agent1 = create_test_agent(user_id="adm_u_001", name="坐席A")
agent2 = create_test_agent(user_id="adm_u_002", name="坐席B")
agent2.mfa_enabled = True
agent2.mfa_secret = pyotp.random_base32()
agent2.mfa_bound_at = datetime.now()
db_session.add_all([agent1, agent2])
# 管理员
admin = create_test_agent(user_id="admin_otp_001", name="管理员")
db_session.add(admin)
await db_session.flush()
await _seed_admin_role(db_session, "admin_otp_001")
admin_token = await _create_token_in_redis(
mock_redis, "admin_otp_001", "管理员", ["admin"]
)
response = await client.get(
"/auth/otp-admin-users",
headers=_bearer(admin_token),
)
assert response.status_code == 200
body = response.json()
assert body["code"] == 0
users = body["data"]
assert isinstance(users, list)
assert len(users) >= 2
# 验证字段完整性
for user in users:
assert "employee_id" in user
assert "name" in user
assert "mfa_enabled" in user
assert "mfa_bound_at" in user # 可为 null
assert "mfa_last_verified_at" in user # 可为 null
# 验证坐席A (mfa_enabled=False)
user_a = next(u for u in users if u["employee_id"] == "adm_u_001")
assert user_a["mfa_enabled"] is False
assert user_a["mfa_bound_at"] is None
# 验证坐席B (mfa_enabled=True)
user_b = next(u for u in users if u["employee_id"] == "adm_u_002")
assert user_b["mfa_enabled"] is True
assert user_b["mfa_bound_at"] is not None
@pytest.mark.asyncio
async def test_non_admin_access_returns_403(self, client, db_session, mock_redis):
"""非 admin 角色访问 → 403。"""
agent = create_test_agent(user_id="normal_agent_001", name="普通坐席")
db_session.add(agent)
await db_session.flush()
token = await _create_token_in_redis(
mock_redis, "normal_agent_001", "普通坐席", ["agent"]
)
response = await client.get(
"/auth/otp-admin-users",
headers=_bearer(token),
)
assert response.status_code == 403, \
f"非 admin 应返回 403,实际: {response.status_code}"
class TestAdminResetOtpEndpoint:
"""C3: POST /auth/otp-admin-reset/{employee_id}"""
@pytest.mark.asyncio
async def test_admin_reset_clears_binding(self, otp_client, db_session, mock_redis):
"""管理员清除绑定 → DB 清空 mfa_* 字段。"""
secret = pyotp.random_base32()
target = create_test_agent(user_id="reset_target_001", name="被重置坐席")
target.mfa_secret = secret
target.mfa_enabled = True
target.mfa_bound_at = datetime.now()
target.mfa_last_verified_at = datetime.now()
db_session.add(target)
admin = create_test_agent(user_id="admin_reset_001", name="重置管理员")
db_session.add(admin)
await db_session.flush()
await _seed_admin_role(db_session, "admin_reset_001")
admin_token = await _create_token_in_redis(
mock_redis, "admin_reset_001", "重置管理员", ["admin"]
)
# 先写 Redis 验证标记
await mock_redis.setex("mfa:verified:reset_target_001", 1800, "1")
response = await otp_client.post(
"/auth/otp-admin-reset/reset_target_001",
headers=_bearer(admin_token),
)
assert response.status_code == 200
body = response.json()
assert body["code"] == 0
assert body["data"]["success"] is True
# DB 验证
stmt = select(Agent).where(Agent.user_id == "reset_target_001")
db_target = (await db_session.execute(stmt)).scalars().first()
assert db_target.mfa_secret is None, \
f"secret 应为 None,实际: {db_target.mfa_secret}"
assert db_target.mfa_enabled is False, \
f"enabled 应为 False,实际: {db_target.mfa_enabled}"
assert db_target.mfa_bound_at is None, \
f"bound_at 应为 None,实际: {db_target.mfa_bound_at}"
# Redis 验证标记应被清除
assert not await mock_redis.exists("mfa:verified:reset_target_001"), \
"Redis 验证标记应被清除"
@pytest.mark.asyncio
async def test_admin_reset_nonexistent_agent_returns_error(self, client, db_session, mock_redis):
"""清除不存在的坐席 → 错误。"""
admin = create_test_agent(user_id="admin_reset_002", name="重置管理员2")
db_session.add(admin)
await db_session.flush()
await _seed_admin_role(db_session, "admin_reset_002")
admin_token = await _create_token_in_redis(
mock_redis, "admin_reset_002", "重置管理员2", ["admin"]
)
response = await client.post(
"/auth/otp-admin-reset/nonexistent_999",
headers=_bearer(admin_token),
)
body = response.json()
assert body["code"] != 0, \
f"不存在的坐席应返回错误,实际: {body}"
@pytest.mark.asyncio
async def test_non_admin_cannot_reset(self, client, db_session, mock_redis):
"""非 admin 角色调用 reset → 403。"""
agent = create_test_agent(user_id="normal_reset_001", name="普通坐席")
db_session.add(agent)
await db_session.flush()
token = await _create_token_in_redis(
mock_redis, "normal_reset_001", "普通坐席", ["agent"]
)
response = await client.post(
"/auth/otp-admin-reset/some_target",
headers=_bearer(token),
)
assert response.status_code == 403, \
f"非 admin 应返回 403,实际: {response.status_code}"
# =============================================================================
# Part D: 身份验证缺口探查(Auth Gap Detection
# =============================================================================
class TestAuthGapDetection:
"""探查 require_otp_bind 后的认证缺口。
当前设计:agent_login 对 mfa_enabled=False 返回 require_otp_bind 但不签发 token
但后续 otp-bind / otp-verify 端点需要 Bearer token 认证。
"""
@pytest.mark.asyncio
async def test_otp_bind_requires_auth(self, client, db_session, mock_redis):
"""无 token 调用 otp-bind → 401/403。
这验证了认证缺口:require_otp_bind 之后前端无 token 可用来调用 otp-bind。
"""
response = await client.post("/auth/otp-bind") # 无 Authorization 头
# 期望 401(未认证)或 403(禁止访问)
assert response.status_code in (401, 403), \
f"无 token 调用 otp-bind 应返回 401/403,实际: {response.status_code}"
@pytest.mark.asyncio
async def test_otp_verify_requires_auth(self, client, db_session, mock_redis):
"""无 token 调用 otp-verify → 401/403。
验证:require_otp_bind 之后无法直接调 otp-verify。
"""
response = await client.post(
"/auth/otp-verify",
json={"otp_code": "123456"},
) # 无 Authorization 头
assert response.status_code in (401, 403), \
f"无 token 调用 otp-verify 应返回 401/403,实际: {response.status_code}"
@pytest.mark.asyncio
async def test_full_first_bind_flow_auth_gap(self, otp_client, db_session, mock_redis):
"""BUG-001 修复后:完整首次绑定流程无需 token 注入即可完成。
修复前: login → require_otp_bind(无 token)→ otp-bind → 401
修复后: login → require_otp_bind + token → otp-bind → otp-verify → 成功
"""
agent = create_test_agent(user_id="gap_test_001", name="缺口测试坐席")
db_session.add(agent)
await db_session.flush()
# Step 1: 登录 → 得到 require_otp_bind + 半认证 token
data = await _login_and_get_token(otp_client, "gap_test_001", "缺口测试坐席")
assert data.get("require_otp_bind") is True
assert "token" in data, \
f"BUG-001 修复: require_otp_bind 应携带 token,实际: {data}"
bind_token = data["token"]
# Step 2: 用半认证 token 调 otp-bind → 应成功(不再 401
bind_resp = await otp_client.post(
"/auth/otp-bind", headers=_bearer(bind_token)
)
assert bind_resp.status_code == 200, \
f"BUG-001 修复: 半认证 token 应能访问 otp-bind" \
f"实际: {bind_resp.status_code}, body: {bind_resp.json()}"
bind_data = bind_resp.json()["data"]
secret = bind_data["secret"]
# Step 3: 用半认证 token + 正确 OTP 调 otp-verify → 成功
otp_code = pyotp.TOTP(secret).now()
verify_resp = await otp_client.post(
"/auth/otp-verify",
headers=_bearer(bind_token),
json={"otp_code": otp_code},
)
assert verify_resp.status_code == 200
verify_data = verify_resp.json()["data"]
assert verify_data["verified"] is True
assert verify_data.get("is_first_bind") is True
assert "token" in verify_data, \
f"首次绑定应返回完整 token,实际: {verify_data}"
# Step 4: 验证返回的完整 token 可用于认证
full_token = verify_data["token"]
me_resp = await otp_client.get("/agents/me", headers=_bearer(full_token))
assert me_resp.status_code == 200
assert me_resp.json()["data"]["user_id"] == "gap_test_001"
# =============================================================================
# Part E: 全链路端到端流程(with workaround token injection
# =============================================================================
class TestEndToEndFirstBindFlow:
"""端到端首次绑定流程(使用 token 注入绕过认证缺口验证业务逻辑正确性)"""
@pytest.mark.asyncio
async def test_e2e_first_bind_flow(self, otp_client, db_session, mock_redis):
"""全链路:登录→获取 secret→验证→获得 token→认证可用。
通过 token 注入来绕过当前认证缺口,验证业务逻辑链路的正确性。
"""
# 1. 创建坐席并登录(mfa_enabled=False
agent = create_test_agent(user_id="e2e_test_001", name="E2E测试坐席")
db_session.add(agent)
await db_session.flush()
login_data = await _login_and_get_token(otp_client, "e2e_test_001", "E2E测试坐席")
assert login_data.get("require_otp_bind") is True
# 2. 注入 token(模拟认证状态)
token = await _create_token_in_redis(mock_redis, "e2e_test_001", "E2E测试坐席")
# 3. 调用 otp-bind 获取 secret 和二维码
bind_resp = await otp_client.post("/auth/otp-bind", headers=_bearer(token))
assert bind_resp.status_code == 200
bind_data = bind_resp.json()["data"]
assert "secret" in bind_data
assert "otpauth_url" in bind_data
assert "qr_code_base64" in bind_data
secret = bind_data["secret"]
# 4. 用生成的 secret 计算 OTP 并验证
otp_code = pyotp.TOTP(secret).now()
verify_resp = await otp_client.post(
"/auth/otp-verify",
headers=_bearer(token),
json={"otp_code": otp_code},
)
assert verify_resp.status_code == 200
verify_data = verify_resp.json()["data"]
assert verify_data["verified"] is True
assert verify_data.get("is_first_bind") is True
assert "token" in verify_data, "首次绑定应返回登录 token"
# 5. 用绑定返回的 token 调用认证端点
bind_token = verify_data["token"]
me_resp = await otp_client.get("/agents/me", headers=_bearer(bind_token))
assert me_resp.status_code == 200
me_data = me_resp.json()
assert me_data["data"]["user_id"] == "e2e_test_001"
# =============================================================================
# Part F: Reset 后重新绑定流程
# =============================================================================
class TestResetThenRebind:
"""管理员清除绑定后坐席重新走首次绑定流程"""
@pytest.mark.asyncio
async def test_reset_then_login_returns_require_otp_bind(self, client, db_session, mock_redis):
"""管理员清除后,坐席登录应再次返回 require_otp_bind。"""
# 1. 创建已绑定的坐席
secret = pyotp.random_base32()
agent = create_test_agent(user_id="reset_rebind_001", name="重置重绑坐席")
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
# 2. 管理员
admin = create_test_agent(user_id="admin_rebind_001", name="重绑管理员")
db_session.add(admin)
await db_session.flush()
await _seed_admin_role(db_session, "admin_rebind_001")
# 3. 管理员清除绑定
admin_token = await _create_token_in_redis(
mock_redis, "admin_rebind_001", "重绑管理员", ["admin"]
)
reset_resp = await client.post(
"/auth/otp-admin-reset/reset_rebind_001",
headers=_bearer(admin_token),
)
assert reset_resp.json()["data"]["success"] is True
# 4. 坐席重新登录 → 应返回 require_otp_bind
data = await _login_and_get_token(client, "reset_rebind_001", "重置重绑坐席")
assert data.get("require_otp_bind") is True, \
f"清除后登录应返回 require_otp_bind,实际: {data}"
if __name__ == "__main__":
pytest.main([__file__, "-v"])
+392
View File
@@ -0,0 +1,392 @@
# =============================================================================
# 三端认证重构 AUTH-03 — 统一 OTP 路由测试
# =============================================================================
# 验证 otp.py 的 6 个端点:
# GET /api/auth/otp-status — 查询绑定状态
# POST /api/auth/otp-bind — 生成 secret + 二维码
# POST /api/auth/otp-verify — 验证 OTP(写 Redis 30 分钟)
# POST /api/auth/otp-unbind — 用户主动关闭 OTP
# POST /api/auth/otp-admin-reset/{id} — 管理员重置
# GET /api/auth/otp-admin-users — 管理员查看全部坐席状态
# =============================================================================
import pyotp
import pytest
from sqlalchemy import select
from app.models.agent import Agent
from app.services.mfa_service import MFA_VERIFIED_TTL_SECONDS
from tests.conftest import create_test_agent
# -----------------------------------------------------------------------------
# 辅助函数
# -----------------------------------------------------------------------------
async def _login_and_get_token(client, user_id: str, name: str, otp_code: str = None) -> str:
"""调用 /agents/login 获取 token
Args:
client: 测试客户端
user_id: 用户 ID
name: 用户名
otp_code: 可选的 OTP 验证码(当登录返回 require_otp: true 时需要提供)
"""
payload = {"user_id": user_id, "name": name}
if otp_code:
payload["otp_code"] = otp_code
response = await client.post(
"/agents/login",
json=payload,
)
assert response.status_code == 200, f"登录失败: {response.text}"
body = response.json()
assert body.get("code") == 0, f"登录业务码非 0: {body}"
# 如果返回 require_otp,说明需要 OTP 验证
if body["data"].get("require_otp"):
# 需要先绑定/启用 OTP,这里返回 None 表示需要 OTP
return None
assert "token" in body["data"], f"登录响应没有 token: {body}"
return body["data"]["token"]
def _bearer(token: str) -> dict:
return {"Authorization": f"Bearer {token}"}
async def _seed_admin_role(db_session, employee_id: str) -> str:
"""为用户分配 admin 角色"""
from app.models.role import Role
from app.models.user_role import UserRole
import uuid
from datetime import datetime
# 1. 确保 admin 角色存在
stmt = select(Role).where(Role.name == "admin")
role = (await db_session.execute(stmt)).scalars().first()
if not role:
role = Role(
id=str(uuid.uuid4()),
name="admin",
display_name="管理员",
is_default=False,
permissions=[],
)
db_session.add(role)
await db_session.flush()
# 2. 建立关联
stmt = select(UserRole).where(
UserRole.employee_id == employee_id,
UserRole.role_id == role.id,
)
existing = (await db_session.execute(stmt)).scalars().first()
if not existing:
user_role = UserRole(
id=str(uuid.uuid4()),
employee_id=employee_id,
role_id=role.id,
source="manual",
assigned_at=datetime.now(),
)
db_session.add(user_role)
await db_session.flush()
return role.id
# =============================================================================
# 1. GET /api/auth/otp-status
# =============================================================================
class TestOTPSatus:
"""GET /api/auth/otp-status 测试"""
@pytest.mark.asyncio
async def test_new_user_status_unbound(self, client, db_session):
"""全新用户 → bound=false, enabled=false"""
agent = create_test_agent(user_id="alice_001", name="Alice")
db_session.add(agent)
await db_session.flush()
token = await _login_and_get_token(client, "alice_001", "Alice")
resp = await client.get("/auth/otp-status", headers=_bearer(token))
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
data = body["data"]
assert data["bound"] is False
assert data["enabled"] is False
assert data["verified"] is False
@pytest.mark.asyncio
async def test_bound_user_status(self, client, db_session):
"""已绑定用户 → bound=true, enabled=true
注意:已绑定用户的 mfa_enabled=True, mfa_secret 有值
这样的用户登录时会返回 require_otp: true
"""
from datetime import datetime
agent = create_test_agent(user_id="bob_001", name="Bob")
# 直接设置已绑定状态(mfa_enabled=True, mfa_secret 有值)
agent.mfa_secret = pyotp.random_base32()
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
# 使用 OTP 验证码登录获取 token
otp_code = pyotp.TOTP(agent.mfa_secret).now()
token = await _login_and_get_token(client, "bob_001", "Bob", otp_code)
# 查询状态
resp = await client.get("/auth/otp-status", headers=_bearer(token))
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
data = body["data"]
assert data["bound"] is True
assert data["enabled"] is True
# =============================================================================
# 2. POST /api/auth/otp-bind
# =============================================================================
class TestOTPBind:
"""POST /api/auth/otp-bind 测试"""
@pytest.mark.asyncio
async def test_bind_returns_secret_and_qrcode(self, client, db_session):
"""返回 secret + otpauth_url + qr_code_base64"""
agent = create_test_agent(user_id="carol_001", name="Carol")
db_session.add(agent)
await db_session.flush()
token = await _login_and_get_token(client, "carol_001", "Carol")
resp = await client.post("/auth/otp-bind", headers=_bearer(token))
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
data = body["data"]
assert "secret" in data
assert "otpauth_url" in data
assert "qr_code_base64" in data
assert len(data["secret"]) == 32
@pytest.mark.asyncio
async def test_bind_already_enabled_rejected(self, client, db_session):
"""已启用则拒绝重新绑定
已绑定 MFA 的用户需要通过 otp-verify 流程来获取 token
"""
from datetime import datetime
agent = create_test_agent(user_id="dave_001", name="Dave")
secret = pyotp.random_base32()
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
# 使用 OTP 验证码登录
otp_code = pyotp.TOTP(secret).now()
token = await _login_and_get_token(client, "dave_001", "Dave", otp_code)
resp = await client.post("/auth/otp-bind", headers=_bearer(token))
assert resp.status_code == 200
body = resp.json()
assert body["code"] != 0
# =============================================================================
# 3. POST /api/auth/otp-verify
# =============================================================================
class TestOTPVerify:
"""POST /api/auth/otp-verify 测试"""
@pytest.mark.asyncio
async def test_verify_correct_code(self, client, db_session):
"""正确码 → verified=True + 返回 expires_in"""
from datetime import datetime
agent = create_test_agent(user_id="eve_001", name="Eve")
secret = pyotp.random_base32()
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
# 使用 OTP 验证码登录
otp_code = pyotp.TOTP(secret).now()
token = await _login_and_get_token(client, "eve_001", "Eve", otp_code)
# 验证 OTP(使用新的验证码)
new_otp_code = pyotp.TOTP(secret).now()
resp = await client.post(
"/auth/otp-verify",
headers=_bearer(token),
json={"otp_code": new_otp_code},
)
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
data = body["data"]
assert data["verified"] is True
assert data["expires_in"] == MFA_VERIFIED_TTL_SECONDS
@pytest.mark.asyncio
async def test_verify_wrong_code(self, client, db_session):
"""错误码 → verified=False"""
from datetime import datetime
agent = create_test_agent(user_id="frank_001", name="Frank")
secret = pyotp.random_base32()
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
# 使用 OTP 验证码登录
otp_code = pyotp.TOTP(secret).now()
token = await _login_and_get_token(client, "frank_001", "Frank", otp_code)
# 使用错误的 OTP 验证
resp = await client.post(
"/auth/otp-verify",
headers=_bearer(token),
json={"otp_code": "000000"},
)
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
assert body["data"]["verified"] is False
# =============================================================================
# 4. POST /api/auth/otp-unbind
# =============================================================================
class TestOTPUnbind:
"""POST /api/auth/otp-unbind 测试"""
@pytest.mark.asyncio
async def test_unbind_correct_code(self, client, db_session):
"""正确 OTP → 清空 secret + enabled=False"""
from datetime import datetime
agent = create_test_agent(user_id="grace_001", name="Grace")
secret = pyotp.random_base32()
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
# 使用 OTP 验证码登录
otp_code = pyotp.TOTP(secret).now()
token = await _login_and_get_token(client, "grace_001", "Grace", otp_code)
# 解绑 OTP
resp = await client.post(
"/auth/otp-unbind",
headers=_bearer(token),
json={"otp_code": otp_code},
)
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
assert body["data"]["success"] is True
# DB 验证
stmt = select(Agent).where(Agent.user_id == "grace_001")
db_agent = (await db_session.execute(stmt)).scalars().first()
assert db_agent.mfa_secret is None
assert db_agent.mfa_enabled is False
# =============================================================================
# 5. POST /api/auth/otp-admin-reset/{employee_id}
# =============================================================================
class TestOTPAdminReset:
"""POST /api/auth/otp-admin-reset/{employee_id} 测试"""
@pytest.mark.asyncio
async def test_admin_reset_target_user(self, client, db_session):
"""管理员重置 → 目标用户清空"""
from datetime import datetime
# 目标用户
target = create_test_agent(user_id="henry_001", name="Henry")
target.mfa_secret = pyotp.random_base32()
target.mfa_enabled = True
target.mfa_bound_at = datetime.now()
db_session.add(target)
# 管理员
admin = create_test_agent(user_id="admin_001", name="Admin")
db_session.add(admin)
await db_session.flush()
await _seed_admin_role(db_session, "admin_001")
admin_token = await _login_and_get_token(client, "admin_001", "Admin")
resp = await client.post(
f"/auth/otp-admin-reset/henry_001",
headers=_bearer(admin_token),
)
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
assert body["data"]["success"] is True
# DB 验证
stmt = select(Agent).where(Agent.user_id == "henry_001")
db_target = (await db_session.execute(stmt)).scalars().first()
assert db_target.mfa_secret is None
assert db_target.mfa_enabled is False
# =============================================================================
# 6. GET /api/auth/otp-admin-users
# =============================================================================
class TestOTPAdminUsers:
"""GET /api/auth/otp-admin-users 测试"""
@pytest.mark.asyncio
async def test_admin_list_users(self, client, db_session):
"""管理员查看 → 返回所有用户"""
# 添加两个用户
agent1 = create_test_agent(user_id="user_001", name="User1")
agent2 = create_test_agent(user_id="user_002", name="User2")
db_session.add(agent1)
db_session.add(agent2)
# 管理员
admin = create_test_agent(user_id="admin_002", name="Admin2")
db_session.add(admin)
await db_session.flush()
await _seed_admin_role(db_session, "admin_002")
admin_token = await _login_and_get_token(client, "admin_002", "Admin2")
resp = await client.get("/auth/otp-admin-users", headers=_bearer(admin_token))
assert resp.status_code == 200
body = resp.json()
assert body["code"] == 0
# 应该包含所有用户
users = body["data"]
assert len(users) >= 2
if __name__ == "__main__":
pytest.main([__file__, "-v"])
+225
View File
@@ -0,0 +1,225 @@
# =============================================================================
# 三端认证重构 CTRT — 响应契约统一测试
# =============================================================================
# 验证后端响应格式符合统一信封规范:
# - 成功响应: {code: 0, data: {...}, message: "success"}
# - 失败响应: {code: 非0, data: null, message: "..."}
#
# 前端拦截器会根据 code 判断,成功时返回内层 dataCTRT-01
# 失败时抛出错对象 {code, message}CTRT-03
# =============================================================================
import pytest
from tests.conftest import create_test_agent
# =============================================================================
# 辅助函数
# =============================================================================
async def _login_and_get_token(client, user_id: str, name: str, otp_code: str = None) -> str:
"""登录获取 token
Args:
client: 测试客户端
user_id: 用户 ID
name: 用户名
otp_code: 可选的 OTP 验证码
"""
payload = {"user_id": user_id, "name": name}
if otp_code:
payload["otp_code"] = otp_code
response = await client.post(
"/agents/login",
json=payload,
)
assert response.status_code == 200
body = response.json()
# 如果返回 require_otp,说明需要 OTP 验证
if body["data"].get("require_otp"):
return None
return body["data"]["token"]
def _bearer(token: str) -> dict:
return {"Authorization": f"Bearer {token}"}
# =============================================================================
# 响应格式测试
# =============================================================================
class TestResponseEnvelope:
"""测试统一响应信封格式"""
@pytest.mark.asyncio
async def test_success_response_format(self, client, db_session):
"""成功响应应符合 {code: 0, data: {...}, message: "success"} 格式"""
# 创建一个坐席
agent = create_test_agent(user_id="test_agent_001", name="TestAgent")
db_session.add(agent)
await db_session.flush()
# 登录
token = await _login_and_get_token(client, "test_agent_001", "TestAgent")
# 调用需要认证的端点
response = await client.get("/agents/me", headers=_bearer(token))
assert response.status_code == 200
body = response.json()
# 验证响应格式
assert "code" in body
assert "data" in body
assert "message" in body
assert body["code"] == 0
assert body["message"] == "success"
@pytest.mark.asyncio
async def test_error_response_format(self, client):
"""错误响应应符合 {code: 非0, data: null, message: "..."} 格式"""
# 使用无效 token 调用
response = await client.get(
"/agents/me",
headers=_bearer("invalid_token_12345")
)
# 401 会返回统一错误格式
assert response.status_code in [200, 401]
body = response.json()
# 验证错误响应格式
assert "code" in body
assert "message" in body
# code 不为 0
assert body["code"] != 0
class TestOTPResponseContract:
"""测试 OTP 端点响应契约"""
@pytest.mark.asyncio
async def test_otp_status_response(self, client, db_session):
"""OTP status 端点返回内层 data"""
agent = create_test_agent(user_id="otp_test_001", name="OTPTest")
db_session.add(agent)
await db_session.flush()
token = await _login_and_get_token(client, "otp_test_001", "OTPTest")
response = await client.get("/auth/otp-status", headers=_bearer(token))
assert response.status_code == 200
body = response.json()
# 验证统一格式
assert body["code"] == 0
assert body["data"]["bound"] is False
assert body["data"]["enabled"] is False
@pytest.mark.asyncio
async def test_otp_bind_response(self, client, db_session):
"""OTP bind 端点返回内层 data"""
agent = create_test_agent(user_id="otp_test_002", name="OTPTest2")
db_session.add(agent)
await db_session.flush()
token = await _login_and_get_token(client, "otp_test_002", "OTPTest2")
response = await client.post("/auth/otp-bind", headers=_bearer(token))
assert response.status_code == 200
body = response.json()
# 验证统一格式
assert body["code"] == 0
assert "secret" in body["data"]
assert "otpauth_url" in body["data"]
assert "qr_code_base64" in body["data"]
@pytest.mark.asyncio
async def test_otp_verify_response(self, client, db_session):
"""OTP verify 端点返回内层 data"""
from datetime import datetime
import pyotp
agent = create_test_agent(user_id="otp_test_003", name="OTPTest3")
secret = pyotp.random_base32()
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
# 已启用 MFA 的用户需要使用 OTP 验证码登录
otp_code = pyotp.TOTP(secret).now()
token = await _login_and_get_token(client, "otp_test_003", "OTPTest3", otp_code)
response = await client.post(
"/auth/otp-verify",
headers=_bearer(token),
json={"otp_code": otp_code},
)
assert response.status_code == 200
body = response.json()
# 验证统一格式
assert body["code"] == 0
assert body["data"]["verified"] is True
class TestAgentsLoginResponse:
"""测试登录响应契约"""
@pytest.mark.asyncio
async def test_login_without_mfa_returns_token(self, client, db_session):
"""无 MFA 时登录直接返回 token"""
agent = create_test_agent(user_id="login_test_001", name="LoginTest")
db_session.add(agent)
await db_session.flush()
response = await client.post(
"/agents/login",
json={"user_id": "login_test_001", "name": "LoginTest"}
)
assert response.status_code == 200
body = response.json()
# 验证统一格式
assert body["code"] == 0
assert "token" in body["data"]
assert "user_id" in body["data"]
assert "name" in body["data"]
@pytest.mark.asyncio
async def test_login_with_mfa_requires_otp(self, client, db_session):
"""有 MFA 时登录返回 require_otp"""
from datetime import datetime
import pyotp
agent = create_test_agent(user_id="login_test_002", name="LoginTest2")
secret = pyotp.random_base32()
agent.mfa_secret = secret
agent.mfa_enabled = True
agent.mfa_bound_at = datetime.now()
db_session.add(agent)
await db_session.flush()
# 登录时不带 otp_code
response = await client.post(
"/agents/login",
json={"user_id": "login_test_002", "name": "LoginTest2"}
)
assert response.status_code == 200
body = response.json()
# 应该返回 require_otp: true(不带 token
assert body["code"] == 0
assert body["data"].get("require_otp") is True
assert "token" not in body["data"]
if __name__ == "__main__":
pytest.main([__file__, "-v"])
+834
View File
@@ -0,0 +1,834 @@
# -*- coding: utf-8 -*-
"""Tier1 新增 API 测试(Round 2
覆盖:
1. Vision API — POST /api/vision/analyze + GET /api/vision/models
2. RAGFlow Ingestion API — POST /api/ragflow/ingest + GET /api/ragflow/tasks/{id}
3. 独立审批队列 API — GET /admin/approval-queue/queued + stats + dequeue-approve
4. 知识迭代 API admin 端点 — 路由挂载 + audience/confidence 筛选
依赖: conftest.py 提供的 client / db_session / mock_redis / login_test_agent
"""
import io
import uuid
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import pytest_asyncio
from httpx import AsyncClient
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.knowledge_suggestion import KnowledgeSuggestion
# ═══════════════════════════════════════════════════════════════════════════════
# 辅助函数 — 管理员登录
# ═══════════════════════════════════════════════════════════════════════════════
async def _login_admin(client: AsyncClient, db_session: AsyncSession) -> str:
"""创建 admin 角色用户并返回 Bearer token。
与 conftest.login_test_agent 同模式,但角色为 admin。
"""
from app.models.role import Role
from app.models.user_role import UserRole
admin_id = f"test_admin_{uuid.uuid4().hex[:8]}"
# 1. 确保 admin 角色存在
stmt = select(Role).where(Role.name == "admin")
result = await db_session.execute(stmt)
admin_role = result.scalars().first()
if not admin_role:
admin_role = Role(
name="admin", display_name="管理员",
description="系统管理员", permissions=[],
)
db_session.add(admin_role)
await db_session.flush()
# 2. 创建 UserRole 关联
ur_stmt = select(UserRole).where(
UserRole.employee_id == admin_id,
UserRole.role_id == admin_role.id,
)
ur_result = await db_session.execute(ur_stmt)
if not ur_result.scalars().first():
db_session.add(UserRole(
employee_id=admin_id, role_id=admin_role.id,
source="manual", assigned_by="test_fixture",
))
await db_session.flush()
# 3. 登录
resp = await client.post("/agents/login", json={
"user_id": admin_id, "name": "测试管理员",
})
data = resp.json()
return data["data"]["token"]
async def _login_any_user(client: AsyncClient, db_session: AsyncSession) -> str:
"""创建普通用户并返回 Bearer token(用于 require_any_user 测试)。"""
from app.models.role import Role
from app.models.user_role import UserRole
user_id = f"test_user_{uuid.uuid4().hex[:8]}"
# 确保 user 角色存在
stmt = select(Role).where(Role.name == "user")
result = await db_session.execute(stmt)
user_role = result.scalars().first()
if not user_role:
user_role = Role(
name="user", display_name="普通用户",
description="普通员工", permissions=[],
)
db_session.add(user_role)
await db_session.flush()
# 创建 UserRole 关联
ur_stmt = select(UserRole).where(
UserRole.employee_id == user_id,
UserRole.role_id == user_role.id,
)
ur_result = await db_session.execute(ur_stmt)
if not ur_result.scalars().first():
db_session.add(UserRole(
employee_id=user_id, role_id=user_role.id,
source="manual", assigned_by="test_fixture",
))
await db_session.flush()
# 登录
resp = await client.post("/agents/login", json={
"user_id": user_id, "name": "测试用户",
})
data = resp.json()
return data["data"]["token"]
def _make_test_png() -> bytes:
"""生成一个最小的有效 PNG 图片(1x1 白色像素)。"""
import struct, zlib
def chunk(ctype, data):
c = ctype + data
return struct.pack(">I", len(data)) + c + struct.pack(">I", zlib.crc32(c) & 0xFFFFFFFF)
ihdr = struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0)
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(b"\x00")) + chunk(b"IEND", b"")
# ═══════════════════════════════════════════════════════════════════════════════
# Section A — Vision API
# ═══════════════════════════════════════════════════════════════════════════════
class TestVisionModels:
"""GET /api/vision/models — 无需认证,公开查询。"""
@pytest.mark.asyncio
async def test_list_models_no_auth(self, client: AsyncClient):
"""无需 Token 即可获取模型列表。"""
resp = await client.get("/api/vision/models")
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert "models" in data["data"]
assert len(data["data"]["models"]) >= 1
assert "default_model" in data["data"]
class TestVisionAnalyze:
"""POST /api/vision/analyze — 截图分析(需认证)。"""
@pytest.mark.asyncio
async def test_analyze_requires_auth(self, client: AsyncClient):
"""未携带 Token → 401(或 403)。"""
resp = await client.post("/api/vision/analyze")
assert resp.status_code in (401, 403)
@pytest.mark.asyncio
async def test_missing_image_field(self, client: AsyncClient, db_session: AsyncSession):
"""缺少必填 image 字段。"""
token = await _login_any_user(client, db_session)
resp = await client.post(
"/api/vision/analyze",
data={"conversation_id": "conv-test-001"},
headers={"Authorization": f"Bearer {token}"},
)
# FastAPI 会返回 422(缺少必填的 UploadFile 字段)
assert resp.status_code == 422
@pytest.mark.asyncio
async def test_missing_conversation_id(self, client: AsyncClient, db_session: AsyncSession):
"""缺少必填 conversation_id 字段。"""
token = await _login_any_user(client, db_session)
png = _make_test_png()
resp = await client.post(
"/api/vision/analyze",
files={"image": ("test.png", io.BytesIO(png), "image/png")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 422
@pytest.mark.asyncio
async def test_invalid_file_type(self, client: AsyncClient, db_session: AsyncSession):
"""上传不支持的 MIME 类型 → 400。"""
token = await _login_any_user(client, db_session)
resp = await client.post(
"/api/vision/analyze",
data={"conversation_id": "conv-test-001"},
files={"image": ("test.svg", b"<svg></svg>", "image/svg+xml")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200 # 业务 400 由 code 字段表达
data = resp.json()
assert data["code"] == 400
assert "不支持" in data["message"]
@pytest.mark.asyncio
async def test_oversized_file(self, client: AsyncClient, db_session: AsyncSession):
"""上传超过 10MB 的文件 → 400。"""
token = await _login_any_user(client, db_session)
big_data = b"\x00" * (11 * 1024 * 1024) # 11MB
resp = await client.post(
"/api/vision/analyze",
data={"conversation_id": "conv-test-001"},
files={"image": ("big.png", io.BytesIO(big_data), "image/png")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 400
assert "过大" in data["message"]
@pytest.mark.asyncio
async def test_analyze_ok_with_mock_vision_service(
self, client: AsyncClient, db_session: AsyncSession,
):
"""正常截图分析 — Mock VisionService 返回结构化描述。"""
token = await _login_any_user(client, db_session)
png = _make_test_png()
mock_service = MagicMock()
mock_service.analyze_screenshot = AsyncMock(return_value={
"description": "这是一个蓝色背景的错误弹窗,显示'网络连接失败'",
"confidence": 0.92,
"metadata": {"ui_elements": ["error_dialog", "retry_button"]},
})
mock_service.inject_to_conversation_context = AsyncMock(return_value=True)
mock_service.close = AsyncMock()
with patch("app.api.vision.VisionService", return_value=mock_service):
resp = await client.post(
"/api/vision/analyze",
data={
"conversation_id": "conv-test-001",
"vision_model": "Qwen3-VL-8B-Instruct",
},
files={"image": ("test.png", io.BytesIO(png), "image/png")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert data["message"] == "视觉分析完成"
assert data["data"]["description"] != ""
assert data["data"]["confidence"] > 0.8
assert data["data"]["injected"] is True
@pytest.mark.asyncio
async def test_analyze_handles_service_exception(
self, client: AsyncClient, db_session: AsyncSession,
):
"""VisionService 抛出异常 → 500 降级。"""
token = await _login_any_user(client, db_session)
png = _make_test_png()
mock_service = MagicMock()
mock_service.analyze_screenshot = AsyncMock(
side_effect=RuntimeError("模型推理超时")
)
mock_service.close = AsyncMock()
with patch("app.api.vision.VisionService", return_value=mock_service):
resp = await client.post(
"/api/vision/analyze",
data={"conversation_id": "conv-test-001"},
files={"image": ("test.png", io.BytesIO(png), "image/png")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 500
assert "视觉分析失败" in data["message"]
# ═══════════════════════════════════════════════════════════════════════════════
# Section B — RAGFlow Ingestion API
# ═══════════════════════════════════════════════════════════════════════════════
class TestRagflowIngestion:
"""POST /api/ragflow/ingest — 文档摄入(需管理员权限)。"""
@pytest.mark.asyncio
async def test_ingest_requires_admin(self, client: AsyncClient, db_session: AsyncSession):
"""普通用户调用 → 403。发送有效文件以确保先通过参数校验再到权限检查。"""
token = await _login_any_user(client, db_session)
resp = await client.post(
"/api/ragflow/ingest",
data={"category_hint": "其他"},
files={"file": ("test.txt", io.BytesIO(b"hello"), "text/plain")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 403
@pytest.mark.asyncio
async def test_ingest_no_auth(self, client: AsyncClient):
"""未认证 → 401 或 403。"""
resp = await client.post("/api/ragflow/ingest")
assert resp.status_code in (401, 403)
@pytest.mark.asyncio
async def test_invalid_extension(self, client: AsyncClient, db_session: AsyncSession):
"""上传不支持的文件格式 → 400。"""
token = await _login_admin(client, db_session)
resp = await client.post(
"/api/ragflow/ingest",
data={"category_hint": "网络"},
files={"file": ("test.exe", b"binary", "application/octet-stream")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 400
assert "不支持" in data["message"]
@pytest.mark.asyncio
async def test_empty_file(self, client: AsyncClient, db_session: AsyncSession):
"""上传空文件 → 400。"""
token = await _login_admin(client, db_session)
resp = await client.post(
"/api/ragflow/ingest",
data={"category_hint": "软件"},
files={"file": ("empty.txt", b"", "text/plain")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 400
assert "" in data["message"]
@pytest.mark.asyncio
async def test_oversized_file(self, client: AsyncClient, db_session: AsyncSession):
"""上传超过 20MB 的文件 → 400。"""
token = await _login_admin(client, db_session)
big_data = b"\x00" * (21 * 1024 * 1024) # 21MB
resp = await client.post(
"/api/ragflow/ingest",
data={"category_hint": "其他"},
files={"file": ("big.pdf", io.BytesIO(big_data), "application/pdf")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 400
assert "过大" in data["message"]
@pytest.mark.asyncio
async def test_ingest_ok_txt(self, client: AsyncClient, db_session: AsyncSession):
"""正常上传 .txt 文档 → 200,生成 KnowledgeSuggestion。"""
token = await _login_admin(client, db_session)
mock_service = MagicMock()
mock_service.upload_and_process = AsyncMock(return_value={
"task_id": "task-001",
"status": "completed",
"suggestions": [{
"suggestion_type": "new_faq",
"title": "VPN 连接失败排查",
"content": "1. 检查网络 2. 重启 VPN",
"category": "网络",
"tags": ["VPN", "连接"],
"source_data": ["chunk-001"],
"reason": "RAGFlow 提取",
"confidence": 0.85,
"issue": "VPN",
"action": "重启",
"relation_type": "LEADS_TO",
"parent_issue": "",
"graph_meta": {},
"source_failed": False,
}],
})
with patch("app.api.ragflow_ingestion.RagflowIngestionService", return_value=mock_service):
resp = await client.post(
"/api/ragflow/ingest",
data={"category_hint": "网络"},
files={"file": ("vpn_faq.txt", io.BytesIO("VPN troubleshooting steps...".encode()), "text/plain")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert data["data"]["status"] == "completed"
assert data["data"]["suggestions_count"] == 1
assert data["data"]["suggestions"][0]["title"] == "VPN 连接失败排查"
# 验证 KnowledgeSuggestion 已入库
stmt = select(KnowledgeSuggestion).where(
KnowledgeSuggestion.source_type == "document_ragflow"
)
result = await db_session.execute(stmt)
suggestions = result.scalars().all()
assert len(suggestions) >= 1
assert suggestions[0].audience == "engineer_workguide"
@pytest.mark.asyncio
async def test_ingest_ok_docx(self, client: AsyncClient, db_session: AsyncSession):
"""正常上传 .docx 文档 → 200。"""
token = await _login_admin(client, db_session)
mock_service = MagicMock()
mock_service.upload_and_process = AsyncMock(return_value={
"task_id": "task-002",
"status": "completed",
"suggestions": [],
})
with patch("app.api.ragflow_ingestion.RagflowIngestionService", return_value=mock_service):
resp = await client.post(
"/api/ragflow/ingest",
data={"category_hint": "安全"},
files={"file": ("policy.docx", io.BytesIO(b"PK\x03\x04 fake docx"), "application/vnd.openxmlformats-officedocument.wordprocessingml.document")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert data["data"]["suggestions_count"] == 0
class TestRagflowTasks:
"""GET /api/ragflow/tasks/{task_id} — 查询任务状态(需管理员)。"""
@pytest.mark.asyncio
async def test_task_not_found(self, client: AsyncClient, db_session: AsyncSession):
"""不存在的 task_id → 404。"""
token = await _login_admin(client, db_session)
resp = await client.get(
"/api/ragflow/tasks/nonexistent-task-id",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 404
@pytest.mark.asyncio
async def test_task_found(self, client: AsyncClient, db_session: AsyncSession):
"""先 ingest 再查状态 → 200 找到。"""
token = await _login_admin(client, db_session)
mock_service = MagicMock()
mock_service.upload_and_process = AsyncMock(return_value={
"task_id": "task-found-001",
"status": "completed",
"suggestions": [],
})
with patch("app.api.ragflow_ingestion.RagflowIngestionService", return_value=mock_service):
# 先上传文档创建 task
ingest_resp = await client.post(
"/api/ragflow/ingest",
data={"category_hint": "其他"},
files={"file": ("note.txt", io.BytesIO(b"content"), "text/plain")},
headers={"Authorization": f"Bearer {token}"},
)
task_id = ingest_resp.json()["data"]["task_id"]
# 再查询该 task
resp = await client.get(
f"/api/ragflow/tasks/{task_id}",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert data["data"]["task_id"] == task_id
assert data["data"]["status"] == "completed"
# ═══════════════════════════════════════════════════════════════════════════════
# Section C — 独立审批队列 API
# ═══════════════════════════════════════════════════════════════════════════════
def _seed_suggestions(db_session: AsyncSession, count: int = 3):
"""播种测试用 KnowledgeSuggestion 数据。"""
statuses = ["pending", "pending", "queued", "approved", "rejected"]
for i in range(min(count, len(statuses))):
db_session.add(KnowledgeSuggestion(
suggestion_type="new_faq",
status=statuses[i],
title=f"测试建议 {i+1}",
content=f"测试内容 {i+1}",
category="网络" if i % 2 == 0 else "软件",
tags=["测试"],
source_type="conversation",
source_data=[f"conv-{i}"],
reason="队列测试",
confidence=0.75 + i * 0.05,
audience="employee_quick_reply" if i % 2 == 0 else "engineer_workguide",
))
class TestApprovalQueueList:
"""GET /admin/approval-queue/queued — 队列列表。"""
@pytest.mark.asyncio
async def test_list_requires_admin(self, client: AsyncClient, db_session: AsyncSession):
"""普通用户 → 403。"""
token = await _login_any_user(client, db_session)
resp = await client.get(
"/admin/approval-queue/queued",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 403
@pytest.mark.asyncio
async def test_list_empty(self, client: AsyncClient, db_session: AsyncSession):
"""无数据时返回空列表。"""
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/approval-queue/queued",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert data["data"]["total"] == 0
assert data["data"]["items"] == []
@pytest.mark.asyncio
async def test_list_with_data(self, client: AsyncClient, db_session: AsyncSession):
"""有 pending+queued 数据时返回正确列表。"""
_seed_suggestions(db_session)
await db_session.commit()
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/approval-queue/queued",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
# pending(1) + queued(1) = 2
assert data["data"]["total"] >= 2
items = data["data"]["items"]
# 所有 item 的 status 应为 pending 或 queued
for item in items:
assert item["status"] in ("pending", "queued")
@pytest.mark.asyncio
async def test_list_filter_by_status(self, client: AsyncClient, db_session: AsyncSession):
"""按 status=queued 筛选。"""
_seed_suggestions(db_session)
await db_session.commit()
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/approval-queue/queued?status=queued",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
for item in data["data"]["items"]:
assert item["status"] == "queued"
@pytest.mark.asyncio
async def test_list_filter_by_audience(self, client: AsyncClient, db_session: AsyncSession):
"""按 audience 筛选。"""
_seed_suggestions(db_session)
await db_session.commit()
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/approval-queue/queued?audience=employee_quick_reply",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
for item in data["data"]["items"]:
assert item["audience"] == "employee_quick_reply"
@pytest.mark.asyncio
async def test_list_pagination(self, client: AsyncClient, db_session: AsyncSession):
"""分页参数生效。"""
_seed_suggestions(db_session, count=5)
await db_session.commit()
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/approval-queue/queued?page=1&page_size=2",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert len(data["data"]["items"]) <= 2
class TestApprovalQueueStats:
"""GET /admin/approval-queue/queued/stats — 队列统计。"""
@pytest.mark.asyncio
async def test_stats_requires_admin(self, client: AsyncClient, db_session: AsyncSession):
"""普通用户 → 403。"""
token = await _login_any_user(client, db_session)
resp = await client.get(
"/admin/approval-queue/queued/stats",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 403
@pytest.mark.asyncio
async def test_stats_with_data(self, client: AsyncClient, db_session: AsyncSession):
"""正常统计返回。"""
_seed_suggestions(db_session)
await db_session.commit()
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/approval-queue/queued/stats",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert "queued_total" in data["data"]
assert "pending_total" in data["data"]
assert "by_audience" in data["data"]
assert "by_source_type" in data["data"]
class TestApprovalQueueDequeueApprove:
"""POST /admin/approval-queue/queued/{id}/dequeue-approve — 队列审批。"""
@pytest.mark.asyncio
async def test_dequeue_requires_admin(self, client: AsyncClient, db_session: AsyncSession):
"""普通用户 → 403。"""
token = await _login_any_user(client, db_session)
resp = await client.post(
"/admin/approval-queue/queued/fake-id/dequeue-approve",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 403
@pytest.mark.asyncio
async def test_dequeue_not_found(self, client: AsyncClient, db_session: AsyncSession):
"""不存在的 suggestion_id → 404。"""
token = await _login_admin(client, db_session)
resp = await client.post(
"/admin/approval-queue/queued/nonexistent-id/dequeue-approve",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 404
@pytest.mark.asyncio
async def test_dequeue_approve_ok(self, client: AsyncClient, db_session: AsyncSession):
"""正常队列审批流程:queued → approved → applied → graph_synced。"""
# 创建一个 queued 状态的建议
suggestion = KnowledgeSuggestion(
suggestion_type="new_faq",
status="queued",
title="队列审批测试",
content="测试内容",
category="软件",
tags=["测试"],
source_type="conversation",
source_data=["conv-dequeue-test"],
reason="独立队列审批测试",
confidence=0.88,
audience="employee_quick_reply",
issue="测试问题",
action="测试动作",
relation_type="LEADS_TO",
parent_issue="",
)
db_session.add(suggestion)
await db_session.commit()
await db_session.refresh(suggestion)
token = await _login_admin(client, db_session)
resp = await client.post(
f"/admin/approval-queue/queued/{suggestion.id}/dequeue-approve",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert "队列审批通过" in data["message"]
# 状态应为 applied(经 approve_suggestion 全流程)
assert data["data"]["status"] in ("applied", "graph_synced")
# ═══════════════════════════════════════════════════════════════════════════════
# Section D — 知识迭代 API 路由挂载 + Tier1 扩展字段验证
# ═══════════════════════════════════════════════════════════════════════════════
class TestKnowledgeIterationRouting:
"""确认 /admin/knowledge-iteration/* 路由正确挂载(Tier1 新增端点)。"""
@pytest.mark.asyncio
async def test_suggestions_endpoint_accessible(self, client: AsyncClient, db_session: AsyncSession):
"""GET /admin/knowledge-iteration/suggestions 路由存在且需管理员。"""
# 无 Token → 401/403
resp = await client.get("/admin/knowledge-iteration/suggestions")
assert resp.status_code in (401, 403)
# 管理员 Token → 200
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/knowledge-iteration/suggestions",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
@pytest.mark.asyncio
async def test_suggestions_audience_filter(self, client: AsyncClient, db_session: AsyncSession):
"""GET /suggestions?audience=... 筛选生效。"""
# 播种不同 audience 的建议
db_session.add(KnowledgeSuggestion(
suggestion_type="new_faq", status="pending",
title="A", content="A", category="网络", tags=[],
source_type="conversation", source_data=["c-a"],
reason="测试", confidence=0.8, audience="employee_quick_reply",
))
db_session.add(KnowledgeSuggestion(
suggestion_type="new_faq", status="pending",
title="B", content="B", category="网络", tags=[],
source_type="conversation", source_data=["c-b"],
reason="测试", confidence=0.8, audience="engineer_workguide",
))
await db_session.commit()
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/knowledge-iteration/suggestions?audience=employee_quick_reply",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
for item in data["data"]["items"]:
assert item["audience"] == "employee_quick_reply"
@pytest.mark.asyncio
async def test_suggestions_confidence_filter(self, client: AsyncClient, db_session: AsyncSession):
"""GET /suggestions?confidence_min=...&confidence_max=... 筛选生效。"""
db_session.add(KnowledgeSuggestion(
suggestion_type="new_faq", status="pending",
title="Low", content="Low", category="软件", tags=[],
source_type="conversation", source_data=["c-low"],
reason="测试", confidence=0.45, audience="employee_quick_reply",
))
db_session.add(KnowledgeSuggestion(
suggestion_type="new_faq", status="pending",
title="High", content="High", category="软件", tags=[],
source_type="conversation", source_data=["c-high"],
reason="测试", confidence=0.92, audience="employee_quick_reply",
))
await db_session.commit()
token = await _login_admin(client, db_session)
# 只查 >= 0.7 的
resp = await client.get(
"/admin/knowledge-iteration/suggestions?confidence_min=0.7",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
for item in data["data"]["items"]:
assert item["confidence"] >= 0.7
@pytest.mark.asyncio
async def test_stats_endpoint(self, client: AsyncClient, db_session: AsyncSession):
"""GET /admin/knowledge-iteration/stats 返回正确统计。"""
_seed_suggestions(db_session)
await db_session.commit()
token = await _login_admin(client, db_session)
resp = await client.get(
"/admin/knowledge-iteration/stats",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert "pending" in data["data"]
assert "queued" in data["data"]
assert "total" in data["data"]
@pytest.mark.asyncio
async def test_rewrite_endpoint(self, client: AsyncClient, db_session: AsyncSession):
"""POST /suggestions/{id}/rewrite 改写提案并重置为 pending。"""
suggestion = KnowledgeSuggestion(
suggestion_type="new_faq", status="rejected",
title="原标题", content="原内容", category="网络", tags=[""],
source_type="conversation", source_data=["conv-rewrite"],
reason="改写测试", confidence=0.8, audience="employee_quick_reply",
)
db_session.add(suggestion)
await db_session.commit()
await db_session.refresh(suggestion)
token = await _login_admin(client, db_session)
resp = await client.post(
f"/admin/knowledge-iteration/suggestions/{suggestion.id}/rewrite",
json={"title": "改写的标题", "content": "改写的内容"},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert data["data"]["title"] == "改写的标题"
assert data["data"]["status"] == "pending"
@pytest.mark.asyncio
async def test_queue_endpoint(self, client: AsyncClient, db_session: AsyncSession):
"""POST /suggestions/{id}/queue 放入独立队列。"""
suggestion = KnowledgeSuggestion(
suggestion_type="new_faq", status="pending",
title="待入队", content="待入队内容", category="网络", tags=[],
source_type="conversation", source_data=["conv-queue"],
reason="入队测试", confidence=0.8, audience="employee_quick_reply",
)
db_session.add(suggestion)
await db_session.commit()
await db_session.refresh(suggestion)
token = await _login_admin(client, db_session)
resp = await client.post(
f"/admin/knowledge-iteration/suggestions/{suggestion.id}/queue",
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
assert data["data"]["status"] == "queued"
assert data["data"]["queued_at"] is not None