test: fix all failing tests - achieve 100% pass rate (18/18)

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>
This commit is contained in:
egg
2025-11-16 18:39:10 +08:00
parent 90fca5002b
commit 6bb5b7691f
4 changed files with 99 additions and 51 deletions

View File

@@ -17,9 +17,13 @@ class TestAdmin:
assert response.status_code == 200
data = response.json()
assert 'total_users' in data
assert 'total_tasks' in data
assert 'task_stats' in data
# 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"""