fix(medusa): Normalize discount code before querying DB (#2224)

**What**
Normalize discount code before querying DB

Fixes CORE-567
This commit is contained in:
Oliver Windall Juhl
2022-09-16 14:01:36 +02:00
committed by GitHub
parent f7177c9033
commit 3f73170288
7 changed files with 143 additions and 20 deletions

View File

@@ -403,7 +403,7 @@ describe("/admin/discounts", () => {
expect.objectContaining({
id: "dynamic-discount",
code: "Dyn100",
})
}),
])
)
})
@@ -2365,4 +2365,120 @@ describe("/admin/discounts", () => {
}
})
})
describe("GET /admin/discounts/code/:code", () => {
beforeEach(async () => {
const manager = dbConnection.manager
await adminSeeder(dbConnection)
await manager.insert(DiscountRule, {
id: "test-discount-rule-fixed",
description: "Test discount rule",
type: "fixed",
value: 10,
allocation: "total",
})
await simpleDiscountFactory(dbConnection, {
id: "test-discount",
code: "TEST",
rule: {
type: "percentage",
value: "10",
allocation: "total",
},
})
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should retrieve discount using uppercase code", async () => {
const api = useApi()
const response = await api
.get("/admin/discounts/code/TEST", {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
const disc = response.data.discount
expect(response.status).toEqual(200)
expect(disc).toEqual(
expect.objectContaining({
id: "test-discount",
code: "TEST",
})
)
})
it("should retrieve discount using lowercase code", async () => {
const api = useApi()
const response = await api
.get("/admin/discounts/code/test", {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
const disc = response.data.discount
expect(response.status).toEqual(200)
expect(disc).toEqual(
expect.objectContaining({
id: "test-discount",
code: "TEST",
})
)
})
it("should retrieve discount using mixed casing code", async () => {
const api = useApi()
const response = await api
.get("/admin/discounts/code/TesT", {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
const disc = response.data.discount
expect(response.status).toEqual(200)
expect(disc).toEqual(
expect.objectContaining({
id: "test-discount",
code: "TEST",
})
)
})
it("should respond with 404 on non-existing code", async () => {
const api = useApi()
try {
await api.get("/admin/discounts/code/non-existing", {
headers: {
Authorization: "Bearer test_token",
},
})
} catch (error) {
expect(error.response.status).toEqual(404)
expect(error.response.data.message).toBe(
"Discount with code non-existing was not found"
)
}
})
})
})

View File

@@ -420,7 +420,7 @@ describe("/admin/draft-orders", () => {
code: "TEST",
}),
expect.objectContaining({
code: "free-shipping",
code: "FREE-SHIPPING",
}),
])
)