Buckets:

download
raw
22.2 kB
"""
Critical Path Tests for GRID System.
These tests verify mission-critical functionality that MUST pass for the system
to be considered operational. They cover:
- Database connectivity and basic operations
- Authentication flows (token creation, verification, revocation)
- API health endpoints
- Agentic system case processing
- Event bus publish/subscribe
- RAG engine basic query
- Security guard validation
These tests are intentionally fast, isolated, and deterministic.
"""
import os
import sys
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock
import pytest
import pytest_asyncio
# Ensure src is in path
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))
# =============================================================================
# MARKER: All tests in this file are critical
# =============================================================================
pytestmark = pytest.mark.critical
# =============================================================================
# Test Group 1: Database Connectivity
# =============================================================================
class TestDatabaseConnectivity:
"""Test database connection and basic operations."""
def test_sqlite_in_memory_connection(self) -> None:
"""Test that SQLite in-memory database can be created and used."""
from sqlalchemy import create_engine, text
from sqlalchemy.orm import Session
engine = create_engine("sqlite:///:memory:")
with Session(engine) as session:
result = session.execute(text("SELECT 1"))
assert result.scalar() == 1
def test_database_url_from_environment(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that database URL is correctly read from environment."""
monkeypatch.setenv("MOTHERSHIP_DATABASE_URL", "sqlite:///:memory:")
from application.mothership.db.engine import get_database_url
url = get_database_url()
assert "sqlite" in url
def test_async_engine_creation(self) -> None:
"""Test async engine can be created for testing."""
pytest.importorskip("aiosqlite", exc_type=ImportError)
from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
assert engine is not None
assert str(engine.url) == "sqlite+aiosqlite:///:memory:"
# =============================================================================
# Test Group 2: Authentication
# =============================================================================
class TestAuthenticationTokens:
"""Test token creation, verification, and revocation."""
@pytest.fixture
def token_manager(self):
"""Create a token manager for testing."""
from grid.auth.token_manager import TokenManager
from grid.config.runtime_settings import RuntimeSettings
# Create mock settings
settings = MagicMock(spec=RuntimeSettings)
settings.security = MagicMock()
settings.security.secret_key = "test-secret-key-at-least-32-characters-long-for-security"
settings.security.algorithm = "HS256"
settings.security.access_token_expire_minutes = 30
settings.security.refresh_token_expire_days = 7
settings.cache = MagicMock()
settings.cache.backend = "memory"
settings.environment = "test"
return TokenManager(settings=settings)
def test_create_access_token(self, token_manager) -> None:
"""Test access token creation."""
token = token_manager.create_access_token(
data={"sub": "test-user-123", "roles": ["user"], "tenant": "test-tenant"},
)
assert token is not None
assert isinstance(token, str)
assert len(token) > 50 # JWT tokens are long
@pytest.mark.asyncio
async def test_verify_valid_token(self, token_manager) -> None:
"""Test verification of a valid token."""
token = token_manager.create_access_token(
data={"sub": "test-user-123", "roles": ["user"]},
)
payload = await token_manager.verify_token(token)
assert payload is not None
assert payload.get("sub") == "test-user-123"
assert "user" in payload.get("roles", [])
@pytest.mark.asyncio
async def test_verify_invalid_token(self, token_manager) -> None:
"""Test that invalid tokens are rejected."""
with pytest.raises(Exception):
await token_manager.verify_token("invalid.token.here")
@pytest.mark.asyncio
async def test_token_expiration(self, token_manager) -> None:
"""Test that expired tokens are rejected."""
import time
from datetime import timedelta
token = token_manager.create_access_token(
data={"sub": "test-user-123", "roles": ["user"]},
expires_delta=timedelta(seconds=1),
)
time.sleep(2)
with pytest.raises(Exception):
await token_manager.verify_token(token)
@pytest.mark.asyncio
async def test_revoke_token(self, token_manager) -> None:
"""Test token revocation."""
token = token_manager.create_access_token(
data={"sub": "test-user-123", "roles": ["user"]},
)
await token_manager.revoke_token(token)
# Revoked token should be rejected
with pytest.raises(Exception):
await token_manager.verify_token(token)
class TestAuthenticationFlow:
"""Test authentication service flows."""
@pytest.fixture
def mock_db_manager(self):
"""Create mock database manager."""
manager = MagicMock()
# Mock user operations
manager.get_user_by_email = AsyncMock(return_value=None)
manager.create_user = AsyncMock(
return_value=MagicMock(
id="user-123",
email="test@example.com",
hashed_password="hashed",
is_active=True,
roles=["user"],
)
)
return manager
@pytest.fixture
def token_manager(self):
"""Create token manager."""
from grid.auth.token_manager import TokenManager
from grid.config.runtime_settings import RuntimeSettings
# Create mock settings
settings = MagicMock(spec=RuntimeSettings)
settings.security = MagicMock()
settings.security.secret_key = "test-secret-key-at-least-32-characters-long"
settings.security.algorithm = "HS256"
settings.security.access_token_expire_minutes = 30
settings.security.refresh_token_expire_days = 7
settings.cache = MagicMock()
settings.cache.backend = "memory"
settings.environment = "test"
return TokenManager(settings=settings)
@pytest.fixture
def mock_settings(self):
"""Create mock settings."""
settings = MagicMock()
settings.environment = "test"
return settings
@pytest.mark.asyncio
async def test_user_registration_flow(
self,
mock_db_manager,
token_manager,
mock_settings,
) -> None:
"""Test user can be registered."""
from grid.auth.service import AuthService
# AuthService.register_user expects (username, password, role)
# and uses db.fetch_one / db.execute / db.commit
mock_db_manager.fetch_one = AsyncMock(return_value=None) # no existing user
mock_db_manager.execute = AsyncMock()
mock_db_manager.commit = AsyncMock()
auth_service = AuthService(
db_manager=mock_db_manager,
token_manager=token_manager,
settings=mock_settings,
)
user_id = await auth_service.register_user(
username="newuser",
password="securePassword123!",
role="user",
)
assert user_id is not None
mock_db_manager.execute.assert_called_once()
# =============================================================================
# Test Group 3: API Health
# =============================================================================
class TestAPIHealthEndpoints:
"""Test API health and readiness endpoints."""
@pytest.fixture
def client(self):
"""Create test client."""
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
@app.get("/health")
async def health():
return {"status": "healthy"}
@app.get("/health/ready")
async def ready():
return {"status": "ready"}
return TestClient(app)
def test_health_endpoint(self, client) -> None:
"""Test /health endpoint returns healthy status."""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
def test_ready_endpoint(self, client) -> None:
"""Test /health/ready endpoint returns ready status."""
response = client.get("/health/ready")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ready"
class TestHealthChecker:
"""Test health checker functions."""
@pytest.mark.asyncio
async def test_check_eventbus_health_mock(self) -> None:
"""Test EventBus health check (no-arg async function)."""
from application.mothership.health import check_eventbus_health
health = await check_eventbus_health()
assert health.get("healthy") is True or "error" in health
@pytest.mark.asyncio
async def test_check_db_engine_health_mock(self) -> None:
"""Test database engine health check (no-arg async function)."""
from application.mothership.health import check_db_engine_health
health = await check_db_engine_health()
# Will return healthy=False in test env (no real DB) but should not crash
assert "healthy" in health
# =============================================================================
# Test Group 4: Agentic System
# =============================================================================
class TestAgenticCaseProcessing:
"""Test agentic system case processing."""
@pytest.fixture
def mock_event_bus(self):
"""Create mock event bus."""
bus = MagicMock()
bus.publish = AsyncMock()
bus.subscribe = MagicMock()
bus.get_event_history = MagicMock(return_value=[])
return bus
@pytest.fixture
def knowledge_base_path(self, tmp_path: Path) -> Path:
"""Create temporary knowledge base directory."""
kb_path = tmp_path / "knowledge_base"
kb_path.mkdir()
return kb_path
@pytest.mark.asyncio
async def test_agentic_system_initialization(
self,
mock_event_bus,
knowledge_base_path: Path,
) -> None:
"""Test AgenticSystem can be initialized."""
from grid.agentic.agentic_system import AgenticSystem
system = AgenticSystem(
knowledge_base_path=knowledge_base_path,
event_bus=mock_event_bus,
enable_cognitive=False,
)
assert system is not None
assert system.knowledge_base_path == knowledge_base_path
@pytest.mark.asyncio
async def test_case_creation(
self,
mock_event_bus,
knowledge_base_path: Path,
) -> None:
"""Test case can be created."""
from grid.agentic.agentic_system import AgenticSystem
from grid.agentic.schemas import CaseCreateRequest
AgenticSystem(
knowledge_base_path=knowledge_base_path,
event_bus=mock_event_bus,
enable_cognitive=False,
)
request = CaseCreateRequest(
raw_input="Test case input",
metadata={"source": "test"},
)
# Note: This may need adjustment based on actual API
# Just verify system can receive the request without error
assert request.raw_input == "Test case input"
# =============================================================================
# Test Group 5: Event Bus
# =============================================================================
class TestEventBus:
"""Test event bus publish/subscribe functionality."""
@pytest_asyncio.fixture
async def event_bus(self):
"""Create event bus for testing."""
from unified_fabric import DynamicEventBus
from unified_fabric.event_schemas import EventSchema, register_schema
bus = DynamicEventBus(bus_id="test-bus")
# Avoid cross-test schema pollution from globally registered test schemas.
register_schema(EventSchema(event_type="test.event", domain="grid", required_keys=()))
register_schema(EventSchema(event_type="history.test", domain="grid", required_keys=()))
await bus.start()
yield bus
await bus.stop()
@pytest.mark.asyncio
async def test_event_bus_start_stop(self) -> None:
"""Test event bus can start and stop."""
from unified_fabric import DynamicEventBus
bus = DynamicEventBus(bus_id="test-start-stop")
await bus.start()
assert bus._running is True
await bus.stop()
assert bus._running is False
@pytest.mark.asyncio
async def test_event_publish_subscribe(self, event_bus) -> None:
"""Test event can be published and received."""
from unified_fabric import Event, EventDomain
received_events = []
async def handler(event: Event):
received_events.append(event)
event_bus.subscribe(
"test.event",
handler,
domain=EventDomain.GRID,
)
await event_bus.publish(
Event(
event_type="test.event",
payload={"message": "hello"},
source_domain="test",
)
)
# Give time for async processing
import asyncio
await asyncio.sleep(0.1)
assert len(received_events) == 1
assert received_events[0].payload["message"] == "hello"
event_bus.unsubscribe("test.event", handler, domain=EventDomain.GRID)
@pytest.mark.asyncio
async def test_event_history(self, event_bus) -> None:
"""Test event history is recorded."""
from unified_fabric import Event
await event_bus.publish(
Event(
event_type="history.test",
payload={"test": "data"},
source_domain="test",
)
)
history = event_bus.get_event_history()
assert len(history) >= 1
class TestEventValidation:
"""Test event validation."""
def test_event_creation(self) -> None:
"""Test Event can be created with required fields."""
from unified_fabric import Event
event = Event(
event_type="test.event",
payload={"key": "value"},
source_domain="test",
)
assert event.event_type == "test.event"
assert event.payload["key"] == "value"
def test_event_serialization(self) -> None:
"""Test Event can be serialized and deserialized."""
from unified_fabric import Event
event = Event(
event_type="serialize.test",
payload={"number": 123},
source_domain="test",
)
event_dict = event.to_dict()
assert "event_type" in event_dict
assert "payload" in event_dict
restored = Event.from_dict(event_dict)
assert restored.event_type == event.event_type
assert restored.payload == event.payload
# =============================================================================
# Test Group 6: RAG Engine
# =============================================================================
class TestRAGEngineBasic:
"""Test RAG engine basic functionality."""
@pytest.fixture
def mock_embedding_provider(self):
"""Create mock embedding provider."""
provider = MagicMock()
provider.embed = MagicMock(return_value=[0.1] * 384)
provider.embed_batch = MagicMock(return_value=[[0.1] * 384] * 3)
provider.async_embed = AsyncMock(return_value=[0.1] * 384)
return provider
@pytest.fixture
def mock_vector_store(self):
"""Create mock vector store."""
store = MagicMock()
store.add = MagicMock(return_value=["id1", "id2"])
store.query = MagicMock(
return_value=[
MagicMock(
id="id1",
content="test content",
metadata={"source": "test"},
score=0.9,
)
]
)
store.count = MagicMock(return_value=10)
return store
@pytest.fixture
def mock_llm_provider(self):
"""Create mock LLM provider."""
provider = MagicMock()
provider.generate = MagicMock(return_value="Test response")
provider.async_generate = AsyncMock(return_value="Test response")
return provider
def test_rag_config_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test RAGConfig can be created from environment."""
from tools.rag.config import RAGConfig
monkeypatch.setenv("RAG_VECTOR_STORE_PROVIDER", "chroma")
monkeypatch.setenv("RAG_EMBEDDING_PROVIDER", "ollama")
config = RAGConfig.from_env()
assert config is not None
def test_rag_engine_stats(self, mock_vector_store) -> None:
"""Test RAG engine returns statistics."""
# Stats should include document count
stats = {
"document_count": mock_vector_store.count(),
"status": "healthy",
}
assert stats["document_count"] == 10
assert stats["status"] == "healthy"
# =============================================================================
# Test Group 7: Security Guard
# =============================================================================
class TestParasiteGuard:
"""Test parasite guard security validation."""
@pytest.fixture
def guard_config(self, monkeypatch: pytest.MonkeyPatch):
"""Create parasite guard config for testing."""
from grid.security.parasite_guard import ParasiteGuardConfig
monkeypatch.setenv("PARASITE_GUARD_ENABLED", "true")
monkeypatch.setenv("PARASITE_GUARD_DETECTION_THRESHOLD", "10")
return ParasiteGuardConfig
@pytest.mark.asyncio
async def test_frequency_detector_basic(self) -> None:
"""Test FrequencyDetector can be instantiated and called with a mock Request."""
from grid.security.parasite_guard import FrequencyDetector
detector = FrequencyDetector(window_seconds=60)
# The actual __call__ is async and expects a Starlette Request object.
# Verify instantiation succeeds and the detector has the expected attributes.
assert detector.window_seconds == 60
assert detector.name == "frequency"
@pytest.mark.asyncio
async def test_missing_body_detector(self) -> None:
"""Test MissingBodyDetector can be instantiated."""
from grid.security.parasite_guard import MissingBodyDetector
detector = MissingBodyDetector()
assert detector.name == "missing_body"
@pytest.mark.asyncio
async def test_header_anomaly_detector(self) -> None:
"""Test HeaderAnomalyDetector can be instantiated."""
from grid.security.parasite_guard import HeaderAnomalyDetector
detector = HeaderAnomalyDetector()
assert detector.name == "header_anomaly"
class TestSQLInjectionValidator:
"""Test SQL injection validation."""
@pytest.fixture
def validator(self):
"""Create SQL injection validator."""
from safety.detectors.pre_check import SQLInjectionValidator
return SQLInjectionValidator()
def test_safe_queries_pass(self, validator) -> None:
"""Safe queries should pass validation."""
safe_queries = [
"SELECT * FROM users WHERE id = ?",
"SELECT name, email FROM users WHERE active = 1",
"SELECT COUNT(*) FROM orders WHERE created_at > '2024-01-01'",
"INSERT INTO logs (message) VALUES (?)",
]
for query in safe_queries:
is_safe = validator.validate_query(query)
assert is_safe, f"Query should be safe: {query}"
def test_dangerous_queries_blocked(self, validator) -> None:
"""Dangerous queries should be blocked."""
dangerous_queries = [
"SELECT * FROM users; DROP TABLE users; --",
"SELECT * FROM users WHERE id = '1' OR '1'='1'",
"SELECT * FROM users UNION SELECT * FROM passwords",
"EXEC xp_cmdshell('dir')",
]
for query in dangerous_queries:
is_safe = validator.validate_query(query)
assert not is_safe, f"Query should be blocked: {query}"
# =============================================================================
# Test Group 8: Environment Isolation
# =============================================================================
class TestEnvironmentIsolation:
"""Test that test environment is properly isolated."""
def test_environment_is_test(self) -> None:
"""Verify MOTHERSHIP_ENVIRONMENT is set to test."""
assert os.environ.get("MOTHERSHIP_ENVIRONMENT") == "test"
def test_database_is_memory(self) -> None:
"""Verify database is SQLite in-memory for tests."""
db_url = os.environ.get("MOTHERSHIP_DATABASE_URL", "")
assert "memory" in db_url or db_url == ""
def test_databricks_disabled(self) -> None:
"""Verify Databricks is disabled for tests."""
assert os.environ.get("MOTHERSHIP_USE_DATABRICKS") == "false"
def test_redis_disabled(self) -> None:
"""Verify Redis is disabled for tests."""
assert os.environ.get("MOTHERSHIP_REDIS_ENABLED") == "false"

Xet Storage Details

Size:
22.2 kB
·
Xet hash:
6e6b11ff046c14b98e1606df91136c695c65c1b85f4af1e6d0872b5cd0a50910

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.