diff --git a/apps/codecov-api/services/billing.py b/apps/codecov-api/services/billing.py index 698756865a..662eb3d036 100644 --- a/apps/codecov-api/services/billing.py +++ b/apps/codecov-api/services/billing.py @@ -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): diff --git a/apps/codecov-api/services/tests/test_billing.py b/apps/codecov-api/services/tests/test_billing.py index 43da0faa76..85b71842db 100644 --- a/apps/codecov-api/services/tests/test_billing.py +++ b/apps/codecov-api/services/tests/test_billing.py @@ -1859,20 +1859,29 @@ def test_update_email_address(self, modify_customer_mock): self.stripe.update_email_address(owner, "test@gmail.com") 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 - - # 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) @@ -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"