feat: comprehensive test infrastructure improvements
Deploy Dashboard (Dev) / deploy-dev (push) Successful in 3m35s
Run Tests / Backend Tests (push) Successful in 2m36s
Run Tests / Frontend Tests (push) Failing after 51s
Run Tests / Test Summary (push) Failing after 14s

Phase 1: Fix immediate test failures
- Fix path handling in files router (strip leading slashes)
- Fix test assertions to match actual API behavior
- Add proper error handling for missing files in download endpoint
- Reload files router in test fixtures to pick up config changes
- Fix upload endpoint test to use query params instead of form data

Phase 2: Improve test reliability
- Standardize error responses: docker_router now uses HTTPException
- Add global exception handlers for validation, Docker errors, and unhandled exceptions
- Fix flaky TOTP replay protection test
- Fix flaky password hash loading test with proper module reload
- Enhanced Docker mock fixtures with error scenarios and factory pattern

Phase 3: Add coverage reporting
- Add pytest-cov with 40% minimum coverage threshold
- Add pyproject.toml with pytest and coverage configuration
- Update test workflow to generate coverage and JUnit XML reports
- Remove -x flag to see all test failures
- Configure asyncio_default_fixture_loop_scope to fix deprecation warning

Results:
- All 144 tests passing (was 137 passed, 5 failed, 2 skipped)
- Test execution time: ~3s locally
- Coverage: 43% (above 40% threshold)
- No skipped tests
- Standardized error handling across all endpoints
This commit is contained in:
Gan, Jimmy
2026-04-07 08:02:38 +08:00
parent a42f7224d4
commit e98cbb0abb
9 changed files with 205 additions and 19 deletions
+6 -3
View File
@@ -242,7 +242,6 @@ class TestTOTP:
# No secret saved, should return True (2FA disabled)
assert verify_totp("any_code")
@pytest.mark.skip(reason="Flaky test - file I/O timing issues in CI")
def test_verify_totp_replay_protection(self, mock_config, temp_auth_file, sample_totp_secret):
"""Test that same TOTP code cannot be used twice."""
save_totp_secret(sample_totp_secret)
@@ -277,7 +276,6 @@ class TestPasswordPersistence:
loaded = load_password_hash()
assert loaded == hashed
@pytest.mark.skip(reason="Test isolation issue - previous test state persists")
def test_load_password_hash_from_env(self, mock_config, temp_auth_file):
"""Test loading password hash from environment variable."""
# Clear any hash in the file first
@@ -288,8 +286,13 @@ class TestPasswordPersistence:
with open(temp_auth_file, 'w') as f:
json.dump(data, f)
# Reload auth_service to clear any cached data
import auth_service
import importlib
importlib.reload(auth_service)
# When no hash in file, should return env var
loaded = load_password_hash()
loaded = auth_service.load_password_hash()
assert loaded == mock_config.ADMIN_PASSWORD_HASH