前后端更新
This commit is contained in:
1 parent
a0c8721fbb
commit
24da604727
33 files changed
+1951
-580
No files matched your search
@@ -0,0 +1,106 @@
|
||||
import pytest
|
||||
import json
|
||||
from app import create_app, db
|
||||
from app.models import User, Agent
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
app = create_app('testing')
|
||||
app.config['TESTING'] = True
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
|
||||
|
||||
with app.test_client() as client:
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
# Create admin user
|
||||
admin = User(username='admin', role='admin', email='admin@example.com')
|
||||
admin.set_password('admin123')
|
||||
db.session.add(admin)
|
||||
|
||||
# Create normal users
|
||||
u1 = User(username='u1', role='user', email='u1@example.com')
|
||||
u2 = User(username='u2', role='user', email='u2@example.com')
|
||||
u3 = User(username='u3', role='user', email='u3@example.com')
|
||||
db.session.add_all([u1, u2, u3])
|
||||
|
||||
# Create agents
|
||||
a1 = Agent(name='A1', dify_app_id='app1')
|
||||
a2 = Agent(name='A2', dify_app_id='app2')
|
||||
db.session.add_all([a1, a2])
|
||||
|
||||
db.session.commit()
|
||||
|
||||
yield client
|
||||
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
|
||||
def get_headers(client):
|
||||
res = client.post('/api/user/login', json={'username': 'admin', 'password': 'admin123'})
|
||||
data = res.get_json()
|
||||
return {
|
||||
'X-User-Id': str(data['id']),
|
||||
'X-Username': data['username'],
|
||||
'X-Role': data['role'],
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
def test_batch_permissions(client):
|
||||
headers = get_headers(client)
|
||||
|
||||
# Get IDs
|
||||
with client.application.app_context():
|
||||
u1_id = User.query.filter_by(username='u1').first().id
|
||||
u2_id = User.query.filter_by(username='u2').first().id
|
||||
a1_id = Agent.query.filter_by(name='A1').first().id
|
||||
a2_id = Agent.query.filter_by(name='A2').first().id
|
||||
|
||||
# 1. Add A1 to U1, U2
|
||||
payload = {
|
||||
'user_ids': [u1_id, u2_id],
|
||||
'agent_ids': [a1_id],
|
||||
'action': 'add'
|
||||
}
|
||||
res = client.post('/api/admin/permissions/batch', headers=headers, json=payload)
|
||||
assert res.status_code == 200
|
||||
|
||||
# Verify
|
||||
res = client.get('/api/admin/users', headers=headers)
|
||||
users = res.get_json()
|
||||
u1 = next(u for u in users if u['username'] == 'u1')
|
||||
u2 = next(u for u in users if u['username'] == 'u2')
|
||||
assert a1_id in u1['agent_ids']
|
||||
assert a1_id in u2['agent_ids']
|
||||
|
||||
# 2. Set A2 to U1 (should replace A1)
|
||||
payload = {
|
||||
'user_ids': [u1_id],
|
||||
'agent_ids': [a2_id],
|
||||
'action': 'set'
|
||||
}
|
||||
res = client.post('/api/admin/permissions/batch', headers=headers, json=payload)
|
||||
assert res.status_code == 200
|
||||
|
||||
res = client.get('/api/admin/users', headers=headers)
|
||||
users = res.get_json()
|
||||
u1 = next(u for u in users if u['username'] == 'u1')
|
||||
assert a1_id not in u1['agent_ids']
|
||||
assert a2_id in u1['agent_ids']
|
||||
|
||||
def test_agent_permissions(client):
|
||||
headers = get_headers(client)
|
||||
|
||||
with client.application.app_context():
|
||||
u3_id = User.query.filter_by(username='u3').first().id
|
||||
a1_id = Agent.query.filter_by(name='A1').first().id
|
||||
|
||||
# Grant A1 to U3 via agent-centric API
|
||||
payload = {'user_ids': [u3_id]}
|
||||
res = client.post(f'/api/admin/agents/{a1_id}/permissions', headers=headers, json=payload)
|
||||
assert res.status_code == 200
|
||||
|
||||
# Verify
|
||||
res = client.get('/api/admin/users', headers=headers)
|
||||
users = res.get_json()
|
||||
u3 = next(u for u in users if u['username'] == 'u3')
|
||||
assert a1_id in u3['agent_ids']
|
||||
Reference in new issue
Block a user