Skip to content

Commit

Permalink
Stripe webhook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Jun 8, 2024
1 parent 4029a62 commit 42a3ee1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
68 changes: 48 additions & 20 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
import pytest
import os
import responses
Expand All @@ -8,6 +9,7 @@

from model_bakery import baker
from stripe_payments.models import Invoice, Seller
from stripe_payments.tests.mock_connector import MockConnector


User = get_user_model()
Expand All @@ -20,6 +22,8 @@ def configured_user():
email='[email protected]',
password='test'
)
user.userprofile.stripe_customer_id = "cus-1"
user.userprofile.save()
yield user


Expand Down Expand Up @@ -69,34 +73,58 @@ def invoice(configured_user):
)


@pytest.fixture
def get_mock_payment_intent():
def payment_intent(webhook_event_type=None, **params):
defaults = {
"id": "mock-intent-id",
"amount": 1000,
"description": "",
"status": "succeeded",
"metadata": {},
"currency": "gbp",
"client_secret": "secret",
"charges": Mock(data=[{"billing_details": {"email": "[email protected]"}}])
}
options = {**defaults, **params}
if webhook_event_type == "payment_intent.payment_failed":
options["last_payment_error"] = {'error': 'an error'}
return Mock(**options)
return payment_intent
def get_mock_payment_intent(webhook_event_type=None, **params):
defaults = {
"id": "mock-intent-id",
"amount": 1000,
"description": "",
"status": "succeeded",
"metadata": {},
"currency": "gbp",
"client_secret": "secret",
"charges": Mock(data=[{"billing_details": {"email": "[email protected]"}}])
}
options = {**defaults, **params}
if webhook_event_type == "payment_intent.payment_failed":
options["last_payment_error"] = {'error': 'an error'}
return Mock(**options)


class MockSubscription:
def __init__(self, **init_dict):
for k, v in init_dict.items():
setattr(self, k, v)

def __getitem__(self, item):
return getattr(self, item)


def get_mock_subscription(webhook_event_type, **params):
defaults = {
"id": "id",
"status": "active",
"items": Mock(data=[Mock(price=Mock(id="price_1234"))]), # matches the id returned by the MockStripeConnector
"customer": "cus-1",
"start_date": datetime(2024, 6, 25).timestamp(),
"metadata": {},
}
options = {**defaults, **params}
return MockSubscription(**options)


@pytest.fixture
def get_mock_webhook_event(seller, get_mock_payment_intent):
def get_mock_webhook_event(seller):
def mock_webhook_event(**params):
webhook_event_type = params.pop("webhook_event_type", "payment_intent.succeeded")
seller_id = params.pop("seller_id", seller.stripe_user_id)
if webhook_event_type in ["payment_intent.succeeded", "payment_intent.payment_failed"]:
object = get_mock_payment_intent(webhook_event_type, **params)
elif webhook_event_type == "customer.subscription.created":
object = get_mock_subscription(webhook_event_type, **params)
mock_event = Mock(
account=seller_id,
data=Mock(object=get_mock_payment_intent(webhook_event_type, **params)), type=webhook_event_type
data=Mock(object=object),
type=webhook_event_type,
)
return mock_event
return mock_webhook_event
Expand Down
1 change: 1 addition & 0 deletions stripe_payments/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def send_processed_refund_emails(invoice, event_object):
user, user_membership = _get_user_from_membership(event_object)
else:
user = _get_user_from_invoice(invoice)
user_membership = None
ctx = {
'host': f"https://{Site.objects.get_current().domain}",
'user': user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
{% block messagecontent %}
<p>Thank you for setting up your new membership!</p>
<p><strong>Your membership:</strong> {{ user_membership.membership.name }}</p>
<p><strong>Start date:</strong> {{ user_membership.start_date|date 'd M Y' }}</p>
<p><strong>Start date:</strong> {{ user_membership.start_date|date:'d M Y' }}</p>
<p>View your membership details <a href="https://{{ domain }}/memberships/">here</a></p>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Thank you for setting up your new membership!

Your membership: {{ user_membership.membership.name }}
Start date: {{ user_membership.start_date|date 'd M Y' }}
Start date: {{ user_membership.start_date|date:'d M Y' }}

View your membership details:
https://{{ domain }}/memberships/
Expand Down
16 changes: 12 additions & 4 deletions stripe_payments/tests/test_stripe_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from unittest.mock import patch, Mock
import json
import pytest

from django.conf import settings
Expand All @@ -10,7 +10,7 @@
import stripe
from model_bakery import baker

from booking.models import Block, Booking, TicketBooking, Ticket
from booking.models import Block, Booking, TicketBooking, Ticket, Membership
from ..models import Invoice, Seller, StripePaymentIntent
from .mock_connector import MockConnector

Expand Down Expand Up @@ -673,16 +673,24 @@ def test_webhook_deauthorized_account(


# Memberships
@patch("booking.models.membership_models.StripeConnector", MockConnector)
@patch("stripe_payments.views.stripe.Webhook")
def test_webhook_subscription_created(
mock_webhook, get_mock_webhook_event, client
mock_webhook, get_mock_webhook_event, client, configured_user
):
membership = baker.make(Membership, name="membership1")
assert not membership.user_memberships.exists()
mock_webhook.construct_event.return_value = get_mock_webhook_event(
webhook_event_type="customer.subscription.created", metadata={}
webhook_event_type="customer.subscription.created",
start_date = datetime(2024, 6, 25).timestamp()
)
resp = client.post(webhook_url, data={}, HTTP_STRIPE_SIGNATURE="foo")
assert resp.status_code == 200
# email sent to user
assert len(mail.outbox) == 1
# membership created, with start date as first of next month
assert membership.user_memberships.count() == 1
assert membership.user_memberships.first().start_date.date() == datetime(2024, 7, 1).date()


@patch("stripe_payments.views.stripe.Webhook")
Expand Down

0 comments on commit 42a3ee1

Please sign in to comment.