feat(schedule): 优化排程任务配置与进度契约处理逻辑

This commit is contained in:
hugo committed 2026-07-08 16:09:28 +08:00
1 parent d54f51ddc3
commit 99453aa52a
65 files changed
+2837 -487

No files matched your search

+79 -10
View File
@@ -1,12 +1,30 @@
import pytest
from app import db
from app.core.auth.jwt import issue_local_access_token
from app.models import Agent, ScheduleRun, ScheduleTask, User
def _mk_admin():
admin = User(username='admin', email='admin@example.com', role='admin', status='active')
admin.set_password('x')
db.session.add(admin)
return admin
@pytest.fixture
def admin_jwt_headers(app):
"""创建 admin 用户并签发 Bearer JWT。"""
with app.app_context():
user = db.session.query(User).filter_by(username='admin').first()
if not user:
user = User(
username='admin',
email='admin@example.com',
role='admin',
status='active',
)
user.set_password('admin')
db.session.add(user)
db.session.commit()
token = issue_local_access_token(user)
return {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json',
}
def _mk_user(name: str, email: str):
@@ -16,9 +34,8 @@ def _mk_user(name: str, email: str):
return user
def test_schedule_crud_and_workspace_visibility(app, client, admin_headers):
def test_schedule_crud_and_workspace_visibility(app, client, admin_jwt_headers):
with app.app_context():
_mk_admin()
owner = _mk_user('owner_u', 'owner@example.com')
notify = _mk_user('notify_u', 'notify@example.com')
other = _mk_user('other_u', 'other@example.com')
@@ -31,9 +48,10 @@ def test_schedule_crud_and_workspace_visibility(app, client, admin_headers):
other_id = other.id
agent_id = agent.id
create_res = client.post('/api/admin/schedules', headers=admin_headers, json={
create_res = client.post('/api/admin/schedules', headers=admin_jwt_headers, json={
'name': 'rohs-schedule',
'schedule_type': 'dify_app',
'schedule_type': 'data_pipeline',
'data_source_type': 'database',
'owner_user_id': owner_id,
'cron_expr': '0 */30 * * *',
'wait_seconds': 5,
@@ -46,7 +64,7 @@ def test_schedule_crud_and_workspace_visibility(app, client, admin_headers):
task = create_res.get_json()
task_id = task['id']
list_res = client.get('/api/admin/schedules', headers=admin_headers)
list_res = client.get('/api/admin/schedules', headers=admin_jwt_headers)
assert list_res.status_code == 200
assert any(x['id'] == task_id for x in list_res.get_json())
@@ -85,3 +103,54 @@ def test_schedule_crud_and_workspace_visibility(app, client, admin_headers):
denied = client.get(f'/api/workspace/schedule-runs/{run_id}', headers=other_headers)
assert denied.status_code == 403
def test_post_schedule_defaults_data_source_type_to_database(app, client, admin_jwt_headers):
"""兼容旧客户端:data_pipeline 不传 data_source_type 时应默认 database 而非 400。"""
with app.app_context():
owner = _mk_user('legacy_owner_u', 'legacy_owner@example.com')
db.session.commit()
owner_id = owner.id
res = client.post('/api/admin/schedules', headers=admin_jwt_headers, json={
'name': 'legacy-minimal-schedule',
'owner_user_id': owner_id,
'cron_expr': '0 3 * * *',
})
assert res.status_code == 201
body = res.get_json()
assert body['schedule_type'] == 'data_pipeline'
assert body['data_source_type'] == 'database'
def test_post_and_patch_schedule_reject_non_object_config(app, client, admin_jwt_headers):
"""POST/PATCH config 传非 JSON 对象(数组/字符串)应 400,而非 500。"""
with app.app_context():
owner = _mk_user('cfg_type_owner_u', 'cfg_type_owner@example.com')
db.session.commit()
owner_id = owner.id
post_res = client.post('/api/admin/schedules', headers=admin_jwt_headers, json={
'name': 'bad-config-schedule',
'schedule_type': 'data_pipeline',
'data_source_type': 'database',
'owner_user_id': owner_id,
'cron_expr': '0 3 * * *',
'config': ['not-an-object'],
})
assert post_res.status_code == 400
create_res = client.post('/api/admin/schedules', headers=admin_jwt_headers, json={
'name': 'good-schedule',
'schedule_type': 'data_pipeline',
'data_source_type': 'database',
'owner_user_id': owner_id,
'cron_expr': '0 3 * * *',
})
assert create_res.status_code == 201
task_id = create_res.get_json()['id']
patch_res = client.patch(f'/api/admin/schedules/{task_id}', headers=admin_jwt_headers, json={
'config': 'not-an-object',
})
assert patch_res.status_code == 400