-
-
Notifications
You must be signed in to change notification settings - Fork 967
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
ontowhee
wants to merge
5
commits into
django:main
Choose a base branch
from
ontowhee:add-tests-for-fundraising-webhook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5db9f40
Add test for stripe webhook checkout session completed
ontowhee c4fb434
Add assertions to ensure email is sent and check status code.
ontowhee 4606950
Add test for checkout_session_completed when the DjangoHero does not …
ontowhee 936c1bf
Merge branch 'main' into add-tests-for-fundraising-webhook
ontowhee 4cca673
Remove unused test data files
ontowhee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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 = "" | ||
self.hero.save() | ||
response = self.post_event(self.stripe_data("session_completed")) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?