Files
syops/backend/tests/test_schedule_api.py
T

189 lines
6.8 KiB
Python

import pytest
from app import db
from app.core.auth.jwt import issue_local_access_token
from app.models import Agent, ScheduleRun, ScheduleTask, User
@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):
user = User(username=name, email=email, role='user', status='active')
user.set_password('x')
db.session.add(user)
return user
def test_schedule_crud_and_workspace_visibility(app, client, admin_jwt_headers):
with app.app_context():
owner = _mk_user('owner_u', 'owner@example.com')
notify = _mk_user('notify_u', 'notify@example.com')
other = _mk_user('other_u', 'other@example.com')
agent = Agent(name='dify-agent', dify_api_key='k', api_base_url='http://localhost:5001/v1', type='api')
db.session.add(agent)
db.session.commit()
owner_id = owner.id
notify_id = notify.id
other_id = other.id
agent_id = agent.id
create_res = client.post('/api/admin/schedules', headers=admin_jwt_headers, json={
'name': 'rohs-schedule',
'schedule_type': 'data_pipeline',
'data_source_type': 'database',
'owner_user_id': owner_id,
'cron_expr': '0 */30 * * *',
'wait_seconds': 5,
'dify_agent_id': agent_id,
'notify_user_ids_json': [notify_id],
'notify_enabled': True,
'enabled': True,
})
assert create_res.status_code == 201
task = create_res.get_json()
task_id = task['id']
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())
with app.app_context():
run = ScheduleRun(
run_no='RUNTEST001',
schedule_task_id=task_id,
trigger_type='manual',
status='success',
total_count=1,
success_count=1,
failed_count=0,
skipped_count=0,
duration_ms=100,
wecom_notified=False,
)
db.session.add(run)
db.session.commit()
run_id = run.id
owner_headers = {'X-Username': 'owner_u', 'Content-Type': 'application/json'}
notify_headers = {'X-Username': 'notify_u', 'Content-Type': 'application/json'}
other_headers = {'X-Username': 'other_u', 'Content-Type': 'application/json'}
owner_runs = client.get('/api/workspace/schedule-runs', headers=owner_headers)
assert owner_runs.status_code == 200
assert any(r['id'] == run_id for r in owner_runs.get_json())
notify_runs = client.get('/api/workspace/schedule-runs', headers=notify_headers)
assert notify_runs.status_code == 200
assert any(r['id'] == run_id for r in notify_runs.get_json())
other_runs = client.get('/api/workspace/schedule-runs', headers=other_headers)
assert other_runs.status_code == 200
assert all(r['id'] != run_id for r in other_runs.get_json())
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
def test_patch_schedule_creates_config_for_task_without_config(app, client, admin_jwt_headers):
"""PATCH 为尚无 config 的排程写入 config 应成功,不因主键序列漂移 500。"""
with app.app_context():
owner = _mk_user('cfg_create_owner_u', 'cfg_create_owner@example.com')
db.session.commit()
owner_id = owner.id
create_res = client.post('/api/admin/schedules', headers=admin_jwt_headers, json={
'name': 'config-create-schedule',
'schedule_type': 'data_pipeline',
'data_source_type': 'database',
'owner_user_id': owner_id,
'cron_expr': '0 4 * * *',
})
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': {
'row_id_key': 'id',
'data_source': {'db_type': 'sqlserver'},
},
})
assert patch_res.status_code == 200
cfg_res = client.get(f'/api/admin/schedule-config/{task_id}', headers=admin_jwt_headers)
assert cfg_res.status_code == 200
cfg_body = cfg_res.get_json()
assert cfg_body['config'] is not None
assert cfg_body['config']['row_id_key'] == 'id'