Skip to content

Commit

Permalink
billing: Add a test for a race condition in process_initial_upgrade.
Browse files Browse the repository at this point in the history
rishig committed Jan 29, 2019
1 parent 6a1017e commit a5324b6
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 1 addition & 2 deletions corporate/lib/stripe.py
Original file line number Diff line number Diff line change
@@ -258,8 +258,7 @@ def process_initial_upgrade(user: UserProfile, licenses: int, automanage_license
billing_schedule: int, stripe_token: Optional[str]) -> None:
realm = user.realm
customer = update_or_create_stripe_customer(user, stripe_token=stripe_token)
# TODO write a test for this
if CustomerPlan.objects.filter(customer=customer, status=CustomerPlan.ACTIVE).exists(): # nocoverage
if CustomerPlan.objects.filter(customer=customer, status=CustomerPlan.ACTIVE).exists():
# Unlikely race condition from two people upgrading (clicking "Make payment")
# at exactly the same time. Doesn't fully resolve the race condition, but having
# a check here reduces the likelihood.
9 changes: 9 additions & 0 deletions corporate/tests/test_stripe.py
Original file line number Diff line number Diff line change
@@ -628,6 +628,15 @@ def test_upgrade_with_tampered_seat_count(self) -> None:
self.assert_json_error_contains(response, "Something went wrong. Please contact")
self.assertEqual(ujson.loads(response.content)['error_description'], 'tampered seat count')

def test_upgrade_race_condition(self) -> None:
self.login(self.example_email("hamlet"))
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
with patch("corporate.lib.stripe.billing_logger.warning") as mock_billing_logger:
with self.assertRaises(BillingError) as context:
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
self.assertEqual('subscribing with existing subscription', context.exception.description)
mock_billing_logger.assert_called()

def test_check_upgrade_parameters(self) -> None:
# Tests all the error paths except 'not enough licenses'
def check_error(error_description: str, upgrade_params: Dict[str, Any],

0 comments on commit a5324b6

Please sign in to comment.