@@ -133,20 +133,22 @@ def _read_stripe_fixture(*args: Any, **kwargs: Any) -> Any:
133
133
return stripe .util .convert_to_stripe_object (fixture )
134
134
return _read_stripe_fixture
135
135
136
- def mock_stripe (mocked_function_name : str ,
137
- generate_this_fixture : Optional [bool ]= None ) -> Callable [[CallableT ], CallableT ]:
138
- def _mock_stripe (decorated_function : CallableT ) -> CallableT :
139
- mocked_function = operator .attrgetter (mocked_function_name )(sys .modules [__name__ ])
140
- generate_fixture = generate_this_fixture
136
+ def mock_stripe (* mocked_function_names : str ,
137
+ generate : Optional [bool ]= None ) -> Callable [[CallableT ], Callable [..., Any ]]:
138
+ def _mock_stripe (decorated_function : CallableT ) -> Callable [..., Any ]:
139
+ generate_fixture = generate
141
140
if generate_fixture is None :
142
141
generate_fixture = GENERATE_STRIPE_FIXTURES
143
- if generate_fixture :
144
- side_effect = generate_and_save_stripe_fixture (
145
- decorated_function .__name__ , mocked_function_name , mocked_function ) # nocoverage
146
- else :
147
- side_effect = read_stripe_fixture (decorated_function .__name__ , mocked_function_name )
142
+ mocked_function_names_ = ["stripe.{}" .format (name ) for name in mocked_function_names ]
143
+ for mocked_function_name in mocked_function_names_ :
144
+ mocked_function = operator .attrgetter (mocked_function_name )(sys .modules [__name__ ])
145
+ if generate_fixture :
146
+ side_effect = generate_and_save_stripe_fixture (
147
+ decorated_function .__name__ , mocked_function_name , mocked_function ) # nocoverage
148
+ else :
149
+ side_effect = read_stripe_fixture (decorated_function .__name__ , mocked_function_name )
150
+ decorated_function = patch (mocked_function_name , side_effect = side_effect )(decorated_function )
148
151
149
- @patch (mocked_function_name , side_effect = side_effect )
150
152
@wraps (decorated_function )
151
153
def wrapped (* args : Any , ** kwargs : Any ) -> Any :
152
154
return decorated_function (* args , ** kwargs )
@@ -160,9 +162,7 @@ def __eq__(self, other: Any) -> bool:
160
162
return True
161
163
162
164
class StripeTest (ZulipTestCase ):
163
- @mock_stripe ("stripe.Coupon.create" , False )
164
- @mock_stripe ("stripe.Plan.create" , False )
165
- @mock_stripe ("stripe.Product.create" , False )
165
+ @mock_stripe ("Product.create" , "Plan.create" , "Coupon.create" , generate = False )
166
166
def setUp (self , mock3 : Mock , mock2 : Mock , mock1 : Mock ) -> None :
167
167
call_command ("setup_stripe" )
168
168
@@ -215,11 +215,7 @@ def test_billing_not_enabled(self) -> None:
215
215
response = self .client_get ("/upgrade/" )
216
216
self .assert_in_success_response (["Page not found (404)" ], response )
217
217
218
- @mock_stripe ("stripe.Token.create" )
219
- @mock_stripe ("stripe.Customer.create" )
220
- @mock_stripe ("stripe.Subscription.create" )
221
- @mock_stripe ("stripe.Customer.retrieve" )
222
- @mock_stripe ("stripe.Invoice.upcoming" )
218
+ @mock_stripe ("Customer.retrieve" , "Subscription.create" , "Customer.create" , "Token.create" , "Invoice.upcoming" )
223
219
def test_initial_upgrade (self , mock5 : Mock , mock4 : Mock , mock3 : Mock , mock2 : Mock , mock1 : Mock ) -> None :
224
220
user = self .example_user ("hamlet" )
225
221
self .login (user .email )
@@ -283,11 +279,7 @@ def test_initial_upgrade(self, mock5: Mock, mock4: Mock, mock3: Mock, mock2: Moc
283
279
'Card ending in 4242' ]:
284
280
self .assert_in_response (substring , response )
285
281
286
- @mock_stripe ("stripe.Token.create" )
287
- @mock_stripe ("stripe.Invoice.upcoming" )
288
- @mock_stripe ("stripe.Customer.retrieve" )
289
- @mock_stripe ("stripe.Customer.create" )
290
- @mock_stripe ("stripe.Subscription.create" )
282
+ @mock_stripe ("Token.create" , "Invoice.upcoming" , "Customer.retrieve" , "Customer.create" , "Subscription.create" )
291
283
def test_billing_page_permissions (self , mock5 : Mock , mock4 : Mock , mock3 : Mock ,
292
284
mock2 : Mock , mock1 : Mock ) -> None :
293
285
# Check that non-admins can access /upgrade via /billing, when there is no Customer object
@@ -312,10 +304,7 @@ def test_billing_page_permissions(self, mock5: Mock, mock4: Mock, mock3: Mock,
312
304
response = self .client_get ("/billing/" )
313
305
self .assert_in_success_response (["You must be an organization administrator" ], response )
314
306
315
- @mock_stripe ("stripe.Token.create" )
316
- @mock_stripe ("stripe.Customer.create" )
317
- @mock_stripe ("stripe.Subscription.create" )
318
- @mock_stripe ("stripe.Customer.retrieve" )
307
+ @mock_stripe ("Token.create" , "Customer.create" , "Subscription.create" , "Customer.retrieve" )
319
308
def test_upgrade_with_outdated_seat_count (
320
309
self , mock4 : Mock , mock3 : Mock , mock2 : Mock , mock1 : Mock ) -> None :
321
310
self .login (self .example_email ("hamlet" ))
@@ -351,11 +340,7 @@ def test_upgrade_with_outdated_seat_count(
351
340
event_type = RealmAuditLog .STRIPE_PLAN_QUANTITY_RESET ).values_list ('extra_data' , flat = True ).first ()),
352
341
{'quantity' : new_seat_count })
353
342
354
- @mock_stripe ("stripe.Token.create" )
355
- @mock_stripe ("stripe.Customer.create" )
356
- @mock_stripe ("stripe.Subscription.create" )
357
- @mock_stripe ("stripe.Customer.retrieve" )
358
- @mock_stripe ("stripe.Customer.save" )
343
+ @mock_stripe ("Token.create" , "Customer.create" , "Subscription.create" , "Customer.retrieve" , "Customer.save" )
359
344
def test_upgrade_where_subscription_save_fails_at_first (
360
345
self , mock5 : Mock , mock4 : Mock , mock3 : Mock , mock2 : Mock , mock1 : Mock ) -> None :
361
346
user = self .example_user ("hamlet" )
0 commit comments