- 移除 Agent 的 exposure_mode 与 internal_executor 相关逻辑,清理关联的过滤、序列化代码 - 删除废弃的 AgentRuntimeBinding 模型及其依赖 - 新增订单抽取上传批次、请求快照等数据库迁移与业务逻辑 - 修复附件完整性检测、订单抽取状态映射问题 - 更新多语言错误文案与前端单元测试配置 - 清理平台配置中不再使用的暴露模式配置
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
"""
|
|
tests/services/admin/test_permission_service.py
|
|
|
|
用途:permission_service 批量授权与权限申请审批单元测试。
|
|
"""
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import ForbiddenError, NotFoundError, ValidationError
|
|
from app.models import Agent, PermissionRequest, User
|
|
from app.services.admin import permission_service
|
|
|
|
|
|
def test_update_user_permissions(app):
|
|
with app.app_context():
|
|
agent = Agent(name='perm-agent', type='api', dify_app_id='app-p', dify_api_key='key-p')
|
|
user = User(username='perm-user', email='perm@example.com', role='user')
|
|
db.session.add_all([agent, user])
|
|
db.session.commit()
|
|
|
|
permission_service.update_user_permissions(user.id, {'agent_ids': [agent.id]})
|
|
db.session.refresh(user)
|
|
assert len(user.agents) == 1
|
|
assert user.agents[0].id == agent.id
|
|
|
|
|
|
def test_batch_update_permissions_add(app, monkeypatch):
|
|
with app.app_context():
|
|
agent = Agent(name='batch-agent', type='api', dify_app_id='app-b', dify_api_key='key-b')
|
|
user = User(username='batch-user', email='batch@example.com', role='user')
|
|
db.session.add_all([agent, user])
|
|
db.session.commit()
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.admin.permission_service.send_wecom_event_message',
|
|
lambda **kwargs: None,
|
|
)
|
|
|
|
result = permission_service.batch_update_permissions(
|
|
{'user_ids': [user.id], 'agent_ids': [agent.id], 'action': 'add'}
|
|
)
|
|
assert result['updated_count'] == 1
|
|
db.session.refresh(user)
|
|
assert agent in user.agents
|
|
|
|
|
|
def test_batch_update_permissions_no_users(app):
|
|
with app.app_context():
|
|
with pytest.raises(ValidationError):
|
|
permission_service.batch_update_permissions({'user_ids': [], 'agent_ids': [1]})
|
|
|
|
|
|
def test_permission_request_approve(app, monkeypatch):
|
|
with app.app_context():
|
|
agent = Agent(name='req-agent', type='api', dify_app_id='app-r', dify_api_key='key-r')
|
|
user = User(username='req-user', email='req@example.com', role='user')
|
|
db.session.add_all([agent, user])
|
|
db.session.flush()
|
|
req = PermissionRequest(user_id=user.id, agent_id=agent.id, reason='need access', status='pending')
|
|
db.session.add(req)
|
|
db.session.commit()
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.admin.permission_service.send_wecom_event_message',
|
|
lambda **kwargs: None,
|
|
)
|
|
|
|
permission_service.approve_permission_request(req.id)
|
|
db.session.refresh(req)
|
|
db.session.refresh(user)
|
|
assert req.status == 'approved'
|
|
assert req.reviewed_at is not None
|
|
assert agent in user.agents
|
|
|
|
|
|
def test_permission_request_not_found(app):
|
|
with app.app_context():
|