From 8dccb00edda1b0102fcc9031697af7a5161307bb Mon Sep 17 00:00:00 2001 From: Krzysztof Wolski Date: Fri, 13 Jan 2023 16:35:16 +0100 Subject: [PATCH] Fix CloudAPL response parsing (#149) * Fix CloudAPL response parsing * Mock signature fetch --- src/APL/saleor-cloud-apl.ts | 12 +++++++++++- .../next/process-async-saleor-webhook.test.ts | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/APL/saleor-cloud-apl.ts b/src/APL/saleor-cloud-apl.ts index 437deafc..a04298b0 100644 --- a/src/APL/saleor-cloud-apl.ts +++ b/src/APL/saleor-cloud-apl.ts @@ -25,6 +25,15 @@ const mapAuthDataToAPIBody = (authData: AuthData) => ({ token: authData.token, }); +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const mapAPIResponseToAuthData = (response: any): AuthData => ({ + appId: response.saleor_app_id, + domain: response.domain, + jwks: response.jwks, + saleorApiUrl: response.saleor_api_url, + token: response.token, +}); + /** * * Saleor Cloud APL - handle auth data management via REST API. @@ -68,7 +77,8 @@ export class SaleorCloudAPL implements APL { debug("Failed to parse response: %s", e?.message ?? "Unknown error"); })) as unknown; - const authData = authDataFromObject(parsedResponse); + const authData = authDataFromObject(mapAPIResponseToAuthData(parsedResponse)); + if (!authData) { debug("No auth data for given saleorApiUrl"); return undefined; diff --git a/src/handlers/next/process-async-saleor-webhook.test.ts b/src/handlers/next/process-async-saleor-webhook.test.ts index 8e5ac648..4de9d80c 100644 --- a/src/handlers/next/process-async-saleor-webhook.test.ts +++ b/src/handlers/next/process-async-saleor-webhook.test.ts @@ -151,6 +151,9 @@ describe("processAsyncSaleorWebhook", () => { it("Throw error on wrong signature", async () => { mockRequest.headers["saleor-signature"] = "wrong_signature"; + vi.mock("./../../fetch-remote-jwks", () => ({ + fetchRemoteJwks: vi.fn(() => "wrong_signature"), + })); await expect( processAsyncSaleorWebhook({ req: mockRequest,