|
| 1 | +"""Regression tests for issue #1466. |
| 2 | +
|
| 3 | +Issue: https://github.com/Subscribie/subscribie/issues/1466 |
| 4 | +
|
| 5 | +When Stripe sends a ``customer.subscription.deleted`` webhook, the |
| 6 | +``cancellation_details.reason`` it carries (e.g. ``payment_failed``, |
| 7 | +``cancellation_requested``) must be persisted onto the local |
| 8 | +``Subscription.stripe_cancellation_reason`` column, so the shop owner can |
| 9 | +see *why* a subscription was cancelled from the subscribers UI. |
| 10 | +""" |
| 11 | + |
| 12 | +import json |
| 13 | +from unittest.mock import patch |
| 14 | + |
| 15 | +from subscribie.database import database |
| 16 | +from subscribie.models import Company, Person, Plan, Subscription |
| 17 | + |
| 18 | + |
| 19 | +def _ensure_company(): |
| 20 | + if Company.query.first() is None: |
| 21 | + company = Company() |
| 22 | + company.name = "Test Shop" |
| 23 | + database.session.add(company) |
| 24 | + database.session.commit() |
| 25 | + |
| 26 | + |
| 27 | +def _make_subscription(checkout_session_id, stripe_subscription_id): |
| 28 | + _ensure_company() |
| 29 | + person = Person( |
| 30 | + given_name="Ada", |
| 31 | + family_name="Lovelace", |
| 32 | + email="ada@example.com", |
| 33 | + uuid=f"person-uuid-1466-{stripe_subscription_id}", |
| 34 | + ) |
| 35 | + database.session.add(person) |
| 36 | + plan = Plan( |
| 37 | + title="Monthly Widget", |
| 38 | + uuid=f"plan-uuid-1466-{stripe_subscription_id}", |
| 39 | + ) |
| 40 | + database.session.add(plan) |
| 41 | + database.session.commit() |
| 42 | + |
| 43 | + subscription = Subscription( |
| 44 | + sku_uuid=plan.uuid, |
| 45 | + person_id=person.id, |
| 46 | + stripe_subscription_id=stripe_subscription_id, |
| 47 | + subscribie_checkout_session_id=checkout_session_id, |
| 48 | + stripe_status="active", |
| 49 | + ) |
| 50 | + database.session.add(subscription) |
| 51 | + database.session.commit() |
| 52 | + return subscription, person, plan |
| 53 | + |
| 54 | + |
| 55 | +def _build_event( |
| 56 | + checkout_session_id, stripe_subscription_id, reason, person_uuid, plan_uuid |
| 57 | +): |
| 58 | + return { |
| 59 | + "livemode": False, |
| 60 | + "type": "customer.subscription.deleted", |
| 61 | + "account": "acct_1TestConnectAccount", |
| 62 | + "data": { |
| 63 | + "object": { |
| 64 | + "id": stripe_subscription_id, |
| 65 | + "status": "canceled", |
| 66 | + "ended_at": 1700000000, |
| 67 | + "cancellation_details": {"reason": reason}, |
| 68 | + "metadata": { |
| 69 | + "subscribie_checkout_session_id": checkout_session_id, |
| 70 | + "person_uuid": person_uuid, |
| 71 | + "plan_uuid": plan_uuid, |
| 72 | + }, |
| 73 | + } |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +def test_customer_subscription_deleted_persists_payment_failed_reason( |
| 79 | + client, |
| 80 | + app, |
| 81 | + db_session, |
| 82 | + with_shop_owner, |
| 83 | + with_default_country_code_and_default_currency, |
| 84 | +): |
| 85 | + subscription, person, plan = _make_subscription( |
| 86 | + checkout_session_id="cs_1466_pf", |
| 87 | + stripe_subscription_id="sub_1466_pf", |
| 88 | + ) |
| 89 | + |
| 90 | + payload = _build_event( |
| 91 | + checkout_session_id=subscription.subscribie_checkout_session_id, |
| 92 | + stripe_subscription_id=subscription.stripe_subscription_id, |
| 93 | + reason="payment_failed", |
| 94 | + person_uuid=person.uuid, |
| 95 | + plan_uuid=plan.uuid, |
| 96 | + ) |
| 97 | + |
| 98 | + with patch( |
| 99 | + "subscribie.blueprints.checkout.PaymentProvider" |
| 100 | + ) as payment_provider_cls: |
| 101 | + payment_provider_cls.query.first.return_value.stripe_livemode = False |
| 102 | + response = client.post( |
| 103 | + "/stripe_webhook", |
| 104 | + data=json.dumps(payload), |
| 105 | + content_type="application/json", |
| 106 | + ) |
| 107 | + |
| 108 | + assert response.status_code == 200 |
| 109 | + |
| 110 | + refreshed = Subscription.query.filter_by(uuid=subscription.uuid).one() |
| 111 | + assert refreshed.stripe_cancellation_reason == "payment_failed" |
| 112 | + assert refreshed.stripe_status == "canceled" |
| 113 | + assert refreshed.stripe_ended_at == 1700000000 |
| 114 | + |
| 115 | + |
| 116 | +def test_customer_subscription_deleted_persists_cancellation_requested_reason( |
| 117 | + client, |
| 118 | + app, |
| 119 | + db_session, |
| 120 | + with_shop_owner, |
| 121 | + with_default_country_code_and_default_currency, |
| 122 | +): |
| 123 | + subscription, person, plan = _make_subscription( |
| 124 | + checkout_session_id="cs_1466_cr", |
| 125 | + stripe_subscription_id="sub_1466_cr", |
| 126 | + ) |
| 127 | + |
| 128 | + payload = _build_event( |
| 129 | + checkout_session_id=subscription.subscribie_checkout_session_id, |
| 130 | + stripe_subscription_id=subscription.stripe_subscription_id, |
| 131 | + reason="cancellation_requested", |
| 132 | + person_uuid=person.uuid, |
| 133 | + plan_uuid=plan.uuid, |
| 134 | + ) |
| 135 | + |
| 136 | + with patch( |
| 137 | + "subscribie.blueprints.checkout.PaymentProvider" |
| 138 | + ) as payment_provider_cls: |
| 139 | + payment_provider_cls.query.first.return_value.stripe_livemode = False |
| 140 | + response = client.post( |
| 141 | + "/stripe_webhook", |
| 142 | + data=json.dumps(payload), |
| 143 | + content_type="application/json", |
| 144 | + ) |
| 145 | + |
| 146 | + assert response.status_code == 200 |
| 147 | + |
| 148 | + refreshed = Subscription.query.filter_by(uuid=subscription.uuid).one() |
| 149 | + assert refreshed.stripe_cancellation_reason == "cancellation_requested" |
| 150 | + assert refreshed.stripe_status == "canceled" |
0 commit comments