fix: correctly price unlimited traffic (0 GB) in classic subscription mode

_calculate_traffic_price treated unlimited traffic as free because
base_gb=0 triggered the `if base_gb > 0 else 0` guard, skipping
the price lookup. Added early return for total_gb==0 to use the
configured unlimited tier price.
This commit is contained in:
Fringg
2026-03-23 06:42:57 +03:00
parent 0fe3c217f7
commit aec04f0085
2 changed files with 15 additions and 3 deletions
+5
View File
@@ -479,6 +479,11 @@ class PricingEngine:
Prevents purchased top-ups from inflating the tier lookup."""
total_gb = traffic_limit_gb or 0
purchased_gb = purchased_traffic_gb or 0
# 0 = unlimited traffic — has its own price tier, return directly
if total_gb == 0:
return settings.get_traffic_price(0)
base_gb = max(0, total_gb - purchased_gb)
base_price = settings.get_traffic_price(base_gb) if base_gb > 0 else 0
+10 -3
View File
@@ -232,12 +232,19 @@ class TestCalculateTrafficPrice:
price = engine._calculate_traffic_price(traffic_limit_gb=125, purchased_traffic_gb=100)
assert price == 11000 # NOT 12000
def test_zero_traffic(self):
def test_unlimited_traffic_has_price(self):
engine = PricingEngine()
with patch('app.services.pricing_engine.settings') as ms:
ms.get_traffic_price.return_value = 0
ms.get_traffic_price.side_effect = lambda gb: {0: 20000, 5: 2000}.get(gb, 0)
price = engine._calculate_traffic_price(traffic_limit_gb=0, purchased_traffic_gb=0)
assert price == 0
assert price == 20000 # 0 GB = unlimited, charged at unlimited tier
def test_unlimited_traffic_ignores_purchased(self):
engine = PricingEngine()
with patch('app.services.pricing_engine.settings') as ms:
ms.get_traffic_price.side_effect = lambda gb: {0: 20000, 50: 5000}.get(gb, 0)
price = engine._calculate_traffic_price(traffic_limit_gb=0, purchased_traffic_gb=50)
assert price == 20000 # unlimited tier, purchased ignored
def test_purchased_exceeds_total(self):
engine = PricingEngine()