refactor: remove backward compatibility code for pre-launch baseline
Clean up accumulated backward-compat shims, deprecated wrappers, unused aliases, and legacy code across the codebase. Since the platform is not live yet, this establishes a clean baseline. Changes: - Delete deprecated middleware/context.py (RequestContext, get_request_context) - Remove unused factory get_store_email_settings_service() - Remove deprecated pagination_full macro, /admin/platform-homepage route - Remove ConversationResponse, InvoiceSettings* unprefixed aliases - Simplify celery_config.py (remove empty LEGACY_TASK_MODULES) - Standardize billing exceptions: *Error aliases → *Exception names - Consolidate duplicate TierNotFoundError/FeatureNotFoundError classes - Remove deprecated is_admin_request() from Store/PlatformContextManager - Remove is_platform_default field, MediaUploadResponse legacy flat fields - Remove MediaItemResponse.url alias, update JS to use file_url - Update all affected tests and documentation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -377,19 +377,6 @@ async def test_clean_path_context(request: Request):
|
||||
}
|
||||
|
||||
|
||||
@api_router.get("/enum")
|
||||
async def test_api_enum(request: Request):
|
||||
"""Test context enum instance."""
|
||||
from middleware.context import RequestContext
|
||||
|
||||
context = getattr(request.state, "context_type", None)
|
||||
return {
|
||||
"is_enum": isinstance(context, RequestContext) if context else False,
|
||||
"enum_name": context.name if context else None,
|
||||
"enum_value": context.value if context else None,
|
||||
}
|
||||
|
||||
|
||||
@api_router.get("/theme")
|
||||
async def test_api_theme(request: Request):
|
||||
"""Test theme in API context."""
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
# tests/unit/middleware/test_context.py
|
||||
"""
|
||||
DEPRECATED: Tests for backward compatibility of middleware.context module.
|
||||
|
||||
The ContextMiddleware and ContextManager classes have been replaced by:
|
||||
- FrontendTypeMiddleware (middleware/frontend_type.py)
|
||||
- FrontendDetector (app/core/frontend_detector.py)
|
||||
|
||||
These tests verify the backward compatibility layer still works for code
|
||||
that uses the deprecated RequestContext enum and get_request_context() function.
|
||||
|
||||
For new tests, see:
|
||||
- tests/unit/core/test_frontend_detector.py
|
||||
- tests/unit/middleware/test_frontend_type.py
|
||||
"""
|
||||
|
||||
import warnings
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
from fastapi import Request
|
||||
|
||||
from middleware.context import RequestContext, get_request_context
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestRequestContextEnumBackwardCompatibility:
|
||||
"""Test suite for deprecated RequestContext enum."""
|
||||
|
||||
def test_request_context_values(self):
|
||||
"""Test RequestContext enum has correct values."""
|
||||
assert RequestContext.API.value == "api"
|
||||
assert RequestContext.ADMIN.value == "admin"
|
||||
assert RequestContext.STORE_DASHBOARD.value == "store"
|
||||
assert RequestContext.STOREFRONT.value == "storefront"
|
||||
assert RequestContext.FALLBACK.value == "fallback"
|
||||
|
||||
def test_request_context_types(self):
|
||||
"""Test RequestContext enum values are strings."""
|
||||
for context in RequestContext:
|
||||
assert isinstance(context.value, str)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestGetRequestContextBackwardCompatibility:
|
||||
"""Test suite for deprecated get_request_context() function."""
|
||||
|
||||
def test_get_request_context_returns_api_for_api_paths(self):
|
||||
"""Test get_request_context returns API for /api/ paths."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/api/v1/stores")
|
||||
request.state = Mock()
|
||||
request.state.frontend_type = None
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", DeprecationWarning)
|
||||
context = get_request_context(request)
|
||||
|
||||
assert context == RequestContext.API
|
||||
|
||||
def test_get_request_context_deprecation_warning(self):
|
||||
"""Test get_request_context raises DeprecationWarning."""
|
||||
from app.modules.enums import FrontendType
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/admin/dashboard")
|
||||
request.state = Mock()
|
||||
request.state.frontend_type = FrontendType.ADMIN
|
||||
|
||||
with pytest.warns(DeprecationWarning, match="get_request_context.*deprecated"):
|
||||
get_request_context(request)
|
||||
|
||||
def test_get_request_context_maps_admin(self):
|
||||
"""Test get_request_context maps FrontendType.ADMIN to RequestContext.ADMIN."""
|
||||
from app.modules.enums import FrontendType
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/admin/dashboard")
|
||||
request.state = Mock()
|
||||
request.state.frontend_type = FrontendType.ADMIN
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", DeprecationWarning)
|
||||
context = get_request_context(request)
|
||||
|
||||
assert context == RequestContext.ADMIN
|
||||
|
||||
def test_get_request_context_maps_store(self):
|
||||
"""Test get_request_context maps FrontendType.STORE to RequestContext.STORE_DASHBOARD."""
|
||||
from app.modules.enums import FrontendType
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/store/settings")
|
||||
request.state = Mock()
|
||||
request.state.frontend_type = FrontendType.STORE
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", DeprecationWarning)
|
||||
context = get_request_context(request)
|
||||
|
||||
assert context == RequestContext.STORE_DASHBOARD
|
||||
|
||||
def test_get_request_context_maps_storefront(self):
|
||||
"""Test get_request_context maps FrontendType.STOREFRONT to RequestContext.STOREFRONT."""
|
||||
from app.modules.enums import FrontendType
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/storefront/products")
|
||||
request.state = Mock()
|
||||
request.state.frontend_type = FrontendType.STOREFRONT
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", DeprecationWarning)
|
||||
context = get_request_context(request)
|
||||
|
||||
assert context == RequestContext.STOREFRONT
|
||||
|
||||
def test_get_request_context_maps_platform_to_fallback(self):
|
||||
"""Test get_request_context maps FrontendType.PLATFORM to RequestContext.FALLBACK."""
|
||||
from app.modules.enums import FrontendType
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/pricing")
|
||||
request.state = Mock()
|
||||
request.state.frontend_type = FrontendType.PLATFORM
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", DeprecationWarning)
|
||||
context = get_request_context(request)
|
||||
|
||||
assert context == RequestContext.FALLBACK
|
||||
@@ -22,6 +22,7 @@ import pytest
|
||||
from fastapi import Request
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.frontend_detector import FrontendDetector
|
||||
from middleware.platform_context import (
|
||||
DEFAULT_PLATFORM_CODE,
|
||||
PlatformContextManager,
|
||||
@@ -210,7 +211,7 @@ class TestPlatformContextManager:
|
||||
request.headers = {"host": "admin.localhost"}
|
||||
request.url = Mock(path="/dashboard")
|
||||
|
||||
assert PlatformContextManager.is_admin_request(request) is True
|
||||
assert FrontendDetector.is_admin("admin.localhost", "/dashboard") is True
|
||||
|
||||
context = PlatformContextManager.detect_platform_context(request)
|
||||
assert context is None
|
||||
@@ -221,26 +222,18 @@ class TestPlatformContextManager:
|
||||
request.headers = {"host": "localhost"}
|
||||
request.url = Mock(path="/admin/stores")
|
||||
|
||||
assert PlatformContextManager.is_admin_request(request) is True
|
||||
assert FrontendDetector.is_admin("localhost", "/admin/stores") is True
|
||||
|
||||
context = PlatformContextManager.detect_platform_context(request)
|
||||
assert context is None
|
||||
|
||||
def test_skip_admin_path_with_port(self):
|
||||
"""Test admin detection with port in host."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "admin.localhost:9999"}
|
||||
request.url = Mock(path="/dashboard")
|
||||
|
||||
assert PlatformContextManager.is_admin_request(request) is True
|
||||
assert FrontendDetector.is_admin("admin.localhost:9999", "/dashboard") is True
|
||||
|
||||
def test_not_admin_regular_path(self):
|
||||
"""Test non-admin path is not detected as admin."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "localhost"}
|
||||
request.url = Mock(path="/shop/products")
|
||||
|
||||
assert PlatformContextManager.is_admin_request(request) is False
|
||||
assert FrontendDetector.is_admin("localhost", "/shop/products") is False
|
||||
|
||||
# ========================================================================
|
||||
# Static File Detection Tests
|
||||
@@ -925,11 +918,7 @@ class TestEdgeCases:
|
||||
|
||||
def test_admin_subdomain_with_production_domain(self):
|
||||
"""Test admin subdomain detection for production domains."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "admin.oms.lu"}
|
||||
request.url = Mock(path="/dashboard")
|
||||
|
||||
assert PlatformContextManager.is_admin_request(request) is True
|
||||
assert FrontendDetector.is_admin("admin.oms.lu", "/dashboard") is True
|
||||
|
||||
def test_static_file_case_insensitive(self):
|
||||
"""Test static file detection is case-insensitive."""
|
||||
|
||||
@@ -17,6 +17,7 @@ import pytest
|
||||
from fastapi import Request
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.frontend_detector import FrontendDetector
|
||||
from app.modules.tenancy.exceptions import StoreNotFoundException
|
||||
from middleware.store_context import (
|
||||
StoreContextManager,
|
||||
@@ -372,35 +373,19 @@ class TestStoreContextManager:
|
||||
|
||||
def test_is_admin_request_admin_subdomain(self):
|
||||
"""Test admin request detection from subdomain."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "admin.platform.com"}
|
||||
request.url = Mock(path="/dashboard")
|
||||
|
||||
assert StoreContextManager.is_admin_request(request) is True
|
||||
assert FrontendDetector.is_admin("admin.platform.com", "/dashboard") is True
|
||||
|
||||
def test_is_admin_request_admin_path(self):
|
||||
"""Test admin request detection from path."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "localhost"}
|
||||
request.url = Mock(path="/admin/dashboard")
|
||||
|
||||
assert StoreContextManager.is_admin_request(request) is True
|
||||
assert FrontendDetector.is_admin("localhost", "/admin/dashboard") is True
|
||||
|
||||
def test_is_admin_request_with_port(self):
|
||||
"""Test admin request detection with port number."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "admin.localhost:8000"}
|
||||
request.url = Mock(path="/dashboard")
|
||||
|
||||
assert StoreContextManager.is_admin_request(request) is True
|
||||
assert FrontendDetector.is_admin("admin.localhost:8000", "/dashboard") is True
|
||||
|
||||
def test_is_not_admin_request(self):
|
||||
"""Test non-admin request."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "store1.platform.com"}
|
||||
request.url = Mock(path="/storefront")
|
||||
|
||||
assert StoreContextManager.is_admin_request(request) is False
|
||||
assert FrontendDetector.is_admin("store1.platform.com", "/storefront") is False
|
||||
|
||||
def test_is_api_request(self):
|
||||
"""Test API request detection."""
|
||||
@@ -599,7 +584,7 @@ class TestStoreContextMiddleware:
|
||||
|
||||
call_next = AsyncMock(return_value=Mock())
|
||||
|
||||
with patch.object(StoreContextManager, "is_admin_request", return_value=True):
|
||||
with patch.object(FrontendDetector, "is_admin", return_value=True):
|
||||
await middleware.dispatch(request, call_next)
|
||||
|
||||
assert request.state.store is None
|
||||
@@ -775,7 +760,7 @@ class TestStoreContextMiddleware:
|
||||
call_next = AsyncMock(return_value=Mock())
|
||||
|
||||
with (
|
||||
patch.object(StoreContextManager, "is_admin_request", return_value=False),
|
||||
patch.object(FrontendDetector, "is_admin", return_value=False),
|
||||
patch.object(
|
||||
StoreContextManager, "is_static_file_request", return_value=False
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user