""" tests/test_stripe_status.py — unit tests for billing/stripe_status.py. stripe.StripeClient is monkeypatched to a fake so no test ever talks to real Stripe; the fake mirrors the shape of the real SDK's list responses (an object with a `.data` list of attribute-access objects) closely enough to exercise get_billing_status's actual field access. """ from types import SimpleNamespace import pytest from billing.stripe_status import StripeStatusError, get_billing_status from billing.tenants import TenantConfig def _tenant(**overrides): defaults = dict( slug="acme", frappe_api_url="http://frappe.test", frappe_api_key="k", frappe_api_secret="s", stripe_webhook_secret="whsec_x", stripe_restricted_key="rk_test_x", stripe_customer_id="cus_123", stripe_payment_link_url="https://buy.stripe.com/xyz", ) defaults.update(overrides) return TenantConfig(**defaults) class _FakeStripeClient: """Stands in for stripe.StripeClient — records the resource calls made against it.""" def __init__(self, subscriptions_data=None, invoices_data=None, error=None): self._subscriptions_data = subscriptions_data or [] self._invoices_data = invoices_data or [] self._error = error self.subscriptions = SimpleNamespace(list=self._list_subscriptions) self.invoices = SimpleNamespace(list=self._list_invoices) self.calls = [] def _list_subscriptions(self, params=None): self.calls.append(("subscriptions.list", params)) if self._error: raise self._error return SimpleNamespace(data=self._subscriptions_data) def _list_invoices(self, params=None): self.calls.append(("invoices.list", params)) if self._error: raise self._error return SimpleNamespace(data=self._invoices_data) @pytest.fixture def patch_stripe_client(monkeypatch): """Returns a factory: call it with a _FakeStripeClient, and stripe.StripeClient(key) returns it.""" import billing.stripe_status as stripe_status def _patch(fake_client): monkeypatch.setattr(stripe_status.stripe, "StripeClient", lambda key: fake_client) return _patch # --- unconfigured tenant ----------------------------------------------- def test_unconfigured_tenant_returns_empty_status_without_calling_stripe(patch_stripe_client): tenant = _tenant(stripe_restricted_key=None, stripe_customer_id=None) fake = _FakeStripeClient() patch_stripe_client(fake) result = get_billing_status(tenant) assert result == { "subscription_status": None, "current_period_end": None, "trial_end": None, "invoices": [], "payment_link_url": "https://buy.stripe.com/xyz", } assert fake.calls == [] # --- configured tenant, real-shaped data -------------------------------- def test_active_subscription_and_invoices_are_returned(patch_stripe_client): tenant = _tenant() subscription = SimpleNamespace(status="active", current_period_end=1_800_000_000, trial_end=None) invoice = SimpleNamespace( id="in_1", created=1_799_000_000, amount_paid=4900, currency="usd", status="paid", hosted_invoice_url="https://invoice.stripe.com/i/1", ) fake = _FakeStripeClient(subscriptions_data=[subscription], invoices_data=[invoice]) patch_stripe_client(fake) result = get_billing_status(tenant, limit=5) assert result["subscription_status"] == "active" assert result["current_period_end"] == "2027-01-15T08:00:00+00:00" assert result["trial_end"] is None assert result["invoices"] == [ { "id": "in_1", "created": "2027-01-03T18:13:20+00:00", "amount_paid": 4900, "currency": "usd", "status": "paid", "hosted_invoice_url": "https://invoice.stripe.com/i/1", } ] assert result["payment_link_url"] == "https://buy.stripe.com/xyz" # the customer scoping and limit are actually sent to Stripe assert ("subscriptions.list", {"customer": "cus_123", "limit": 1}) in fake.calls assert ("invoices.list", {"customer": "cus_123", "limit": 5}) in fake.calls def test_trialing_subscription_includes_trial_end(patch_stripe_client): tenant = _tenant() subscription = SimpleNamespace(status="trialing", current_period_end=1_800_000_000, trial_end=1_799_500_000) fake = _FakeStripeClient(subscriptions_data=[subscription]) patch_stripe_client(fake) result = get_billing_status(tenant) assert result["subscription_status"] == "trialing" assert result["trial_end"] == "2027-01-09T13:06:40+00:00" def test_no_subscription_returns_none_status(patch_stripe_client): tenant = _tenant() fake = _FakeStripeClient(subscriptions_data=[]) patch_stripe_client(fake) result = get_billing_status(tenant) assert result["subscription_status"] is None assert result["current_period_end"] is None # --- Stripe failures ----------------------------------------------------- def test_stripe_error_raises_stripe_status_error_without_leaking_details(patch_stripe_client): import stripe tenant = _tenant() fake = _FakeStripeClient(error=stripe.StripeError("raw stripe internals")) patch_stripe_client(fake) with pytest.raises(StripeStatusError): get_billing_status(tenant)