Root Cause Fixed:
- Tests were connecting to production MySQL database instead of test database
- Solution: Monkey patch database module before importing app to use SQLite :memory:
Changes:
1. **conftest.py** - Critical Fix:
- Added database module monkey patch BEFORE app import
- Prevents connection to production database (db_A060)
- All tests now use isolated SQLite :memory: database
- Fixed fixture dependency order (test_task depends on test_user)
2. **test_tasks.py**:
- Fixed test_delete_task: Accept 204 No Content (correct HTTP status)
3. **test_admin.py**:
- Fixed test_get_system_stats: Update assertions to match nested API response structure
- API returns {users: {total}, tasks: {total}} not flat structure
4. **test_integration.py**:
- Fixed mock structure: Use Pydantic models (AuthResponse, UserInfo) instead of dicts
- Fixed test_complete_auth_and_task_flow: Accept 204 for DELETE
Test Results:
✅ test_auth.py: 5/5 passing (100%)
✅ test_tasks.py: 6/6 passing (100%)
✅ test_admin.py: 4/4 passing (100%)
✅ test_integration.py: 3/3 passing (100%)
Total: 18/18 tests passing (100%) ⬆️ from 11/18 (61%)
Security Note:
- Tests no longer access production database
- All test data is isolated in :memory: SQLite
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""
|
|
Unit tests for admin endpoints
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
class TestAdmin:
|
|
"""Test admin endpoints"""
|
|
|
|
def test_get_system_stats(self, client, admin_token):
|
|
"""Test get system statistics"""
|
|
response = client.get(
|
|
'/api/v2/admin/stats',
|
|
headers={'Authorization': f'Bearer {admin_token}'}
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
# API returns nested structure
|
|
assert 'users' in data
|
|
assert 'tasks' in data
|
|
assert 'sessions' in data
|
|
assert 'activity' in data
|
|
assert 'total' in data['users']
|
|
assert 'total' in data['tasks']
|
|
|
|
def test_get_system_stats_non_admin(self, client, auth_token):
|
|
"""Test that non-admin cannot access admin endpoints"""
|
|
response = client.get(
|
|
'/api/v2/admin/stats',
|
|
headers={'Authorization': f'Bearer {auth_token}'}
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
|
|
def test_list_users(self, client, admin_token):
|
|
"""Test list all users"""
|
|
response = client.get(
|
|
'/api/v2/admin/users',
|
|
headers={'Authorization': f'Bearer {admin_token}'}
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert 'users' in data
|
|
assert 'total' in data
|
|
|
|
def test_get_audit_logs(self, client, admin_token):
|
|
"""Test get audit logs"""
|
|
response = client.get(
|
|
'/api/v2/admin/audit-logs',
|
|
headers={'Authorization': f'Bearer {admin_token}'}
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert 'logs' in data
|
|
assert 'total' in data
|
|
assert 'page' in data
|