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:
@@ -14,15 +14,25 @@ class TestIntegration:
|
||||
"""Test complete flow: login -> create task -> get task -> delete task"""
|
||||
|
||||
# Step 1: Login
|
||||
from app.services.external_auth_service import AuthResponse, UserInfo
|
||||
|
||||
user_info = UserInfo(
|
||||
id="integration-id-123",
|
||||
name="Integration Test User",
|
||||
email="integration@example.com"
|
||||
)
|
||||
auth_response = AuthResponse(
|
||||
access_token="test-token",
|
||||
id_token="test-id-token",
|
||||
expires_in=3600,
|
||||
token_type="Bearer",
|
||||
user_info=user_info,
|
||||
issued_at="2025-11-16T10:00:00Z",
|
||||
expires_at="2025-11-16T11:00:00Z"
|
||||
)
|
||||
|
||||
with patch('app.routers.auth.external_auth_service.authenticate_user') as mock_auth:
|
||||
mock_auth.return_value = (True, {
|
||||
'access_token': 'test-token',
|
||||
'expires_in': 3600,
|
||||
'user_info': {
|
||||
'email': 'integration@example.com',
|
||||
'name': 'Integration Test User'
|
||||
}
|
||||
}, None)
|
||||
mock_auth.return_value = (True, auth_response, None)
|
||||
|
||||
login_response = client.post('/api/v2/auth/login', json={
|
||||
'username': 'integration@example.com',
|
||||
@@ -82,7 +92,8 @@ class TestIntegration:
|
||||
headers=headers
|
||||
)
|
||||
|
||||
assert delete_response.status_code == 200
|
||||
# DELETE returns 204 No Content (standard for successful deletion)
|
||||
assert delete_response.status_code == 204
|
||||
|
||||
# Step 7: Verify deletion
|
||||
get_after_delete = client.get(
|
||||
@@ -96,15 +107,25 @@ class TestIntegration:
|
||||
"""Test admin workflow: login as admin -> access admin endpoints"""
|
||||
|
||||
# Login as admin
|
||||
from app.services.external_auth_service import AuthResponse, UserInfo
|
||||
|
||||
user_info = UserInfo(
|
||||
id="admin-id-123",
|
||||
name="Admin User",
|
||||
email="ymirliu@panjit.com.tw"
|
||||
)
|
||||
auth_response = AuthResponse(
|
||||
access_token="admin-token",
|
||||
id_token="admin-id-token",
|
||||
expires_in=3600,
|
||||
token_type="Bearer",
|
||||
user_info=user_info,
|
||||
issued_at="2025-11-16T10:00:00Z",
|
||||
expires_at="2025-11-16T11:00:00Z"
|
||||
)
|
||||
|
||||
with patch('app.routers.auth.external_auth_service.authenticate_user') as mock_auth:
|
||||
mock_auth.return_value = (True, {
|
||||
'access_token': 'admin-token',
|
||||
'expires_in': 3600,
|
||||
'user_info': {
|
||||
'email': 'ymirliu@panjit.com.tw',
|
||||
'name': 'Admin User'
|
||||
}
|
||||
}, None)
|
||||
mock_auth.return_value = (True, auth_response, None)
|
||||
|
||||
login_response = client.post('/api/v2/auth/login', json={
|
||||
'username': 'ymirliu@panjit.com.tw',
|
||||
|
||||
Reference in New Issue
Block a user