forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_source.py
122 lines (96 loc) · 3.64 KB
/
test_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
dj-stripe Source Model Tests.
"""
import sys
from copy import deepcopy
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from djstripe.models import Source
from . import (
FAKE_CUSTOMER_III,
FAKE_SOURCE,
FAKE_SOURCE_II,
AssertStripeFksMixin,
SourceDict,
)
class SourceTest(AssertStripeFksMixin, TestCase):
def setUp(self):
user = get_user_model().objects.create_user(
username="testuser", email="[email protected]"
)
# create a source object so that FAKE_CUSTOMER_III with a default source
# can be created correctly.
fake_source_data = deepcopy(FAKE_SOURCE)
fake_source_data["customer"] = None
self.source = Source.sync_from_stripe_data(fake_source_data)
self.customer = FAKE_CUSTOMER_III.create_for_user(user)
self.customer.sources.all().delete()
self.customer.legacy_cards.all().delete()
def test_attach_objects_hook_without_customer(self):
source = Source.sync_from_stripe_data(deepcopy(FAKE_SOURCE_II))
self.assertEqual(source.customer, None)
self.assert_fks(
source,
expected_blank_fks={
"djstripe.Source.customer",
"djstripe.Customer.default_payment_method",
},
)
def test_sync_from_stripe_data(self):
source = Source.sync_from_stripe_data(deepcopy(FAKE_SOURCE))
self.assertEqual(self.customer, source.customer)
self.assert_fks(
source,
expected_blank_fks={
"djstripe.Customer.coupon",
"djstripe.Customer.default_payment_method",
},
)
def test___str__(self):
fake_source = deepcopy(FAKE_SOURCE)
source = Source.sync_from_stripe_data(fake_source)
self.assertEqual(
f"{fake_source['type']} {fake_source['id']}",
str(source),
)
self.assert_fks(
source,
expected_blank_fks={
"djstripe.Customer.coupon",
"djstripe.Customer.default_payment_method",
},
)
@patch("stripe.Source.retrieve", return_value=deepcopy(FAKE_SOURCE), autospec=True)
def test_detach(self, source_retrieve_mock):
original_detach = SourceDict.detach
def mocked_detach(self):
return original_detach(self)
Source.sync_from_stripe_data(deepcopy(FAKE_SOURCE))
self.assertEqual(0, self.customer.legacy_cards.count())
self.assertEqual(1, self.customer.sources.count())
source = self.customer.sources.first()
with patch(
"tests.SourceDict.detach", side_effect=mocked_detach, autospec=True
) as mock_detach:
source.detach()
self.assertEqual(0, self.customer.sources.count())
# need to refresh_from_db since default_source was cleared with a query
self.customer.refresh_from_db()
self.assertIsNone(self.customer.default_source)
# need to refresh_from_db due to the implementation of Source.detach() -
# see TODO in method
source.refresh_from_db()
self.assertIsNone(source.customer)
self.assertEqual(source.status, "consumed")
if sys.version_info >= (3, 6):
# this mock isn't working on py34, py35, but it's not strictly necessary
# for the test
mock_detach.assert_called()
self.assert_fks(
source,
expected_blank_fks={
"djstripe.Source.customer",
"djstripe.Customer.default_payment_method",
},
)