2014-05-16 10 views
12

Sto provando a scrivere un test unitario che registra un evento fittizio sul mio webhook stripe.Come testare i Webook con striping con dati fittizi

sono andato e tirato un evento miei ceppi e ha cercato l'invio che con la modalità di test abilitato, ma io (un po 'prevedibile) ha ottenuto un errore:

a similar object exists in live mode, but a test mode key was used to make this request.

Mi sembra giusto. Quindi, come posso creare un evento fittizio che posso effettivamente inviare al mio webhook e farlo elaborare correttamente?

Ecco il mio test in corso:

class StripeTest(TestCase): 
    def setUp(self): 
     self.client = Client() 

    def test_receiving_a_callback(self): 
     with open('donate/test_assets/stripe_event.json', 'r') as f: 
      stripe_event = simplejson.load(f) 

     self.client.post('/donate/callbacks/stripe/', 
         data=simplejson.dumps(stripe_event), 
         content_type='application/json') 

risposta

11

La soluzione è quella di creare i propri dati finti. Nel codice seguente creiamo un pagamento di prova creando un token stripe, quindi inviandolo tramite il front end (al/donate/endpoint).

Una volta che il front end ha funzionato correttamente, è possibile ottenere l'evento da stripe e quindi inviarlo all'endpoint webhook della propria macchina di sviluppo.

Questo è più lavoro di quanto mi aspettassi, e non mi piace che i miei test stiano colpendo la rete, ma sembra essere una soluzione decente. Mi sento molto più sicuro dei miei pagamenti rispetto a prima.

def test_making_a_donation_and_getting_the_callback(self): 
     """These two tests must live together because they need to be done sequentially. 

     First, we place a donation using the client. Then we send a mock callback to our 
     webhook, to make sure it accepts it properly. 
     """ 
     stripe.api_key = settings.STRIPE_SECRET_KEY 
     # Create a stripe token (this would normally be done via javascript in the front 
     # end when the submit button was pressed) 
     token = stripe.Token.create(
      card={ 
       'number': '4242424242424242', 
       'exp_month': '6', 
       'exp_year': str(datetime.today().year + 1), 
       'cvc': '123', 
      } 
     ) 

     # Place a donation as an anonymous (not logged in) person using the 
     # token we just got 
     r = self.client.post('/donate/', data={ 
      'amount': '25', 
      'payment_provider': 'cc', 
      'first_name': 'Barack', 
      'last_name': 'Obama', 
      'address1': '1600 Pennsylvania Ave.', 
      'address2': 'The Whitehouse', 
      'city': 'DC', 
      'state': 'DC', 
      'zip_code': '20500', 
      'email': '[email protected]', 
      'referrer': 'footer', 
      'stripeToken': token.id, 
     }) 

     self.assertEqual(r.status_code, 302) # 302 because we redirect after a post. 

     # Get the stripe event so we can post it to the webhook 
     # We don't know the event ID, so we have to get the latest ones, then filter... 
     events = stripe.Event.all() 
     event = None 
     for obj in events.data: 
      if obj.data.object.card.fingerprint == token.card.fingerprint: 
       event = obj 
       break 
     self.assertIsNotNone(event, msg="Unable to find correct event for token: %s" % token.card.fingerprint) 

     # Finally, we can test the webhook! 
     r = self.client.post('/donate/callbacks/stripe/', 
          data=simplejson.dumps(event), 
          content_type='application/json') 

     # Does it return properly? 
     self.assertEqual(r.status_code, 200) 
Problemi correlati