150 lines
4.7 KiB
Python
150 lines
4.7 KiB
Python
import pytest
|
|
from app import create_app, db
|
|
from app.models import User, Agent, Department, PermissionRequest
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
app = create_app()
|
|
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('admin')
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
yield client
|
|
db.session.remove()
|
|
db.drop_all()
|
|
|
|
def get_headers(client, username='admin', password='admin'):
|
|
res = client.post('/api/user/login', json={'username': username, 'password': password})
|
|
data = res.get_json()
|
|
return {
|
|
'X-User-Id': str(data['id']),
|
|
'X-Username': data['username'],
|
|
'X-Role': data['role']
|
|
}
|
|
|
|
def test_department_crud(client):
|
|
headers = get_headers(client)
|
|
|
|
# Create
|
|
res = client.post('/api/admin/departments', json={
|
|
'name': 'IT Dept',
|
|
'code': 'IT001',
|
|
'company': 'Tech Corp',
|
|
'description': 'Information Technology'
|
|
}, headers=headers)
|
|
assert res.status_code == 201
|
|
dept_id = res.get_json()['id']
|
|
|
|
# List
|
|
res = client.get('/api/admin/departments', headers=headers)
|
|
assert res.status_code == 200
|
|
depts = res.get_json()
|
|
assert len(depts) == 1
|
|
assert depts[0]['code'] == 'IT001'
|
|
assert depts[0]['company'] == 'Tech Corp'
|
|
|
|
# Update
|
|
res = client.put(f'/api/admin/departments/{dept_id}', json={
|
|
'name': 'IT Department',
|
|
'code': 'IT002'
|
|
}, headers=headers)
|
|
assert res.status_code == 200
|
|
|
|
# Verify Update
|
|
res = client.get('/api/admin/departments', headers=headers)
|
|
depts = res.get_json()
|
|
assert depts[0]['name'] == 'IT Department'
|
|
assert depts[0]['code'] == 'IT002'
|
|
|
|
# Delete
|
|
res = client.delete(f'/api/admin/departments/{dept_id}', headers=headers)
|
|
assert res.status_code == 200
|
|
res = client.get('/api/admin/departments', headers=headers)
|
|
assert len(res.get_json()) == 0
|
|
|
|
def test_agent_crud(client):
|
|
headers = get_headers(client)
|
|
|
|
# Create
|
|
res = client.post('/api/admin/agents', json={
|
|
'name': 'Test Agent',
|
|
'dify_app_id': 'app-123',
|
|
'description': 'Test Description'
|
|
}, headers=headers)
|
|
assert res.status_code == 201
|
|
agent_id = res.get_json()['id']
|
|
|
|
# List
|
|
res = client.get('/api/admin/agents', headers=headers)
|
|
assert res.status_code == 200
|
|
agents = res.get_json()
|
|
assert len(agents) == 1
|
|
assert agents[0]['name'] == 'Test Agent'
|
|
|
|
# Update
|
|
res = client.put(f'/api/admin/agents/{agent_id}', json={
|
|
'name': 'Updated Agent'
|
|
}, headers=headers)
|
|
assert res.status_code == 200
|
|
|
|
# Verify Update
|
|
res = client.get('/api/admin/agents', headers=headers)
|
|
assert res.get_json()[0]['name'] == 'Updated Agent'
|
|
|
|
# Delete
|
|
res = client.delete(f'/api/admin/agents/{agent_id}', headers=headers)
|
|
assert res.status_code == 200
|
|
res = client.get('/api/admin/agents', headers=headers)
|
|
assert len(res.get_json()) == 0
|
|
|
|
def test_permission_flow(client):
|
|
admin_headers = get_headers(client)
|
|
|
|
# Create Agent
|
|
client.post('/api/admin/agents', json={
|
|
'name': 'Test Agent',
|
|
'dify_app_id': 'app-123'
|
|
}, headers=admin_headers)
|
|
agent_id = 1
|
|
|
|
# Create User
|
|
client.post('/api/user/register', json={
|
|
'username': 'user1', 'password': 'password', 'email': 'u1@example.com',
|
|
'real_name': 'User One', 'employee_id': 'U001', 'phone_number': '1234567890'
|
|
})
|
|
|
|
# Approve User
|
|
client.post('/api/admin/users/2/approve', headers=admin_headers) # ID 2 because admin is 1
|
|
|
|
# User Login
|
|
user_headers = get_headers(client, 'user1', 'password')
|
|
|
|
# Request Permission
|
|
res = client.post('/api/user/permission-requests', json={
|
|
'agent_id': agent_id,
|
|
'reason': 'I need it'
|
|
}, headers=user_headers)
|
|
assert res.status_code == 201
|
|
req_id = res.get_json()['id']
|
|
|
|
# Admin List Requests
|
|
res = client.get('/api/admin/permission-requests', headers=admin_headers)
|
|
assert len(res.get_json()) == 1
|
|
|
|
# Admin Approve
|
|
res = client.post(f'/api/admin/permission-requests/{req_id}/approve', headers=admin_headers)
|
|
assert res.status_code == 200
|
|
|
|
# User Check Home (should have agent now)
|
|
res = client.get('/api/user/home', headers=user_headers)
|
|
data = res.get_json()
|
|
assert len(data['agents']) == 1
|
|
assert data['agents'][0]['id'] == agent_id
|