feat: platform-aware storefront routing and billing improvements
Overhaul storefront URL routing to be platform-aware:
- Dev: /platforms/{code}/storefront/{store_code}/
- Prod: subdomain.platform.lu/ (internally rewritten to /storefront/)
- Add subdomain detection in PlatformContextMiddleware
- Add /storefront/ path rewrite for prod mode (subdomain/custom domain)
- Remove all silent platform fallbacks (platform_id=1)
- Add require_platform dependency for clean endpoint validation
- Update route registration, templates, module definitions, base_url calc
- Update StoreContextMiddleware for /storefront/ path detection
- Remove /stores/ from FrontendDetector STOREFRONT_PATH_PREFIXES
Billing service improvements:
- Add store_platform_sync_service to keep store_platforms in sync
- Make tier lookups platform-aware across billing services
- Add tiers for all platforms in seed data
- Add demo subscriptions to seed
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -497,7 +497,7 @@ class TestAdminGetStats:
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_billing_tiers(db):
|
||||
def admin_billing_tiers(db, test_platform):
|
||||
"""Create essential, professional, business tiers for admin tests."""
|
||||
tiers = [
|
||||
SubscriptionTier(
|
||||
@@ -508,6 +508,7 @@ def admin_billing_tiers(db):
|
||||
display_order=1,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
SubscriptionTier(
|
||||
code="professional",
|
||||
@@ -517,6 +518,7 @@ def admin_billing_tiers(db):
|
||||
display_order=2,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
SubscriptionTier(
|
||||
code="business",
|
||||
@@ -526,6 +528,7 @@ def admin_billing_tiers(db):
|
||||
display_order=3,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
]
|
||||
db.add_all(tiers)
|
||||
|
||||
@@ -603,7 +603,7 @@ class TestBillingServiceUpcomingInvoice:
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bs_tier_essential(db):
|
||||
def bs_tier_essential(db, test_platform):
|
||||
"""Create essential subscription tier."""
|
||||
tier = SubscriptionTier(
|
||||
code="essential",
|
||||
@@ -614,6 +614,7 @@ def bs_tier_essential(db):
|
||||
display_order=1,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
)
|
||||
db.add(tier)
|
||||
db.commit()
|
||||
@@ -622,7 +623,7 @@ def bs_tier_essential(db):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bs_tiers(db):
|
||||
def bs_tiers(db, test_platform):
|
||||
"""Create three tiers without Stripe config."""
|
||||
tiers = [
|
||||
SubscriptionTier(
|
||||
@@ -633,6 +634,7 @@ def bs_tiers(db):
|
||||
display_order=1,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
SubscriptionTier(
|
||||
code="professional",
|
||||
@@ -642,6 +644,7 @@ def bs_tiers(db):
|
||||
display_order=2,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
SubscriptionTier(
|
||||
code="business",
|
||||
@@ -651,6 +654,7 @@ def bs_tiers(db):
|
||||
display_order=3,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
]
|
||||
db.add_all(tiers)
|
||||
@@ -661,7 +665,7 @@ def bs_tiers(db):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bs_tiers_with_stripe(db):
|
||||
def bs_tiers_with_stripe(db, test_platform):
|
||||
"""Create tiers with Stripe price IDs configured."""
|
||||
tiers = [
|
||||
SubscriptionTier(
|
||||
@@ -672,6 +676,7 @@ def bs_tiers_with_stripe(db):
|
||||
display_order=1,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
stripe_product_id="prod_essential",
|
||||
stripe_price_monthly_id="price_ess_monthly",
|
||||
stripe_price_annual_id="price_ess_annual",
|
||||
@@ -684,6 +689,7 @@ def bs_tiers_with_stripe(db):
|
||||
display_order=2,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
stripe_product_id="prod_professional",
|
||||
stripe_price_monthly_id="price_pro_monthly",
|
||||
stripe_price_annual_id="price_pro_annual",
|
||||
@@ -696,6 +702,7 @@ def bs_tiers_with_stripe(db):
|
||||
display_order=3,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
stripe_product_id="prod_business",
|
||||
stripe_price_monthly_id="price_biz_monthly",
|
||||
stripe_price_annual_id="price_biz_annual",
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
# app/modules/billing/tests/unit/test_store_platform_sync.py
|
||||
"""Unit tests for StorePlatformSync service."""
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from app.modules.billing.models import (
|
||||
MerchantSubscription,
|
||||
SubscriptionStatus,
|
||||
SubscriptionTier,
|
||||
)
|
||||
from app.modules.billing.services.store_platform_sync_service import StorePlatformSync
|
||||
from app.modules.tenancy.models import StorePlatform
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.billing
|
||||
class TestStorePlatformSyncCreate:
|
||||
"""Tests for creating StorePlatform entries via sync."""
|
||||
|
||||
def setup_method(self):
|
||||
self.service = StorePlatformSync()
|
||||
|
||||
def test_sync_creates_store_platform(self, db, test_store, test_platform):
|
||||
"""Sync with is_active=True creates a new StorePlatform entry."""
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_store.merchant_id, test_platform.id, is_active=True
|
||||
)
|
||||
|
||||
sp = (
|
||||
db.query(StorePlatform)
|
||||
.filter(
|
||||
StorePlatform.store_id == test_store.id,
|
||||
StorePlatform.platform_id == test_platform.id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
assert sp is not None
|
||||
assert sp.is_active is True
|
||||
|
||||
def test_sync_sets_primary_when_none(self, db, test_store, test_platform):
|
||||
"""First platform synced for a store gets is_primary=True."""
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_store.merchant_id, test_platform.id, is_active=True
|
||||
)
|
||||
|
||||
sp = (
|
||||
db.query(StorePlatform)
|
||||
.filter(
|
||||
StorePlatform.store_id == test_store.id,
|
||||
StorePlatform.platform_id == test_platform.id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
assert sp.is_primary is True
|
||||
|
||||
def test_sync_no_primary_override(self, db, test_store, test_platform, another_platform):
|
||||
"""Second platform synced does not override existing primary."""
|
||||
# First platform becomes primary
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_store.merchant_id, test_platform.id, is_active=True
|
||||
)
|
||||
# Second platform should not be primary
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_store.merchant_id, another_platform.id, is_active=True
|
||||
)
|
||||
|
||||
sp1 = (
|
||||
db.query(StorePlatform)
|
||||
.filter(
|
||||
StorePlatform.store_id == test_store.id,
|
||||
StorePlatform.platform_id == test_platform.id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
sp2 = (
|
||||
db.query(StorePlatform)
|
||||
.filter(
|
||||
StorePlatform.store_id == test_store.id,
|
||||
StorePlatform.platform_id == another_platform.id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
assert sp1.is_primary is True
|
||||
assert sp2.is_primary is False
|
||||
|
||||
def test_sync_sets_tier_id(self, db, test_store, test_platform, sync_tier):
|
||||
"""Sync passes tier_id to newly created StorePlatform."""
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_store.merchant_id, test_platform.id,
|
||||
is_active=True, tier_id=sync_tier.id,
|
||||
)
|
||||
|
||||
sp = (
|
||||
db.query(StorePlatform)
|
||||
.filter(
|
||||
StorePlatform.store_id == test_store.id,
|
||||
StorePlatform.platform_id == test_platform.id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
assert sp.tier_id == sync_tier.id
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.billing
|
||||
class TestStorePlatformSyncUpdate:
|
||||
"""Tests for updating existing StorePlatform entries via sync."""
|
||||
|
||||
def setup_method(self):
|
||||
self.service = StorePlatformSync()
|
||||
|
||||
def test_sync_updates_existing_is_active(self, db, test_store, test_platform):
|
||||
"""Sync updates is_active on existing StorePlatform."""
|
||||
# Create initial entry
|
||||
sp = StorePlatform(
|
||||
store_id=test_store.id,
|
||||
platform_id=test_platform.id,
|
||||
is_active=True,
|
||||
is_primary=True,
|
||||
)
|
||||
db.add(sp)
|
||||
db.flush()
|
||||
|
||||
# Deactivate via sync
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_store.merchant_id, test_platform.id, is_active=False
|
||||
)
|
||||
|
||||
db.refresh(sp)
|
||||
assert sp.is_active is False
|
||||
|
||||
def test_sync_updates_tier_id(self, db, test_store, test_platform, sync_tier):
|
||||
"""Sync updates tier_id on existing StorePlatform."""
|
||||
sp = StorePlatform(
|
||||
store_id=test_store.id,
|
||||
platform_id=test_platform.id,
|
||||
is_active=True,
|
||||
is_primary=True,
|
||||
)
|
||||
db.add(sp)
|
||||
db.flush()
|
||||
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_store.merchant_id, test_platform.id,
|
||||
is_active=True, tier_id=sync_tier.id,
|
||||
)
|
||||
|
||||
db.refresh(sp)
|
||||
assert sp.tier_id == sync_tier.id
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.billing
|
||||
class TestStorePlatformSyncEdgeCases:
|
||||
"""Tests for edge cases in sync."""
|
||||
|
||||
def setup_method(self):
|
||||
self.service = StorePlatformSync()
|
||||
|
||||
def test_sync_noop_inactive_missing(self, db, test_store, test_platform):
|
||||
"""Sync with is_active=False for non-existent entry is a no-op."""
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_store.merchant_id, test_platform.id, is_active=False
|
||||
)
|
||||
|
||||
sp = (
|
||||
db.query(StorePlatform)
|
||||
.filter(
|
||||
StorePlatform.store_id == test_store.id,
|
||||
StorePlatform.platform_id == test_platform.id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
assert sp is None
|
||||
|
||||
def test_sync_no_stores(self, db, test_platform):
|
||||
"""Sync with no stores for merchant is a no-op (no error)."""
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, 99999, test_platform.id, is_active=True
|
||||
)
|
||||
# No assertion needed — just verifying no exception
|
||||
|
||||
def test_sync_multiple_stores(self, db, test_merchant, test_platform):
|
||||
"""Sync creates entries for all stores of a merchant."""
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
store1 = Store(
|
||||
merchant_id=test_merchant.id,
|
||||
store_code="SYNC_TEST_1",
|
||||
name="Sync Store 1",
|
||||
subdomain="sync-test-1",
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
created_at=datetime.now(UTC),
|
||||
updated_at=datetime.now(UTC),
|
||||
)
|
||||
store2 = Store(
|
||||
merchant_id=test_merchant.id,
|
||||
store_code="SYNC_TEST_2",
|
||||
name="Sync Store 2",
|
||||
subdomain="sync-test-2",
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
created_at=datetime.now(UTC),
|
||||
updated_at=datetime.now(UTC),
|
||||
)
|
||||
db.add_all([store1, store2])
|
||||
db.flush()
|
||||
|
||||
self.service.sync_store_platforms_for_merchant(
|
||||
db, test_merchant.id, test_platform.id, is_active=True
|
||||
)
|
||||
|
||||
count = (
|
||||
db.query(StorePlatform)
|
||||
.filter(
|
||||
StorePlatform.platform_id == test_platform.id,
|
||||
StorePlatform.store_id.in_([store1.id, store2.id]),
|
||||
)
|
||||
.count()
|
||||
)
|
||||
assert count == 2
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Fixtures
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sync_tier(db, test_platform):
|
||||
"""Create a tier for sync tests."""
|
||||
tier = SubscriptionTier(
|
||||
platform_id=test_platform.id,
|
||||
code="essential",
|
||||
name="Essential",
|
||||
price_monthly_cents=2900,
|
||||
display_order=1,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
)
|
||||
db.add(tier)
|
||||
db.commit()
|
||||
db.refresh(tier)
|
||||
return tier
|
||||
@@ -502,7 +502,7 @@ class TestSubscriptionServiceReactivate:
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def billing_tier_essential(db):
|
||||
def billing_tier_essential(db, test_platform):
|
||||
"""Create essential subscription tier."""
|
||||
tier = SubscriptionTier(
|
||||
code="essential",
|
||||
@@ -513,6 +513,7 @@ def billing_tier_essential(db):
|
||||
display_order=1,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
)
|
||||
db.add(tier)
|
||||
db.commit()
|
||||
@@ -521,7 +522,7 @@ def billing_tier_essential(db):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def billing_tiers(db):
|
||||
def billing_tiers(db, test_platform):
|
||||
"""Create essential, professional, and business tiers."""
|
||||
tiers = [
|
||||
SubscriptionTier(
|
||||
@@ -532,6 +533,7 @@ def billing_tiers(db):
|
||||
display_order=1,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
SubscriptionTier(
|
||||
code="professional",
|
||||
@@ -541,6 +543,7 @@ def billing_tiers(db):
|
||||
display_order=2,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
SubscriptionTier(
|
||||
code="business",
|
||||
@@ -550,6 +553,7 @@ def billing_tiers(db):
|
||||
display_order=3,
|
||||
is_active=True,
|
||||
is_public=True,
|
||||
platform_id=test_platform.id,
|
||||
),
|
||||
]
|
||||
db.add_all(tiers)
|
||||
|
||||
Reference in New Issue
Block a user