Fix CI/CD issues and add comprehensive tests for multi-user features #6

Merged
NyxiumYuuki merged 3 commits from claude/add-mcp-authentication-01V5tbppGEtXc3tvjRGoTcfh into master 2025-11-30 01:01:37 +01:00
NyxiumYuuki commented 2025-11-30 00:13:08 +01:00 (Migrated from github.com)

This commit fixes three critical issues identified in CI/CD and adds comprehensive test coverage for the new multi-user functionality.

Fixes

1. FastMCP Middleware Registration Error

Problem: AttributeError: 'FastMCP' object has no attribute 'app'

Solution: Implemented robust middleware registration that:

  • Tries multiple possible locations where FastMCP might store the app
  • Gracefully handles cases where app isn't immediately available
  • Wraps the run() method to defer middleware addition if needed
  • Attempts: _transport.app, sse.app, http_server.app, _app, _asgi_app
  • Falls back gracefully with warning if middleware can't be added

Files Changed:

  • src/geoguessr_mcp/main.py: Added smart middleware registration logic

2. Test Permission Errors

Problem: PermissionError: [Errno 13] Permission denied: '/app' Schema registry tried to create /app/data/schemas in CI without permission

Solution: Made schema cache directory creation fault-tolerant:

  • Catches PermissionError and OSError when creating cache directory
  • Falls back to temporary directory (tempfile.mkdtemp) if permission denied
  • Logs clear warning messages about fallback behavior
  • Tests can now run in restricted environments

Files Changed:

  • src/geoguessr_mcp/monitoring/schema/schema_registry.py: Added fallback logic

3. Black Formatting Issues

Problem: 10 files needed reformatting

Solution: Ran black src/ --line-length 100 on all source files

Files Formatted:

  • src/geoguessr_mcp/config.py
  • src/geoguessr_mcp/api/dynamic_response.py
  • src/geoguessr_mcp/middleware/auth.py
  • src/geoguessr_mcp/main.py
  • src/geoguessr_mcp/auth/multi_user_session.py
  • src/geoguessr_mcp/tools/auth_tools.py
  • src/tests/integration/test_auth_flow.py
  • src/tests/unit/services/*.py (3 files)

New Tests

Added comprehensive test coverage for multi-user features:

test_user_context.py

  • Tests UserContext creation with/without sessions
  • Tests authentication status checking
  • Tests session expiration handling
  • Tests string representation
  • Tests API key hashing for anonymous users
  • Tests consistency of anonymous user IDs

test_multi_user_session.py

  • Tests MultiUserSessionManager initialization
  • Tests session manager creation per API key
  • Tests session manager reuse for same API key
  • Tests isolation between different users
  • Tests auth status reporting
  • Tests context creation and retrieval

test_request_context.py

  • Tests context variable get/set operations
  • Tests require_user_context() error handling
  • Tests context isolation between requests
  • Tests context updates and clearing
  • Tests None handling

Code Quality

All changes pass:

  • Python syntax checks (py_compile)
  • Black formatting (line-length 100)
  • Test structure validation
  • Import resolution

CI/CD Impact

These fixes should resolve:

  • Test execution failures (permission errors)
  • Black formatting check failures
  • Runtime errors when starting server with auth enabled

Tests can now run in CI environment without requiring:

  • Root permissions
  • /app directory access
  • Pre-created cache directories

Pull Request

Description

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Performance improvement
  • Test improvement

Fixes #

Changes Made

Testing Performed

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed
  • All existing tests pass

Test Details

# Example test commands
pytest tests/

Screenshots (if applicable)

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published
  • I have checked my code and corrected any misspellings

Additional Context

Breaking Changes

Performance Impact


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT License.

This commit fixes three critical issues identified in CI/CD and adds comprehensive test coverage for the new multi-user functionality. ## Fixes ### 1. FastMCP Middleware Registration Error **Problem**: `AttributeError: 'FastMCP' object has no attribute 'app'` **Solution**: Implemented robust middleware registration that: - Tries multiple possible locations where FastMCP might store the app - Gracefully handles cases where app isn't immediately available - Wraps the run() method to defer middleware addition if needed - Attempts: _transport.app, sse.app, http_server.app, _app, _asgi_app - Falls back gracefully with warning if middleware can't be added **Files Changed**: - src/geoguessr_mcp/main.py: Added smart middleware registration logic ### 2. Test Permission Errors **Problem**: `PermissionError: [Errno 13] Permission denied: '/app'` Schema registry tried to create /app/data/schemas in CI without permission **Solution**: Made schema cache directory creation fault-tolerant: - Catches PermissionError and OSError when creating cache directory - Falls back to temporary directory (tempfile.mkdtemp) if permission denied - Logs clear warning messages about fallback behavior - Tests can now run in restricted environments **Files Changed**: - src/geoguessr_mcp/monitoring/schema/schema_registry.py: Added fallback logic ### 3. Black Formatting Issues **Problem**: 10 files needed reformatting **Solution**: Ran `black src/ --line-length 100` on all source files **Files Formatted**: - src/geoguessr_mcp/config.py - src/geoguessr_mcp/api/dynamic_response.py - src/geoguessr_mcp/middleware/auth.py - src/geoguessr_mcp/main.py - src/geoguessr_mcp/auth/multi_user_session.py - src/geoguessr_mcp/tools/auth_tools.py - src/tests/integration/test_auth_flow.py - src/tests/unit/services/*.py (3 files) ## New Tests Added comprehensive test coverage for multi-user features: ### test_user_context.py - Tests UserContext creation with/without sessions - Tests authentication status checking - Tests session expiration handling - Tests string representation - Tests API key hashing for anonymous users - Tests consistency of anonymous user IDs ### test_multi_user_session.py - Tests MultiUserSessionManager initialization - Tests session manager creation per API key - Tests session manager reuse for same API key - Tests isolation between different users - Tests auth status reporting - Tests context creation and retrieval ### test_request_context.py - Tests context variable get/set operations - Tests require_user_context() error handling - Tests context isolation between requests - Tests context updates and clearing - Tests None handling ## Code Quality All changes pass: - ✅ Python syntax checks (py_compile) - ✅ Black formatting (line-length 100) - ✅ Test structure validation - ✅ Import resolution ## CI/CD Impact These fixes should resolve: - ❌ Test execution failures (permission errors) - ❌ Black formatting check failures - ❌ Runtime errors when starting server with auth enabled Tests can now run in CI environment without requiring: - Root permissions - /app directory access - Pre-created cache directories # Pull Request ## Description <!-- Provide a clear and concise description of your changes --> ## Type of Change <!-- Mark the relevant option with an 'x' --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update - [ ] Code refactoring - [ ] Performance improvement - [ ] Test improvement ## Related Issues <!-- Link related issues using keywords: Fixes #123, Closes #456, Related to #789 --> Fixes # ## Changes Made <!-- List the main changes in bullet points --> - - - ## Testing Performed <!-- Describe the testing you've done --> - [ ] Unit tests added/updated - [ ] Integration tests added/updated - [ ] Manual testing performed - [ ] All existing tests pass ### Test Details <!-- Provide details about how you tested your changes --> ```bash # Example test commands pytest tests/ ``` ## Screenshots (if applicable) <!-- Add screenshots to help explain your changes --> ## Checklist <!-- Ensure all items are completed before submitting --> - [ ] My code follows the project's style guidelines - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published - [ ] I have checked my code and corrected any misspellings ## Additional Context <!-- Add any other context about the pull request here --> ## Breaking Changes <!-- If this is a breaking change, describe the impact and migration path --> ## Performance Impact <!-- Describe any performance implications of your changes --> --- **By submitting this pull request, I confirm that my contribution is made under the terms of the MIT License.**
Sign in to join this conversation.
No description provided.