From 75710c3096461e0fb7744e783e1488e9a875f8d8 Mon Sep 17 00:00:00 2001 From: Paul Rill Date: Wed, 7 Aug 2024 12:14:15 +0200 Subject: [PATCH] test: :white_check_mark: get coverage back up to 100% --- src/PrintOne.ts | 15 --------------- test/CouponCode.spec.ts | 5 ++++- test/PrintOne.spec.ts | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/PrintOne.ts b/src/PrintOne.ts index 78f6e11..906903b 100644 --- a/src/PrintOne.ts +++ b/src/PrintOne.ts @@ -584,21 +584,6 @@ export class PrintOne { return new Coupon(this.protected, data); } - public validatedWebhook( - body: string, - headers: Record, - secret: string, - ): boolean { - const hmacHeader = headers["x-printone-hmac-sha256"]; - - const hmac = crypto - .createHmac("sha256", secret) - .update(body) - .digest("base64"); - - return hmac === hmacHeader; - } - public isValidWebhook( body: string, headers: Record, diff --git a/test/CouponCode.spec.ts b/test/CouponCode.spec.ts index ffffcb6..18b46ce 100644 --- a/test/CouponCode.spec.ts +++ b/test/CouponCode.spec.ts @@ -77,7 +77,10 @@ describe("getOrder", function () { // arrange const preOrder = await useCoupon(); const orderId = preOrder.id; - await couponCode.refresh(); + + while (couponCode.orderId === null) { + await couponCode.refresh(); + } // act const order = await couponCode.getOrder(); diff --git a/test/PrintOne.spec.ts b/test/PrintOne.spec.ts index 00cfb58..6b82504 100644 --- a/test/PrintOne.spec.ts +++ b/test/PrintOne.spec.ts @@ -2144,6 +2144,30 @@ describe("isValidWebhook", function () { }); }); +describe("isValidWebhook", function () { + const body = + '{"data":{"id":"ord_QXitaPr7MumnHo2BYXuW9","companyId":"2bd4c679-3d59-4a6f-a815-a60424746f8d","templateId":"tmpl_AyDg3PxvP5ydyGq3kSFfj","finish":"GLOSSY","format":"POSTCARD_A5","mergeVariables":{},"recipient":{"name":"Your Name","address":"Street 1","postalCode":"1234 AB","city":"Amsterdam","country":"NL"},"definitiveCountryId":"NL","region":"NETHERLANDS","deliverySpeed":"FAST","isBillable":true,"status":"order_created","friendlyStatus":"Processing","errors":[],"metadata":{},"sendDate":"2024-01-01T00:00:00.000Z","createdAt":"2024-01-01T00:00:00.000Z","updatedAt":"2024-01-01T00:00:00.000Z","anonymizedAt":null,"csvOrderId":null},"created_at":"2024-06-03T13:14:46.501Z","event":"order_status_update"}'; + const headers = { + "x-printone-hmac-sha256": "blmkCA9eG2fajvgpHx/RBirRO8rA4wRGf6gr1/v+V0g=", + }; + + it("should return false if header does not match", () => { + expect( + client.isValidWebhook(body, headers, "invalid-header-secret"), + ).toBeFalse(); + }); + + it("should return if signature is valid", () => { + expect( + client.isValidWebhook( + body, + headers, + "0YFMgi5yzciEJV2HBL9wKWtNDnos8TaMOqtjSNErnDYWfign0JdW81vpmb6T62r4", + ), + ).toBeTrue(); + }); +}); + describe("validateWebhook", function () { beforeEach(async function () { //mock isValidWebhook