Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/codecov-api/services/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ def update_billing_address(self, owner: Owner, name, billing_address):
"error_type": type(e).__name__,
},
)
raise e

@_log_stripe_error
def apply_cancellation_discount(self, owner: Owner):
Expand Down
74 changes: 61 additions & 13 deletions apps/codecov-api/services/tests/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1859,20 +1859,29 @@ def test_update_email_address(self, modify_customer_mock):
self.stripe.update_email_address(owner, "[email protected]")
modify_customer_mock.assert_called_once_with(customer_id, email=email)

@patch("logging.Logger.error")
def test_update_billing_address_with_invalid_address(self, log_error_mock):
@patch("services.billing.stripe.Customer.retrieve")
@patch("services.billing.stripe.PaymentMethod.modify")
@patch("services.billing.stripe.Customer.modify")
def test_update_billing_address_with_invalid_address(
self,
modify_customer_mock,
modify_payment_mock,
retrieve_customer_mock,
):
owner = OwnerFactory(stripe_customer_id="123", stripe_subscription_id="123")
assert self.stripe.update_billing_address(owner, "John Doe", "gabagool") is None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was failing in CI dunno, was giving error about missing API key again. So changed it to take mocks for the calls to stripe api and still tests invalid address scenario


# TODO: Logically we expect an error about an invalid billing address, but
# CI is getting an error about a missing API key and local envs are getting
# an error about the customer not existing. Needs to be fixed.
msg, log_args = log_error_mock.call_args
assert msg[0] == "Unable to update billing address for customer"
assert log_args["extra"]["customer_id"] == "123"
assert log_args["extra"]["subscription_id"] == "123"
# assert extra["error"] == "Some error about an invalid address"
# assert extra["error_type"] == "AppropriateExceptionType"
# save a copy of the original and set it back after this test
original_side_effect = modify_customer_mock.side_effect
modify_customer_mock.side_effect = Exception("Invalid billing address")
self.addCleanup(
setattr, modify_customer_mock, "side_effect", original_side_effect
)

with self.assertRaises(Exception):
self.stripe.update_billing_address(owner, "John Doe", "gabagool")

retrieve_customer_mock.assert_called_once()
modify_payment_mock.assert_called_once()
modify_customer_mock.assert_called_once()

def test_update_billing_address_when_no_customer_id(self):
owner = OwnerFactory(stripe_customer_id=None)
Expand Down Expand Up @@ -1925,6 +1934,45 @@ def test_update_billing_address(
name="John Doe",
)

@patch("services.billing.stripe.Customer.retrieve")
@patch("services.billing.stripe.PaymentMethod.modify")
@patch("services.billing.stripe.Customer.modify")
def test_update_billing_address_with_error(
self, modify_customer_mock, modify_payment_mock, retrieve_customer_mock
):
subscription_id = "sub_abc"
customer_id = "cus_abc"
owner = OwnerFactory(
stripe_subscription_id=subscription_id, stripe_customer_id=customer_id
)
billing_address = {
"line1": "45 Fremont St.",
"line2": "",
"city": "San Francisco",
"state": "CA",
"country": "US",
"postal_code": "94105",
}
# Set the side effect only for this test, and reset after
original_side_effect = modify_payment_mock.side_effect
modify_payment_mock.side_effect = stripe.error.CardError(
message="Your card was declined.", param="number", code="card_declined"
)
self.addCleanup(
lambda: setattr(modify_payment_mock, "side_effect", original_side_effect)
)

with self.assertRaises(stripe.error.CardError):
self.stripe.update_billing_address(
owner,
name="John Doe",
billing_address=billing_address,
)

retrieve_customer_mock.assert_called_once()
modify_payment_mock.assert_called_once()
modify_customer_mock.assert_not_called()

@patch("services.billing.stripe.Invoice.retrieve")
def test_get_invoice_not_found(self, retrieve_invoice_mock):
invoice_id = "abc"
Expand Down