I'm trying to write unit tests for Stripe webhooks. The problem is I'm also verifying the stripe-signature
and it fails as expected.
Is there a way to pass a correct signature in tests to the webhook with mock data?
This is the beginning of the webhook route I'm trying to handle
// Retrieve the event by verifying the signature using the raw body and secret.
let event: Stripe.Event;
const signature = headers["stripe-signature"];
try {
event = stripe.webhooks.constructEvent(
raw,
signature,
context.env.stripeWebhookSecret
);
} catch (err) {
throw new ResourceError(RouteErrorCode.STRIPE_WEBHOOK_SIGNATURE_VERIFICATION_FAILD);
}
// Handle event...
And the current test I'm trying to handle, I'm using Jest:
const postData = { MOCK WEBHOOK EVENT DATA }
const result = await request(app.app)
.post("/webhook/stripe")
.set('stripe-signature', 'HOW TO GET THIS SIGNATURE?')
.send(postData);