用于测试的Mock Stripe方法

Shu*_*ham 6 unit-testing django-testing python-mock

所以我试图模拟stripe web hooks方法中的所有内容,以便我可以编写Unit test它.我正在使用模拟库模拟条带方法.这是我试图模拟的方法:

class AddCardView(APIView):
"""
* Add card for the customer
"""

permission_classes = (
    CustomerPermission,
)

def post(self, request, format=None):
    name = request.DATA.get('name', None)
    cvc = request.DATA.get('cvc', None)
    number = request.DATA.get('number', None)
    expiry = request.DATA.get('expiry', None)

    expiry_month, expiry_year = expiry.split("/")

    customer_obj = request.user.contact.business.customer

    customer = stripe.Customer.retrieve(customer_obj.stripe_id)

    try:
        card = customer.sources.create(
            source={
                "object": "card",
                "number": number,
                "exp_month": expiry_month,
                "exp_year": expiry_year,
                "cvc": cvc,
                "name": name
            }
        )
        # making it the default card
        customer.default_source = card.id
        customer.save()
    except CardError as ce:
        logger.error("Got CardError for customer_id={0}, CardError={1}".format(customer_obj.pk, ce.json_body))
        return Response({"success": False, "error": "Failed to add card"})
    else:
        customer_obj.card_last_4 = card.get('last4')
        customer_obj.card_kind = card.get('type', '')
        customer_obj.card_fingerprint = card.get('fingerprint')
        customer_obj.save()

    return Response({"success": True})
Run Code Online (Sandbox Code Playgroud)

这是方法unit testing:

@mock.patch('stripe.Customer.retrieve')
@mock.patch('stripe.Customer.create')
def test_add_card(self,create_mock,retrieve_mock):
    response = {
        'default_card': None,
        'cards': {
            "count": 0,
            "data": []
        }
    }

    # save_mock.return_value = response
    create_mock.return_value = response
    retrieve_mock.return_value = response

    self.api_client.client.login(username = self.username, password = self.password)
    res = self.api_client.post('/biz/api/auth/card/add')

    print res
Run Code Online (Sandbox Code Playgroud)

现在stripe.Customer.retrieve正在被嘲笑.但我无法嘲笑customer.sources.create.我真的很困惑.

Shu*_*ham 11

这是正确的方法:

@mock.patch('stripe.Customer.retrieve')
def test_add_card_failure(self, retrieve_mock):
    data = {
        'name': "shubham",
        'cvc': 123,
        'number': "4242424242424242",
        'expiry': "12/23",
    }
    e = CardError("Card Error", "", "")
    retrieve_mock.return_value.sources.create.return_value = e

    self.api_client.client.login(username=self.username, password=self.password)

    res = self.api_client.post('/biz/api/auth/card/add', data=data)

    self.assertEqual(self.deserialize(res)['success'], False)
Run Code Online (Sandbox Code Playgroud)


pan*_*sen 5

即使给出的答案是正确的,也有一种使用更舒适的解决方案vcrpy。一旦给定的记录还不存在,那就是在创建一个盒带(记录)。完成后,模拟将透明地完成,并且记录将被重播。美丽。

使用py.test拥有一个香草金字塔应用程序,我的测试现在看起来像这样:

import vcr 
# here we have some FactoryBoy fixtures   
from tests.fixtures import PaymentServiceProviderFactory, SSOUserFactory

def test_post_transaction(sqla_session, test_app):
    # first we need a PSP and a User existent in the DB
    psp = PaymentServiceProviderFactory()  # type: PaymentServiceProvider
    user = SSOUserFactory()
    sqla_session.add(psp, user)
    sqla_session.flush()

    with vcr.use_cassette('tests/casettes/tests.checkout.services.transaction_test.test_post_transaction.yaml'):
        # with that PSP we create a new PSPTransaction ...
        res = test_app.post(url='/psps/%s/transaction' % psp.id,
                            params={
                                'token': '4711',
                                'amount': '12.44',
                                'currency': 'EUR',
                            })
        assert 201 == res.status_code
        assert 'id' in res.json_body
Run Code Online (Sandbox Code Playgroud)


JPG*_*JPG 5

IMO,以下方法比其他答案更好

import unittest
import stripe
import json
from unittest.mock import patch
from stripe.http_client import RequestsClient # to mock the request session

stripe.api_key = "foo"

stripe.default_http_client = RequestsClient() # assigning the default HTTP client

null = None
false = False
true = True
charge_resp = {
    "id": "ch_1FgmT3DotIke6IEFVkwh2N6Y",
    "object": "charge",
    "amount": 1000,
    "amount_captured": 1000,
    "amount_refunded": 0,
    "billing_details": {
        "address": {
            "city": "Los Angeles",
            "country": "USA",
        },
        "email": null,
        "name": "Jerin",
        "phone": null
    },
    "captured": true,
}


def get_customer_city_from_charge(stripe_charge_id):
    # this is our function and we are writing unit-test for this function
    charge_response = stripe.Charge.retrieve("foo-bar")
    return charge_response.billing_details.address.city


class TestStringMethods(unittest.TestCase):

    @patch("stripe.default_http_client._session")
    def test_get_customer_city_from_charge(self, mock_session):
        mock_response = mock_session.request.return_value
        mock_response.content.decode.return_value = json.dumps(charge_resp)
        mock_response.status_code = 200

        city_name = get_customer_city_from_charge("some_id")
        self.assertEqual(city_name, "Los Angeles")


if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

这种方法的优点

  1. 就可以生成对应的类对象(这里charge_response变量是一个类型Charge--(source code)
  2. 您可以在响应上使用点 (.) 运算符(就像我们对真实的stripe SDK 所做的那样)
  3. 点运算符对深层属性的支持