-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
100 additions
and
45 deletions.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from datetime import datetime | ||
import pytest | ||
import os | ||
import responses | ||
|
@@ -8,9 +9,11 @@ | |
|
||
from model_bakery import baker | ||
from stripe_payments.models import Invoice, Seller | ||
from stripe_payments.tests.mock_connector import MockConnector | ||
|
||
|
||
User = get_user_model() | ||
|
||
@pytest.fixture | ||
def configured_user(): | ||
user = User.objects.create_user( | ||
|
@@ -23,6 +26,20 @@ def configured_user(): | |
yield user | ||
|
||
|
||
@pytest.fixture | ||
def configured_stripe_user(): | ||
user = User.objects.create_user( | ||
username='stripe_customer', | ||
first_name="Test", | ||
last_name="User", | ||
email='[email protected]', | ||
password='test' | ||
) | ||
user.userprofile.stripe_customer_id = "cus-1" | ||
user.userprofile.save() | ||
yield user | ||
|
||
|
||
@pytest.fixture | ||
def superuser(): | ||
yield User.objects.create_superuser( | ||
|
@@ -69,34 +86,60 @@ 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) | ||
else: | ||
object = Mock(**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 | ||
|
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
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
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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from model_bakery import baker | ||
|
||
from stripe_payments.models import Invoice, StripePaymentIntent | ||
from conftest import get_mock_payment_intent | ||
|
||
from ..admin import StripePaymentIntentAdmin, InvoiceAdmin | ||
|
||
|
@@ -28,7 +29,7 @@ def test_invoice_display_no_payment_intent_or_items(): | |
assert invoice_admin.pi(invoice) == "" | ||
|
||
|
||
def test_invoice_display_payment_intent(get_mock_payment_intent): | ||
def test_invoice_display_payment_intent(): | ||
invoice = baker.make( | ||
Invoice, invoice_id="foo123", username="[email protected]", amount=10 | ||
) | ||
|
@@ -40,7 +41,7 @@ def test_invoice_display_payment_intent(get_mock_payment_intent): | |
assert invoice_admin.pi(invoice) == f'<a href="{pi_admin_url}">mock-intent-id</a>' | ||
|
||
|
||
def test_invoice_display_items(get_mock_payment_intent): | ||
def test_invoice_display_items(): | ||
invoice = baker.make( | ||
Invoice, invoice_id="foo123", username="[email protected]", amount=10 | ||
) | ||
|
@@ -52,7 +53,7 @@ def test_invoice_display_items(get_mock_payment_intent): | |
assert invoice_admin.items(invoice) == f"<ul><li>{booking.event.str_no_location()}</li></ul>" | ||
|
||
|
||
def test_payment_intent_admin_display(get_mock_payment_intent, block_gift_voucher): | ||
def test_payment_intent_admin_display(block_gift_voucher): | ||
invoice = baker.make( | ||
Invoice, invoice_id="foo123", username="[email protected]", amount=10 | ||
) | ||
|
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
from model_bakery import baker | ||
|
||
from conftest import get_mock_payment_intent | ||
from booking.models import Booking, Block, TicketBooking, Ticket | ||
from ..models import Invoice, Seller, StripePaymentIntent | ||
|
||
|
@@ -141,15 +142,15 @@ def test_seller_str(): | |
assert str(seller) == "[email protected]" | ||
|
||
|
||
def test_invoice_payment_intent_ids(get_mock_payment_intent): | ||
def test_invoice_payment_intent_ids(): | ||
invoice = baker.make(Invoice, invoice_id="foo123") | ||
stripe_pi, _ = StripePaymentIntent.update_or_create_payment_intent_instance( | ||
get_mock_payment_intent(), invoice | ||
) | ||
assert invoice.payment_intent_ids == "mock-intent-id" | ||
|
||
|
||
def test_create_stripe_payment_intent_instance_from_pi(get_mock_payment_intent): | ||
def test_create_stripe_payment_intent_instance_from_pi(): | ||
payment_intent = get_mock_payment_intent() | ||
invoice = baker.make(Invoice, invoice_id="foo123") | ||
assert not StripePaymentIntent.objects.exists() | ||
|
@@ -167,7 +168,7 @@ def test_create_stripe_payment_intent_instance_from_pi(get_mock_payment_intent): | |
assert pi.seller == seller | ||
|
||
|
||
def test_stripe_payment_intent_str(get_mock_payment_intent): | ||
def test_stripe_payment_intent_str(): | ||
payment_intent = get_mock_payment_intent() | ||
invoice = baker.make(Invoice, invoice_id="foo123", username="[email protected]") | ||
pi, _ = StripePaymentIntent.update_or_create_payment_intent_instance(payment_intent, invoice) | ||
|
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