Files
orion/tests/integration/middleware/conftest.py
Samir Boulahtit d7a0ff8818 refactor: complete module-driven architecture migration
This commit completes the migration to a fully module-driven architecture:

## Models Migration
- Moved all domain models from models/database/ to their respective modules:
  - tenancy: User, Admin, Vendor, Company, Platform, VendorDomain, etc.
  - cms: MediaFile, VendorTheme
  - messaging: Email, VendorEmailSettings, VendorEmailTemplate
  - core: AdminMenuConfig
- models/database/ now only contains Base and TimestampMixin (infrastructure)

## Schemas Migration
- Moved all domain schemas from models/schema/ to their respective modules:
  - tenancy: company, vendor, admin, team, vendor_domain
  - cms: media, image, vendor_theme
  - messaging: email
- models/schema/ now only contains base.py and auth.py (infrastructure)

## Routes Migration
- Moved admin routes from app/api/v1/admin/ to modules:
  - menu_config.py -> core module
  - modules.py -> tenancy module
  - module_config.py -> tenancy module
- app/api/v1/admin/ now only aggregates auto-discovered module routes

## Menu System
- Implemented module-driven menu system with MenuDiscoveryService
- Extended FrontendType enum: PLATFORM, ADMIN, VENDOR, STOREFRONT
- Added MenuItemDefinition and MenuSectionDefinition dataclasses
- Each module now defines its own menu items in definition.py
- MenuService integrates with MenuDiscoveryService for template rendering

## Documentation
- Updated docs/architecture/models-structure.md
- Updated docs/architecture/menu-management.md
- Updated architecture validation rules for new exceptions

## Architecture Validation
- Updated MOD-019 rule to allow base.py in models/schema/
- Created core module exceptions.py and schemas/ directory
- All validation errors resolved (only warnings remain)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 21:02:56 +01:00

195 lines
5.7 KiB
Python

# tests/integration/middleware/conftest.py
"""
Fixtures specific to middleware integration tests.
The middleware (VendorContextMiddleware, ThemeContextMiddleware) calls get_db()
directly rather than using FastAPI's dependency injection. Since the middleware
modules import get_db at module load time (before tests run), we need to patch
get_db directly in each middleware module.
Solution: We patch get_db in both middleware.vendor_context and middleware.theme_context
to use a generator that yields the test database session.
"""
import uuid
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from app.core.database import get_db
from main import app
from app.modules.tenancy.models import Company
from app.modules.tenancy.models import Vendor
from app.modules.tenancy.models import VendorDomain
from app.modules.cms.models import VendorTheme
# Register test routes for middleware tests
from tests.integration.middleware.middleware_test_routes import (
admin_router,
api_router,
router as test_router,
shop_router,
vendor_router,
)
# Include the test routers in the app (only once)
if not any(r.path.startswith("/middleware-test") for r in app.routes if hasattr(r, "path")):
app.include_router(test_router)
app.include_router(api_router)
app.include_router(admin_router)
app.include_router(vendor_router)
app.include_router(shop_router)
@pytest.fixture
def client(db):
"""
Create a test client with database dependency override.
This patches:
1. get_db in both middleware modules to use the test database
2. settings.platform_domain in vendor_context to use 'platform.com' for testing
This ensures middleware can see test fixtures and detect subdomains correctly.
"""
# Override the dependency for FastAPI endpoints
def override_get_db():
try:
yield db
finally:
pass
app.dependency_overrides[get_db] = override_get_db
# Patch get_db in middleware modules - they have their own imports
# The middleware calls: db_gen = get_db(); db = next(db_gen)
# Also patch settings.platform_domain so subdomain detection works with test hosts
with patch("middleware.vendor_context.get_db", override_get_db):
with patch("middleware.theme_context.get_db", override_get_db):
with patch("middleware.vendor_context.settings") as mock_settings:
mock_settings.platform_domain = "platform.com"
client = TestClient(app)
yield client
# Clean up
if get_db in app.dependency_overrides:
del app.dependency_overrides[get_db]
@pytest.fixture
def middleware_test_company(db, test_user):
"""Create a company for middleware test vendors."""
unique_id = str(uuid.uuid4())[:8]
company = Company(
name=f"Middleware Test Company {unique_id}",
contact_email=f"middleware{unique_id}@test.com",
owner_user_id=test_user.id,
is_active=True,
is_verified=True,
)
db.add(company)
db.commit()
db.refresh(company)
return company
@pytest.fixture
def vendor_with_subdomain(db, middleware_test_company):
"""Create a vendor with subdomain for testing."""
unique_id = str(uuid.uuid4())[:8]
vendor = Vendor(
company_id=middleware_test_company.id,
name="Test Vendor",
vendor_code=f"TESTVENDOR_{unique_id.upper()}",
subdomain="testvendor",
is_active=True,
is_verified=True,
)
db.add(vendor)
db.commit()
db.refresh(vendor)
return vendor
@pytest.fixture
def vendor_with_custom_domain(db, middleware_test_company):
"""Create a vendor with custom domain for testing."""
unique_id = str(uuid.uuid4())[:8]
vendor = Vendor(
company_id=middleware_test_company.id,
name="Custom Domain Vendor",
vendor_code=f"CUSTOMVENDOR_{unique_id.upper()}",
subdomain="customvendor",
is_active=True,
is_verified=True,
)
db.add(vendor)
db.commit()
db.refresh(vendor)
# Add custom domain
domain = VendorDomain(
vendor_id=vendor.id, domain="customdomain.com", is_active=True, is_primary=True
)
db.add(domain)
db.commit()
return vendor
@pytest.fixture
def vendor_with_theme(db, middleware_test_company):
"""Create a vendor with custom theme for testing."""
unique_id = str(uuid.uuid4())[:8]
vendor = Vendor(
company_id=middleware_test_company.id,
name="Themed Vendor",
vendor_code=f"THEMEDVENDOR_{unique_id.upper()}",
subdomain="themedvendor",
is_active=True,
is_verified=True,
)
db.add(vendor)
db.commit()
db.refresh(vendor)
# Add custom theme
theme = VendorTheme(
vendor_id=vendor.id,
theme_name="custom",
colors={
"primary": "#FF5733",
"secondary": "#33FF57",
"accent": "#ec4899",
"background": "#ffffff",
"text": "#1f2937",
"border": "#e5e7eb",
},
logo_url="/static/vendors/themedvendor/logo.png",
favicon_url="/static/vendors/themedvendor/favicon.ico",
custom_css="body { background: #FF5733; }",
)
db.add(theme)
db.commit()
return vendor
@pytest.fixture
def middleware_inactive_vendor(db, middleware_test_company):
"""Create an inactive vendor for testing."""
unique_id = str(uuid.uuid4())[:8]
vendor = Vendor(
company_id=middleware_test_company.id,
name="Inactive Vendor",
vendor_code=f"INACTIVE_{unique_id.upper()}",
subdomain="inactive",
is_active=False,
is_verified=False,
)
db.add(vendor)
db.commit()
db.refresh(vendor)
return vendor