feat: Initial commit with full deployment architecture and scripts
This commit is contained in:
commit
abb3efe114
96 files changed
+16789
No files matched your search
@@ -0,0 +1,194 @@
|
||||
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"
|
||||
}
|
||||
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_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/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
|
||||
Reference in new issue
Block a user