@@ -23,6 +23,14 @@ def setUp(self):
23
23
self .user = get (User )
24
24
self .organization = get (Organization , stripe_id = '123' , owners = [self .user ])
25
25
self .plan = get (Plan , published = True , slug = settings .ORG_DEFAULT_SUBSCRIPTION_PLAN_SLUG )
26
+ self .stripe_subscription = self ._create_stripe_subscription (
27
+ customer_id = self .organization .stripe_id ,
28
+ subscription_id = "sub_a1b2c3d4" ,
29
+ )
30
+ self .stripe_customer = self .stripe_subscription .customer
31
+
32
+ self .organization .stripe_customer = self .stripe_customer
33
+ self .organization .save ()
26
34
self .subscription = get (
27
35
Subscription ,
28
36
organization = self .organization ,
@@ -31,11 +39,38 @@ def setUp(self):
31
39
)
32
40
self .client .force_login (self .user )
33
41
42
+ def _create_stripe_subscription (
43
+ self , customer_id = "cus_a1b2c3" , subscription_id = "sub_a1b2c3"
44
+ ):
45
+ stripe_customer = get (
46
+ djstripe .Customer ,
47
+ id = customer_id ,
48
+ )
49
+ stripe_subscription = get (
50
+ djstripe .Subscription ,
51
+ id = subscription_id ,
52
+ start_date = timezone .now (),
53
+ current_period_end = timezone .now () + timezone .timedelta (days = 30 ),
54
+ trial_end = timezone .now () + timezone .timedelta (days = 30 ),
55
+ status = SubscriptionStatus .active ,
56
+ customer = stripe_customer ,
57
+ )
58
+ stripe_price = get (
59
+ djstripe .Price ,
60
+ unit_amount = 50000 ,
61
+ )
62
+ stripe_item = get (
63
+ djstripe .SubscriptionItem ,
64
+ price = stripe_price ,
65
+ subscription = stripe_subscription ,
66
+ )
67
+ return stripe_subscription
68
+
34
69
def test_active_subscription (self ):
35
70
resp = self .client .get (reverse ('subscription_detail' , args = [self .organization .slug ]))
36
71
self .assertEqual (resp .status_code , 200 )
37
- self .assertEqual (resp .context ['subscription' ], self .subscription )
38
- self .assertContains (resp , ' active' )
72
+ self .assertEqual (resp .context ["stripe_subscription" ], self .stripe_subscription )
73
+ self .assertContains (resp , " active" )
39
74
# The subscribe form isn't shown, but the manage susbcription button is.
40
75
self .assertContains (resp , 'Manage Subscription' )
41
76
self .assertNotContains (resp , 'Create Subscription' )
@@ -66,25 +101,16 @@ def test_manage_subscription(self, mock_request):
66
101
def test_user_without_subscription (
67
102
self , customer_create_mock , customer_retrieve_mock
68
103
):
69
- stripe_customer = get (
70
- djstripe .Customer ,
71
- id = "cus_a1b2c3" ,
72
- )
73
- stripe_subscription = get (
74
- djstripe .Subscription ,
75
- id = "sub_a1b2c3" ,
76
- start_date = timezone .now (),
77
- current_period_end = timezone .now () + timezone .timedelta (days = 30 ),
78
- trial_end = timezone .now () + timezone .timedelta (days = 30 ),
79
- status = SubscriptionStatus .active ,
80
- customer = stripe_customer ,
81
- )
104
+ stripe_subscription = self ._create_stripe_subscription ()
105
+ stripe_customer = stripe_subscription .customer
82
106
stripe_customer .subscribe = mock .MagicMock ()
83
107
stripe_customer .subscribe .return_value = stripe_subscription
84
108
customer_retrieve_mock .return_value = stripe_customer
85
109
86
- self .subscription .delete ()
87
110
self .organization .refresh_from_db ()
111
+ self .organization .stripe_customer = None
112
+ self .organization .save ()
113
+ self .subscription .delete ()
88
114
self .assertFalse (hasattr (self .organization , 'subscription' ))
89
115
self .assertIsNone (self .organization .stripe_customer )
90
116
@@ -106,26 +132,16 @@ def test_user_without_subscription(
106
132
def test_user_without_subscription_and_customer (
107
133
self , customer_create_mock , customer_retrieve_mock , sync_from_stripe_data_mock
108
134
):
109
- stripe_customer = get (
110
- djstripe .Customer ,
111
- id = "cus_a1b2c3" ,
112
- )
113
- stripe_subscription = get (
114
- djstripe .Subscription ,
115
- id = "sub_a1b2c3" ,
116
- start_date = timezone .now (),
117
- current_period_end = timezone .now () + timezone .timedelta (days = 30 ),
118
- trial_end = timezone .now () + timezone .timedelta (days = 30 ),
119
- status = SubscriptionStatus .active ,
120
- customer = stripe_customer ,
121
- )
135
+ stripe_subscription = self ._create_stripe_subscription ()
136
+ stripe_customer = stripe_subscription .customer
122
137
stripe_customer .subscribe = mock .MagicMock ()
123
138
stripe_customer .subscribe .return_value = stripe_subscription
124
139
customer_retrieve_mock .return_value = None
125
140
sync_from_stripe_data_mock .return_value = stripe_customer
126
141
127
142
# When stripe_id is None, a new customer is created.
128
143
self .organization .stripe_id = None
144
+ self .organization .stripe_customer = None
129
145
self .organization .save ()
130
146
self .subscription .delete ()
131
147
self .organization .refresh_from_db ()
@@ -147,10 +163,12 @@ def test_user_without_subscription_and_customer(
147
163
148
164
def test_user_with_canceled_subscription (self ):
149
165
self .subscription .status = 'canceled'
166
+ self .stripe_subscription .status = SubscriptionStatus .canceled
167
+ self .stripe_subscription .save ()
150
168
self .subscription .save ()
151
169
resp = self .client .get (reverse ('subscription_detail' , args = [self .organization .slug ]))
152
170
self .assertEqual (resp .status_code , 200 )
153
- self .assertEqual (resp .context ['subscription' ], self .subscription )
171
+ self .assertEqual (resp .context ["stripe_subscription" ], self .stripe_subscription )
154
172
# The Manage Subscription form isn't shown, but the Subscribe is.
155
173
self .assertNotContains (resp , 'Manage Subscription' )
156
174
self .assertContains (resp , 'Create Subscription' )
0 commit comments