239 lines
8.8 KiB
Python
239 lines
8.8 KiB
Python
import pytest
|
|
from app.models import Agent, User, PermissionRequest
|
|
from app import db
|
|
|
|
def test_agent_lifecycle(client, admin_headers):
|
|
"""
|
|
Test complete lifecycle of an Agent: Create -> Read -> Update -> Delete
|
|
"""
|
|
# 1. Create Agent
|
|
new_agent = {
|
|
"name": "Test Agent",
|
|
"description": "Initial Description",
|
|
"type": "api",
|
|
"dify_app_id": "app-123",
|
|
"dify_api_key": "key-123",
|
|
"is_active": True
|
|
}
|
|
response = client.post('/api/admin/agents', json=new_agent, headers=admin_headers)
|
|
assert response.status_code == 201
|
|
agent_id = response.json['id']
|
|
|
|
# 2. Read Agent List
|
|
response = client.get('/api/admin/agents', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
agents = response.json
|
|
assert len(agents) == 1
|
|
assert agents[0]['name'] == "Test Agent"
|
|
assert agents[0]['description'] == "Initial Description"
|
|
|
|
# 3. Update Agent
|
|
updated_data = {
|
|
"name": "Updated Agent",
|
|
"description": "Updated Description"
|
|
}
|
|
response = client.put(f'/api/admin/agents/{agent_id}', json=updated_data, headers=admin_headers)
|
|
assert response.status_code == 200
|
|
|
|
# Verify Update
|
|
response = client.get('/api/admin/agents', headers=admin_headers)
|
|
agents = response.json
|
|
assert agents[0]['name'] == "Updated Agent"
|
|
assert agents[0]['description'] == "Updated Description"
|
|
|
|
# 4. Delete Agent
|
|
response = client.delete(f'/api/admin/agents/{agent_id}', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
|
|
# Verify Deletion
|
|
response = client.get('/api/admin/agents', headers=admin_headers)
|
|
assert len(response.json) == 0
|
|
|
|
def test_user_management(client, admin_headers):
|
|
"""
|
|
Test user creation, approval, and permission assignment
|
|
"""
|
|
# 1. Create User
|
|
new_user = {
|
|
"username": "newuser",
|
|
"email": "new@example.com",
|
|
"role": "user",
|
|
"password": "password123",
|
|
"real_name": "Test User",
|
|
"employee_id": "EMP001",
|
|
"phone_number": "13800000001"
|
|
}
|
|
response = client.post('/api/admin/users', json=new_user, headers=admin_headers)
|
|
assert response.status_code == 201
|
|
user_id = response.json['id']
|
|
|
|
# 2. Approve User
|
|
response = client.post(f'/api/admin/users/{user_id}/approve', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
|
|
# Verify Status and Fields
|
|
response = client.get('/api/admin/users', headers=admin_headers)
|
|
users = response.json
|
|
target_user = next(u for u in users if u['id'] == user_id)
|
|
assert target_user['status'] == 'active'
|
|
assert target_user['real_name'] == "Test User"
|
|
assert target_user['employee_id'] == "EMP001"
|
|
|
|
# 3. Assign Permissions (needs an Agent first)
|
|
agent_res = client.post('/api/admin/agents', json={
|
|
"name": "Permission Agent",
|
|
"type": "api",
|
|
"dify_app_id": "p1",
|
|
"dify_api_key": "k1"
|
|
}, headers=admin_headers)
|
|
agent_id = agent_res.json['id']
|
|
|
|
response = client.post(f'/api/admin/users/{user_id}/permissions', json={
|
|
"agent_ids": [agent_id]
|
|
}, headers=admin_headers)
|
|
assert response.status_code == 200
|
|
|
|
# Verify Permissions
|
|
response = client.get('/api/admin/users', headers=admin_headers)
|
|
target_user = next(u for u in response.json if u['id'] == user_id)
|
|
assert agent_id in target_user['agent_ids']
|
|
|
|
# 4. Delete User (New Endpoint)
|
|
response = client.delete(f'/api/admin/users/{user_id}', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
|
|
# Verify Deletion
|
|
response = client.get('/api/admin/users', headers=admin_headers)
|
|
users = response.json
|
|
assert not any(u['id'] == user_id for u in users)
|
|
|
|
def test_user_list_pagination(client, admin_headers):
|
|
"""分页用户列表:带 page 时返回 items/total,并支持 hide_rejected 筛选。"""
|
|
for idx in range(3):
|
|
client.post('/api/admin/users', json={
|
|
'username': f'pageuser{idx}',
|
|
'email': f'pageuser{idx}@example.com',
|
|
'role': 'user',
|
|
'password': 'password123',
|
|
'real_name': f'Page User {idx}',
|
|
'employee_id': f'PAGE{idx:03d}',
|
|
'phone_number': f'1390000000{idx}',
|
|
'status': 'rejected' if idx == 2 else 'active',
|
|
}, headers=admin_headers)
|
|
|
|
response = client.get('/api/admin/users?page=1&page_size=2&hide_rejected=true', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
data = response.json
|
|
assert set(data.keys()) == {'items', 'total', 'page', 'page_size', 'pages'}
|
|
assert data['page'] == 1
|
|
assert data['page_size'] == 2
|
|
assert len(data['items']) == 2
|
|
assert all(item['status'] != 'rejected' for item in data['items'])
|
|
|
|
legacy = client.get('/api/admin/users', headers=admin_headers)
|
|
assert legacy.status_code == 200
|
|
assert isinstance(legacy.json, list)
|
|
|
|
def test_agent_update_validation(client, admin_headers):
|
|
"""
|
|
Test validation when updating agents (especially API mode constraints)
|
|
"""
|
|
# Create Agent
|
|
new_agent = {
|
|
"name": "Validation Agent",
|
|
"type": "api",
|
|
"dify_app_id": "v-app-1",
|
|
"dify_api_key": "v-key-1",
|
|
"is_active": True
|
|
}
|
|
response = client.post('/api/admin/agents', json=new_agent, headers=admin_headers)
|
|
agent_id = response.json['id']
|
|
|
|
# 1. Update with Valid Data
|
|
response = client.put(f'/api/admin/agents/{agent_id}', json={"dify_api_key": "new-key"}, headers=admin_headers)
|
|
assert response.status_code == 200
|
|
|
|
# 2. Try to Clear Key for API Agent (Should Fail)
|
|
response = client.put(f'/api/admin/agents/{agent_id}', json={"dify_api_key": ""}, headers=admin_headers)
|
|
assert response.status_code == 400
|
|
assert "API mode requires Dify API Key" in response.json['error']
|
|
|
|
# 3. Try to Clear App ID for API Agent (Should Fail)
|
|
response = client.put(f'/api/admin/agents/{agent_id}', json={"dify_app_id": ""}, headers=admin_headers)
|
|
assert response.status_code == 400
|
|
assert "API mode requires Dify App ID" in response.json['error']
|
|
|
|
def test_permission_requests(client, app, admin_headers):
|
|
"""
|
|
Test permission request approval/rejection flow
|
|
"""
|
|
# Setup: Create User, Agent, and Request manually
|
|
with app.app_context():
|
|
user = User(username="requester", email="req@example.com", role="user")
|
|
agent = Agent(name="Requested Agent", type="api", dify_app_id="r1", dify_api_key="k1", is_active=True)
|
|
db.session.add(user)
|
|
db.session.add(agent)
|
|
db.session.commit()
|
|
|
|
req = PermissionRequest(user_id=user.id, agent_id=agent.id, reason="Need access", status="pending")
|
|
db.session.add(req)
|
|
db.session.commit()
|
|
req_id = req.id
|
|
user_id = user.id
|
|
agent_id = agent.id
|
|
|
|
# 1. List Requests
|
|
response = client.get('/api/admin/permission-requests', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
requests = response.json
|
|
assert len(requests) == 1
|
|
assert requests[0]['reason'] == "Need access"
|
|
|
|
# 2. Approve Request
|
|
response = client.post(f'/api/admin/permission-requests/{req_id}/approve', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
|
|
# Verify Request Status
|
|
response = client.get('/api/admin/permission-requests', headers=admin_headers)
|
|
requests = response.json
|
|
assert requests[0]['status'] == 'approved'
|
|
|
|
# Verify User Permission Updated
|
|
response = client.get('/api/admin/users', headers=admin_headers)
|
|
users = response.json
|
|
target_user = next(u for u in users if u['id'] == user_id)
|
|
assert agent_id in target_user['agent_ids']
|
|
|
|
def test_reports(client, admin_headers):
|
|
"""
|
|
Test report endpoints
|
|
"""
|
|
response = client.get('/api/admin/dashboard/summary', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
data = response.json
|
|
assert set(data.keys()) == {'agent_count', 'user_count', 'total_conversations', 'avg_score'}
|
|
assert isinstance(data['agent_count'], int)
|
|
assert isinstance(data['user_count'], int)
|
|
assert isinstance(data['total_conversations'], int)
|
|
assert isinstance(data['avg_score'], (int, float))
|
|
|
|
response = client.get('/api/admin/dashboard/badges', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
badges = response.json
|
|
assert set(badges.keys()) == {'pending_approvals', 'pending_requests'}
|
|
assert isinstance(badges['pending_approvals'], int)
|
|
assert isinstance(badges['pending_requests'], int)
|
|
|
|
response = client.get('/api/admin/reports/usage', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
data = response.json
|
|
assert 'total' in data
|
|
assert 'by_user' in data
|
|
assert 'by_agent' in data
|
|
|
|
response = client.get('/api/admin/reports/feedback', headers=admin_headers)
|
|
assert response.status_code == 200
|
|
data = response.json
|
|
assert 'avg_score' in data
|
|
assert 'distribution' in data
|