From 7fcf2b86a9ed44895d4098f6af6985f5c64e4212 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Wed, 7 Dec 2022 10:23:20 +0100 Subject: [PATCH] chore: prevent adminSession variable from being mutated in the helpers --- .../discounts/__tests__/create-discount.js | 20 +++++++++---------- packages/medusa/src/helpers/test-request.js | 11 ++++++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js b/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js index ea83bc239e..9d0cbb7964 100644 --- a/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js +++ b/packages/medusa/src/api/routes/admin/discounts/__tests__/create-discount.js @@ -5,11 +5,11 @@ import { DiscountServiceMock } from "../../../../../services/__mocks__/discount" const validRegionId = IdMap.getId("region-france") describe("POST /admin/discounts", () => { - const generateAdminJwtSession = () => ({ + const adminSession = { jwt: { userId: IdMap.getId("admin_user") } - }) + } describe("successful creation", () => { let subject @@ -28,7 +28,7 @@ describe("POST /admin/discounts", () => { starts_at: "02/02/2021 13:45", ends_at: "03/14/2021 04:30", }, - adminSession: generateAdminJwtSession(), + adminSession, }) }) @@ -75,7 +75,7 @@ describe("POST /admin/discounts", () => { is_dynamic: true, valid_duration: "PaMT2D", }, - adminSession: generateAdminJwtSession(), + adminSession, }) }) @@ -109,7 +109,7 @@ describe("POST /admin/discounts", () => { is_dynamic: true, valid_duration: "P1Y2M03DT04H05M", }, - adminSession: generateAdminJwtSession(), + adminSession, }) }) @@ -150,7 +150,7 @@ describe("POST /admin/discounts", () => { }, regions: [validRegionId], }, - adminSession: generateAdminJwtSession(), + adminSession, }) }) @@ -190,7 +190,7 @@ describe("POST /admin/discounts", () => { is_dynamic: true, valid_duration: "P1Y2M03DT04H05M", }, - adminSession: generateAdminJwtSession(), + adminSession, }) }) @@ -222,7 +222,7 @@ describe("POST /admin/discounts", () => { ends_at: "02/02/2021", starts_at: "03/14/2021", }, - adminSession: generateAdminJwtSession(), + adminSession, }) }) @@ -255,7 +255,7 @@ describe("POST /admin/discounts", () => { regions: [validRegionId], starts_at: "03/14/2021 14:30", }, - adminSession: generateAdminJwtSession(), + adminSession, }) }) @@ -296,7 +296,7 @@ describe("POST /admin/discounts", () => { allocation: "total", }, }, - adminSession: generateAdminJwtSession(), + adminSession, }) }) diff --git a/packages/medusa/src/helpers/test-request.js b/packages/medusa/src/helpers/test-request.js index 8654410abb..c9951f80ad 100644 --- a/packages/medusa/src/helpers/test-request.js +++ b/packages/medusa/src/helpers/test-request.js @@ -11,6 +11,7 @@ import { moduleHelper } from "../loaders/module" import passportLoader from "../loaders/passport" import servicesLoader from "../loaders/services" import strategiesLoader from "../loaders/strategies" +import { clone } from "lodash" const adminSessionOpts = { cookieName: "session", @@ -87,16 +88,18 @@ export async function request(method, url, opts = {}) { ) headers.Cookie = headers.Cookie || "" if (opts.adminSession) { - if (opts.adminSession.jwt) { - opts.adminSession.jwt = jwt.sign( - opts.adminSession.jwt, + const adminSession = clone(opts.adminSession) + + if (adminSession.jwt) { + adminSession.jwt = jwt.sign( + adminSession.jwt, config.projectConfig.jwt_secret, { expiresIn: "30m", } ) } - headers.Cookie = JSON.stringify(opts.adminSession) || "" + headers.Cookie = JSON.stringify(adminSession) || "" } if (opts.clientSession) { if (opts.clientSession.jwt) {