Skip to content

Commit f812b76

Browse files
hackerkidrishig
authored andcommitted
billing: Allow mock_stripe to take multiple function_name arguments.
1 parent c0a8f0a commit f812b76

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

corporate/tests/test_stripe.py

+18-33
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,22 @@ def _read_stripe_fixture(*args: Any, **kwargs: Any) -> Any:
133133
return stripe.util.convert_to_stripe_object(fixture)
134134
return _read_stripe_fixture
135135

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
141140
if generate_fixture is None:
142141
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)
148151

149-
@patch(mocked_function_name, side_effect=side_effect)
150152
@wraps(decorated_function)
151153
def wrapped(*args: Any, **kwargs: Any) -> Any:
152154
return decorated_function(*args, **kwargs)
@@ -160,9 +162,7 @@ def __eq__(self, other: Any) -> bool:
160162
return True
161163

162164
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)
166166
def setUp(self, mock3: Mock, mock2: Mock, mock1: Mock) -> None:
167167
call_command("setup_stripe")
168168

@@ -215,11 +215,7 @@ def test_billing_not_enabled(self) -> None:
215215
response = self.client_get("/upgrade/")
216216
self.assert_in_success_response(["Page not found (404)"], response)
217217

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")
223219
def test_initial_upgrade(self, mock5: Mock, mock4: Mock, mock3: Mock, mock2: Mock, mock1: Mock) -> None:
224220
user = self.example_user("hamlet")
225221
self.login(user.email)
@@ -283,11 +279,7 @@ def test_initial_upgrade(self, mock5: Mock, mock4: Mock, mock3: Mock, mock2: Moc
283279
'Card ending in 4242']:
284280
self.assert_in_response(substring, response)
285281

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")
291283
def test_billing_page_permissions(self, mock5: Mock, mock4: Mock, mock3: Mock,
292284
mock2: Mock, mock1: Mock) -> None:
293285
# 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,
312304
response = self.client_get("/billing/")
313305
self.assert_in_success_response(["You must be an organization administrator"], response)
314306

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")
319308
def test_upgrade_with_outdated_seat_count(
320309
self, mock4: Mock, mock3: Mock, mock2: Mock, mock1: Mock) -> None:
321310
self.login(self.example_email("hamlet"))
@@ -351,11 +340,7 @@ def test_upgrade_with_outdated_seat_count(
351340
event_type=RealmAuditLog.STRIPE_PLAN_QUANTITY_RESET).values_list('extra_data', flat=True).first()),
352341
{'quantity': new_seat_count})
353342

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")
359344
def test_upgrade_where_subscription_save_fails_at_first(
360345
self, mock5: Mock, mock4: Mock, mock3: Mock, mock2: Mock, mock1: Mock) -> None:
361346
user = self.example_user("hamlet")

0 commit comments

Comments
 (0)