import pytest from app import create_app, db from app.models import User, Agent, Department, PermissionRequest @pytest.fixture def client(): # 必须在扩展初始化前选择 testing 配置;create_app() 默认会绑定开发 PostgreSQL, # 之后再覆盖 URI 不会迁移已绑定的 engine,容易把夹具写入共享库并污染全量测试。 app = create_app('testing') 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 { 'Authorization': f"Bearer {data['access_token']}", 'Content-Type': 'application/json', } 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): from tests.conftest import parse_api_data headers = get_headers(client) # Create res = client.post('/api/admin/agents', json={ 'name': 'Test Agent', 'dify_app_id': 'app-123', 'dify_api_key': 'sk-test-key', 'description': 'Test Description' }, headers=headers) assert res.status_code == 201 agent_id = parse_api_data(res.get_json())['id'] # List res = client.get('/api/admin/agents', headers=headers) assert res.status_code == 200 agents = parse_api_data(res.get_json()) assert len(agents) == 1 assert agents[0]['name'] == 'Test Agent' assert agents[0]['dify_api_key'] == 'sk-test-key' # 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 parse_api_data(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(parse_api_data(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' , 'dify_api_key': 'test-key' }, 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') # 当前后端只保留管理员审批 API;测试直接创建待审事实,避免依赖已移除的用户申请路由。 with client.application.app_context(): request_row = PermissionRequest(user_id=2, agent_id=agent_id, reason='I need it', status='pending') db.session.add(request_row) db.session.commit() req_id = request_row.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