refactor(backend): 重构后端admin和schedule部分
This commit is contained in:
1 parent
9f5493185d
commit
4a14960e01
74 files changed
+5937
-3644
No files matched your search
@@ -0,0 +1,79 @@
|
||||
"""
|
||||
tests/services/admin/test_permission_service.py
|
||||
|
||||
用途:permission_service 批量授权与权限申请审批单元测试。
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.core.errors import 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():
|
||||
with pytest.raises(NotFoundError):
|
||||
permission_service.approve_permission_request(99999)
|
||||
Reference in new issue
Block a user