Some checks failed
The setting `settings.platform_domain` (the global/main domain like "wizard.lu") was easily confused with `platform.domain` (per-platform domain like "rewardflow.lu"). Renamed to `settings.main_domain` / `MAIN_DOMAIN` env var across the entire codebase. Also updated docs to reflect the refactored store detection logic with `is_platform_domain` / `is_subdomain_of_platform` guards. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
149 lines
6.1 KiB
Python
149 lines
6.1 KiB
Python
# tests/integration/middleware/test_store_context_flow.py
|
|
"""
|
|
Integration tests for store context detection end-to-end flow.
|
|
|
|
These tests verify that store detection works correctly through real HTTP requests
|
|
for all routing modes: subdomain, custom domain, and path-based.
|
|
|
|
Note: These tests require the middleware conftest.py which patches:
|
|
1. get_db in middleware modules to use the test database session
|
|
2. settings.main_domain to use 'platform.com' for testing subdomain detection
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.mark.integration
|
|
@pytest.mark.middleware
|
|
@pytest.mark.store
|
|
class TestStoreContextFlow:
|
|
"""Test store context detection through real HTTP requests."""
|
|
|
|
# ========================================================================
|
|
# Subdomain Detection Tests
|
|
# ========================================================================
|
|
|
|
def test_subdomain_store_detection(self, client, store_with_subdomain):
|
|
"""Test store detection via subdomain routing."""
|
|
response = client.get(
|
|
"/middleware-test/subdomain-detection",
|
|
headers={"host": f"{store_with_subdomain.subdomain}.platform.com"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["store_detected"] is True
|
|
assert data["store_code"] == store_with_subdomain.store_code
|
|
assert data["store_name"] == store_with_subdomain.name
|
|
|
|
def test_subdomain_with_port_detection(self, client, store_with_subdomain):
|
|
"""Test store detection via subdomain with port number."""
|
|
response = client.get(
|
|
"/middleware-test/subdomain-port",
|
|
headers={"host": f"{store_with_subdomain.subdomain}.platform.com:8000"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["store_detected"] is True
|
|
assert data["store_code"] == store_with_subdomain.store_code
|
|
|
|
def test_nonexistent_subdomain_returns_no_store(self, client):
|
|
"""Test that nonexistent subdomain doesn't crash and returns no store."""
|
|
response = client.get(
|
|
"/middleware-test/nonexistent-subdomain",
|
|
headers={"host": "nonexistent.platform.com"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["store_detected"] is False
|
|
|
|
# ========================================================================
|
|
# Custom Domain Detection Tests
|
|
# ========================================================================
|
|
|
|
def test_custom_domain_store_detection(self, client, store_with_custom_domain):
|
|
"""Test store detection via custom domain."""
|
|
response = client.get(
|
|
"/middleware-test/custom-domain", headers={"host": "customdomain.com"}
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
# Custom domain detection requires is_verified=True on the domain
|
|
# If store not detected, it's because the domain isn't verified
|
|
# This is expected behavior - adjust assertion based on fixture setup
|
|
if data["store_detected"]:
|
|
assert data["store_code"] == store_with_custom_domain.store_code
|
|
|
|
def test_custom_domain_with_www_detection(self, client, store_with_custom_domain):
|
|
"""Test store detection via custom domain with www prefix."""
|
|
response = client.get(
|
|
"/middleware-test/custom-domain-www",
|
|
headers={"host": "www.customdomain.com"},
|
|
)
|
|
|
|
# This might fail if your implementation doesn't strip www
|
|
# Adjust assertion based on your actual behavior
|
|
assert response.status_code == 200
|
|
|
|
# ========================================================================
|
|
# Store State Injection Tests
|
|
# ========================================================================
|
|
|
|
def test_store_id_injected_into_request_state(self, client, store_with_subdomain):
|
|
"""Test that store_id is correctly injected into request.state."""
|
|
response = client.get(
|
|
"/middleware-test/store-id-injection",
|
|
headers={"host": f"{store_with_subdomain.subdomain}.platform.com"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["has_store_id"] is True
|
|
assert data["store_id"] == store_with_subdomain.id
|
|
assert data["store_id_type"] == "int"
|
|
|
|
def test_store_object_injected_into_request_state(
|
|
self, client, store_with_subdomain
|
|
):
|
|
"""Test that full store object is injected into request.state."""
|
|
response = client.get(
|
|
"/middleware-test/store-object-injection",
|
|
headers={"host": f"{store_with_subdomain.subdomain}.platform.com"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["has_store"] is True
|
|
assert data["store_attributes"]["id"] == store_with_subdomain.id
|
|
assert data["store_attributes"]["name"] == store_with_subdomain.name
|
|
assert data["store_attributes"]["code"] == store_with_subdomain.store_code
|
|
assert data["store_attributes"]["is_active"] is True
|
|
|
|
# ========================================================================
|
|
# Edge Cases and Error Handling
|
|
# ========================================================================
|
|
|
|
def test_inactive_store_not_detected(self, client, middleware_inactive_store):
|
|
"""Test that inactive stores are not detected."""
|
|
response = client.get(
|
|
"/middleware-test/inactive-store-detection",
|
|
headers={"host": f"{middleware_inactive_store.subdomain}.platform.com"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["store_detected"] is False
|
|
|
|
def test_main_domain_without_subdomain_no_store(self, client):
|
|
"""Test that platform domain without subdomain doesn't detect store."""
|
|
response = client.get(
|
|
"/middleware-test/platform-domain", headers={"host": "platform.com"}
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["store_detected"] is False
|