fix: update tests for current API structure and model changes

- Fix middleware fixtures: vendor_code instead of code, add owner_user_id to company
- Fix performance tests: MarketplaceProduct uses translations for title/description
- Fix security tests: use correct API endpoints (/api/v1/admin/*, /api/v1/vendor/*)
- Fix workflow tests: use actual admin API endpoints
- Fix background task tests: remove invalid vendor_name field, add language

Note: Many middleware integration tests still fail due to dynamic routes
being caught by the /{slug} catch-all route. These tests need further
refactoring to use /api/* prefixed routes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-13 16:01:16 +01:00
parent f749cfc081
commit 4cb4fec8e2
9 changed files with 248 additions and 281 deletions

View File

@@ -3,18 +3,44 @@
Fixtures specific to middleware integration tests.
"""
import uuid
import pytest
from models.database.company import Company
from models.database.vendor import Vendor
from models.database.vendor_domain import VendorDomain
from models.database.vendor_theme import VendorTheme
@pytest.fixture
def vendor_with_subdomain(db):
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(
name="Test Vendor", code="testvendor", subdomain="testvendor", is_active=True
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()
@@ -23,13 +49,16 @@ def vendor_with_subdomain(db):
@pytest.fixture
def vendor_with_custom_domain(db):
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",
code="customvendor",
vendor_code=f"CUSTOMVENDOR_{unique_id.upper()}",
subdomain="customvendor",
is_active=True,
is_verified=True,
)
db.add(vendor)
db.commit()
@@ -46,13 +75,16 @@ def vendor_with_custom_domain(db):
@pytest.fixture
def vendor_with_theme(db):
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",
code="themedvendor",
vendor_code=f"THEMEDVENDOR_{unique_id.upper()}",
subdomain="themedvendor",
is_active=True,
is_verified=True,
)
db.add(vendor)
db.commit()
@@ -74,10 +106,16 @@ def vendor_with_theme(db):
@pytest.fixture
def inactive_vendor(db):
def middleware_inactive_vendor(db, middleware_test_company):
"""Create an inactive vendor for testing."""
unique_id = str(uuid.uuid4())[:8]
vendor = Vendor(
name="Inactive Vendor", code="inactive", subdomain="inactive", is_active=False
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()

View File

@@ -339,7 +339,7 @@ class TestMiddlewareStackIntegration:
# Should still set a context type (fallback)
assert data["context_type"] is not None
def test_inactive_vendor_not_loaded(self, client, inactive_vendor):
def test_inactive_vendor_not_loaded(self, client, middleware_inactive_vendor):
"""Test that inactive vendors are not loaded."""
from fastapi import Request
@@ -358,7 +358,7 @@ class TestMiddlewareStackIntegration:
mock_settings.platform_domain = "platform.com"
response = client.get(
"/test-inactive-vendor",
headers={"host": f"{inactive_vendor.subdomain}.platform.com"},
headers={"host": f"{middleware_inactive_vendor.subdomain}.platform.com"},
)
assert response.status_code == 200

View File

@@ -38,7 +38,7 @@ class TestVendorContextFlow:
else None
),
"vendor_code": (
request.state.vendor.code
request.state.vendor.vendor_code
if hasattr(request.state, "vendor") and request.state.vendor
else None
),
@@ -61,7 +61,7 @@ class TestVendorContextFlow:
data = response.json()
assert data["vendor_detected"] is True
assert data["vendor_id"] == vendor_with_subdomain.id
assert data["vendor_code"] == vendor_with_subdomain.code
assert data["vendor_code"] == vendor_with_subdomain.vendor_code
assert data["vendor_name"] == vendor_with_subdomain.name
def test_subdomain_with_port_detection(self, client, vendor_with_subdomain):
@@ -76,7 +76,7 @@ class TestVendorContextFlow:
"vendor_detected": hasattr(request.state, "vendor")
and request.state.vendor is not None,
"vendor_code": (
request.state.vendor.code
request.state.vendor.vendor_code
if hasattr(request.state, "vendor") and request.state.vendor
else None
),
@@ -94,7 +94,7 @@ class TestVendorContextFlow:
assert response.status_code == 200
data = response.json()
assert data["vendor_detected"] is True
assert data["vendor_code"] == vendor_with_subdomain.code
assert data["vendor_code"] == vendor_with_subdomain.vendor_code
def test_nonexistent_subdomain_returns_no_vendor(self, client):
"""Test that nonexistent subdomain doesn't crash and returns no vendor."""
@@ -144,7 +144,7 @@ class TestVendorContextFlow:
else None
),
"vendor_code": (
request.state.vendor.code
request.state.vendor.vendor_code
if hasattr(request.state, "vendor") and request.state.vendor
else None
),
@@ -161,7 +161,7 @@ class TestVendorContextFlow:
data = response.json()
assert data["vendor_detected"] is True
assert data["vendor_id"] == vendor_with_custom_domain.id
assert data["vendor_code"] == vendor_with_custom_domain.code
assert data["vendor_code"] == vendor_with_custom_domain.vendor_code
def test_custom_domain_with_www_detection(self, client, vendor_with_custom_domain):
"""Test vendor detection via custom domain with www prefix."""
@@ -175,7 +175,7 @@ class TestVendorContextFlow:
"vendor_detected": hasattr(request.state, "vendor")
and request.state.vendor is not None,
"vendor_code": (
request.state.vendor.code
request.state.vendor.vendor_code
if hasattr(request.state, "vendor") and request.state.vendor
else None
),
@@ -211,7 +211,7 @@ class TestVendorContextFlow:
and request.state.vendor is not None,
"vendor_code_param": vendor_code,
"vendor_code_state": (
request.state.vendor.code
request.state.vendor.vendor_code
if hasattr(request.state, "vendor") and request.state.vendor
else None
),
@@ -225,15 +225,15 @@ class TestVendorContextFlow:
with patch("app.core.config.settings") as mock_settings:
mock_settings.platform_domain = "platform.com"
response = client.get(
f"/vendors/{vendor_with_subdomain.code}/test-path",
f"/vendors/{vendor_with_subdomain.vendor_code}/test-path",
headers={"host": "localhost:8000"},
)
assert response.status_code == 200
data = response.json()
assert data["vendor_detected"] is True
assert data["vendor_code_param"] == vendor_with_subdomain.code
assert data["vendor_code_state"] == vendor_with_subdomain.code
assert data["vendor_code_param"] == vendor_with_subdomain.vendor_code
assert data["vendor_code_state"] == vendor_with_subdomain.vendor_code
def test_path_based_vendor_detection_vendor_prefix(
self, client, vendor_with_subdomain
@@ -249,7 +249,7 @@ class TestVendorContextFlow:
"vendor_detected": hasattr(request.state, "vendor")
and request.state.vendor is not None,
"vendor_code": (
request.state.vendor.code
request.state.vendor.vendor_code
if hasattr(request.state, "vendor") and request.state.vendor
else None
),
@@ -258,14 +258,14 @@ class TestVendorContextFlow:
with patch("app.core.config.settings") as mock_settings:
mock_settings.platform_domain = "platform.com"
response = client.get(
f"/vendor/{vendor_with_subdomain.code}/test",
f"/vendor/{vendor_with_subdomain.vendor_code}/test",
headers={"host": "localhost:8000"},
)
assert response.status_code == 200
data = response.json()
assert data["vendor_detected"] is True
assert data["vendor_code"] == vendor_with_subdomain.code
assert data["vendor_code"] == vendor_with_subdomain.vendor_code
# ========================================================================
# Clean Path Extraction Tests
@@ -293,7 +293,7 @@ class TestVendorContextFlow:
with patch("app.core.config.settings") as mock_settings:
mock_settings.platform_domain = "platform.com"
response = client.get(
f"/vendors/{vendor_with_subdomain.code}/shop/products",
f"/vendors/{vendor_with_subdomain.vendor_code}/shop/products",
headers={"host": "localhost:8000"},
)
@@ -302,7 +302,7 @@ class TestVendorContextFlow:
# Clean path should have vendor prefix removed
assert data["clean_path"] == "/shop/products"
assert (
f"/vendors/{vendor_with_subdomain.code}/shop/products"
f"/vendors/{vendor_with_subdomain.vendor_code}/shop/products"
in data["original_path"]
)
@@ -396,7 +396,7 @@ class TestVendorContextFlow:
{
"id": vendor.id if vendor else None,
"name": vendor.name if vendor else None,
"code": vendor.code if vendor else None,
"code": vendor.vendor_code if vendor else None,
"subdomain": vendor.subdomain if vendor else None,
"is_active": vendor.is_active if vendor else None,
}
@@ -417,14 +417,14 @@ class TestVendorContextFlow:
assert data["has_vendor"] is True
assert data["vendor_attributes"]["id"] == vendor_with_subdomain.id
assert data["vendor_attributes"]["name"] == vendor_with_subdomain.name
assert data["vendor_attributes"]["code"] == vendor_with_subdomain.code
assert data["vendor_attributes"]["code"] == vendor_with_subdomain.vendor_code
assert data["vendor_attributes"]["is_active"] is True
# ========================================================================
# Edge Cases and Error Handling
# ========================================================================
def test_inactive_vendor_not_detected(self, client, inactive_vendor):
def test_inactive_vendor_not_detected(self, client, middleware_inactive_vendor):
"""Test that inactive vendors are not detected."""
from fastapi import Request
@@ -441,7 +441,7 @@ class TestVendorContextFlow:
mock_settings.platform_domain = "platform.com"
response = client.get(
"/test-inactive-vendor-detection",
headers={"host": f"{inactive_vendor.subdomain}.platform.com"},
headers={"host": f"{middleware_inactive_vendor.subdomain}.platform.com"},
)
assert response.status_code == 200