chore: prevent adminSession variable from being mutated in the helpers

This commit is contained in:
Riqwan Thamir
2022-12-07 10:23:20 +01:00
committed by Adrien de Peretti
parent 66bec2f54d
commit 7fcf2b86a9
2 changed files with 17 additions and 14 deletions

View File

@@ -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,
})
})

View File

@@ -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) {