style: apply black and isort formatting across entire codebase
- Standardize quote style (single to double quotes) - Reorder and group imports alphabetically - Fix line breaks and indentation for consistency - Apply PEP 8 formatting standards Also updated Makefile to exclude both venv and .venv from code quality checks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -5,8 +5,10 @@ Integration tests for the complete middleware stack.
|
||||
These tests verify that all middleware components work together correctly
|
||||
through real HTTP requests, ensuring proper execution order and state injection.
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from middleware.context import RequestContext
|
||||
|
||||
|
||||
@@ -23,14 +25,20 @@ class TestMiddlewareStackIntegration:
|
||||
"""Test that /admin/* paths set ADMIN context type."""
|
||||
# Create a simple endpoint to inspect request state
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/admin/test-context")
|
||||
async def test_admin_context(request: Request):
|
||||
return {
|
||||
"context_type": request.state.context_type.value if hasattr(request.state, 'context_type') else None,
|
||||
"has_vendor": hasattr(request.state, 'vendor') and request.state.vendor is not None,
|
||||
"has_theme": hasattr(request.state, 'theme')
|
||||
"context_type": (
|
||||
request.state.context_type.value
|
||||
if hasattr(request.state, "context_type")
|
||||
else None
|
||||
),
|
||||
"has_vendor": hasattr(request.state, "vendor")
|
||||
and request.state.vendor is not None,
|
||||
"has_theme": hasattr(request.state, "theme"),
|
||||
}
|
||||
|
||||
response = client.get("/admin/test-context")
|
||||
@@ -44,20 +52,24 @@ class TestMiddlewareStackIntegration:
|
||||
def test_admin_subdomain_sets_admin_context(self, client):
|
||||
"""Test that admin.* subdomain sets ADMIN context type."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/test-admin-subdomain")
|
||||
async def test_admin_subdomain(request: Request):
|
||||
return {
|
||||
"context_type": request.state.context_type.value if hasattr(request.state, 'context_type') else None
|
||||
"context_type": (
|
||||
request.state.context_type.value
|
||||
if hasattr(request.state, "context_type")
|
||||
else None
|
||||
)
|
||||
}
|
||||
|
||||
# Simulate request with admin subdomain
|
||||
with patch('app.core.config.settings') as mock_settings:
|
||||
with patch("app.core.config.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
response = client.get(
|
||||
"/test-admin-subdomain",
|
||||
headers={"host": "admin.platform.com"}
|
||||
"/test-admin-subdomain", headers={"host": "admin.platform.com"}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -71,12 +83,17 @@ class TestMiddlewareStackIntegration:
|
||||
def test_api_path_sets_api_context(self, client):
|
||||
"""Test that /api/* paths set API context type."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/api/test-context")
|
||||
async def test_api_context(request: Request):
|
||||
return {
|
||||
"context_type": request.state.context_type.value if hasattr(request.state, 'context_type') else None
|
||||
"context_type": (
|
||||
request.state.context_type.value
|
||||
if hasattr(request.state, "context_type")
|
||||
else None
|
||||
)
|
||||
}
|
||||
|
||||
response = client.get("/api/test-context")
|
||||
@@ -89,25 +106,40 @@ class TestMiddlewareStackIntegration:
|
||||
# Vendor Dashboard Context Tests
|
||||
# ========================================================================
|
||||
|
||||
def test_vendor_dashboard_path_sets_vendor_context(self, client, vendor_with_subdomain):
|
||||
def test_vendor_dashboard_path_sets_vendor_context(
|
||||
self, client, vendor_with_subdomain
|
||||
):
|
||||
"""Test that /vendor/* paths with vendor set VENDOR_DASHBOARD context."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/vendor/test-context")
|
||||
async def test_vendor_context(request: Request):
|
||||
return {
|
||||
"context_type": request.state.context_type.value if hasattr(request.state, 'context_type') else None,
|
||||
"vendor_id": request.state.vendor_id if hasattr(request.state, 'vendor_id') else None,
|
||||
"vendor_code": request.state.vendor.code if hasattr(request.state, 'vendor') else None
|
||||
"context_type": (
|
||||
request.state.context_type.value
|
||||
if hasattr(request.state, "context_type")
|
||||
else None
|
||||
),
|
||||
"vendor_id": (
|
||||
request.state.vendor_id
|
||||
if hasattr(request.state, "vendor_id")
|
||||
else None
|
||||
),
|
||||
"vendor_code": (
|
||||
request.state.vendor.code
|
||||
if hasattr(request.state, "vendor")
|
||||
else None
|
||||
),
|
||||
}
|
||||
|
||||
# Request with vendor subdomain
|
||||
with patch('app.core.config.settings') as mock_settings:
|
||||
with patch("app.core.config.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
response = client.get(
|
||||
"/vendor/test-context",
|
||||
headers={"host": f"{vendor_with_subdomain.subdomain}.platform.com"}
|
||||
headers={"host": f"{vendor_with_subdomain.subdomain}.platform.com"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -120,24 +152,35 @@ class TestMiddlewareStackIntegration:
|
||||
# Shop Context Tests
|
||||
# ========================================================================
|
||||
|
||||
def test_shop_path_with_subdomain_sets_shop_context(self, client, vendor_with_subdomain):
|
||||
def test_shop_path_with_subdomain_sets_shop_context(
|
||||
self, client, vendor_with_subdomain
|
||||
):
|
||||
"""Test that /shop/* paths with vendor subdomain set SHOP context."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/shop/test-context")
|
||||
async def test_shop_context(request: Request):
|
||||
return {
|
||||
"context_type": request.state.context_type.value if hasattr(request.state, 'context_type') else None,
|
||||
"vendor_id": request.state.vendor_id if hasattr(request.state, 'vendor_id') else None,
|
||||
"has_theme": hasattr(request.state, 'theme')
|
||||
"context_type": (
|
||||
request.state.context_type.value
|
||||
if hasattr(request.state, "context_type")
|
||||
else None
|
||||
),
|
||||
"vendor_id": (
|
||||
request.state.vendor_id
|
||||
if hasattr(request.state, "vendor_id")
|
||||
else None
|
||||
),
|
||||
"has_theme": hasattr(request.state, "theme"),
|
||||
}
|
||||
|
||||
with patch('app.core.config.settings') as mock_settings:
|
||||
with patch("app.core.config.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
response = client.get(
|
||||
"/shop/test-context",
|
||||
headers={"host": f"{vendor_with_subdomain.subdomain}.platform.com"}
|
||||
headers={"host": f"{vendor_with_subdomain.subdomain}.platform.com"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -146,23 +189,33 @@ class TestMiddlewareStackIntegration:
|
||||
assert data["vendor_id"] == vendor_with_subdomain.id
|
||||
assert data["has_theme"] is True
|
||||
|
||||
def test_shop_path_with_custom_domain_sets_shop_context(self, client, vendor_with_custom_domain):
|
||||
def test_shop_path_with_custom_domain_sets_shop_context(
|
||||
self, client, vendor_with_custom_domain
|
||||
):
|
||||
"""Test that /shop/* paths with custom domain set SHOP context."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/shop/test-custom-domain")
|
||||
async def test_shop_custom_domain(request: Request):
|
||||
return {
|
||||
"context_type": request.state.context_type.value if hasattr(request.state, 'context_type') else None,
|
||||
"vendor_id": request.state.vendor_id if hasattr(request.state, 'vendor_id') else None
|
||||
"context_type": (
|
||||
request.state.context_type.value
|
||||
if hasattr(request.state, "context_type")
|
||||
else None
|
||||
),
|
||||
"vendor_id": (
|
||||
request.state.vendor_id
|
||||
if hasattr(request.state, "vendor_id")
|
||||
else None
|
||||
),
|
||||
}
|
||||
|
||||
with patch('app.core.config.settings') as mock_settings:
|
||||
with patch("app.core.config.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
response = client.get(
|
||||
"/shop/test-custom-domain",
|
||||
headers={"host": "customdomain.com"}
|
||||
"/shop/test-custom-domain", headers={"host": "customdomain.com"}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -174,9 +227,12 @@ class TestMiddlewareStackIntegration:
|
||||
# Middleware Execution Order Tests
|
||||
# ========================================================================
|
||||
|
||||
def test_vendor_context_runs_before_context_detection(self, client, vendor_with_subdomain):
|
||||
def test_vendor_context_runs_before_context_detection(
|
||||
self, client, vendor_with_subdomain
|
||||
):
|
||||
"""Test that VendorContextMiddleware runs before ContextDetectionMiddleware."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/test-execution-order")
|
||||
@@ -184,16 +240,16 @@ class TestMiddlewareStackIntegration:
|
||||
# If vendor context runs first, clean_path should be available
|
||||
# before context detection uses it
|
||||
return {
|
||||
"has_vendor": hasattr(request.state, 'vendor'),
|
||||
"has_clean_path": hasattr(request.state, 'clean_path'),
|
||||
"has_context_type": hasattr(request.state, 'context_type')
|
||||
"has_vendor": hasattr(request.state, "vendor"),
|
||||
"has_clean_path": hasattr(request.state, "clean_path"),
|
||||
"has_context_type": hasattr(request.state, "context_type"),
|
||||
}
|
||||
|
||||
with patch('app.core.config.settings') as mock_settings:
|
||||
with patch("app.core.config.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
response = client.get(
|
||||
"/test-execution-order",
|
||||
headers={"host": f"{vendor_with_subdomain.subdomain}.platform.com"}
|
||||
headers={"host": f"{vendor_with_subdomain.subdomain}.platform.com"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -206,21 +262,26 @@ class TestMiddlewareStackIntegration:
|
||||
def test_theme_context_runs_after_vendor_context(self, client, vendor_with_theme):
|
||||
"""Test that ThemeContextMiddleware runs after VendorContextMiddleware."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/test-theme-loading")
|
||||
async def test_theme_loading(request: Request):
|
||||
return {
|
||||
"has_vendor": hasattr(request.state, 'vendor'),
|
||||
"has_theme": hasattr(request.state, 'theme'),
|
||||
"theme_primary_color": request.state.theme.get('primary_color') if hasattr(request.state, 'theme') else None
|
||||
"has_vendor": hasattr(request.state, "vendor"),
|
||||
"has_theme": hasattr(request.state, "theme"),
|
||||
"theme_primary_color": (
|
||||
request.state.theme.get("primary_color")
|
||||
if hasattr(request.state, "theme")
|
||||
else None
|
||||
),
|
||||
}
|
||||
|
||||
with patch('app.core.config.settings') as mock_settings:
|
||||
with patch("app.core.config.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
response = client.get(
|
||||
"/test-theme-loading",
|
||||
headers={"host": f"{vendor_with_theme.subdomain}.platform.com"}
|
||||
headers={"host": f"{vendor_with_theme.subdomain}.platform.com"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -249,21 +310,27 @@ class TestMiddlewareStackIntegration:
|
||||
def test_missing_vendor_graceful_handling(self, client):
|
||||
"""Test that missing vendor is handled gracefully."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/test-missing-vendor")
|
||||
async def test_missing_vendor(request: Request):
|
||||
return {
|
||||
"has_vendor": hasattr(request.state, 'vendor'),
|
||||
"vendor": request.state.vendor if hasattr(request.state, 'vendor') else None,
|
||||
"context_type": request.state.context_type.value if hasattr(request.state, 'context_type') else None
|
||||
"has_vendor": hasattr(request.state, "vendor"),
|
||||
"vendor": (
|
||||
request.state.vendor if hasattr(request.state, "vendor") else None
|
||||
),
|
||||
"context_type": (
|
||||
request.state.context_type.value
|
||||
if hasattr(request.state, "context_type")
|
||||
else None
|
||||
),
|
||||
}
|
||||
|
||||
with patch('app.core.config.settings') as mock_settings:
|
||||
with patch("app.core.config.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
response = client.get(
|
||||
"/test-missing-vendor",
|
||||
headers={"host": "nonexistent.platform.com"}
|
||||
"/test-missing-vendor", headers={"host": "nonexistent.platform.com"}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -276,20 +343,23 @@ class TestMiddlewareStackIntegration:
|
||||
def test_inactive_vendor_not_loaded(self, client, inactive_vendor):
|
||||
"""Test that inactive vendors are not loaded."""
|
||||
from fastapi import Request
|
||||
|
||||
from main import app
|
||||
|
||||
@app.get("/test-inactive-vendor")
|
||||
async def test_inactive_vendor_endpoint(request: Request):
|
||||
return {
|
||||
"has_vendor": hasattr(request.state, 'vendor'),
|
||||
"vendor": request.state.vendor if hasattr(request.state, 'vendor') else None
|
||||
"has_vendor": hasattr(request.state, "vendor"),
|
||||
"vendor": (
|
||||
request.state.vendor if hasattr(request.state, "vendor") else None
|
||||
),
|
||||
}
|
||||
|
||||
with patch('app.core.config.settings') as mock_settings:
|
||||
with patch("app.core.config.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
response = client.get(
|
||||
"/test-inactive-vendor",
|
||||
headers={"host": f"{inactive_vendor.subdomain}.platform.com"}
|
||||
headers={"host": f"{inactive_vendor.subdomain}.platform.com"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user