forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sync.py
121 lines (95 loc) · 5.36 KB
/
test_sync.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
"""
.. module:: dj-stripe.tests.test_sync
:synopsis: dj-stripe Sync Method Tests.
.. moduleauthor:: Alex Kavanaugh (@kavdev)
"""
import sys
from django.test.testcases import TestCase
from django.contrib.auth import get_user_model
import stripe
from stripe import api_key
from mock import patch, PropertyMock
from djstripe.sync import sync_subscriber, sync_plans
class TestSyncSubscriber(TestCase):
fake_stripe_customer = "test_stripe_customer"
def setUp(self):
self.user = get_user_model().objects.create_user(username="testuser",
email="[email protected]",
password="123")
@patch("djstripe.models.Customer.sync_charges")
@patch("djstripe.models.Customer.sync_invoices")
@patch("djstripe.models.Customer.sync_current_subscription")
@patch("djstripe.models.Customer.sync")
@patch("djstripe.models.Customer.stripe_customer", new_callable=PropertyMock, return_value=fake_stripe_customer)
@patch("stripe.Customer.create", return_value=PropertyMock(id="cus_xxx1234567890"))
def test_sync_success(self, stripe_customer_create_mock, stripe_customer_mock,
sync_mock, sync_current_subscription_mock, sync_invoices_mock,
sync_charges_mock):
sync_subscriber(self.user)
sync_mock.assert_called_once_with(cu=self.fake_stripe_customer)
sync_current_subscription_mock.assert_called_once_with(cu=self.fake_stripe_customer)
sync_invoices_mock.assert_called_once_with(cu=self.fake_stripe_customer)
sync_charges_mock.assert_called_once_with(cu=self.fake_stripe_customer)
@patch("djstripe.models.Customer.sync")
@patch("djstripe.models.Customer.stripe_customer", new_callable=PropertyMock, return_value="test_stripe_customer")
@patch("stripe.Customer.create", return_value=PropertyMock(id="cus_xxx1234567890"))
def test_sync_fail(self, stripe_customer_create_mock, stripe_customer_mock, sync_mock):
sync_mock.side_effect = stripe.InvalidRequestError("No such customer:", "blah")
sync_subscriber(self.user)
self.assertEqual("ERROR: No such customer:", sys.stdout.getvalue().strip())
class TestSyncPlans(TestCase):
@patch("stripe.Plan.create")
def test_plan_created(self, plan_create_mock):
sync_plans(api_key)
self.assertTrue("Plan created for test", sys.stdout.getvalue().strip())
plan_create_mock.assert_any_call(amount=1000,
interval="month",
name="Test Plan 0",
currency="usd",
id="test_id_0",
interval_count=None,
trial_period_days=None,
statement_descriptor=None,
metadata=None)
plan_create_mock.assert_any_call(amount=2500,
interval="month",
name="Test Plan 1",
currency="usd",
id="test_id",
interval_count=None,
trial_period_days=None,
statement_descriptor=None,
metadata=None)
plan_create_mock.assert_any_call(amount=5000,
interval="month",
name="Test Plan 2",
currency="usd",
id="test_id_2",
interval_count=None,
trial_period_days=None,
statement_descriptor=None,
metadata=None)
plan_create_mock.assert_any_call(amount=5000,
interval="month",
name="Test Plan 3",
currency="usd",
id="test_id_3",
interval_count=None,
trial_period_days=None,
statement_descriptor=None,
metadata=None)
plan_create_mock.assert_any_call(amount=7000,
interval="month",
name="Test Plan 4",
currency="usd",
id="test_id_4",
interval_count=None,
trial_period_days=7,
statement_descriptor=None,
metadata=None)
self.assertEqual(5, plan_create_mock.call_count)
@patch("stripe.Plan.create")
def test_plan_exists(self, plan_create_mock):
plan_create_mock.side_effect = stripe.StripeError("Plan already exists.")
sync_plans(api_key)
self.assertTrue("ERROR: Plan already exists.", sys.stdout.getvalue().strip())