Skip to content

Commit

Permalink
Added corresponding tests
Browse files Browse the repository at this point in the history
Also updated existing tests according to changes made in the application code.
  • Loading branch information
arnav13081994 authored and jleclanche committed Aug 11, 2021
1 parent fd0e433 commit 1672e27
Show file tree
Hide file tree
Showing 9 changed files with 547 additions and 70 deletions.
97 changes: 55 additions & 42 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ def pay(self):
"created": 1439185846,
"currency": "usd",
"description": "Test description - 1439185984",
"destination": "acct_16Y9B9Fso9hLaeLu",
"destination": FAKE_STANDARD_ACCOUNT["id"],
"destination_payment": "py_16Y9BKFso9hLaeLueFmWAYUi",
"livemode": False,
"metadata": {},
Expand All @@ -1621,60 +1621,73 @@ def pay(self):
"source_type": "bank_account",
}

FAKE_TRANSFER_II = {
"id": "tr_16hTzv2eZvKYlo2CWuyMmuvV",
FAKE_TRANSFER_WITH_1_REVERSAL = {
"id": "tr_16Y9BK2eZvKYlo2CR0ySu1BA",
"object": "transfer",
"amount": 2000,
"amount": 100,
"amount_reversed": 0,
"application_fee_amount": None,
"balance_transaction": deepcopy(FAKE_BALANCE_TRANSACTION_III),
"bank_account": deepcopy(FAKE_BANK_ACCOUNT),
"created": 1440420000,
"balance_transaction": deepcopy(FAKE_BALANCE_TRANSACTION_II),
"created": 1439185846,
"currency": "usd",
"description": None,
"destination": "ba_16hTzo2eZvKYlo2CeSjfb0tS",
"description": "Test description - 1439185984",
"destination": FAKE_STANDARD_ACCOUNT["id"],
"destination_payment": "py_16Y9BKFso9hLaeLueFmWAYUi",
"livemode": False,
"metadata": {"foo": "bar"},
"recipient": "rp_16hTzu2eZvKYlo2C9A5mgxEj",
"metadata": {},
"recipient": None,
"reversals": {
"object": "list",
"total_count": 0,
"total_count": 1,
"has_more": False,
"url": "/v1/transfers/tr_16hTzv2eZvKYlo2CWuyMmuvV/reversals",
"data": [],
"url": "/v1/transfers/tr_16Y9BK2eZvKYlo2CR0ySu1BA/reversals",
"data": [
{
"id": "trr_1J5UlFJSZQVUcJYgb38m1OZO",
"object": "transfer_reversal",
"amount": 20,
"balance_transaction": deepcopy(FAKE_BALANCE_TRANSACTION_II),
"created": 1624449653,
"currency": "usd",
"destination_payment_refund": "pyr_1J5UlFR44xKqawmIBvFa6gW9",
"metadata": {},
"source_refund": None,
"transfer": deepcopy(FAKE_TRANSFER),
}
],
},
"reversed": False,
"source_transaction": None,
"source_type": "card",
"source_type": "bank_account",
}

FAKE_TRANSFER_III = {
"id": "tr_17O4U52eZvKYlo2CmyYbDAEy",
"object": "transfer",
"amount": 19010,
"amount_reversed": 0,
"application_fee_amount": None,
"balance_transaction": deepcopy(FAKE_BALANCE_TRANSACTION_IV),
"bank_account": deepcopy(FAKE_BANK_ACCOUNT_II),
"created": 1451560845,
"currency": "usd",
"date": 1451560845,
"description": "[email protected]",
"destination": "ba_17O4Tz2eZvKYlo2CMYsxroV5",
"livemode": False,
"metadata": {"foo2": "bar2"},
"recipient": "rp_17O4U42eZvKYlo2CLk4upfDE",
"reversals": {
"object": "list",
"total_count": 0,
"has_more": False,
"url": "/v1/transfers/tr_17O4U52eZvKYlo2CmyYbDAEy/reversals",
"data": [],
},
"reversed": False,
"source_transaction": None,
"source_type": "card",
}
# FAKE_TRANSFER_III = {
# "id": "tr_17O4U52eZvKYlo2CmyYbDAEy",
# "object": "transfer",
# "amount": 19010,
# "amount_reversed": 0,
# "application_fee_amount": None,
# "balance_transaction": deepcopy(FAKE_BALANCE_TRANSACTION_IV),
# "bank_account": deepcopy(FAKE_BANK_ACCOUNT_II),
# "created": 1451560845,
# "currency": "usd",
# "date": 1451560845,
# "description": "[email protected]",
# "destination": "ba_17O4Tz2eZvKYlo2CMYsxroV5",
# "livemode": False,
# "metadata": {"foo2": "bar2"},
# "recipient": "rp_17O4U42eZvKYlo2CLk4upfDE",
# "reversals": {
# "object": "list",
# "total_count": 0,
# "has_more": False,
# "url": "/v1/transfers/tr_17O4U52eZvKYlo2CmyYbDAEy/reversals",
# "data": [],
# },
# "reversed": False,
# "source_transaction": None,
# "source_type": "card",
# }

FAKE_ACCOUNT = {
"id": "acct_1032D82eZvKYlo2C",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from djstripe.enums import ChargeStatus, LegacySourceType
from djstripe.models import Charge, DjstripePaymentMethod
from djstripe.models.connect import Transfer
from djstripe.settings import djstripe_settings

from . import (
Expand Down Expand Up @@ -635,6 +636,7 @@ def test_sync_from_stripe_data_no_customer(
},
)

@patch.object(Transfer, "_attach_objects_post_save_hook")
@patch(
"stripe.BalanceTransaction.retrieve",
return_value=deepcopy(FAKE_BALANCE_TRANSACTION),
Expand Down Expand Up @@ -678,6 +680,7 @@ def test_sync_from_stripe_data_with_transfer(
invoice_retrieve_mock,
charge_retrieve_mock,
balance_transaction_retrieve_mock,
transfer__attach_object_post_save_hook_mock,
):

default_account_mock.return_value = self.account
Expand Down
21 changes: 19 additions & 2 deletions tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
from djstripe import webhooks
from djstripe.models import Event, Transfer

from . import FAKE_CUSTOMER, FAKE_EVENT_TRANSFER_CREATED, FAKE_TRANSFER
from . import (
FAKE_CUSTOMER,
FAKE_EVENT_TRANSFER_CREATED,
FAKE_STANDARD_ACCOUNT,
FAKE_TRANSFER,
IS_STATICMETHOD_AUTOSPEC_SUPPORTED,
)


class EventTest(TestCase):
Expand Down Expand Up @@ -149,10 +155,21 @@ def _create_event(self, event_data, event_retrieve_mock):


class EventRaceConditionTest(TestCase):
@patch.object(Transfer, "_attach_objects_post_save_hook")
@patch(
"stripe.Account.retrieve",
return_value=deepcopy(FAKE_STANDARD_ACCOUNT),
autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED,
)
@patch(
"stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True
)
def test_process_event_race_condition(self, transfer_retrieve_mock):
def test_process_event_race_condition(
self,
transfer_retrieve_mock,
account_retrieve_mock,
transfer__attach_object_post_save_hook_mock,
):
transfer = Transfer.sync_from_stripe_data(deepcopy(FAKE_TRANSFER))
transfer_retrieve_mock.reset_mock()
event_data = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
Expand Down
27 changes: 25 additions & 2 deletions tests/test_event_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2315,9 +2315,21 @@ def test_tax_id_deleted(


class TestTransferEvents(EventTestCase):
@patch.object(Transfer, "_attach_objects_post_save_hook")
@patch(
"stripe.Account.retrieve",
return_value=deepcopy(FAKE_STANDARD_ACCOUNT),
autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED,
)
@patch("stripe.Transfer.retrieve", autospec=True)
@patch("stripe.Event.retrieve", autospec=True)
def test_transfer_created(self, event_retrieve_mock, transfer_retrieve_mock):
def test_transfer_created(
self,
event_retrieve_mock,
transfer_retrieve_mock,
account_retrieve_mock,
transfer__attach_object_post_save_hook_mock,
):
fake_stripe_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
event_retrieve_mock.return_value = fake_stripe_event
transfer_retrieve_mock.return_value = fake_stripe_event["data"]["object"]
Expand All @@ -2331,8 +2343,19 @@ def test_transfer_created(self, event_retrieve_mock, transfer_retrieve_mock):
fake_stripe_event["data"]["object"]["amount"] / Decimal("100"),
)

@patch.object(Transfer, "_attach_objects_post_save_hook")
@patch(
"stripe.Account.retrieve",
return_value=deepcopy(FAKE_STANDARD_ACCOUNT),
autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED,
)
@patch("stripe.Transfer.retrieve", return_value=FAKE_TRANSFER, autospec=True)
def test_transfer_deleted(self, transfer_retrieve_mock):
def test_transfer_deleted(
self,
transfer_retrieve_mock,
account_retrieve_mock,
transfer__attach_object_post_save_hook_mock,
):
event = self._create_event(FAKE_EVENT_TRANSFER_CREATED)
event.invoke_webhook_handlers()

Expand Down
32 changes: 27 additions & 5 deletions tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
FAKE_PLAN,
FAKE_PLAN_II,
FAKE_PRODUCT,
FAKE_STANDARD_ACCOUNT,
FAKE_TRANSFER,
FAKE_TRANSFER_II,
FAKE_TRANSFER_III,
IS_STATICMETHOD_AUTOSPEC_SUPPORTED,
)


Expand Down Expand Up @@ -149,10 +149,32 @@ def test_churn(self):


class TransferManagerTest(TestCase):
def test_transfer_summary(self):
@patch.object(Transfer, "_attach_objects_post_save_hook")
@patch(
"stripe.Account.retrieve",
return_value=deepcopy(FAKE_STANDARD_ACCOUNT),
autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED,
)
def test_transfer_summary(
self, account_retrieve_mock, transfer__attach_object_post_save_hook_mock
):
def FAKE_TRANSFER_III():
data = deepcopy(FAKE_TRANSFER)
data["id"] = "tr_17O4U52eZvKYlo2CmyYbDAEy"
data["amount"] = 19010
data["created"] = 1451560845
return data

def FAKE_TRANSFER_II():
data = deepcopy(FAKE_TRANSFER)
data["id"] = "tr_16hTzv2eZvKYlo2CWuyMmuvV"
data["amount"] = 2000
data["created"] = 1440420000
return data

Transfer.sync_from_stripe_data(deepcopy(FAKE_TRANSFER))
Transfer.sync_from_stripe_data(deepcopy(FAKE_TRANSFER_II))
Transfer.sync_from_stripe_data(deepcopy(FAKE_TRANSFER_III))
Transfer.sync_from_stripe_data(FAKE_TRANSFER_II())
Transfer.sync_from_stripe_data(FAKE_TRANSFER_III())

self.assertEqual(Transfer.objects.during(2015, 8).count(), 2)

Expand Down
16 changes: 7 additions & 9 deletions tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
dj-stripe Migrations Tests
"""
import pytest

from django.test import TestCase, override_settings
from django.conf import settings
from djstripe.settings import djstripe_settings
from djstripe.models.core import Customer
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings

from djstripe.models.core import Customer
from djstripe.settings import djstripe_settings

class TestCustomerSubscriberFK(TestCase):

class TestCustomerSubscriberFK(TestCase):
@override_settings(
DJSTRIPE_SUBSCRIBER_MODEL="testapp.Organization",
DJSTRIPE_SUBSCRIBER_MODEL_REQUEST_CALLBACK=(lambda request: request.org),
)
def setUp(self):
return super().setUp()


def test_customer_subscriber_fk_to_subscriber_model(self):
"""
Test to ensure customer.subscriber fk points to the configured model
Expand All @@ -29,15 +28,14 @@ def test_customer_subscriber_fk_to_subscriber_model(self):
self.assertEqual(field.related_model, djstripe_settings.get_subscriber_model())
self.assertNotEqual(field.related_model, settings.AUTH_USER_MODEL)


def test_customer_subscriber_fk_fallback_to_auth_user_model(self):
"""
Test to ensure customer.subscriber fk points to the fallback AUTH_USER_MODEL
Test to ensure customer.subscriber fk points to the fallback AUTH_USER_MODEL
when DJSTRIPE_SUBSCRIBER_MODEL is not set
"""
# assert DJSTRIPE_SUBSCRIBER_MODEL has not been set
with pytest.raises(AttributeError):
settings.DJSTRIPE_SUBSCRIBER_MODEL

field = Customer._meta.get_field("subscriber")
field = Customer._meta.get_field("subscriber")
self.assertEqual(field.related_model, get_user_model())
Loading

0 comments on commit 1672e27

Please sign in to comment.