def test_customer_sync_null_default_payment_method(
        self, attach_mock, customer_retrieve_mock
    ):
        """Test to make sure a custom'er default_payment_method gets updated to None
        if they remove their only attached payment method"""
        Customer.objects.all().delete()
        PaymentMethod.objects.all().delete()

        customer_fake = deepcopy(FAKE_CUSTOMER)
        customer_fake["invoice_settings"]["default_payment_method"] = (
            FAKE_PAYMENT_METHOD_I["id"]
        )
        customer_retrieve_mock.return_value = customer_fake

        customer = Customer.sync_from_stripe_data(customer_fake)
        self.assertEqual(
            customer.default_payment_method.id,
            customer_fake["invoice_settings"]["default_payment_method"],
        )
        self.assertEqual(customer.payment_methods.count(), 1)

        self.assert_fks(
            customer,
            expected_blank_fks={
                "djstripe.Customer.coupon",
                "djstripe.Customer.subscriber",
            },
        )

        # update customer_retrieve_mock return value
        customer_fake = deepcopy(FAKE_CUSTOMER)
        customer_fake["invoice_settings"]["default_payment_method"] = None
        customer_retrieve_mock.return_value = customer_fake

        # now detach the payment method from customer
        is_detached = customer.default_payment_method.detach()
        assert is_detached is True

        # refresh customer from db
        customer.refresh_from_db()

        self.assertEqual(
            customer.default_payment_method,
            None,
        )
        self.assertEqual(customer.payment_methods.count(), 0)

        self.assert_fks(
            customer,
            expected_blank_fks={
                "djstripe.Customer.coupon",
                "djstripe.Customer.subscriber",
                "djstripe.Customer.default_payment_method",
            },
        )
Test to make sure a custom'er default_payment_method gets updated to None
        if they remove their only attached payment method
tests/test_customer.py