import pytest import json from app.models import User, Agent # ----------------------------------------------------------------------------- # Test File: backend/tests/test_comprehensive.py # Purpose: Comprehensive automated tests for backend functionalities including # User Management, Agent Management, and Permissions. # References: backend/app/api/admin/, backend/app/api/user.py, backend/app/models.py # Logic: # 1. Test User Registration (with new fields) # 2. Test Admin User Approval # 3. Test Admin User Rejection # 4. Test Agent CRUD (Create, Update, Delete) # 5. Test Permission Assignment (User-Agent linkage) # ----------------------------------------------------------------------------- @pytest.fixture def auth_headers(client): """ Helper fixture to get auth headers for a standard user. Note: In real app, we might need a token, but here we use mock headers from conftest or simulate login if needed. For admin, we use admin_headers fixture. """ return { 'Content-Type': 'application/json' } def test_user_registration_flow(client): """ Test the user registration process with all new fields. Steps: 1. Register a new user with phone_number, real_name, employee_id. 2. Verify successful response. 3. Verify data in database. 4. Try to register with duplicate phone_number/employee_id and verify failure. """ # 1. Register a new user payload = { 'username': 'testuser_new', 'password': 'password123', 'email': 'testuser_new@example.com', 'real_name': 'Test User Real', 'employee_id': 'EMP001', 'phone_number': '13800138000' } # Call /api/user/register (Assuming this is the public registration endpoint) # If not, we might use /api/admin/users for admin creation. # Let's check api/user.py for register endpoint, usually it is /api/user/register response = client.post('/api/user/register', json=payload) # 2. Verify response assert response.status_code == 201 or response.status_code == 200 data = response.get_json() assert 'id' in data or 'status' in data # 3. Verify database user = User.query.filter_by(username='testuser_new').first() assert user is not None assert user.real_name == 'Test User Real' assert user.employee_id == 'EMP001' assert user.phone_number == '13800138000' assert user.status == 'pending' # Default status for registration # 4. Duplicate checks # Duplicate Phone payload_dup_phone = payload.copy() payload_dup_phone['username'] = 'testuser_dup_phone' payload_dup_phone['email'] = 'dup_phone@example.com' payload_dup_phone['employee_id'] = 'EMP002' # Different ID # Same phone '13800138000' response = client.post('/api/user/register', json=payload_dup_phone) assert response.status_code == 400 assert 'Phone number already exists' in response.get_json()['error'] # Duplicate Employee ID payload_dup_emp = payload.copy() payload_dup_emp['username'] = 'testuser_dup_emp' payload_dup_emp['email'] = 'dup_emp@example.com' payload_dup_emp['phone_number'] = '13900139000' # Different phone # Same employee_id 'EMP001' response = client.post('/api/user/register', json=payload_dup_emp) assert response.status_code == 400 assert 'Employee ID already exists' in response.get_json()['error'] def test_admin_approve_reject_user(client, admin_headers): """ Test Admin approval and rejection of users. Steps: 1. Create two pending users (via register endpoint or direct DB). 2. Admin approves User A. Verify status 'active'. 3. Admin rejects User B. Verify status 'rejected'. """ # 1. Create users # We can use the register endpoint or admin create endpoint user_a = User(username='user_a', email='a@test.com', status='pending', role='user') user_a.set_password('123') user_b = User(username='user_b', email='b@test.com', status='pending', role='user') user_b.set_password('123') # Need to access db session. conftest app fixture handles db creation/drop # but we need to import db from app to add from app import db db.session.add(user_a) db.session.add(user_b) db.session.commit() # 2. Approve User A response = client.post(f'/api/admin/users/{user_a.id}/approve', headers=admin_headers) assert response.status_code == 200 # Refresh from DB db.session.refresh(user_a) assert user_a.status == 'active' # 3. Reject User B response = client.post(f'/api/admin/users/{user_b.id}/reject', headers=admin_headers) assert response.status_code == 200 db.session.refresh(user_b) assert user_b.status == 'rejected' def test_agent_management(client, admin_headers): """ Test CRUD operations for Agents. Steps: 1. Admin creates a new Agent. 2. Admin updates the Agent. 3. Admin deletes the Agent. """ from app import db # 1. Create Agent payload = { 'name': 'Test Agent', 'dify_app_id': 'app-uuid-123', 'dify_api_key': 'key-secret-123', 'description': 'A test agent', 'type': 'api' } response = client.post('/api/admin/agents', json=payload, headers=admin_headers) assert response.status_code == 201 data = response.get_json() agent_id = data['id'] # Verify DB agent = Agent.query.get(agent_id) assert agent.name == 'Test Agent' # 2. Update Agent update_payload = { 'name': 'Updated Agent Name', 'description': 'Updated description' } response = client.put(f'/api/admin/agents/{agent_id}', json=update_payload, headers=admin_headers) assert response.status_code == 200 db.session.refresh(agent) assert agent.name == 'Updated Agent Name' assert agent.description == 'Updated description' # 3. Delete Agent response = client.delete(f'/api/admin/agents/{agent_id}', headers=admin_headers) assert response.status_code == 200 agent = Agent.query.get(agent_id) assert agent is None def test_permission_management(client, admin_headers): """ Test assigning Agents to Users (Permissions). Steps: 1. Create a User and an Agent. 2. Verify User initially has no agents. 3. Admin updates User to assign the Agent. 4. Verify User has the Agent. 5. Admin removes the Agent. 6. Verify User has no agents. """ from app import db # 1. Setup Data user = User(username='perm_user', email='perm@test.com', role='user', status='active') user.set_password('123') agent = Agent(name='Perm Agent', dify_app_id='app-perm', dify_api_key='key-perm') db.session.add(user) db.session.add(agent) db.session.commit() user_id = user.id agent_id = agent.id # 2. Verify initial state assert len(user.agents) == 0 # 3. Assign Agent # The update_user endpoint accepts 'agent_ids' list payload = { 'agent_ids': [agent_id] } response = client.put(f'/api/admin/users/{user_id}', json=payload, headers=admin_headers) assert response.status_code == 200 # 4. Verify assignment # Need to expire/refresh to see changes in relationship db.session.expire(user) # Or query again user = User.query.get(user_id) assert len(user.agents) == 1 assert user.agents[0].id == agent_id # 5. Remove Agent (assign empty list) payload = { 'agent_ids': [] } response = client.put(f'/api/admin/users/{user_id}', json=payload, headers=admin_headers) assert response.status_code == 200 # 6. Verify removal db.session.expire(user) user = User.query.get(user_id) assert len(user.agents) == 0