Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /CIRISAI_CIRISAgent /tests /adapters /api /conftest.py
| """ | |
| Shared fixtures for API adapter tests. | |
| High-quality centralized fixtures for comprehensive API testing including: | |
| - Complete APICommunicationService setup with all dependencies | |
| - Mock services and correlations for testing message flow | |
| - WebSocket client simulation | |
| - Time service mocking for consistent timestamps | |
| - Service correlation fixtures for testing message fetching | |
| Note: CIRIS_TESTING_MODE is set globally in tests/conftest.py | |
| """ | |
| import asyncio | |
| from datetime import datetime, timezone | |
| from typing import Any, Dict, List | |
| from unittest.mock import AsyncMock, MagicMock, Mock | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from ciris_engine.logic.adapters.api.api_communication import APICommunicationService | |
| from ciris_engine.logic.adapters.api.app import create_app | |
| from ciris_engine.logic.adapters.api.services.auth_service import APIAuthService | |
| from ciris_engine.schemas.api.auth import Permission, UserRole | |
| from ciris_engine.schemas.runtime.messages import FetchedMessage | |
| from ciris_engine.schemas.telemetry.core import ( | |
| ServiceCorrelation, | |
| ServiceCorrelationStatus, | |
| ServiceRequestData, | |
| ServiceResponseData, | |
| ) | |
| def mock_time_service(): | |
| """Create consistent mock time service.""" | |
| service = Mock() | |
| # Fixed time for consistent testing | |
| fixed_time = datetime(2025, 9, 8, 12, 0, 0, tzinfo=timezone.utc) | |
| service.now.return_value = fixed_time | |
| service.now_iso.return_value = fixed_time.isoformat() | |
| service.strftime.return_value = fixed_time.strftime("%Y%m%d_%H%M%S") | |
| return service | |
| def mock_app_state(): | |
| """Create mock app state with message tracking.""" | |
| state = Mock() | |
| state.message_channel_map = {} | |
| state.sent_messages = {} | |
| return state | |
| def mock_websocket_client(): | |
| """Create mock WebSocket client for testing.""" | |
| client = Mock() | |
| client.send_json = AsyncMock() | |
| return client | |
| async def api_communication_service(mock_time_service, mock_app_state): | |
| """Create fully configured APICommunicationService for testing.""" | |
| service = APICommunicationService() | |
| service._time_service = mock_time_service | |
| service._app_state = mock_app_state | |
| # Start the service | |
| await service.start() | |
| yield service | |
| # Clean up | |
| await service.stop() | |
| def sample_speak_correlation(mock_time_service): | |
| """Create sample 'speak' correlation for testing message fetching.""" | |
| fixed_time = mock_time_service.now() | |
| return ServiceCorrelation( | |
| correlation_id="speak-corr-123", | |
| service_type="api", | |
| handler_name="APIAdapter", | |
| action_type="speak", | |
| status=ServiceCorrelationStatus.COMPLETED, | |
| created_at=fixed_time, | |
| updated_at=fixed_time, | |
| timestamp=fixed_time, | |
| request_data=ServiceRequestData( | |
| service_type="api", | |
| method_name="speak", | |
| request_timestamp=fixed_time, | |
| channel_id="api_127.0.0.1_8080", | |
| parameters={"content": "Hello from CIRIS!", "channel_id": "api_127.0.0.1_8080"}, | |
| ), | |
| response_data=ServiceResponseData( | |
| success=True, result_summary="Message sent", execution_time_ms=15, response_timestamp=fixed_time | |
| ), | |
| ) | |
| def sample_observe_correlation(mock_time_service): | |
| """Create sample 'observe' correlation for testing message fetching.""" | |
| fixed_time = mock_time_service.now() | |
| return ServiceCorrelation( | |
| correlation_id="observe-corr-456", | |
| service_type="api", | |
| handler_name="APIAdapter", | |
| action_type="observe", | |
| status=ServiceCorrelationStatus.COMPLETED, | |
| created_at=fixed_time, | |
| updated_at=fixed_time, | |
| timestamp=fixed_time, | |
| request_data=ServiceRequestData( | |
| service_type="api", | |
| method_name="observe", | |
| request_timestamp=fixed_time, | |
| channel_id="api_127.0.0.1_8080", | |
| parameters={ | |
| "content": "Hello CIRIS!", | |
| "author_id": "user123", | |
| "author_name": "Test User", | |
| "message_id": "msg-456", | |
| }, | |
| ), | |
| response_data=ServiceResponseData( | |
| success=True, result_summary="Message observed", execution_time_ms=5, response_timestamp=fixed_time | |
| ), | |
| ) | |
| def sample_correlations(sample_speak_correlation, sample_observe_correlation): | |
| """Create list of sample correlations for comprehensive testing.""" | |
| return [sample_observe_correlation, sample_speak_correlation] | |
| def expected_fetched_messages(mock_time_service): | |
| """Create expected FetchedMessage objects for testing.""" | |
| timestamp = mock_time_service.now().isoformat() | |
| return [ | |
| # User message (observe) - now uses original message_id from parameters | |
| FetchedMessage( | |
| message_id="msg-456", | |
| author_id="user123", | |
| author_name="Test User", | |
| content="Hello CIRIS!", | |
| timestamp=timestamp, | |
| is_bot=False, | |
| ), | |
| # Agent message (speak) - uses correlation_id (no message_id in params) | |
| FetchedMessage( | |
| message_id="speak-corr-123", | |
| author_id="ciris", | |
| author_name="CIRIS", | |
| content="Hello from CIRIS!", | |
| timestamp=timestamp, | |
| is_bot=True, | |
| ), | |
| ] | |
| def mock_persistence(): | |
| """Mock persistence module for testing correlation storage/retrieval.""" | |
| with pytest.MonkeyPatch().context() as m: | |
| mock_add_correlation = Mock() | |
| mock_get_correlations = Mock() | |
| m.setattr("ciris_engine.logic.persistence.add_correlation", mock_add_correlation) | |
| m.setattr("ciris_engine.logic.persistence.get_correlations_by_channel", mock_get_correlations) | |
| yield { | |
| "add_correlation": mock_add_correlation, | |
| "get_correlations_by_channel": mock_get_correlations, | |
| } | |
| def app(): | |
| """Create FastAPI app with minimal required state. | |
| Note: CIRIS_TESTING_MODE is set by the autouse enable_testing_mode fixture. | |
| """ | |
| from tests.fixtures.auth import setup_test_admin_user | |
| app = create_app() | |
| # Initialize auth service (required for auth endpoints) | |
| app.state.auth_service = APIAuthService() | |
| # Set up test admin user for auth (tests don't go through setup wizard) | |
| setup_test_admin_user(app.state.auth_service) | |
| return app | |
| def client(app): | |
| """Create test client.""" | |
| return TestClient(app) | |
| # NOTE: auth_headers fixture is now provided by tests.fixtures.auth | |
| # which is loaded via pytest_plugins in tests/conftest.py | |
| # This ensures all tests use the dynamically generated fallback password | |
| def mock_runtime(app): | |
| """Add mock runtime to app state.""" | |
| mock_runtime = MagicMock() | |
| mock_runtime.is_running = True | |
| app.state.runtime = mock_runtime | |
| return mock_runtime | |
| def mock_auth_context(): | |
| """Create mock AuthContext for testing agent routes.""" | |
| auth = Mock(spec=AuthContext) | |
| auth.user_id = "test-user-123" | |
| auth.role = UserRole.OBSERVER | |
| auth.permissions = {Permission.VIEW_MESSAGES} | |
| auth.api_key_id = "test-key-123" | |
| auth.authenticated_at = datetime(2025, 9, 8, 12, 0, 0, tzinfo=timezone.utc) | |
| auth.has_permission = Mock(return_value=False) # Default to no permission | |
| return auth | |
| def mock_auth_context_with_send(): | |
| """Create mock AuthContext with SEND_MESSAGES permission.""" | |
| auth = Mock(spec=AuthContext) | |
| auth.user_id = "test-admin-123" | |
| auth.role = UserRole.ADMIN | |
| auth.permissions = {Permission.VIEW_MESSAGES, Permission.SEND_MESSAGES} | |
| auth.api_key_id = "test-admin-key-123" | |
| auth.authenticated_at = datetime(2025, 9, 8, 12, 0, 0, tzinfo=timezone.utc) | |
| auth.has_permission = Mock(side_effect=lambda perm: perm in {Permission.VIEW_MESSAGES, Permission.SEND_MESSAGES}) | |
| return auth | |
| def mock_oauth_user(): | |
| """Create mock OAuth user for testing permission requests.""" | |
| user = Mock() | |
| user.wa_id = "oauth-user-123" | |
| user.auth_type = "oauth" | |
| user.permission_requested_at = None | |
| return user | |
| def mock_consent_service(): | |
| """Create mock ConsentService for testing consent flows.""" | |
| service = AsyncMock() | |
| # Mock successful consent check (existing user) | |
| mock_consent = Mock() | |
| mock_consent.user_id = "test-user-123" | |
| mock_consent.stream = "TEMPORARY" | |
| service.get_consent.return_value = mock_consent | |
| # Mock consent creation for new users | |
| service.grant_consent.return_value = mock_consent | |
| return service | |
| def mock_agent_processor(): | |
| """Create mock agent processor for testing pause/resume functionality.""" | |
| processor = Mock() | |
| processor._is_paused = False | |
| processor.get_current_state.return_value = "WORK" | |
| processor.pause_processing = AsyncMock() | |
| processor.resume_processing = AsyncMock() | |
| return processor | |
| def mock_api_config(): | |
| """Create mock API configuration.""" | |
| config = Mock() | |
| # Fixture pins an explicit value so it does not silently track the | |
| # production default; aligned to it (110.0, #1013) so the two agree. | |
| config.interaction_timeout = 110.0 | |
| config.max_message_length = 4000 | |
| config.rate_limit_per_minute = 60 | |
| return config | |
| def mock_request_with_full_state(mock_runtime, mock_api_config, mock_agent_processor, mock_consent_service): | |
| """Create mock request with fully populated app state.""" | |
| request = Mock(spec=Request) | |
| request.app.state = Mock() | |
| # Add all state components | |
| request.app.state.runtime = mock_runtime | |
| request.app.state.runtime.agent_processor = mock_agent_processor | |
| request.app.state.runtime.agent_processor.state_manager = Mock() | |
| request.app.state.runtime.agent_processor.state_manager.current_state = "WORK" | |
| request.app.state.api_config = mock_api_config | |
| request.app.state.consent_manager = mock_consent_service | |
| # Mock auth service | |
| auth_service = Mock() | |
| auth_service.get_user.return_value = None | |
| auth_service._users = {} | |
| request.app.state.auth_service = auth_service | |
| # Mock message handler | |
| request.app.state.on_message = AsyncMock() | |
| return request | |
| # ============================================================================= | |
| # COGNITIVE STATE FIXTURES | |
| # ============================================================================= | |
| def mock_state_manager_work(): | |
| """Create a mock state manager in WORK state.""" | |
| state_manager = Mock() | |
| state_manager.current_state = "WORK" | |
| return state_manager | |
| def mock_state_manager_dream(): | |
| """Create a mock state manager in DREAM state.""" | |
| state_manager = Mock() | |
| state_manager.current_state = "DREAM" | |
| return state_manager | |
| def mock_agent_processor_with_state(mock_state_manager_work): | |
| """Create a mock agent processor with proper state manager structure.""" | |
| processor = Mock() | |
| processor.state_manager = mock_state_manager_work | |
| return processor | |
| def mock_runtime_with_cognitive_state(mock_agent_processor_with_state): | |
| """Create a mock runtime with proper cognitive state structure.""" | |
| runtime = Mock() | |
| runtime.agent_processor = mock_agent_processor_with_state | |
| return runtime | |
| def mock_app_state_with_cognitive_state(mock_runtime_with_cognitive_state): | |
| """Create a mock app state with cognitive state properly configured.""" | |
| app_state = Mock() | |
| app_state.runtime = mock_runtime_with_cognitive_state | |
| return app_state | |
| def mock_request_with_cognitive_state(mock_app_state_with_cognitive_state): | |
| """Create a mock request with proper cognitive state structure.""" | |
| request = Mock() | |
| request.app = Mock() | |
| request.app.state = mock_app_state_with_cognitive_state | |
| return request | |
| # ============================================================================= | |
| # AUTH OVERRIDE FIXTURES - Use FastAPI dependency override system | |
| # ============================================================================= | |
| async def _get_admin_user(): | |
| """Mock dependency that returns admin user.""" | |
| from ciris_engine.logic.adapters.api.models import TokenData | |
| return TokenData(username="admin", email="admin@ciris.ai", role="ADMIN") | |
| async def _get_regular_user(): | |
| """Mock dependency that returns regular user.""" | |
| from ciris_engine.logic.adapters.api.models import TokenData | |
| return TokenData(username="user", email="user@example.com", role="USER") | |
| async def _get_system_admin_user(): | |
| """Mock dependency that returns system admin user.""" | |
| from ciris_engine.logic.adapters.api.models import TokenData | |
| return TokenData(username="sysadmin", email="sysadmin@ciris.ai", role="SYSTEM_ADMIN") | |
| def client_with_admin_auth(app): | |
| """Create test client with admin auth override.""" | |
| from ciris_engine.logic.adapters.api.auth import get_current_user | |
| app.dependency_overrides[get_current_user] = _get_admin_user | |
| client = TestClient(app) | |
| yield client | |
| # Clean up override | |
| app.dependency_overrides.clear() | |
| def client_with_user_auth(app): | |
| """Create test client with regular user auth override.""" | |
| from ciris_engine.logic.adapters.api.auth import get_current_user | |
| app.dependency_overrides[get_current_user] = _get_regular_user | |
| client = TestClient(app) | |
| yield client | |
| # Clean up override | |
| app.dependency_overrides.clear() | |
| def client_with_sysadmin_auth(app): | |
| """Create test client with system admin auth override.""" | |
| from ciris_engine.logic.adapters.api.auth import get_current_user | |
| app.dependency_overrides[get_current_user] = _get_system_admin_user | |
| client = TestClient(app) | |
| yield client | |
| # Clean up override | |
| app.dependency_overrides.clear() | |
Xet Storage Details
- Size:
- 14.1 kB
- Xet hash:
- 9938d234f44a0569b98476004eec6f9682eb60664b2ed157b400c01058757d42
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.