1
1
from unittest import mock
2
2
3
3
import requests_mock
4
- import stripe
5
4
from django .conf import settings
6
5
from django .contrib .auth .models import User
7
6
from django .test import TestCase , override_settings
8
7
from django .urls import reverse
8
+ from django .utils import timezone
9
9
from django_dynamic_fixture import get
10
+ from djstripe import models as djstripe
11
+ from djstripe .enums import SubscriptionStatus
10
12
11
13
from readthedocs .organizations .models import Organization
12
14
from readthedocs .subscriptions .models import Plan , Subscription
@@ -56,65 +58,81 @@ def test_manage_subscription(self, mock_request):
56
58
fetch_redirect_response = False ,
57
59
)
58
60
59
- @mock .patch ("readthedocs.subscriptions.managers.stripe.Subscription.create" )
60
- @mock .patch ("readthedocs.subscriptions.utils.stripe.Customer.retrieve" )
61
+ @mock .patch (
62
+ "readthedocs.subscriptions.utils.stripe.Customer.modify" , new = mock .MagicMock
63
+ )
64
+ @mock .patch ("readthedocs.subscriptions.utils.djstripe.Customer._get_or_retrieve" )
61
65
@mock .patch ("readthedocs.subscriptions.utils.stripe.Customer.create" )
62
66
def test_user_without_subscription (
63
- self , customer_create_mock , customer_retrieve_mock , subscription_create_mock
67
+ self , customer_create_mock , customer_retrieve_mock
64
68
):
65
- subscription_create_mock .return_value = stripe .Subscription .construct_from (
66
- values = {
67
- "id" : "sub_a1b2c3" ,
68
- "start_date" : 1610532715.085267 ,
69
- "current_period_end" : 1610532715.085267 ,
70
- "trial_end" : 1610532715.085267 ,
71
- "status" : "active" ,
72
- },
73
- key = None ,
69
+ stripe_customer = get (
70
+ djstripe .Customer ,
71
+ id = "cus_a1b2c3" ,
74
72
)
75
- customer_retrieve_mock .return_value = stripe .Customer .construct_from (
76
- values = {"id" : "cus_a1b2c3" },
77
- key = None ,
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 ,
78
81
)
82
+ stripe_customer .subscribe = mock .MagicMock ()
83
+ stripe_customer .subscribe .return_value = stripe_subscription
84
+ customer_retrieve_mock .return_value = stripe_customer
85
+
79
86
self .subscription .delete ()
80
87
self .organization .refresh_from_db ()
81
88
self .assertFalse (hasattr (self .organization , 'subscription' ))
89
+ self .assertIsNone (self .organization .stripe_customer )
90
+
82
91
resp = self .client .get (reverse ('subscription_detail' , args = [self .organization .slug ]))
83
92
self .assertEqual (resp .status_code , 200 )
84
93
self .organization .refresh_from_db ()
85
94
subscription = self .organization .subscription
86
95
self .assertEqual (subscription .status , 'active' )
87
96
self .assertEqual (subscription .stripe_id , 'sub_a1b2c3' )
97
+ self .assertEqual (self .organization .stripe_customer , stripe_customer )
88
98
customer_retrieve_mock .assert_called_once ()
89
99
customer_create_mock .assert_not_called ()
90
100
91
- @mock .patch ("readthedocs.subscriptions.managers.stripe.Subscription.create" )
92
- @mock .patch ("readthedocs.subscriptions.utils.stripe.Customer.retrieve" )
101
+ @mock .patch (
102
+ "readthedocs.subscriptions.utils.djstripe.Customer.sync_from_stripe_data"
103
+ )
104
+ @mock .patch ("readthedocs.subscriptions.utils.djstripe.Customer._get_or_retrieve" )
93
105
@mock .patch ("readthedocs.subscriptions.utils.stripe.Customer.create" )
94
106
def test_user_without_subscription_and_customer (
95
- self , customer_create_mock , customer_retrieve_mock , subscription_create_mock
107
+ self , customer_create_mock , customer_retrieve_mock , sync_from_stripe_data_mock
96
108
):
97
- subscription_create_mock .return_value = stripe .Subscription .construct_from (
98
- values = {
99
- "id" : "sub_a1b2c3" ,
100
- "start_date" : 1610532715.085267 ,
101
- "current_period_end" : 1610532715.085267 ,
102
- "trial_end" : 1610532715.085267 ,
103
- "status" : "active" ,
104
- },
105
- key = None ,
109
+ stripe_customer = get (
110
+ djstripe .Customer ,
111
+ id = "cus_a1b2c3" ,
106
112
)
107
- customer_create_mock .return_value = stripe .Customer .construct_from (
108
- values = {"id" : "cus_a1b2c3" },
109
- key = None ,
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 ,
110
121
)
122
+ stripe_customer .subscribe = mock .MagicMock ()
123
+ stripe_customer .subscribe .return_value = stripe_subscription
124
+ customer_retrieve_mock .return_value = None
125
+ sync_from_stripe_data_mock .return_value = stripe_customer
126
+
111
127
# When stripe_id is None, a new customer is created.
112
128
self .organization .stripe_id = None
113
129
self .organization .save ()
114
130
self .subscription .delete ()
115
131
self .organization .refresh_from_db ()
116
132
self .assertFalse (hasattr (self .organization , 'subscription' ))
117
133
self .assertIsNone (self .organization .stripe_id )
134
+ self .assertIsNone (self .organization .stripe_customer )
135
+
118
136
customer_retrieve_mock .reset_mock ()
119
137
resp = self .client .get (reverse ('subscription_detail' , args = [self .organization .slug ]))
120
138
self .assertEqual (resp .status_code , 200 )
@@ -123,9 +141,9 @@ def test_user_without_subscription_and_customer(
123
141
self .assertEqual (subscription .status , 'active' )
124
142
self .assertEqual (subscription .stripe_id , 'sub_a1b2c3' )
125
143
self .assertEqual (self .organization .stripe_id , 'cus_a1b2c3' )
144
+ self .assertEqual (self .organization .stripe_customer , stripe_customer )
126
145
customer_create_mock .assert_called_once ()
127
- # Called from a signal of .save()
128
- customer_retrieve_mock .assert_called_once ()
146
+ customer_retrieve_mock .assert_not_called ()
129
147
130
148
def test_user_with_canceled_subscription (self ):
131
149
self .subscription .status = 'canceled'
0 commit comments