2015-07-08 4 views
5

Так что я пытаюсь высмеять все stripe web hooks в методе, чтобы я мог написать Unit test для него. Я использую mock library для издевательств по методам полосы. Вот метод, который я пытаюсь издеваться:Методы Mock Stripe в Python для тестирования

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}) 

Это метод 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 

Теперь stripe.Customer.retrieve это осмеиваемым правильно. Но я не могу издеваться над customer.sources.create. Я действительно застрял на этом.

ответ

8

Это правильный способ сделать это:

@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) 
0

Даже если данный ответ правильный, есть способ более удобное решение с использованием 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 
Смежные вопросы