88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
from app import db
|
|
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
|
|
|
|
|
|
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_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')
|
|
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_headers, json={
|
|
'name': 'rohs-schedule',
|
|
'schedule_type': 'dify_app',
|
|
'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_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
|