From 0af7b80a3456485fc5cd41d86d5285386bc61fae Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Tue, 27 Oct 2026 09:43:12 -0700 Subject: [PATCH 1/2] Preserve event context on fetched objects Committed-By-Agent: codex Co-authored-by: codex --- stripe/v2/core/_event.py | 2 +- tests/test_v2_event.py | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/stripe/v2/core/_event.py b/stripe/v2/core/_event.py index d2fee6755..4c4920c6d 100644 --- a/stripe/v2/core/_event.py +++ b/stripe/v2/core/_event.py @@ -188,7 +188,7 @@ def __init__( if parsed_body.get("reason"): self.reason = Reason(parsed_body["reason"]) - self._client = client + self._client = client.with_stripe_context(self.context) @staticmethod def from_json( diff --git a/tests/test_v2_event.py b/tests/test_v2_event.py index 11909fae9..7f59ca3d5 100644 --- a/tests/test_v2_event.py +++ b/tests/test_v2_event.py @@ -14,6 +14,9 @@ V1BillingMeterErrorReportTriggeredEventNotification, V1BillingMeterErrorReportTriggeredEvent, ) +from stripe.events._v1_product_updated_event import ( + V1ProductUpdatedEventNotification, +) from stripe.v2.core._event import ( EventNotification, RelatedSingletonObject, @@ -398,3 +401,117 @@ def test_v2_events_integration( assert isinstance(meter, Meter) assert meter.event_name == "cool event" + + def test_related_object_retains_event_context( + self, http_client_mock: HTTPClientMock + ): + payload = json.dumps( + { + "id": "evt_123", + "object": "v2.core.event", + "type": "v1.product.updated", + "created": "2022-02-15T00:27:45.330Z", + "livemode": False, + "context": "acct_child", + "related_object": { + "id": "prod_shared", + "type": "product", + "url": "/v1/products/prod_shared", + }, + } + ) + path = "/v1/products/prod_shared" + http_client_mock.stub_request( + "get", + path=path, + rbody=json.dumps({"id": "prod_shared", "object": "product"}), + ) + http_client_mock.stub_request( + "delete", + path=path, + rbody=json.dumps( + {"id": "prod_shared", "object": "product", "deleted": True} + ), + ) + client = StripeClient( + api_key="sk_test_1234", + stripe_context="acct_platform", + http_client=http_client_mock.get_mock_http_client(), + ) + + notification = client.parse_event_notification( + payload, + WebhookSignature.generate_signature_header( + payload, DUMMY_WEBHOOK_SECRET + ), + DUMMY_WEBHOOK_SECRET, + ) + assert isinstance(notification, V1ProductUpdatedEventNotification) + + product = notification.fetch_related_object() + product.delete() + + http_client_mock.assert_requested( + "get", path=path, stripe_context="acct_child" + ) + http_client_mock.assert_requested( + "delete", path=path, stripe_context="acct_child" + ) + + @pytest.mark.anyio + async def test_related_object_retains_event_context_async( + self, http_client_mock: HTTPClientMock + ): + payload = json.dumps( + { + "id": "evt_123", + "object": "v2.core.event", + "type": "v1.product.updated", + "created": "2022-02-15T00:27:45.330Z", + "livemode": False, + "context": "acct_child", + "related_object": { + "id": "prod_shared", + "type": "product", + "url": "/v1/products/prod_shared", + }, + } + ) + path = "/v1/products/prod_shared" + http_client_mock.stub_request( + "get", + path=path, + query_string="usage[0]=fetch_related_object", + rbody=json.dumps({"id": "prod_shared", "object": "product"}), + ) + http_client_mock.stub_request( + "delete", + path=path, + rbody=json.dumps( + {"id": "prod_shared", "object": "product", "deleted": True} + ), + ) + client = StripeClient( + api_key="sk_test_1234", + stripe_context="acct_platform", + http_client=http_client_mock.get_mock_http_client(), + ) + + notification = client.parse_event_notification( + payload, + WebhookSignature.generate_signature_header( + payload, DUMMY_WEBHOOK_SECRET + ), + DUMMY_WEBHOOK_SECRET, + ) + assert isinstance(notification, V1ProductUpdatedEventNotification) + + product = await notification.fetch_related_object_async() + await product.delete_async() + + http_client_mock.assert_requested( + "get", path=path, stripe_context="acct_child" + ) + http_client_mock.assert_requested( + "delete", path=path, stripe_context="acct_child" + ) From 023d4a55171ffe043e15c36608be9c5b45ef5e44 Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Tue, 27 Oct 2026 14:11:48 -0700 Subject: [PATCH 2/2] Add event context changefile Committed-By-Agent: codex Co-authored-by: codex --- .../2026-10-27_zacchua_preserve-event-context.change.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .hark/changes/2026-10-27_zacchua_preserve-event-context.change.md diff --git a/.hark/changes/2026-10-27_zacchua_preserve-event-context.change.md b/.hark/changes/2026-10-27_zacchua_preserve-event-context.change.md new file mode 100644 index 000000000..c7ef3c1be --- /dev/null +++ b/.hark/changes/2026-10-27_zacchua_preserve-event-context.change.md @@ -0,0 +1,6 @@ +--- +title: Preserve event context on fetched related objects +semver_level: patch +jira_tickets_closed: +- RUN_DEVSDK-3614 +---