Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1722 -- Add more tests for fundraising webhook #1761

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
86 changes: 86 additions & 0 deletions fundraising/test_data/session_completed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"id": "evt_103MXP2kbIgHtIBFeIFqPxp7",
"object": "event",
"api_version": "2015-01-11",
"created": 1683928544,
"data": {
"object": {
"id": "cs_test_a11YYufWQzNY63zpQ6QSNRQhkUpVph4WRmzW0zWJO2znZKdVujZ0N0S22u",
"object": "checkout.session",
"after_expiration": null,
"allow_promotion_codes": null,
"amount_subtotal": 2198,
"amount_total": 2198,
"automatic_tax": {
"enabled": false,
"liability": null,
"status": null
},
"billing_address_collection": null,
"cancel_url": null,
"client_reference_id": null,
"consent": null,
"consent_collection": null,
"created": 1679600215,
"currency": "usd",
"custom_fields": [],
"custom_text": {
"shipping_address": null,
"submit": null
},
"customer": "cus_3MXPY5pvYMWTBf",
"customer_creation": "if_required",
"customer_details": null,
"customer_email": null,
"expires_at": 1679686615,
"invoice": null,
"invoice_creation": {
"enabled": false,
"invoice_data": {
"account_tax_ids": null,
"custom_fields": null,
"description": null,
"footer": null,
"issuer": null,
"metadata": {},
"rendering_options": null
}
},
"livemode": false,
"locale": null,
"metadata": {},
"mode": "payment",
"payment_intent": "pi_2MXkIN4ao19wAZWp0atVBWd2",
"payment_link": null,
"payment_method_collection": "always",
"payment_method_options": {},
"payment_method_types": [
"card"
],
"payment_status": "unpaid",
"phone_number_collection": {
"enabled": false
},
"recovered_from": null,
"setup_intent": null,
"shipping_address_collection": null,
"shipping_cost": null,
"shipping_details": null,
"shipping_options": [],
"status": "open",
"submit_type": null,
"subscription": null,
"success_url": "https://example.com/success",
"total_details": {
"amount_discount": 0,
"amount_shipping": 0,
"amount_tax": 0
},
"url": "https://checkout.stripe.com/c/pay/cs_test_a11YYufWQzNY63zpQ6QSNRQhkUpVph4WRmzW0zWJO2znZKdVujZ0N0S22u#fidkdWxOYHwnPyd1blpxYHZxWjA0SDdPUW5JbmFMck1wMmx9N2BLZjFEfGRUNWhqTmJ%2FM2F8bUA2SDRySkFdUV81T1BSV0YxcWJcTUJcYW5rSzN3dzBLPUE0TzRKTTxzNFBjPWZEX1NKSkxpNTVjRjN8VHE0YicpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPyd2bGtiaWBabHFgaCcpJ2BrZGdpYFVpZGZgbWppYWB3dic%2FcXdwYHgl"
}
},
"livemode": true,
"pending_webhooks": 1,
"request": null,
"type": "checkout.session.completed"
}
25 changes: 24 additions & 1 deletion fundraising/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ def _stripe_signature_header(data):

class TestWebhooks(TestCase):
def setUp(self):
self.hero = DjangoHero.objects.create(email="[email protected]")
self.hero = DjangoHero.objects.create(
email="[email protected]",
stripe_customer_id="cus_3MXPY5pvYMWTBf",
)
self.donation = Donation.objects.create(
donor=self.hero,
interval="monthly",
Expand Down Expand Up @@ -281,3 +284,23 @@ def test_unknown_event_type(self):
data["type"] = "unknown"
response = self.post_event(data)
self.assertEqual(response.status_code, 422)

def test_checkout_session_completed(self):
response = self.post_event(self.stripe_data("session_completed"))
self.assertEqual(response.status_code, 204)

self.assertEqual(len(mail.outbox), 1)
expected_url = django_hosts_reverse(
"fundraising:manage-donations", kwargs={"hero": self.hero.id}
)
self.assertTrue(expected_url in mail.outbox[0].body)

self.assertEqual(DjangoHero.objects.count(), 1)

def test_checkout_session_completed_new_hero(self):
self.hero.stripe_customer_id = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there's a better way to do this. Either by deleting the Hero in this test before the event or by not hardcoding the stripe customer id in the setUp.
This approach works, tho.
@bmispelon Any suggestions?

self.hero.save()
response = self.post_event(self.stripe_data("session_completed"))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check here too that the mail is sent?

self.assertEqual(response.status_code, 204)
self.assertEqual(DjangoHero.objects.count(), 2)
Loading