Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Preserve event context on fetched related objects
semver_level: patch
jira_tickets_closed:
- RUN_DEVSDK-3614
---
2 changes: 1 addition & 1 deletion stripe/v2/core/_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
117 changes: 117 additions & 0 deletions tests/test_v2_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
V1BillingMeterErrorReportTriggeredEventNotification,
V1BillingMeterErrorReportTriggeredEvent,
)
from stripe.events._v1_product_updated_event import (
V1ProductUpdatedEventNotification,
)
from stripe.v2.core._event import (
EventNotification,
RelatedSingletonObject,
Expand Down Expand Up @@ -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"
)
Loading