-
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
5 changed files
with
63 additions
and
26 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,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() | ||
|
@@ -20,6 +22,8 @@ def configured_user(): | |
email='[email protected]', | ||
password='test' | ||
) | ||
user.userprofile.stripe_customer_id = "cus-1" | ||
user.userprofile.save() | ||
yield user | ||
|
||
|
||
|
@@ -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 | ||
|
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