Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /contextfs_contextfs /tests /integration /test_billing.py
| """ | |
| Integration tests for billing and subscription management. | |
| Tests tier limits, device limits, and subscription enforcement. | |
| Requires PostgreSQL. Run with: CONTEXTFS_TEST_POSTGRES=1 pytest tests/integration/test_billing.py | |
| """ | |
| import os | |
| from uuid import uuid4 | |
| import pytest | |
| # Skip if PostgreSQL not available | |
| pytestmark = pytest.mark.skipif( | |
| os.environ.get("CONTEXTFS_TEST_POSTGRES") != "1", | |
| reason="PostgreSQL tests disabled. Set CONTEXTFS_TEST_POSTGRES=1 to enable.", | |
| ) | |
| # Tier limits as defined in billing_routes.py | |
| TIER_LIMITS = { | |
| "free": {"device_limit": 2, "memory_limit": 5000}, | |
| "pro": {"device_limit": 5, "memory_limit": 50000}, | |
| "team": {"device_limit": 10, "memory_limit": -1}, | |
| "enterprise": {"device_limit": -1, "memory_limit": -1}, | |
| "admin": {"device_limit": -1, "memory_limit": -1}, | |
| } | |
| class TestTierLimits: | |
| """Tests for subscription tier limits.""" | |
| def test_free_tier_limits(self): | |
| """Free tier should have 2 devices and 5000 memories.""" | |
| assert TIER_LIMITS["free"]["device_limit"] == 2 | |
| assert TIER_LIMITS["free"]["memory_limit"] == 5000 | |
| def test_pro_tier_limits(self): | |
| """Pro tier should have 5 devices and 50000 memories.""" | |
| assert TIER_LIMITS["pro"]["device_limit"] == 5 | |
| assert TIER_LIMITS["pro"]["memory_limit"] == 50000 | |
| def test_team_tier_limits(self): | |
| """Team tier should have 10 devices and unlimited memories.""" | |
| assert TIER_LIMITS["team"]["device_limit"] == 10 | |
| assert TIER_LIMITS["team"]["memory_limit"] == -1 # Unlimited | |
| def test_enterprise_tier_limits(self): | |
| """Enterprise tier should have unlimited everything.""" | |
| assert TIER_LIMITS["enterprise"]["device_limit"] == -1 | |
| assert TIER_LIMITS["enterprise"]["memory_limit"] == -1 | |
| def test_admin_tier_limits(self): | |
| """Admin tier should have unlimited everything.""" | |
| assert TIER_LIMITS["admin"]["device_limit"] == -1 | |
| assert TIER_LIMITS["admin"]["memory_limit"] == -1 | |
| def test_db_session(): | |
| """Create a test database session.""" | |
| import asyncio | |
| from service.db.session import get_session, init_db | |
| async def setup(): | |
| await init_db() | |
| async with get_session() as session: | |
| yield session | |
| loop = asyncio.new_event_loop() | |
| try: | |
| gen = setup() | |
| session = loop.run_until_complete(gen.__anext__()) | |
| yield session | |
| try: | |
| loop.run_until_complete(gen.__anext__()) | |
| except StopAsyncIteration: | |
| pass | |
| finally: | |
| loop.close() | |
| class TestSubscriptionCreation: | |
| """Tests for creating subscriptions with correct limits.""" | |
| def mock_user_id(self): | |
| """Generate a unique user ID for testing.""" | |
| return f"test_user_{uuid4().hex[:8]}" | |
| def test_new_subscription_has_correct_defaults(self, mock_user_id): | |
| """New subscriptions should have correct default limits when explicitly set.""" | |
| from service.db.models import SubscriptionModel | |
| # Note: SQLAlchemy defaults are applied at database level, not Python level | |
| # When creating objects directly, we need to set the values explicitly | |
| sub = SubscriptionModel( | |
| id=str(uuid4()), | |
| user_id=mock_user_id, | |
| tier="free", | |
| device_limit=2, # Free tier default | |
| memory_limit=5000, # Free tier default | |
| ) | |
| # Check values | |
| assert sub.tier == "free" | |
| assert sub.device_limit == 2 | |
| assert sub.memory_limit == 5000 | |
| def test_subscription_limits_match_tier_config(self): | |
| """Tier configuration should have correct values.""" | |
| # Test the tier configuration itself, not SQLAlchemy defaults | |
| assert TIER_LIMITS["free"]["device_limit"] == 2 | |
| assert TIER_LIMITS["free"]["memory_limit"] == 5000 | |
| class TestDeviceLimitEnforcement: | |
| """Tests for device limit enforcement.""" | |
| def test_device_limit_check_free_tier(self): | |
| """Free tier should be limited to 2 devices.""" | |
| limit = TIER_LIMITS["free"]["device_limit"] | |
| current_count = 2 | |
| # Should not allow adding more devices | |
| assert current_count >= limit | |
| def test_device_limit_check_unlimited(self): | |
| """Unlimited tier (-1) should allow any number of devices.""" | |
| limit = TIER_LIMITS["enterprise"]["device_limit"] | |
| current_count = 1000 | |
| # -1 means unlimited | |
| assert limit == -1 or current_count < limit | |
| def test_device_limit_enforcement_logic(self): | |
| """Test the device limit enforcement logic.""" | |
| def can_add_device(device_limit: int, current_count: int) -> bool: | |
| """Check if user can add another device.""" | |
| if device_limit == -1: # Unlimited | |
| return True | |
| return current_count < device_limit | |
| # Free tier with 2 devices - cannot add more | |
| assert can_add_device(2, 2) is False | |
| # Pro tier with 3 devices - can add 2 more | |
| assert can_add_device(5, 3) is True | |
| # Enterprise with any count - always can add | |
| assert can_add_device(-1, 100) is True | |
| class TestUsageCalculation: | |
| """Tests for usage percentage calculations.""" | |
| def test_usage_percent_normal(self): | |
| """Normal usage percentage calculation.""" | |
| current = 1000 | |
| limit = 5000 | |
| percent = (current / limit) * 100 | |
| assert percent == 20.0 | |
| def test_usage_percent_unlimited(self): | |
| """Usage percent for unlimited should be 0.""" | |
| current = 1000 | |
| limit = -1 # Unlimited | |
| percent = 0.0 if limit == -1 else (current / limit) * 100 | |
| assert percent == 0.0 | |
| def test_usage_percent_at_limit(self): | |
| """Usage at 100% of limit.""" | |
| current = 5000 | |
| limit = 5000 | |
| percent = (current / limit) * 100 | |
| assert percent == 100.0 | |
| class TestTierUpgradeDowngrade: | |
| """Tests for tier changes.""" | |
| def test_upgrade_increases_limits(self): | |
| """Upgrading tier should increase limits.""" | |
| free_limits = TIER_LIMITS["free"] | |
| pro_limits = TIER_LIMITS["pro"] | |
| assert pro_limits["device_limit"] > free_limits["device_limit"] | |
| assert pro_limits["memory_limit"] > free_limits["memory_limit"] | |
| def test_downgrade_preserves_data(self): | |
| """Downgrading should not delete data, just prevent new additions.""" | |
| # If a Pro user has 4 devices and downgrades to Free (2 device limit) | |
| # They keep their 4 devices but can't add more | |
| current_devices = 4 | |
| new_limit = TIER_LIMITS["free"]["device_limit"] # 2 | |
| # User is over limit but keeps existing devices | |
| is_over_limit = current_devices > new_limit | |
| assert is_over_limit is True | |
| # Can they add more? No | |
| can_add_more = current_devices < new_limit | |
| assert can_add_more is False | |
| class TestSubscriptionTeamFields: | |
| """Tests for team-tier specific fields.""" | |
| def test_team_subscription_has_seats(self): | |
| """Team subscriptions should have seat tracking.""" | |
| from service.db.models import SubscriptionModel | |
| # Note: SQLAlchemy defaults are applied at database level | |
| # When creating objects directly, we set values explicitly | |
| sub = SubscriptionModel( | |
| id="test", | |
| user_id="test", | |
| tier="team", | |
| device_limit=10, | |
| memory_limit=-1, | |
| seats_included=5, # Team tier default | |
| seats_used=1, | |
| ) | |
| # Team tier values | |
| assert sub.seats_included == 5 | |
| assert sub.seats_used == 1 | |
| def test_team_tier_seat_calculation(self): | |
| """Team seat usage calculation.""" | |
| seats_included = 5 | |
| seats_used = 3 | |
| can_add_member = seats_used < seats_included | |
| assert can_add_member is True | |
| seats_used = 5 | |
| can_add_member = seats_used < seats_included | |
| assert can_add_member is False | |
Xet Storage Details
- Size:
- 8.04 kB
- Xet hash:
- d95b2f042ba544681434a89a50ad29044505a4c2a46e52f26249c67a6400e381
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.