""" tests/test_billing_status.py — tests for the /internal/billing-status/ route. get_billing_status is monkeypatched on the billing_status module (the name it was imported into) so no test ever talks to real Stripe. """ from unittest.mock import MagicMock import pytest import app as app_module import billing.billing_status as billing_status from billing.stripe_status import StripeStatusError from billing.tenants import TenantConfig, TenantRegistry @pytest.fixture def client(): flask_app = app_module.app flask_app.config["TESTING"] = True tenant = TenantConfig( slug="acme", frappe_api_url="http://frappe.test", frappe_api_key="key", frappe_api_secret="secret", stripe_webhook_secret="whsec_acme", ) flask_app.config["TENANT_REGISTRY"] = TenantRegistry({"acme": tenant}) with flask_app.test_client() as test_client: yield test_client def test_unknown_tenant_returns_404(client): response = client.get("/internal/billing-status/not-a-real-tenant") assert response.status_code == 404 assert response.get_json()["error"] == "unknown tenant" def test_success_returns_status_with_trace_id(client, monkeypatch): fake_status = { "subscription_status": "active", "current_period_end": "2027-01-15T00:00:00+00:00", "trial_end": None, "invoices": [], "payment_link_url": "https://buy.stripe.com/xyz", } monkeypatch.setattr(billing_status, "get_billing_status", MagicMock(return_value=dict(fake_status))) response = client.get("/internal/billing-status/acme") assert response.status_code == 200 body = response.get_json() assert body["subscription_status"] == "active" assert body["payment_link_url"] == "https://buy.stripe.com/xyz" assert "trace_id" in body def test_stripe_failure_returns_502(client, monkeypatch): monkeypatch.setattr( billing_status, "get_billing_status", MagicMock(side_effect=StripeStatusError("boom")) ) response = client.get("/internal/billing-status/acme") assert response.status_code == 502 assert "trace_id" in response.get_json()