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,63 @@
|
||||
"""
|
||||
tests/services/admin/test_agent_service.py
|
||||
|
||||
用途:agent_service CRUD 与权限绑定单元测试。
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.core.errors import NotFoundError, ValidationError
|
||||
from app.models import Agent, User
|
||||
from app.services.admin import agent_service
|
||||
|
||||
|
||||
def test_agent_crud(app):
|
||||
with app.app_context():
|
||||
created = agent_service.create_agent({
|
||||
'name': 'Test Agent',
|
||||
'type': 'iframe',
|
||||
'dify_app_id': 'app-1',
|
||||
'extra_config': {'iframe_url': 'https://example.com'},
|
||||
})
|
||||
agent_id = created['id']
|
||||
|
||||
items = agent_service.list_agents()
|
||||
assert any(item['id'] == agent_id for item in items)
|
||||
|
||||
agent_service.update_agent(agent_id, {'description': 'updated'})
|
||||
refreshed = db.session.get(Agent, agent_id)
|
||||
assert refreshed.description == 'updated'
|
||||
|
||||
agent_service.delete_agent(agent_id)
|
||||
assert db.session.get(Agent, agent_id) is None
|
||||
|
||||
|
||||
def test_create_agent_requires_dify_app_id_for_api_type(app):
|
||||
with app.app_context():
|
||||
with pytest.raises(ValidationError, match='App ID'):
|
||||
agent_service.create_agent({'name': 'API Agent', 'type': 'api'})
|
||||
|
||||
|
||||
def test_update_agent_permissions(app):
|
||||
with app.app_context():
|
||||
agent = Agent(name='perm-agent', type='iframe', is_active=True)
|
||||
user_a = User(username='u-a', email='a@test.com', status='active', role='user')
|
||||
user_b = User(username='u-b', email='b@test.com', status='active', role='user')
|
||||
db.session.add_all([agent, user_a, user_b])
|
||||
db.session.commit()
|
||||
|
||||
agent_service.update_agent_permissions(agent.id, [user_a.id])
|
||||
db.session.refresh(user_a)
|
||||
assert any(a.id == agent.id for a in user_a.agents)
|
||||
|
||||
agent_service.update_agent_permissions(agent.id, [user_b.id])
|
||||
db.session.refresh(user_a)
|
||||
db.session.refresh(user_b)
|
||||
assert user_a.agents == []
|
||||
assert any(a.id == agent.id for a in user_b.agents)
|
||||
|
||||
|
||||
def test_test_agent_connectivity_not_found(app):
|
||||
with app.app_context():
|
||||
with pytest.raises(NotFoundError):
|
||||
agent_service.test_agent_connectivity(99999)
|
||||
Reference in new issue
Block a user