【问题标题】:How to Test Stripe Webhooks with Mock Data如何使用 Mock 数据测试 Stripe Webhook
【发布时间】:2014-07-05 13:04:47
【问题描述】:

我正在尝试编写一个单元测试,将模拟事件发布到我的条带 webhook。

我从日志中提取了一个事件并尝试在启用测试模式的情况下发送该事件,但我(在某种程度上可以预见)得到了一个错误:

在实时模式中存在类似的对象,但使用了测试模式密钥来发出此请求。

很公平。那么如何创建一个可以实际发送到我的 webhook 并正确处理它的模拟事件呢?

这是我目前的测试:

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

【问题讨论】:

    标签: django unit-testing testing stripe-payments webhooks


    【解决方案1】:

    解决方案是创建您自己的模拟数据。在下面的代码中,我们通过创建条带令牌来创建测试支付,然后通过前端(在 /donate/ 端点)提交它。

    一旦前端正常工作,您就可以从条带中获取事件,然后将其发送到您的开发机器的 webhook 端点。

    这比我预期的要多,我不喜欢我的测试在网络上运行,但这似乎是一个不错的解决方案。我对自己的付款比以前更有信心了。

        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': 'barack@freelawproject.org',
                '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)
    

    【讨论】:

      猜你喜欢
      • 2021-01-11
      • 2017-01-30
      • 2021-04-03
      • 2021-10-24
      • 2020-11-18
      • 2020-06-10
      • 2021-05-14
      • 2016-03-09
      • 2015-04-15
      相关资源
      最近更新 更多