removing legacy code on path_rewrite_middleware

This commit is contained in:
2025-11-18 23:32:07 +01:00
parent f14686c131
commit d947fa5ca0
9 changed files with 114 additions and 302 deletions

View File

@@ -1,17 +1,18 @@
# tests/unit/middleware/test_theme_logging_path_decorators.py
"""
Comprehensive unit tests for remaining middleware components:
Comprehensive unit tests for middleware components:
- ThemeContextMiddleware and ThemeContextManager
- LoggingMiddleware
- path_rewrite_middleware
- rate_limit decorator
Tests cover:
- Theme loading and caching
- Request/response logging
- Path rewriting for vendor routing
- Rate limit decorators
- Edge cases and error handling
Note: path_rewrite_middleware has been deprecated in favor of double router mounting.
See main.py for current implementation using app.include_router() with different prefixes.
"""
import pytest
@@ -25,7 +26,6 @@ from middleware.theme_context import (
get_current_theme,
)
from middleware.logging_middleware import LoggingMiddleware
from middleware.path_rewrite_middleware import path_rewrite_middleware
from middleware.decorators import rate_limit
from app.exceptions.base import RateLimitException
@@ -351,98 +351,6 @@ class TestLoggingMiddleware:
assert process_time >= 0.1
# =============================================================================
# Path Rewrite Middleware Tests
# =============================================================================
@pytest.mark.unit
class TestPathRewriteMiddleware:
"""Test suite for path_rewrite_middleware."""
@pytest.mark.asyncio
async def test_rewrites_path_when_clean_path_different(self):
"""Test path is rewritten when clean_path differs from original."""
request = Mock(spec=Request)
request.url = Mock(path="/vendor/testvendor/shop/products")
request.state = Mock(clean_path="/shop/products")
request.scope = {"path": "/vendor/testvendor/shop/products"}
call_next = AsyncMock(return_value=Mock())
await path_rewrite_middleware(request, call_next)
# Path should be rewritten in scope
assert request.scope["path"] == "/shop/products"
call_next.assert_called_once_with(request)
@pytest.mark.asyncio
async def test_does_not_rewrite_when_paths_same(self):
"""Test path is not rewritten when clean_path same as original."""
request = Mock(spec=Request)
original_path = "/shop/products"
request.url = Mock(path=original_path)
request.state = Mock(clean_path=original_path)
request.scope = {"path": original_path}
call_next = AsyncMock(return_value=Mock())
await path_rewrite_middleware(request, call_next)
# Path should remain unchanged
assert request.scope["path"] == original_path
call_next.assert_called_once()
@pytest.mark.asyncio
async def test_does_nothing_when_no_clean_path(self):
"""Test middleware does nothing when no clean_path set."""
request = Mock(spec=Request)
request.url = Mock(path="/shop/products")
request.state = Mock(spec=[]) # No clean_path attribute
original_path = "/shop/products"
request.scope = {"path": original_path}
call_next = AsyncMock(return_value=Mock())
await path_rewrite_middleware(request, call_next)
# Path should remain unchanged
assert request.scope["path"] == original_path
call_next.assert_called_once()
@pytest.mark.asyncio
async def test_updates_request_url(self):
"""Test middleware updates request._url."""
request = Mock(spec=Request)
original_url = Mock(path="/vendor/test/shop")
request.url = original_url
request.url.replace = Mock(return_value=Mock(path="/shop"))
request.state = Mock(clean_path="/shop")
request.scope = {"path": "/vendor/test/shop"}
call_next = AsyncMock(return_value=Mock())
await path_rewrite_middleware(request, call_next)
# URL replace should have been called
request.url.replace.assert_called_once_with(path="/shop")
@pytest.mark.asyncio
async def test_preserves_vendor_context(self):
"""Test middleware preserves vendor context in request.state."""
request = Mock(spec=Request)
request.url = Mock(path="/vendor/testvendor/products")
mock_vendor = Mock()
request.state = Mock(clean_path="/products", vendor=mock_vendor)
request.scope = {"path": "/vendor/testvendor/products"}
call_next = AsyncMock(return_value=Mock())
await path_rewrite_middleware(request, call_next)
# Vendor should still be accessible
assert request.state.vendor is mock_vendor
# =============================================================================
# Rate Limit Decorator Tests
# =============================================================================
@@ -560,22 +468,6 @@ class TestMiddlewareEdgeCases:
# Verify database was closed
mock_db.close.assert_called_once()
@pytest.mark.asyncio
async def test_path_rewrite_with_query_parameters(self):
"""Test path rewrite preserves query parameters."""
request = Mock(spec=Request)
original_url = Mock(path="/vendor/test/shop?page=1")
request.url = original_url
request.url.replace = Mock(return_value=Mock(path="/shop?page=1"))
request.state = Mock(clean_path="/shop?page=1")
request.scope = {"path": "/vendor/test/shop?page=1"}
call_next = AsyncMock(return_value=Mock())
await path_rewrite_middleware(request, call_next)
request.url.replace.assert_called_once_with(path="/shop?page=1")
def test_theme_default_immutability(self):
"""Test that getting default theme doesn't share state."""
theme1 = ThemeContextManager.get_default_theme()